-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstatusmanager.py
More file actions
78 lines (66 loc) · 2.2 KB
/
statusmanager.py
File metadata and controls
78 lines (66 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from dbmanager import PMDbManager
import threading
import re
import time
from subprocess import getstatusoutput as gso
class PMPingThread:
def __init__(self, server):
self.server = server
self.status = "Pending"
self.cycle_pause = 2
self.stopped = False
self.thread = threading.Thread(target=self.ping_loop)
self.thread.start()
def ping_loop(self):
while True:
if self.stopped:
break
ping_status_output = gso('ping -W 2 -c 1 ' + self.server)
if ping_status_output[0] == 0:
host_state = re.search('time=(\d+\.*\d+\s)ms', ping_status_output[1])
if host_state is not None:
time.sleep(self.cycle_pause)
self.status = '{} ms'.format(host_state.group(1))
elif ping_status_output[0] == 1:
self.status = 'offline'
elif ping_status_output[0] == 2:
time.sleep(self.cycle_pause)
if ping_status_output[1].find('unknown host'):
self.status = 'unk host'
elif ping_status_output[1].find('connect:'):
self.status = 'no net'
def stop(self):
self.cycle_pause = 0
self.stopped = True
class PMStatusManager:
def __init__(self, db_manager: PMDbManager):
self.db = db_manager
self.storage = {}
self.start()
def start(self):
"""
Starts all ping threads
"""
for i in self.db.get_servers():
s = (i[0], PMPingThread(i[1]))
if i[2] not in self.storage:
self.storage[i[2]] = [s]
else:
self.storage[i[2]].append(s)
def get_group(self, group_id):
"""
:param group_id:
:return: list of tuples (server_name, server_status) owned by group
"""
return self.storage[int(group_id)]
def stop(self):
"""
Stops all ping threads
"""
for group in self.storage:
for server in self.storage[group]:
server[1].stop()
self.storage = {}
def restart(self):
self.stop()
self.start()