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]
|
[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
|
||||||
|
|
|
@ -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)
|
||||||
|
self.threadID = thread_id
|
||||||
|
self.date_format = config.get(section, 'format')
|
||||||
|
self.status = dict()
|
||||||
if config.has_option(section, 'color'):
|
if config.has_option(section, 'color'):
|
||||||
result['color'] = config.get(section, 'color')
|
self.status['color'] = config.get(section, 'color')
|
||||||
result['full_text'] = time.strftime(fmt)
|
self.freq = 1
|
||||||
return result
|
|
||||||
|
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
|
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
|
|
||||||
|
|
29
vdstatus.py
29
vdstatus.py
|
@ -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():
|
||||||
|
plugins_l = load_plugins(configuration)
|
||||||
|
for plugin in plugins_l:
|
||||||
|
plugin.start()
|
||||||
|
|
||||||
|
while True:
|
||||||
outputs = list()
|
outputs = list()
|
||||||
for section in configuration.sections():
|
for plugin in plugins_l:
|
||||||
if section == 'main':
|
outputs.append(plugin.status)
|
||||||
continue
|
print(json.dumps(outputs) + ',')
|
||||||
plugin_name = '.' + configuration.get(section, 'plugin')
|
time.sleep(1)
|
||||||
plugin_module = importlib.import_module(plugin_name, 'plugins')
|
|
||||||
outputs.append(plugin_module.execute(configuration, section))
|
|
||||||
return outputs
|
|
||||||
|
|
|
@ -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)
|
|
||||||
|
|
Loading…
Reference in a new issue