|
1 |
| -import RPi.GPIO as GPIO ## Import GPIO library |
2 |
| -import subprocess,time,requests,plotly |
| 1 | +# coding=utf-8 |
| 2 | + |
| 3 | +import subprocess |
| 4 | +import requests |
| 5 | +import plotly |
| 6 | +import time |
3 | 7 | import plotly.graph_objs as go
|
4 | 8 | from datetime import datetime
|
5 | 9 |
|
6 |
| -# coding=utf-8 |
7 | 10 |
|
8 |
| -# Use board pin numbering |
9 |
| -GPIO.setmode(GPIO.BOARD) |
| 11 | +serverslist = [] |
| 12 | + |
| 13 | + |
| 14 | +class servers: |
10 | 15 |
|
11 |
| -#set warning |
12 |
| -GPIO.setwarnings(False) |
| 16 | + def __init__(self, name, ip, type, gpio): |
| 17 | + self.name = name |
| 18 | + self.ip = ip |
| 19 | + self.type = type |
| 20 | + self.gpio = gpio |
| 21 | + self.ping = [] |
| 22 | + serverslist.append(self) |
13 | 23 |
|
14 | 24 |
|
15 |
| -## you can use input |
16 |
| -# hostname = input('hostname: ') |
17 |
| -# local = input('local: ') |
18 |
| -# condition = input('condition: ') |
19 |
| -# testserver = input('test server: ') |
| 25 | +# server config |
| 26 | +# name = servers('server name', 'ip', 'ping in offline/online', |
| 27 | +# 'enable GPIO (on raspberrypi) check with condition True/False') |
| 28 | +google = servers('Google', '8.8.8.8', 'online', True) |
| 29 | +modem = servers('DSL', '192.168.1.1', 'offline', False) |
| 30 | +router = servers('mikrotik', '10.0.0.1', 'offline', False) |
| 31 | +verizon = servers('Verizon', '4.2.2.4', 'online', False) |
20 | 32 |
|
21 |
| -## or just static value |
22 |
| -hostname = '8.8.8.8' |
23 |
| -local = '10.0.0.1' |
24 |
| -condition = '150' |
25 |
| -testserver = 'http://icanhazip.com' |
26 |
| -sleeptime = 1 #int not string |
27 |
| -reset = 8000 |
28 |
| -plottime = 10 |
29 | 33 |
|
| 34 | +# config |
| 35 | +condition = 150 # condition to comparison |
| 36 | +net_status_server = 'http://icanhazip.com' # check conection to net |
| 37 | +sleeptime = 1 # sleep between each ping (second) |
| 38 | +reset = 5000 # reset data plot after this time (Natural numbers) |
| 39 | +plottime = 10 # drow plot after this time (Natural numbers) |
| 40 | +gpiomode = False # enable on raspberrypi (True/Flase) |
| 41 | +greenpin = 7 # gpio pin conected to Green LED on BOARD mode |
| 42 | +redpin = 3 # gpio pin conected to Red LED on BOARD mode |
| 43 | +bluepin = 10 # gpio pin conected to Blue LED on BOARD mode |
| 44 | + |
| 45 | + |
| 46 | +if gpiomode: |
| 47 | + import RPi.GPIO as GPIO # Import GPIO library |
30 | 48 |
|
31 |
| -# variable |
32 |
| -yplot = [] |
33 |
| -xplot = [] |
34 |
| -lpp = [] |
35 |
| -n = 0 |
36 |
| -cplot = [condition] |
37 | 49 |
|
38 | 50 | # main ping function
|
39 |
| -def check_ping(hostname): |
| 51 | +def ping(hostname): |
40 | 52 | try:
|
41 | 53 | # ping command
|
42 | 54 | pingcmd = "ping -c 1 " + hostname + " | tail -1 | awk \'{print $4}\' | cut -d '/' -f 2 "
|
43 | 55 | # get ping stdout
|
44 |
| - response = subprocess.run(pingcmd,shell=True,stdout=subprocess.PIPE) |
45 |
| - pingtime = response.stdout.decode('utf-8') |
| 56 | + response = subprocess.run(pingcmd, shell=True, stdout=subprocess.PIPE) |
| 57 | + pingresponse = response.stdout.decode('utf-8') |
| 58 | + pingtime = pingresponse.split('.')[0] |
46 | 59 | # get ping time
|
47 |
| - pingtimenum = pingtime.split('.')[0] |
48 |
| - return pingtimenum |
| 60 | + if pingtime.isdigit(): |
| 61 | + pingtimenum = int(pingtime) |
| 62 | + return pingtimenum |
| 63 | + else: |
| 64 | + print('Error while pinging!') |
| 65 | + return None |
49 | 66 |
|
50 | 67 | # Error in pinging
|
51 | 68 | except:
|
52 |
| - print('Error!') |
53 |
| - GPIO.output(3,True) |
54 |
| - time.sleep(0.3) |
55 |
| - GPIO.output(3,False) |
56 |
| - time.sleep(0.3) |
57 |
| - GPIO.output(3,True) |
58 |
| - time.sleep(0.3) |
59 |
| - GPIO.output(3,False) |
60 |
| - |
61 |
| -GPIO.setup(7, GPIO.OUT) # Setup GPIO Pin 7 to OUT , Green LED |
62 |
| -GPIO.setup(3, GPIO.OUT) # Setup GPIO Pin 3 to OUT , Red LED |
63 |
| -GPIO.setup(10, GPIO.OUT) # Setup GPIO Pin 10 to OUT , Blue LED |
64 |
| - |
65 |
| -# main loop |
66 |
| -while True: |
67 |
| - # sleep between each ping |
68 |
| - time.sleep(sleeptime) |
69 |
| - |
70 |
| - # loop counter |
71 |
| - n += 1 |
72 |
| - |
73 |
| - # now time |
74 |
| - t = datetime.now().strftime('%Y-%m-%d %H:%M:%S') |
75 |
| - # send ping time to x list for plot |
76 |
| - xplot.append(t) |
77 |
| - |
78 |
| - # ping local router |
79 |
| - localpingtimenum = check_ping(local) |
80 |
| - print(local + ' :: ' + localpingtimenum) |
81 |
| - if localpingtimenum.isdigit(): |
82 |
| - # send local ping to lpp list for plot |
83 |
| - lpp.append(int(localpingtimenum)) |
84 |
| - |
85 |
| - # blink blue LED if router is available |
86 |
| - if localpingtimenum : |
87 |
| - GPIO.output(10,True) |
88 |
| - time.sleep(0.1) |
89 |
| - GPIO.output(10,False) |
90 |
| - |
91 |
| - # blink Red LED if router is not available |
92 |
| - else : |
93 |
| - GPIO.output(3,True) |
94 |
| - time.sleep(0.3) |
95 |
| - GPIO.output(3,False) |
96 |
| - time.sleep(0.3) |
97 |
| - GPIO.output(3,True) |
98 |
| - time.sleep(0.3) |
99 |
| - GPIO.output(3,False) |
100 |
| - print('local is not available') |
101 |
| - |
102 |
| - |
103 |
| - # sleep between local and server ping |
104 |
| - time.sleep(0.3) |
105 |
| - |
106 |
| - # check internet connection |
107 |
| - try: |
108 |
| - netstat = requests.get(testserver,timeout=1).status_code |
109 |
| - if netstat == 200 : |
110 |
| - status = True |
111 |
| - except: |
112 |
| - status = False |
113 |
| - print('internet is not reachable') |
114 |
| - |
115 |
| - if status : |
116 |
| - # check server ping |
117 |
| - pingtimenum = check_ping(hostname) |
118 |
| - print(hostname + ' :: ' + pingtimenum) |
119 |
| - |
120 |
| - if pingtimenum.isdigit(): |
121 |
| - # blink Green LED for low ping |
122 |
| - if int(pingtimenum) < int(condition) : |
123 |
| - print("green") |
124 |
| - GPIO.output(7,True) |
125 |
| - time.sleep(0.1) |
126 |
| - GPIO.output(7,False) |
127 |
| - # blink Red LED for high ping |
128 |
| - elif int(pingtimenum) > int(condition) : |
129 |
| - print("red") |
130 |
| - GPIO.output(3,True) |
131 |
| - time.sleep(0.1) |
132 |
| - GPIO.output(3,False) |
133 |
| - |
134 |
| - # send server ping for plot |
135 |
| - yplot.append(int(pingtimenum)) |
136 |
| - |
137 |
| - # internet connection is not reachable |
138 |
| - else: |
139 |
| - yplot.append(None) |
140 |
| - print('internet is not reachable') |
141 |
| - GPIO.output(3,True) |
142 |
| - time.sleep(0.3) |
143 |
| - GPIO.output(3,False) |
144 |
| - time.sleep(0.3) |
145 |
| - GPIO.output(3,True) |
146 |
| - time.sleep(0.3) |
147 |
| - GPIO.output(3,False) |
148 |
| - |
149 |
| - |
150 |
| - cplot.append(condition) |
151 |
| - |
152 |
| - # creat plot every 10 ping |
153 |
| - if n%plottime == 0 : |
154 |
| - # local ping plot |
155 |
| - localplot = go.Scatter( |
156 |
| - x = xplot, |
157 |
| - y = lpp, |
158 |
| - mode = 'lines+markers', |
159 |
| - name = 'local ping' |
160 |
| - ) |
161 |
| - # server ping plot |
162 |
| - serverplot = go.Scatter( |
163 |
| - x = xplot, |
164 |
| - y = yplot, |
165 |
| - mode = 'lines+markers', |
166 |
| - name = hostname |
167 |
| - ) |
168 |
| - # condition plot |
169 |
| - conditionplot = go.Scatter( |
170 |
| - x = xplot, |
171 |
| - y = cplot, |
172 |
| - mode = 'lines', |
173 |
| - name = 'condition' |
174 |
| - ) |
175 |
| - plotdata = [localplot,serverplot,conditionplot] |
176 |
| - plotlayout = dict(title = 'Ping Graph') |
177 |
| - plotinput = dict(data=plotdata, layout=plotlayout) |
178 |
| - |
179 |
| - # drow plot |
180 |
| - plot = plotly.offline.plot(plotinput,filename='ping-graph.html',auto_open=False) |
181 |
| - print('plot: ' + plot) |
182 |
| - |
183 |
| - if n%reset == 0 : |
184 |
| - yplot = [] |
185 |
| - xplot = [] |
186 |
| - lpp = [] |
187 |
| - n = 0 |
| 69 | + print('Error while pinging!') |
| 70 | + return None |
| 71 | + |
| 72 | + |
| 73 | +# check internet connection |
| 74 | +def net_status(net_status_server): |
| 75 | + # check internet connection |
| 76 | + try: |
| 77 | + netstat = requests.get(net_status_server, timeout=1).status_code |
| 78 | + if netstat == 200: |
| 79 | + return True |
| 80 | + else: |
| 81 | + return False |
| 82 | + if gpiomode: |
| 83 | + blink(redpin, 0.3, 2) |
| 84 | + except: |
| 85 | + return False |
| 86 | + |
| 87 | + |
| 88 | +def blink(pin, timeon, number): |
| 89 | + # Use board pin numbering |
| 90 | + GPIO.setmode(GPIO.BOARD) |
| 91 | + # set warning |
| 92 | + GPIO.setwarnings(False) |
| 93 | + GPIO.setup(pin, GPIO.OUT) |
| 94 | + for i in range(0, number): |
| 95 | + GPIO.output(pin, True) |
| 96 | + time.sleep(timeon) |
| 97 | + GPIO.output(pin, False) |
| 98 | + time.sleep(timeon) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + timeplot = [] |
| 103 | + plotdata = [] |
| 104 | + cplot = [] |
| 105 | + n = 0 |
| 106 | + |
| 107 | + while True: |
| 108 | + time.sleep(sleeptime) |
| 109 | + |
| 110 | + n += 1 |
| 111 | + # send ping time to x list for plot |
| 112 | + timeplot.append(datetime.now().strftime('%Y-%m-%d %H:%M:%S')) |
| 113 | + |
| 114 | + cplot.append(condition) |
| 115 | + |
| 116 | + for server in serverslist: |
| 117 | + if server.type == 'online': |
| 118 | + if net_status(net_status_server): |
| 119 | + pingnum = ping(server.ip) |
| 120 | + server.ping.append(pingnum) |
| 121 | + if server.gpio and gpiomode and pingnum: |
| 122 | + if pingnum <= condition: |
| 123 | + blink(greenpin, 0.3, 1) |
| 124 | + elif pingnum > condition: |
| 125 | + blink(redpin, 0.3, 1) |
| 126 | + else: |
| 127 | + server.ping.append(None) |
| 128 | + elif server.type == 'offline': |
| 129 | + pingnum = ping(server.ip) |
| 130 | + server.ping.append(pingnum) |
| 131 | + if gpiomode and pingnum: |
| 132 | + blink(bluepin, 0.3, 1) |
| 133 | + |
| 134 | + else: |
| 135 | + print('server type Error') |
| 136 | + |
| 137 | + if n % plottime == 0: |
| 138 | + plotdata.clear() |
| 139 | + for server in serverslist: |
| 140 | + plotdata.append(go.Scatter( |
| 141 | + x = timeplot, |
| 142 | + y = server.ping, |
| 143 | + mode = 'lines+markers', |
| 144 | + name = server.name |
| 145 | + )) |
| 146 | + # condition plot |
| 147 | + conditionplot = go.Scatter( |
| 148 | + x = timeplot, |
| 149 | + y = cplot, |
| 150 | + mode = 'lines', |
| 151 | + name = 'condition' |
| 152 | + ) |
| 153 | + plotdata.append(conditionplot) |
| 154 | + plotlayout = dict(title = 'Ping Graph') |
| 155 | + plotinput = dict(data=plotdata, layout=plotlayout) |
| 156 | + |
| 157 | + # drow plot |
| 158 | + plot = plotly.offline.plot(plotinput, filename='ping-graph.html', auto_open=False) |
| 159 | + print('plot: ' + plot) |
| 160 | + |
| 161 | + if n % reset == 0: |
| 162 | + for server in serverslist: |
| 163 | + server.ping.clear() |
| 164 | + timeplot.clear() |
| 165 | + cplot.clear() |
| 166 | + n = 0 |
0 commit comments