-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreal_inspector.py
More file actions
47 lines (33 loc) · 1.29 KB
/
real_inspector.py
File metadata and controls
47 lines (33 loc) · 1.29 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
import datetime
import requests
class Server:
def __init__(self, name, url):
self.name = name
self.url = url
self.status = "Unknown"
self.response_code = 0
def check_health(self):
try:
r = requests.get(self.url, timeout=2)
self.response_code = r.status_code
if r.status_code == 200:
self.status = "Online"
else:
self.status = f"ERROR ({r.status_code})"
except requests.exceptions.RequestException:
self.status = "Offline"
self.response_code = 0
server_list = [Server("web-01", "http://google.com"),Server("db-01", "192.168.7.39"),Server("api-01","http://yahoo.com")]
print("---- start health checking ----")
with open("health_report.txt","w") as report:
now = datetime.datetime.now()
report.write(f"Report generated: {now}\n")
report.write("-" * 30 + "\n")
for srv in server_list:
srv.check_health()
if srv.status == "Online":
print(f"{srv.name} is UP")
else:
print(f"{srv.name} in Down")
report.write(f"{srv.name} ({srv.url}): {srv.status}\n")
print("\nDone! check 'health_report.txt'.")