plugins, vdstatus: massive plugin overhaul, conf: some formatting

This commit is contained in:
Von Random 2019-07-05 18:24:46 +03:00
parent 9902696f1f
commit bd549b0cc9
12 changed files with 73 additions and 58 deletions

View file

@ -1,4 +1,12 @@
# Typical plugin properties:
# - name: plugin name (always required, all other properties are optional)
# - title: title representation on the panel
# - hide_ok: false to force display value even if it is below problem threshold
# - problem: a threshold value to make a value urgent and / or display
# the value on the panel
output_format: i3 output_format: i3
plugins: plugins:
- name: ping - name: ping
title: NET title: NET
@ -6,15 +14,22 @@ plugins:
- de-ber-as20647.anchors.atlas.ripe.net - de-ber-as20647.anchors.atlas.ripe.net
- nl-ams-as1101.anchors.atlas.ripe.net - nl-ams-as1101.anchors.atlas.ripe.net
- uk-boh-as196745.anchors.atlas.ripe.net - uk-boh-as196745.anchors.atlas.ripe.net
hide_ok: false
- name: disk - name: disk
partition: / partition: /
problem: 80 problem: 80
hide_ok: false hide_ok: false
- name: disk - name: disk
partition: /home partition: /home
problem: 90 problem: 90
- name: pacman - name: pacman
- name: mem - name: mem
- name: load - name: load
- name: date - name: date
format: '%a %d %H:%M' format: '%a %d %H:%M'

View file

@ -17,11 +17,18 @@ class PluginThreadCommon:
self.thread = threading.Thread(target=self.run) self.thread = threading.Thread(target=self.run)
self.thread.daemon = True self.thread.daemon = True
def format_status(self, status, urgent=False):
if 'title' in self.conf:
full_text = self.conf['title'] + ': ' + status
else:
full_text = status
self.status.update({'full_text': full_text, 'urgent': urgent})
def start(self): def start(self):
self.thread.start() self.thread.start()
def main(self): def main(self):
self.status['full_text'] = 'placeholder' pass
def run(self): def run(self):
while True: while True:

View file

@ -17,13 +17,14 @@ class PluginThread(plugins.PluginThreadCommon):
with \ with \
open(BATTERY_DIR + 'capacity', 'r') as batt_capacity, \ open(BATTERY_DIR + 'capacity', 'r') as batt_capacity, \
open(BATTERY_DIR + 'status', 'r') as batt_status: open(BATTERY_DIR + 'status', 'r') as batt_status:
status = batt_status.read().strip() capacity = batt_capacity.readline().strip()
capacity = batt_capacity.read().strip() status_value = batt_status.readline().strip()
if status != 'Discharging':
if status_value != 'Discharging':
symbol = self.conf['symbol_charging'] symbol = self.conf['symbol_charging']
self.status['urgent'] = False urgent = False
else: else:
symbol = self.conf['symbol_discharging'] symbol = self.conf['symbol_discharging']
self.status['urgent'] = float(capacity) <= self.conf['problem'] urgent = float(capacity) <= self.conf['problem']
self.status['full_text'] = 'BAT: ' + capacity + '% ' + symbol self.format_status(capacity + '% ' + symbol, urgent=urgent)

View file

@ -15,4 +15,4 @@ class PluginThread(plugins.PluginThreadCommon):
def main(self): def main(self):
now = datetime.datetime.now(tz=self.timezone) now = datetime.datetime.now(tz=self.timezone)
self.status['full_text'] = now.strftime(self.conf['format']) self.format_status(now.strftime(self.conf['format']))

View file

