-
Notifications
You must be signed in to change notification settings - Fork 3
/
piping.py
executable file
·172 lines (144 loc) · 4.87 KB
/
piping.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# coding=utf-8
import os
import subprocess
import requests
import plotly
import time
import http.server
import socketserver
import threading
import plotly.graph_objs as go
from datetime import datetime
from config import *
os.chdir(os.path.dirname(os.path.realpath(__file__)))
# write to file function
def filewrite(filename, mode, string):
try:
f = open(filename, mode)
f.write(str(string))
f.close()
except Exception as e:
filewrite(filename, 'a', 'Error: File write' + e)
filewrite(
log_file,
'a',
'\n\t PiPing raised up\n' + datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
if gpiomode:
filewrite(log_file, 'a', 'Mode: GPIO MODE')
import RPi.GPIO as GPIO # Import GPIO library
# main ping function
def ping(hostname):
try:
# ping command
pingcmd = "ping -c 1 " + hostname + \
" | tail -1 | awk \'{print $4}\' | cut -d '/' -f 2 "
# get ping stdout
response = subprocess.run(pingcmd, shell=True, stdout=subprocess.PIPE)
pingresponse = response.stdout.decode('utf-8')
pingtime = pingresponse.split('.')[0]
# get ping time
if pingtime.isdigit():
pingtimenum = int(pingtime)
return pingtimenum
else:
filewrite(log_file, 'a', '\nE: Error while pinging!')
return None
# Error in pinging
except Exception as e:
filewrite(log_file, 'a', '\nE: Error while pinging ' + e)
return None
# check internet connection
def net_status(net_status_server):
# check internet connection
try:
netstat = requests.get(net_status_server, timeout=1).status_code
if netstat == 200:
return True
else:
return False
if gpiomode:
blink(redpin, 0.3, 2)
except Exception as e:
return False
filewrite(log_file, 'a', '\nE: No Connection ' + e)
def blink(pin, timeon, number):
# Use board pin numbering
GPIO.setmode(GPIO.BOARD)
# set warning
GPIO.setwarnings(False)
GPIO.setup(pin, GPIO.OUT)
for i in range(0, number):
GPIO.output(pin, True)
time.sleep(timeon)
GPIO.output(pin, False)
time.sleep(timeon)
class HttpThread(threading.Thread):
def run(self):
httpd = socketserver.TCPServer(("", http_port), Handler)
httpd.serve_forever()
if __name__ == '__main__':
timeplot = []
plotdata = []
cplot = []
n = 0
if http_server:
# add http handler
Handler = http.server.SimpleHTTPRequestHandler
# run http server on port
HttpThread().start()
filewrite(log_file, 'a', '\nplot on http://localhost:' + str(http_port))
while True:
time.sleep(sleeptime)
n += 1
# send ping time to x list for plot
timeplot.append(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
cplot.append(condition)
for server in serverslist:
if server.type == 'online':
if net_status(net_status_server):
pingnum = ping(server.ip)
server.ping.append(pingnum)
if server.gpio and gpiomode and pingnum:
if pingnum <= condition:
blink(greenpin, 0.3, 1)
elif pingnum > condition:
blink(redpin, 0.3, 1)
else:
server.ping.append(None)
if gpiomode:
blink(redpin, 0.5, 2)
elif server.type == 'offline':
pingnum = ping(server.ip)
server.ping.append(pingnum)
if gpiomode and pingnum:
blink(bluepin, 0.3, 1)
else:
filewrite(log_file, 'a', '\nE: Server type error ')
if n % plottime == 0:
plotdata.clear()
for server in serverslist:
plotdata.append(go.Scatter(
x=timeplot,
y=server.ping,
mode='lines+markers',
name=server.name
))
# condition plot
conditionplot = go.Scatter(
x=timeplot,
y=cplot,
mode='lines',
name='condition'
)
plotdata.append(conditionplot)
plotlayout = dict(title='Ping Graph')
plotinput = dict(data=plotdata, layout=plotlayout)
# drow plot
plot = plotly.offline.plot(plotinput, filename='index.html', auto_open=False)
filewrite(log_file, 'a', '\nPlot: ' + plot)
if n % reset == 0:
for server in serverslist:
server.ping.clear()
timeplot.clear()
cplot.clear()
n = 0