From fd62fb861e33f09ce55312bbf99875dc46d9fa95 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sat, 3 Aug 2019 16:17:54 +0200 Subject: [PATCH] Add mondash status block Signed-off-by: Knut Ahlers --- .config/i3blocks/block.py | 90 +++++++++++++++++++++++++++++++++++++++ .config/i3blocks/config | 8 ++++ .config/i3blocks/mondash | 70 ++++++++++++++++++++++++++++++ 3 files changed, 168 insertions(+) create mode 100644 .config/i3blocks/block.py create mode 100755 .config/i3blocks/mondash diff --git a/.config/i3blocks/block.py b/.config/i3blocks/block.py new file mode 100644 index 0000000..7f2b9fa --- /dev/null +++ b/.config/i3blocks/block.py @@ -0,0 +1,90 @@ +import os + +BTN_LEFT = 1 +BTN_MIDDLE = 2 +BTN_RIGHT = 3 + +COLOR_NONE = '#ffffff' +COLOR_DANGER = '#dd0000' +COLOR_SUCCESS = '#50fa7b' +COLOR_WARNING = '#ffd966' +COLOR_PRIMARY = '#8faafc' +COLOR_SECONDARY = '#7f7f7f' + +VAR_BTN = 'BLOCK_BUTTON' +VAR_INSTANCE = 'BLOCK_INSTANCE' +VAR_NAME = 'BLOCK_NAME' +VAR_X = 'BLOCK_X' +VAR_Y = 'BLOCK_Y' + + +class Block: + def __init__(self, icon=None, icon_color=None): + self.ICON = icon if icon is not None else '\uf128' + self.ICON_COLOR = icon_color if icon_color is not None else COLOR_NONE + + def button_is(self, btn): + """ + Checks whether a button was pressed on the widget and if so whether it was the expected one + """ + return VAR_BTN in os.environ and os.environ[VAR_BTN] != '' and int(os.environ[VAR_BTN]) == btn + + def color_text(self, text, color=COLOR_NONE): + """ + Formats the given text with given color in pango markup + """ + return '{text}'.format( + color=color, + text=text, + ) + + def execute(self): + """ + Contains the logic of this block and needs to be overwritten in child class + """ + raise Exception('No exec implementation specified') + + def instance(self): + """ + Returns the block-defined instance or empty string if none + """ + return os.environ[VAR_INSTANCE] if VAR_INSTANCE in os.environ else '' + + def name(self): + """ + Returns the block-defined name or empty string if none + """ + return os.environ[VAR_NAME] if VAR_NAME in os.environ else '' + + def render(self): + """ + Executes the block and prints out the formatted string for the block + """ + result = self.execute() + + if result is None or len(result) == 0: + print(self.color_text(self.ICON, self.ICON_COLOR)) + return + + text = '' + if type(result) is list: + text = ' '.join(result) + else: + text = result + + print('{icon} {text}'.format( + icon=self.color_text(self.ICON, self.ICON_COLOR), + text=text, + )) + + def set_icon(self, icon): + """ + Overwrites the icon speicfied in constructor + """ + self.ICON = icon + + def set_icon_color(self, color): + """ + Overwrites the icon color speicfied in constructor + """ + self.ICON_COLOR = color diff --git a/.config/i3blocks/config b/.config/i3blocks/config index c720729..7271bf7 100644 --- a/.config/i3blocks/config +++ b/.config/i3blocks/config @@ -32,6 +32,14 @@ markup=pango [mpc] interval=5 +[mondash] +instance=$env_MONDASH_PRIVATE +interval=30 + +[mondash] +instance=$env_MONDASH_ARCHNAS +interval=30 + [github] interval=300 diff --git a/.config/i3blocks/mondash b/.config/i3blocks/mondash new file mode 100755 index 0000000..7ad3cc3 --- /dev/null +++ b/.config/i3blocks/mondash @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 + +import json +import os +import subprocess + +from block import * + +import requests + +ICON = '\ufa6d' +MONDASH_URL = 'https://mondash.org/{dashid}' +STATUS_COLOR = { + 'OK': COLOR_SUCCESS, + 'Warning': COLOR_WARNING, + 'Critical': COLOR_DANGER, + 'Unknown': COLOR_SECONDARY, +} + + +class Mondash(Block): + def dashid(self): + id = self.instance() + + if id.startswith('$env_'): + id = os.environ[id[5:]] + + return id + + def execute(self): + if self.button_is(BTN_LEFT): + subprocess.check_call([ + 'xdg-open', + MONDASH_URL.format(dashid=self.dashid()), + ]) + + result = [] + + for status, count in self.get_counts().items(): + if count == 0: + continue + + color = STATUS_COLOR[status] + + result.append(self.color_text(str(count), color)) + + return ' / '.join(result) + + def get_counts(self): + counts = {'OK': 0, 'Warning': 0, 'Critical': 0, 'Unknown': 0} + + url = MONDASH_URL.format(dashid=self.dashid()) + '.json' + data = requests.get(url).json() + + if not 'metrics' in data: + raise Exception('No metrics block found in result data') + + for metric in data['metrics']: + counts[metric['status']] += 1 + + return counts + + +def main(): + block = Mondash(ICON) + block.render() + + +if __name__ == '__main__': + main()