now threading is functional, but plugins are suddenly way to huge
This commit is contained in:
parent
d77dbeb1de
commit
c272a21aa7
5 changed files with 55 additions and 48 deletions
7
conf.ini
7
conf.ini
|
@ -1,14 +1,15 @@
|
|||
[main]
|
||||
abc = useless value
|
||||
|
||||
[ping_ext]
|
||||
plugin = ping
|
||||
hosts = de-ber-as20647.anchors.atlas.ripe.net,nl-ams-as1101.anchors.atlas.ripe.net,uk-boh-as196745.anchors.atlas.ripe.net
|
||||
title = NET:
|
||||
title = NET
|
||||
|
||||
[ping_local]
|
||||
plugin = ping
|
||||
hosts = 127.0.0.1,127.0.0.2
|
||||
title = LOCAL:
|
||||
hosts = 10.2.13.2,10.2.13.4
|
||||
title = BROKEN
|
||||
|
||||
[day]
|
||||
plugin = date
|
||||
|
|
|
@ -1,10 +1,18 @@
|
|||
import time
|
||||
import threading
|
||||
|
||||
|
||||
def execute(config, section):
|
||||
fmt = config.get(section, 'format')
|
||||
result = dict()
|
||||
class PluginThread(threading.Thread):
|
||||
def __init__(self, section, config, thread_id):
|
||||
threading.Thread.__init__(self)
|
||||
self.threadID = thread_id
|
||||
self.date_format = config.get(section, 'format')
|
||||
self.status = dict()
|
||||
if config.has_option(section, 'color'):
|
||||
result['color'] = config.get(section, 'color')
|
||||
result['full_text'] = time.strftime(fmt)
|
||||
return result
|
||||
self.status['color'] = config.get(section, 'color')
|
||||
self.freq = 1
|
||||
|
||||
def run(self):
|
||||
while True:
|
||||
self.status['full_text'] = time.strftime(self.date_format)
|
||||
time.sleep(self.freq)
|
||||
|
|
|
@ -4,22 +4,20 @@ import time
|
|||
import threading
|
||||
|
||||
|
||||
class ConnTest(threading.Thread):
|
||||
def __init__(self, threadID, name):
|
||||
class PluginThread(threading.Thread):
|
||||
def __init__(self, section, config, thread_id):
|
||||
threading.Thread.__init__(self)
|
||||
self.threadID = threadID
|
||||
self.name = name
|
||||
self.hosts = list()
|
||||
self.status = 'n/a'
|
||||
self.freq = int()
|
||||
self.threadID = thread_id
|
||||
self.hosts = config.get(section, 'hosts').split(',')
|
||||
self.title = config.get(section, 'title')
|
||||
self.status = dict()
|
||||
self.freq = 5
|
||||
self.format_status('n/a')
|
||||
|
||||
def configure(self, hosts, freq=5):
|
||||
self.hosts = hosts.split(',')
|
||||
self.freq = freq
|
||||
def format_status(self, state):
|
||||
self.status['full_text'] = self.title + ': ' + state
|
||||
|
||||
def run(self):
|
||||
global is_running
|
||||
is_running = True
|
||||
while True:
|
||||
random.shuffle(self.hosts)
|
||||
try:
|
||||
|
@ -27,23 +25,10 @@ class ConnTest(threading.Thread):
|
|||
fping = 'fping -q -c1 -t100 ' + host + ' &>/dev/null'
|
||||
response = os.system(fping)
|
||||
if response == 0:
|
||||
self.status = 'on'
|
||||
self.format_status('on')
|
||||
break
|
||||
self.status = 'off'
|
||||
self.format_status('off')
|
||||
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
break
|
||||
time.sleep(self.freq)
|
||||
|
||||
is_running = False
|
||||
ping_object = ConnTest(1, 'test_network')
|
||||
|
||||
|
||||
def execute(config, section):
|
||||
result = dict()
|
||||
global ping_object
|
||||
if is_running is False:
|
||||
ping_object.configure(config.get(section, 'hosts'), 10)
|
||||
ping_object.start()
|
||||
result['full_text'] = config.get(section, 'title') + ' ' + ping_object.status
|
||||
return result
|
||||
|
|
29
vdstatus.py
29
vdstatus.py
|
@ -3,6 +3,7 @@ import os
|
|||
import configparser
|
||||
import importlib
|
||||
import plugins
|
||||
import time
|
||||
|
||||
|
||||
DEFAULT_CONFIG = os.path.join(os.environ['HOME'], 'IdeaProjects/vdstatus/conf.ini')
|
||||
|
@ -11,12 +12,26 @@ configuration = configparser.ConfigParser()
|
|||
configuration.read(DEFAULT_CONFIG)
|
||||
|
||||
|
||||
def load_plugins(config):
|
||||
plugins_loaded = list()
|
||||
config.remove_section('main')
|
||||
for section in config.sections():
|
||||
plugin_name = config.get(section, 'plugin')
|
||||
plugin_id = len(plugins_loaded)
|
||||
module = importlib.import_module('.' + plugin_name, 'plugins')
|
||||
thread_object = module.PluginThread(section, config, plugin_id)
|
||||
plugins_loaded.append(thread_object)
|
||||
return plugins_loaded
|
||||
|
||||
|
||||
def run_plugins():
|
||||
plugins_l = load_plugins(configuration)
|
||||
for plugin in plugins_l:
|
||||
plugin.start()
|
||||
|
||||
while True:
|
||||
outputs = list()
|
||||
for section in configuration.sections():
|
||||
if section == 'main':
|
||||
continue
|
||||
plugin_name = '.' + configuration.get(section, 'plugin')
|
||||
plugin_module = importlib.import_module(plugin_name, 'plugins')
|
||||
outputs.append(plugin_module.execute(configuration, section))
|
||||
return outputs
|
||||
for plugin in plugins_l:
|
||||
outputs.append(plugin.status)
|
||||
print(json.dumps(outputs) + ',')
|
||||
time.sleep(1)
|
||||
|
|
|
@ -7,6 +7,4 @@ import time
|
|||
print('{"version":1}')
|
||||
print('[')
|
||||
while True:
|
||||
output = vdstatus.run_plugins()
|
||||
print(json.dumps(output) + ',')
|
||||
time.sleep(1)
|
||||
vdstatus.run_plugins()
|
||||
|
|
Loading…
Reference in a new issue