Add mondash status block

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2019-08-03 16:17:54 +02:00
parent 9355c7e7bc
commit fd62fb861e
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
3 changed files with 168 additions and 0 deletions

90
.config/i3blocks/block.py Normal file
View file

@ -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 '<span color="{color}">{text}</span>'.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

View file

@ -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

70
.config/i3blocks/mondash Executable file
View file

@ -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()