a functional ping plugin, but needs moar exceptions and sanity checks

This commit is contained in:
Von Random 2016-10-19 20:06:19 +03:00
parent e2ca550f37
commit d77dbeb1de
5 changed files with 76 additions and 6 deletions

View file

@ -1,10 +1,20 @@
[main] [main]
[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:
[ping_local]
plugin = ping
hosts = 127.0.0.1,127.0.0.2
title = LOCAL:
[day] [day]
plugin = date plugin = date
color = red color = #FF0000
format = %%A %%d format = %%A %%d
[time] [time]
plugin = date plugin = date
format = %%H:%%M format = %%H:%%M

View file

@ -1,7 +1,7 @@
import time import time
def run(config, section): def execute(config, section):
fmt = config.get(section, 'format') fmt = config.get(section, 'format')
result = dict() result = dict()
if config.has_option(section, 'color'): if config.has_option(section, 'color'):

49
plugins/ping.py Normal file
View file

@ -0,0 +1,49 @@
import os
import random
import time
import threading
class ConnTest(threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.hosts = list()
self.status = 'n/a'
self.freq = int()
def configure(self, hosts, freq=5):
self.hosts = hosts.split(',')
self.freq = freq
def run(self):
global is_running
is_running = True
while True:
random.shuffle(self.hosts)
try:
for host in self.hosts:
fping = 'fping -q -c1 -t100 ' + host + ' &>/dev/null'
response = os.system(fping)
if response == 0:
self.status = 'on'
break
self.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

View file

@ -18,5 +18,5 @@ def run_plugins():
continue continue
plugin_name = '.' + configuration.get(section, 'plugin') plugin_name = '.' + configuration.get(section, 'plugin')
plugin_module = importlib.import_module(plugin_name, 'plugins') plugin_module = importlib.import_module(plugin_name, 'plugins')
outputs.append(plugin_module.run(configuration, section)) outputs.append(plugin_module.execute(configuration, section))
print(json.dumps(outputs) + ',') return outputs

View file

@ -1 +1,12 @@
#!/usr/bin/env python3 -u #!/usr/bin/python3 -u
import json
import vdstatus
import time
print('{"version":1}')
print('[')
while True:
output = vdstatus.run_plugins()
print(json.dumps(output) + ',')
time.sleep(1)