10
10
import threading
11
11
import plotly .graph_objs as go
12
12
from datetime import datetime
13
+ from config import *
13
14
14
- serverslist = []
15
15
os .chdir (os .path .dirname (os .path .realpath (__file__ )))
16
16
17
17
18
- class servers :
19
-
20
- def __init__ (self , name , ip , type , gpio ):
21
- self .name = name
22
- self .ip = ip
23
- self .type = type
24
- self .gpio = gpio
25
- self .ping = []
26
- serverslist .append (self )
27
-
28
-
29
- # server config
30
- # name = servers('server name', 'ip', 'ping in offline/online',
31
- # 'enable GPIO (on raspberrypi) check with condition True/False')
32
- google = servers ('Google' , '8.8.8.8' , 'online' , True )
33
- modem = servers ('DSL' , '192.168.1.1' , 'offline' , False )
34
- router = servers ('mikrotik' , '10.0.0.1' , 'offline' , False )
35
- verizon = servers ('Verizon' , '4.2.2.4' , 'online' , False )
36
-
18
+ # write to file function
19
+ def filewrite (filename , mode , string ):
20
+ try :
21
+ f = open (filename , mode )
22
+ f .write (str (string ))
23
+ f .close ()
24
+ except Exception as e :
25
+ filewrite (filename , 'a' , 'Error: File write' + e )
37
26
38
- # config
39
- condition = 150 # condition to comparison
40
- net_status_server = 'http://icanhazip.com' # check conection to net
41
- sleeptime = 1 # sleep between each ping (second)
42
- reset = 5000 # reset data plot after this time (Natural numbers)
43
- plottime = 10 # drow plot after this time (Natural numbers)
44
- http_server = True # enable/disable web server
45
- http_port = 8000 # http port
46
- gpiomode = False # enable on raspberrypi (True/Flase)
47
- greenpin = 7 # gpio pin conected to Green LED on BOARD mode
48
- redpin = 3 # gpio pin conected to Red LED on BOARD mode
49
- bluepin = 10 # gpio pin conected to Blue LED on BOARD mode
50
27
28
+ filewrite (
29
+ log_file ,
30
+ 'a' ,
31
+ '\n \t PiPing raised up\n ' + datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' ))
51
32
52
33
if gpiomode :
34
+ filewrite (log_file , 'a' , 'Mode: GPIO MODE' )
53
35
import RPi .GPIO as GPIO # Import GPIO library
54
36
55
37
56
38
# main ping function
57
39
def ping (hostname ):
58
40
try :
59
41
# ping command
60
- pingcmd = "ping -c 1 " + hostname + " | tail -1 | awk \' {print $4}\' | cut -d '/' -f 2 "
42
+ pingcmd = "ping -c 1 " + hostname + \
43
+ " | tail -1 | awk \' {print $4}\' | cut -d '/' -f 2 "
61
44
# get ping stdout
62
45
response = subprocess .run (pingcmd , shell = True , stdout = subprocess .PIPE )
63
46
pingresponse = response .stdout .decode ('utf-8' )
@@ -67,12 +50,12 @@ def ping(hostname):
67
50
pingtimenum = int (pingtime )
68
51
return pingtimenum
69
52
else :
70
- print ( ' Error while pinging!' )
53
+ filewrite ( log_file , 'a' , ' \n E: Error while pinging!' )
71
54
return None
72
55
73
56
# Error in pinging
74
- except :
75
- print ( ' Error while pinging!' )
57
+ except Exception as e :
58
+ filewrite ( log_file , 'a' , ' \n E: Error while pinging ' + e )
76
59
return None
77
60
78
61
@@ -87,8 +70,9 @@ def net_status(net_status_server):
87
70
return False
88
71
if gpiomode :
89
72
blink (redpin , 0.3 , 2 )
90
- except :
73
+ except Exception as e :
91
74
return False
75
+ filewrite (log_file , 'a' , '\n E: No Connection ' + e )
92
76
93
77
94
78
def blink (pin , timeon , number ):
@@ -117,13 +101,13 @@ def run(self):
117
101
cplot = []
118
102
n = 0
119
103
104
+
120
105
if http_server :
121
106
# add http handler
122
107
Handler = http .server .SimpleHTTPRequestHandler
123
108
# run http server on port
124
109
HttpThread ().start ()
125
- print ('see plot on http://localhost:' + str (http_port ))
126
-
110
+ filewrite (log_file , 'a' , '\n plot on http://localhost:' + str (http_port ))
127
111
while True :
128
112
time .sleep (sleeptime )
129
113
@@ -154,7 +138,7 @@ def run(self):
154
138
blink (bluepin , 0.3 , 1 )
155
139
156
140
else :
157
- print ( 'server type Error ' )
141
+ filewrite ( log_file , 'a' , ' \n E: Server type error ' )
158
142
159
143
if n % plottime == 0 :
160
144
plotdata .clear ()
@@ -178,7 +162,7 @@ def run(self):
178
162
179
163
# drow plot
180
164
plot = plotly .offline .plot (plotinput , filename = 'index.html' , auto_open = False )
181
- print ( 'plot : ' + plot )
165
+ filewrite ( log_file , 'a' , ' \n Plot : ' + plot )
182
166
183
167
if n % reset == 0 :
184
168
for server in serverslist :
0 commit comments