-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoni.py
63 lines (52 loc) · 1.58 KB
/
moni.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
import subprocess
import sys
import time
import subprocess
import socket
import logging
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
def lamp_red_on_fix():
subprocess.run("echo 1 > /sys/class/leds/orangepi\:red\:status/brightness", shell=True)
def lamp_red_on_blink(tim):
timeout = time.time() + tim # 5 minutes from now
while True:
subprocess.run("echo 0 > /sys/class/leds/orangepi\:red\:status/brightness", shell=True)
time.sleep(0.5)
subprocess.run("echo 1 > /sys/class/leds/orangepi\:red\:status/brightness", shell=True)
time.sleep(0.5)
if time.time() > timeout:
break
def internet(host="8.8.8.8", port=53, timeout=3):
"""
Host: 8.8.8.8 (google-public-dns-a.google.com)
OpenPort: 53/tcp
Service: domain (DNS/TCP)
"""
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
logger.info('Internet is there!!')
return True
except Exception as ex:
logger.warning('Internet is gone!!')
return False
def main():
test = 0
while True:
test+=1
success = internet()
if success:
lamp_red_on_fix()
time.sleep(60)
else:
lamp_red_on_blink(30)
if __name__ == '__main__':
try:
main()
except (KeyboardInterrupt, SystemExit):
subprocess.run("echo 0 > /sys/class/leds/orangepi\:red\:status/brightness", shell=True)
print('\nkeyboardinterrupt found!')
print('...Program Stopped Manually!')