now threading is functional, but plugins are suddenly way to huge

This commit is contained in:
Von Random 2016-10-22 05:50:11 +03:00
parent d77dbeb1de
commit c272a21aa7
5 changed files with 55 additions and 48 deletions

View file

@ -1,14 +1,15 @@
[main] [main]
abc = useless value
[ping_ext] [ping_ext]
plugin = ping plugin = ping
hosts = de-ber-as20647.anchors.atlas.ripe.net,nl-ams-as1101.anchors.atlas.ripe.net,uk-boh-as196745.anchors.atlas.ripe.net 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] [ping_local]
plugin = ping plugin = ping
hosts = 127.0.0.1,127.0.0.2 hosts = 10.2.13.2,10.2.13.4
title = LOCAL: title = BROKEN
[day] [day]
plugin = date plugin = date

View file

@ -1,10 +1,18 @@
import time import time
import threading
def execute(config, section): class PluginThread(threading.Thread):
fmt = config.get(section, 'format') def __init__(self, section, config, thread_id):
result = dict() threading.Thread.__init__(self)
if config.has_option(section, 'color'): self.threadID = thread_id
result['color'] = config.get(section, 'color') self.date_format = config.get(section, 'format')
result['full_text'] = time.strftime(fmt) self.status = dict()
return result if config.has_option(section, 'color'):
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)

View file

@ -4,22 +4,20 @@ import time
import threading import threading
class ConnTest(threading.Thread): class PluginThread(threading.Thread):
def __init__(self, threadID, name): def __init__(self, section, config, thread_id):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.threadID = threadID self.threadID = thread_id
self.name = name self.hosts = config.get(section, 'hosts').split(',')
self.hosts = list() self.title = config.get(section, 'title')
self.status = 'n/a' self.status = dict()
self.freq = int() self.freq = 5
self.format_status('n/a')
def configure(self, hosts, freq=5): def format_status(self, state):
self.hosts = hosts.split(',') self.status['full_text'] = self.title + ': ' + state
self.freq = freq
def run(self): def run(self):
global is_running
is_running = True
while True: while True:
random.shuffle(self.hosts) random.shuffle(self.hosts)
try: try:
@ -27,23 +25,10 @@ class ConnTest(threading.Thread):
fping = 'fping -q -c1 -t100 ' + host + ' &>/dev/null' fping = 'fping -q -c1 -t100 ' + host + ' &>/dev/null'
response = os.system(fping) response = os.system(fping)
if response == 0: if response == 0:
self.status = 'on' self.format_status('on')
break break
self.status = 'off' self.format_status('off')
except (KeyboardInterrupt, SystemExit): except (KeyboardInterrupt, SystemExit):
break break
time.sleep(self.freq) 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

View file

@ -3,6 +3,7 @@ import os
import configparser import configparser
import importlib import importlib
import plugins import plugins
import time
DEFAULT_CONFIG = os.path.join(os.environ['HOME'], 'IdeaProjects/vdstatus/conf.ini') DEFAULT_CONFIG = os.path.join(os.environ['HOME'], 'IdeaProjects/vdstatus/conf.ini')
@ -11,12 +12,26 @@ configuration = configparser.ConfigParser()
configuration.read(DEFAULT_CONFIG) 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(): def run_plugins():
outputs = list() plugins_l = load_plugins(configuration)
for section in configuration.sections(): for plugin in plugins_l:
if section == 'main': plugin.start()
continue
plugin_name = '.' + configuration.get(section, 'plugin') while True:
plugin_module = importlib.import_module(plugin_name, 'plugins') outputs = list()
outputs.append(plugin_module.execute(configuration, section)) for plugin in plugins_l:
return outputs outputs.append(plugin.status)
print(json.dumps(outputs) + ',')
time.sleep(1)

View file

@ -7,6 +7,4 @@ import time
print('{"version":1}') print('{"version":1}')
print('[') print('[')
while True: while True:
output = vdstatus.run_plugins() vdstatus.run_plugins()
print(json.dumps(output) + ',')
time.sleep(1)