-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathikmp.py
87 lines (73 loc) · 2.58 KB
/
ikmp.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
from icmplib import ping
import json
import os
import time
import requests
with open('config.json') as fd:
json_data = json.load(fd)
def icmp(hostname: str):
host = ping(hostname, count=5, interval=0.2)
return host.packets_sent == host.packets_received
def http(hostname: str):
try:
r = requests.head(hostname)
return str(r.status_code)
except:
return False
with open('uptime.json', 'r') as f:
uptimeFile = json.load(f)
interval = json_data['interval']
debug = int(json_data['debug'])
def uptime():
for i in json_data['hosts']:
if json_data['hosts'][i]['monitor_type'] == "icmp":
if debug == 1 or debug == 2:
print(json_data['hosts'][i]['address'])
host = json_data['hosts'][i]['address']
try:
if icmp(host):
if debug == 1 or debug == 2:
print("UP")
uptimeFile['hosts'][i] = (f"UP")
except Exception as e:
if debug == 1:
print("DOWN")
if debug == 2:
print("DOWN - Error :", e)
uptimeFile['hosts'][i] = (f"DOWN")
os.remove('uptime.json')
with open('uptime.json', 'w') as f:
json.dump(uptimeFile, f, indent=4)
elif json_data['hosts'][i]['monitor_type'] == "http":
if debug == 1 or debug == 2:
print(json_data['hosts'][i]['address'])
host = json_data['hosts'][i]['address']
expResp = json_data['hosts'][i]['HERC']
respCode = http(host)
try:
if respCode in expResp:
if debug == 1:
print("UP")
if debug == 2:
print("UP",respCode)
uptimeFile['hosts'][i] = (f"UP")
else:
if debug == 1:
print("DOWN")
if debug == 2:
print("DOWN",respCode)
uptimeFile['hosts'][i] = (f"DOWN")
except Exception as e:
if debug == 1:
print("DOWN")
if debug == 2:
print("DOWN - Error :", e)
uptimeFile['hosts'][i] = (f"DOWN")
os.remove('uptime.json')
with open('uptime.json', 'w') as f:
json.dump(uptimeFile, f, indent=4)
while True:
uptime();
if debug == 1 or debug == 2:
print(f"Waiting for {interval} seconds")
time.sleep(int(interval))