-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping_worker.py
executable file
·138 lines (84 loc) · 2.92 KB
/
ping_worker.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import json, os, sys, time, datetime, traceback, logging
pathname = os.path.dirname(sys.argv[0])
fullpath = os.path.abspath(pathname)
config_file = fullpath + "/config/config.json"
data_file = fullpath + "/data.json"
delay = 20
def success(x):
hostname = data_object['People'][x]['hostname']
name = data_object['People'][x]['name']
online = True
current_time = str(datetime.datetime.now()).rsplit(".")[0].rsplit(":",1)[0]
data_object['People'][x] = {
"hostname": hostname,
"name": name,
"online": online,
"last_seen": current_time
}
def fail(x):
hostname = data_object['People'][x]['hostname']
name = data_object['People'][x]['name']
online = False
last_seen = ''
try:
last_seen = data_object['People'][x]['last_seen']
except Exception:
logging.exception('')
if last_seen == '':
data_object['People'][x] = {
"hostname": hostname,
"name": name,
"online": online
}
else:
data_object['People'][x] = {
"hostname": hostname,
"name": name,
"online": online,
"last_seen": last_seen
}
def pinghost(people, x):
# global pingthreads
ip = os.system("ping -c 1 " + people[x]['hostname'] + " >> /dev/null")
logging.info("pinging %s returned %s" % (people[x]['hostname'], ip))
if ip == 0:
# msg = str("PERSON ," + person['name'] + ",IN")
# self.sendMessage(msg)
success(x)
else:
# msg = str("PERSON ," + person['name'] + ",OUT")
# self.sendMessage(msg)
fail(x)
def update_people_list():
### Get people
with open(config_file) as conf_file:
json_data = json.load(conf_file)
people = json_data['Web']['People']
return people
############################
people = update_people_list()
### reset data file
## get file contents
with open(data_file) as d_file:
data_object = json.load(d_file)
## overwrite people array and write updated contents to file
with open(data_file, 'w') as d_file:
data_object['People'] = []
data_object['People'] = people
d_file.write(json.dumps(data_object, indent=4))
### setup done!
def main():
global people
while 1:
### start pinging
for x in range(0, len(people)):
logging.debug("Trying to ping %s..." % people[x]['hostname'])
pinghost(people, x)
### write results to file
data_to_file = json.dumps(data_object, indent=4)
with open(data_file, 'w') as d_file_out:
d_file_out.write(data_to_file)
logging.debug("Sleeping for %s seconds..." % delay)
time.sleep(delay)
logging.debug("Updating people list...")
people = update_people_list()