-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.py
executable file
·72 lines (49 loc) · 1.51 KB
/
monitor.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
import urllib2
import subprocess, signal
from datetime import datetime, timedelta
REQUEST_URLS = 'http://dreamtale90.free.ngrok.cc/heartbeat'
def my_print(data):
curTime = datetime.now().strftime('%Y/%m/%d %H:%M:%S')
print '[%s] %s' %(curTime, data)
def send_request():
for i in range(3):
result = None
try:
req = urllib2.Request(REQUEST_URLS)
response = urllib2.urlopen(req, timeout = 20)
result = response.read()
except urllib2.URLError, e:
my_print(e.reason)
#check result
if result == 'OK4LIVE':
return True
return False
def kill_proc_by_name(ProcName):
p = subprocess.Popen(['ps', '-A'], stdout = subprocess.PIPE)
out, err = p.communicate()
for line in out.splitlines():
if ProcName in line:
pid = int(line.split(None, 1)[0])
os.kill(pid, signal.SIGKILL)
return True
my_print(ProcName + ' process is not exist !')
return False
def check_loop():
while True:
time.sleep(900)
#send a request
send_ret = send_request()
if send_ret == False:
my_print('http server not respond !')
#kill ngrok process
kill_ret = kill_proc_by_name('sunny')
if kill_ret == False:
#reboot
os.system('reboot')
if __name__ == '__main__':
my_print('Monitor Start !')
check_loop()