-
Notifications
You must be signed in to change notification settings - Fork 0
/
monit.py
55 lines (42 loc) · 1.2 KB
/
monit.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
import requests
import json
import os
import sys
import pdb
import time
import threading
class Monit:
self.auth = "[PUT YOUR PUSHBULLET TOKEN HERE]"
def __init__(self, ip):
self.ip = ip
thread = threading.Thread(target=self.logic, args=())
thread.daemon = True # Daemonize thread
thread.start()
def send(self):
remote = "https://api.pushbullet.com/v2/pushes"
data = {"type": "note", "title": "Server Down!", "body": "Server "+self.ip+ " cannot be reached."}
session = requests.Session()
session.auth = (self.auth, "")
session.headers.update({'Content-Type': 'application/json'})
pureturn = session.post(remote, data=json.dumps(data))
return pureturn.reason
def ping(self):
response = os.system("ping -c 2 " + self.ip)
if response == 0:
return 0
else:
return 1
def logic(self):
res = self.ping()
if res == 1:
self.send()
time.sleep(3600)
self.logic()
else:
time.sleep(300)
self.logic()
monit = Monit(sys.argv[1])
time.sleep(3)
print('Checkpoint')
time.sleep(2)
print('Bye')