@ -8,6 +8,8 @@ DISK_DEFAULTS = {'partition': '/', 'problem': 80, 'freq': 15}
class PluginThread(plugins.PluginThreadCommon): class PluginThread(plugins.PluginThreadCommon):
def __init__(self, config): def __init__(self, config):
super(PluginThread, self).__init__(config, DISK_DEFAULTS) super(PluginThread, self).__init__(config, DISK_DEFAULTS)
if 'title' not in self.conf:
self.conf['title'] = self.conf['partition']
def main(self): def main(self):
du_stat = psutil.disk_usage(self.conf['partition']) du_stat = psutil.disk_usage(self.conf['partition'])
@ -17,6 +19,5 @@ class PluginThread(plugins.PluginThreadCommon):
else: else:
self.hide = True self.hide = True
self.status['urgent'] = False self.status['urgent'] = False
du_free = str(round(du_stat.free / 2**30, 2)) status = '{:.2f}G'.format(du_stat.free / 2**30)
disk_usage = self.conf['partition'] + ': ' + du_free + 'G' self.format_status(status)
self.status['full_text'] = disk_usage

View file

@ -13,10 +13,6 @@ class PluginThread(plugins.PluginThreadCommon):
self.hide = False self.hide = False
self.format_status(self.conf['title'], False) self.format_status(self.conf['title'], False)
def format_status(self, contents, urgent):
self.status['urgent'] = urgent
self.status['full_text'] = self.conf['title'] + ': ' + contents
def main(self): def main(self):
try: try:
with open(self.conf['filename'], 'r') as datafile: with open(self.conf['filename'], 'r') as datafile:

View file

