-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckup.py
88 lines (77 loc) · 2.3 KB
/
checkup.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
import time
import os
# debug level: 0 = least, 1 = some, 2 = more
debug = 0
# parse config file for parameters
def parseConfigFile():
global Host
global Interval
global Notify
global Address
with open("checkup.conf", "r") as f:
for line in f:
if line[0] == '#' or line[0] == ' ' or line[0] == '\n':
pass
else:
if debug >= 2:
print("parsing",line,end='')
args = line.strip().split(" ")
if debug >= 2:
print("args",args)
if args[0] == "Interval":
if debug >= 1:
print("Setting Interval to",args[1])
Interval = int(args[1])
if args[0] == "Notify":
if debug >= 1:
print("Will notify",args[1])
Notify = args[1]
if args[0] == "Host":
if debug >= 2:
print("Adding host",args[1])
parts = args[1].split("@")
if debug >= 1:
print("Adding host",parts[0],"located at",parts[1])
Host = parts[0]
Address = parts[1]
def downToUp():
print("Alert: Host came up!")
def upToDown():
print("Alert: Host went down!")
# loop through checks
def loop():
if debug >= 2:
print("Starting loop")
firstLoop = True
command = "ping -c 1 "+Address+"> /dev/null 2>&1"
if debug >= 2:
print("State command:",command)
while(True):
state = os.system(command)
if state == 0:
if debug >= 1:
print("Host is up")
else:
if debug >= 1:
print("Host is down")
if firstLoop:
firstLoop = False
lastState = state
else:
if lastState != state:
if state:
upToDown()
else:
downToUp()
lastState = state
if debug >= 2:
print("Finished loop")
time.sleep(Interval)
# get things going
def startUp():
print("Initializing....")
print("Parsing config file....")
parseConfigFile()
print("Starting...")
loop()
startUp()