-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathping_contestant_machines.py
107 lines (75 loc) · 3.24 KB
/
ping_contestant_machines.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
#!/usr/bin/env python3
import csv
import json
import time
import subprocess
from collections import defaultdict
SEATING_LAYOUT = "seating.csv"
CONTESTANT_DATA = "users.csv"
JSON_CONTESTANT_SITE = "public/site.json"
JSON_STATUS = "public/status.json"
INTERVAL_IN_SECOND = 10
def arrange_contestant_data(contestants_filename, seating_filename):
"""Read contestant data, returns a seat -> username,ip matching"""
with open(contestants_filename) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=",")
next(csv_reader, None)
contestant_data = list(csv_reader)
contestant_lookup = {}
for contestant_row in contestant_data:
contestant_lookup[contestant_row[4]] = contestant_row
with open(seating_filename) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=",")
seating_data = list(csv_reader)
site_map = {}
for i in range(len(seating_data)):
for j in range(len(seating_data[i])):
seatcode = chr(ord('A') + i) + str(j + 1)
user_id = seating_data[i][j]
if user_id:
site_map[seatcode] = { 'user': user_id, 'ip': contestant_lookup[user_id][6] }
return site_map
def read_contestant_data(filename):
"""Read contestant data, returns a map of hostname to its ip address"""
with open(filename) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=",")
next(csv_reader, None)
# change the index if csv format is changed
return {row[6]: row[4] for row in csv_reader}
def read_status(filename):
"""Read latest status, returns a map of hostname to its latest status"""
try:
with open(filename) as json_file:
status = json.load(json_file)
return {row[0]: row[1] for row in status["results"]}
except (FileNotFoundError, json.decoder.JSONDecodeError):
return defaultdict(lambda: None)
def get_hosts_status(ip_to_hostname, hostname_to_status):
"""Ping all hosts, returns an array of [hostname, status] sorted by hostname"""
cmd = ["fping", "-C", "1", "-t", "1000", "-q"] + list(ip_to_hostname.keys())
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ip_to_status = defaultdict(lambda: None)
for line in proc.communicate()[1].decode("utf-8").split("\n"):
if not line:
continue
ip, status = line.split(" : ")
ip = ip.strip()
hostname = ip_to_hostname[ip]
if status != "-" or hostname_to_status[hostname]:
ip_to_status[ip] = status
result = [[hostname, ip_to_status[ip]] for ip, hostname in ip_to_hostname.items()]
return sorted(result)
if __name__ == "__main__":
with open(JSON_CONTESTANT_SITE, "w") as json_file:
json.dump(arrange_contestant_data(CONTESTANT_DATA, SEATING_LAYOUT), json_file)
ip_to_hostname = read_contestant_data(CONTESTANT_DATA)
while True:
hostname_to_status = read_status(JSON_STATUS)
results = get_hosts_status(ip_to_hostname, hostname_to_status)
status = {
"time": time.time(),
"results": results,
}
with open(JSON_STATUS, "w") as json_file:
json.dump(status, json_file)
time.sleep(INTERVAL_IN_SECOND)