@ -3,7 +3,7 @@ import requests
import time import time
FGA_DEFAULTS = { FORTUNE_DEFAULTS = {
'uri': 'http://fucking-great-advice.ru/api/random', 'uri': 'http://fucking-great-advice.ru/api/random',
'freq': 120, 'retry': 3 'freq': 120, 'retry': 3
} }
@ -11,21 +11,21 @@ FGA_DEFAULTS = {
class PluginThread(plugins.PluginThreadCommon): class PluginThread(plugins.PluginThreadCommon):
def __init__(self, config): def __init__(self, config):
super(PluginThread, self).__init__(config, FGA_DEFAULTS) super(PluginThread, self).__init__(config, FORTUNE_DEFAULTS)
self.retry = False self.retry = False
def main(self): def main(self):
try: try:
req = requests.get(self.conf['uri'], timeout=2) req = requests.get(self.conf['uri'], timeout=2)
advice = req.json()['text'] if req.status_code == 200 else 'N/A' fortune = req.json()['text'] if req.status_code == 200 else 'N/A'
self.retry = False self.retry = False
except requests.exceptions.Timeout: except requests.exceptions.Timeout:
advice = 'N/A (timeout)' fortune = 'N/A (timeout)'
self.retry = True self.retry = True
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
advice = 'N/A (offline)' fortune = 'N/A (offline)'
self.retry = True self.retry = True
self.status['full_text'] = advice self.format_status(fortune)
def run(self): def run(self):
while True: while True:

View file

@ -2,7 +2,7 @@ import os
import plugins import plugins
LOAD_DEFAULTS = {'freq': 20, 'problem': 1} LOAD_DEFAULTS = {'title': 'LA', 'freq': 20, 'problem': 1}
class PluginThread(plugins.PluginThreadCommon): class PluginThread(plugins.PluginThreadCommon):
@ -13,8 +13,9 @@ class PluginThread(plugins.PluginThreadCommon):
loads = os.getloadavg() loads = os.getloadavg()
if loads[0] >= self.conf['problem']: if loads[0] >= self.conf['problem']:
self.hide = False self.hide = False
self.status['urgent'] = True urgent = True
else: else:
self.hide = True self.hide = True
self.status['urgent'] = False urgent = False
self.status['full_text'] = 'LA: {:.2f} {:.2f} {:.2f}'.format(*loads) status = '{:.2f} {:.2f} {:.2f}'.format(*loads)
self.format_status(status, urgent=urgent)

View file

@ -2,7 +2,7 @@ import psutil
import plugins import plugins
MEM_DEFAULTS = {'problem': 85} MEM_DEFAULTS = {'title': 'RAM', 'problem': 85}
class PluginThread(plugins.PluginThreadCommon): class PluginThread(plugins.PluginThreadCommon):
@ -13,9 +13,9 @@ class PluginThread(plugins.PluginThreadCommon):
mem_stat = psutil.virtual_memory() mem_stat = psutil.virtual_memory()
if mem_stat.percent > self.conf['problem']: if mem_stat.percent > self.conf['problem']:
self.hide = False self.hide = False
self.status['urgent'] = True urgent = True
else: else:
self.hide = True self.hide = True
self.status['urgent'] = False urgent = False
mem_available = round(mem_stat.available / 2**30, 2) mem_available = round(mem_stat.available / 2**30, 2)
self.status['full_text'] = 'RAM: {:.2f}G'.format(mem_available) self.format_status('{:.2f}G'.format(mem_available), urgent)

View file

@ -11,12 +11,6 @@ PACMAN_DEFAULTS = {
class PluginThread(plugins.PluginThreadCommon): class PluginThread(plugins.PluginThreadCommon):
def __init__(self, config): def __init__(self, config):
super(PluginThread, self).__init__(config, PACMAN_DEFAULTS) super(PluginThread, self).__init__(config, PACMAN_DEFAULTS)
self.format_status(0)
def format_status(self, count):
self.hide = count == 0
self.status['urgent'] = count >= self.conf['problem']
self.status['full_text'] = self.conf['title'] + ': ' + str(count)
def main(self): def main(self):
pacman_qu = subprocess.Popen( pacman_qu = subprocess.Popen(
@ -25,5 +19,11 @@ class PluginThread(plugins.PluginThreadCommon):
encoding='UTF-8' encoding='UTF-8'
) )
out = pacman_qu.communicate()[0].strip().splitlines() out = pacman_qu.communicate()[0].strip().splitlines()
packages = [pkg for pkg in out if not '[ignored]' in pkg] packages = len([pkg for pkg in out if not '[ignored]' in pkg])
self.format_status(len(packages)) if packages:
self.hide = False
else:
self.hide = True
self.format_status(
str(packages), urgent=packages >= self.conf['problem']
)

View file

@ -1,33 +1,26 @@
import os import os
import subprocess
import random import random
import plugins import plugins
PING_DEFAULTS = { PING_DEFAULTS = {
'hosts': list(), 'title': 'PING', 'timeout': 150 'hosts': tuple(), 'title': 'PING', 'timeout': 150
} }
class PluginThread(plugins.PluginThreadCommon): class PluginThread(plugins.PluginThreadCommon):
def __init__(self, config): def __init__(self, config):
super(PluginThread, self).__init__(config, PING_DEFAULTS) super(PluginThread, self).__init__(config, PING_DEFAULTS)
self.format_status('n/a') self.ping_cmd = ('fping', '-c1', '-qt' + str(self.conf['timeout']))
def format_status(self, state):
self.status['full_text'] = self.conf['title'] + ': ' + state
if state == 'up':
self.status['urgent'] = False
self.hide = True
else:
self.status['urgent'] = True
def main(self): def main(self):
random.shuffle(self.conf['hosts']) host = random.choice(self.conf['hosts'])
for host in self.conf['hosts']: fping = subprocess.run(
fping = 'fping -qc1t' + str(self.conf['timeout'])\ (*self.ping_cmd, host),
+ ' ' + host + ' &>/dev/null' stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
response = os.system(fping) )
if response == 0: if fping.returncode == 0:
self.format_status('on') self.format_status('up')
break else:
self.format_status('down') self.format_status('down', urgent=True)

View file

@ -49,8 +49,9 @@ class PluginRunner:
outputs = list() outputs = list()
for plugin in self.plugins_loaded: for plugin in self.plugins_loaded:
if \ if \
not plugin.conf['hide_ok'] or \ 'full_text' in plugin.status and (
not plugin.hide: not plugin.conf['hide_ok'] or not plugin.hide
):
outputs.append(plugin.status) outputs.append(plugin.status)
print(self.format_output(outputs), flush=True) print(self.format_output(outputs), flush=True)