-
Notifications
You must be signed in to change notification settings - Fork 1
/
ping.py
47 lines (46 loc) · 1.63 KB
/
ping.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
import os
import json
from pushover import Client
#set pushover client, hostnames to check, pushover message, and string to store results
client = Client("client goes here", api_token="api token here", device="device name optional")
#add hosts to be checked
hostname = ['localhost', '192.168.0.1']
msg = ""
data = { "offline":[]}
#open json file and store contents
with open('/home/pi/ping.json') as data_file:
data = json.load(data_file)
#ping each host set in hostname variable
for host in hostname:
response = os.system("ping -c 1 " + host)
if response == 0:
print str(host), 'is up!'
#check content for each offline key in json data
if host in data["offline"]:
#host is back online, remove host from offline key in json file
data["offline"].remove(host)
else:
#check to see if host is already known as offline
if host in data["offline"]:
#host found in offline key, no action necessary
print "Already sent notification for host " + str(host)
else:
#host not known as offline, send notification, add host to offline key
print str(host), 'is down!'
data["offline"].append(host)
msg = msg + "\n" + str(host) + " is down"
print "-----------------------------------------------------------------------------"
print " "
#converts our string to json
json_data = json.dumps(data)
print "Writing json data..."
print json_data
#writes udpated json file
with open('/home/pi/ping.json', 'r+') as f:
f.seek(0)
f.write(json.dumps(data))
f.truncate()
print "Json data written successfully"
if msg:
#msg has content, send notification
client.send_message(msg, title="pingbot")