vdstatus/plugins/ping.py

44 lines
1.4 KiB
Python
Raw Normal View History

import os
import random
import time
import threading
class PluginThread(threading.Thread):
def __init__(self, section, config, thread_id):
threading.Thread.__init__(self)
self.threadID = thread_id
self.hosts = config.get(section, 'hosts').split(',')
self.title = config.get(section, 'title')
2016-10-22 19:27:28 +03:00
if config.has_option(section, 'colors'):
self.colors = config.get(section, 'colors').split(',')
else:
self.colors = None
self.timeout = config.get(section, 'timeout', fallback='150')
self.status = dict()
2016-10-22 19:27:28 +03:00
self.freq = config.getint(section, 'freq', fallback=5)
self.format_status('n/a')
def format_status(self, state):
self.status['full_text'] = self.title + ': ' + state
2016-10-22 19:27:28 +03:00
if self.colors is not None:
if state == 'on':
self.status['color'] = self.colors[0]
else:
self.status['color'] = self.colors[1]
def main(self):
random.shuffle(self.hosts)
for host in self.hosts:
2016-10-23 12:09:06 +03:00
fping = 'fping -qc1t' + self.timeout + ' ' + host + ' &>/dev/null'
2016-10-22 19:27:28 +03:00
response = os.system(fping)
if response == 0:
self.format_status('on')
break
self.format_status('off')
def run(self):
while True:
2016-10-22 19:27:28 +03:00
self.main()
time.sleep(self.freq)