2016-10-19 20:06:19 +03:00
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import time
|
|
|
|
import threading
|
|
|
|
|
|
|
|
|
2016-10-22 05:50:11 +03:00
|
|
|
class PluginThread(threading.Thread):
|
|
|
|
def __init__(self, section, config, thread_id):
|
2016-10-19 20:06:19 +03:00
|
|
|
threading.Thread.__init__(self)
|
2016-10-22 05:50:11 +03:00
|
|
|
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
|
|
|
self.timeout = config.get(section, 'timeout', fallback='150')
|
2016-10-22 05:50:11 +03:00
|
|
|
self.status = dict()
|
2016-10-22 19:27:28 +03:00
|
|
|
self.freq = config.getint(section, 'freq', fallback=5)
|
2016-10-22 05:50:11 +03:00
|
|
|
self.format_status('n/a')
|
2016-10-25 19:48:40 +03:00
|
|
|
self.hide = False
|
2016-11-18 17:42:28 +02:00
|
|
|
self.should_stop = False
|
2016-10-19 20:06:19 +03:00
|
|
|
|
2016-10-22 05:50:11 +03:00
|
|
|
def format_status(self, state):
|
|
|
|
self.status['full_text'] = self.title + ': ' + state
|
2016-10-25 19:48:40 +03:00
|
|
|
if state == 'on':
|
|
|
|
self.status['urgent'] = False
|
|
|
|
self.hide = True
|
|
|
|
else:
|
|
|
|
self.status['urgent'] = True
|
2016-10-22 19:27:28 +03:00
|
|
|
|
|
|
|
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')
|
2016-10-19 20:06:19 +03:00
|
|
|
|
2016-11-18 17:42:28 +02:00
|
|
|
def sleep(self):
|
|
|
|
seconds = 0
|
|
|
|
while seconds < self.freq:
|
|
|
|
time.sleep(1)
|
|
|
|
seconds += 1
|
|
|
|
del seconds
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.should_stop = True
|
|
|
|
|
2016-10-19 20:06:19 +03:00
|
|
|
def run(self):
|
|
|
|
while True:
|
2016-11-18 17:42:28 +02:00
|
|
|
if self.should_stop is False:
|
|
|
|
self.main()
|
|
|
|
self.sleep()
|
|
|
|
else:
|
|
|
|
break
|