Skip to content

Commit 87e2ac4

Browse files
committed
config and some minor fix
1 parent b02755b commit 87e2ac4

File tree

3 files changed

+92
-45
lines changed

3 files changed

+92
-45
lines changed

README.md

+32-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# About piping
1+
# About PiPing
22
This is a Python script which pings your desired IPs and generates a plot, showing target up-time based on received replies.
33
The plot is generated as a standard HTML file which is view-able in any standard web browser.
44
Furthermore, if you are running this script on RaspberryPi or compatible SBC, it is possible to connect an RGB LED to RPi's GPIO
@@ -14,20 +14,48 @@ user configurable.
1414
- `git clone https://github.com/mostafaasadi/piping`
1515
- `cd piping`
1616
- `nohup python3 piping.py &`
17+
- you can also create systemd unit to manage PiPing:
18+
19+
- make `/etc/systemd/system/piping.service` with the following contents
20+
21+
for first run: `sudo systemctl daemon-reload `
22+
23+
and manage with :
24+
25+
`sudo systemctl enable`
26+
`sudo systemctl start`
27+
`sudo systemctl stop`
28+
29+
30+
```
31+
[Unit]
32+
Description=PiPing service
33+
34+
[Service]
35+
Type=simple
36+
ExecStart=/usr/bin/python3 /home/mostafa/piping/piping.py
37+
WorkingDirectory=/home/mostafa/piping
38+
Restart=always
39+
RestartSec=2
40+
41+
[Install]
42+
WantedBy=sysinit.target
43+
```
44+
1745

1846
# Configuration
19-
To change IPs (add/remove/edit) line 25-32
47+
To change IPs (add/remove/edit) `config.py`
2048
```
2149
name = servers(
2250
'server name',
2351
'ip',
2452
'ping in offline/online',
2553
'enable GPIO (on raspberrypi) check with condition True/False (recommanded: enable for IPs outside your LAN)')
2654
```
27-
and other configurations (line 34-43).
55+
and other configurations.
2856

2957
## RaspberryPi GPIO activation
30-
If you are running piping on RaspberryPi, enable `gpiomode` on line 40. Then connect RGB LED according to this diagram:
58+
If you are running piping on RaspberryPi, enable `gpiomode` on `config.py`. Then connect RGB LED according to this diagram:
3159
![piping](https://raw.githubusercontent.com/mostafaasadi/piping/master/physical-pin-numbers.png)
3260
finally set `redpin`,`greenpin` and `bluepin` in configuration section.
3361

config.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
serverslist = []
2+
3+
4+
class servers:
5+
6+
def __init__(self, name, ip, type, gpio):
7+
self.name = name
8+
self.ip = ip
9+
self.type = type
10+
self.gpio = gpio
11+
self.ping = []
12+
serverslist.append(self)
13+
14+
15+
# server config
16+
# name = servers('server name', 'ip', 'ping in offline/online',
17+
# 'enable GPIO (on raspberrypi) check with condition True/False')
18+
google = servers('Google', '8.8.8.8', 'online', True)
19+
modem = servers('DSL', '192.168.1.1', 'offline', False)
20+
router = servers('mikrotik', '10.0.0.1', 'offline', False)
21+
verizon = servers('Verizon', '4.2.2.4', 'online', False)
22+
23+
# config
24+
condition = 150 # condition to comparison
25+
net_status_server = 'http://icanhazip.com' # check conection to net
26+
log_file = 'pipinglog'
27+
sleeptime = 1 # sleep between each ping (second)
28+
reset = 5000 # reset data plot after this time (Natural numbers)
29+
plottime = 10 # drow plot after this time (Natural numbers)
30+
http_server = True # enable/disable web server
31+
http_port = 8000 # http port
32+
gpiomode = False # enable on raspberrypi (True/Flase)
33+
greenpin = 7 # gpio pin conected to Green LED on BOARD mode
34+
redpin = 3 # gpio pin conected to Red LED on BOARD mode
35+
bluepin = 10 # gpio pin conected to Blue LED on BOARD mode

piping.py

+25-41
Original file line numberDiff line numberDiff line change
@@ -10,54 +10,37 @@
1010
import threading
1111
import plotly.graph_objs as go
1212
from datetime import datetime
13+
from config import *
1314

14-
serverslist = []
1515
os.chdir(os.path.dirname(os.path.realpath(__file__)))
1616

1717

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)
3726

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
5027

28+
filewrite(
29+
log_file,
30+
'a',
31+
'\n\t PiPing raised up\n' + datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
5132

5233
if gpiomode:
34+
filewrite(log_file, 'a', 'Mode: GPIO MODE')
5335
import RPi.GPIO as GPIO # Import GPIO library
5436

5537

5638
# main ping function
5739
def ping(hostname):
5840
try:
5941
# 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 "
6144
# get ping stdout
6245
response = subprocess.run(pingcmd, shell=True, stdout=subprocess.PIPE)
6346
pingresponse = response.stdout.decode('utf-8')
@@ -67,12 +50,12 @@ def ping(hostname):
6750
pingtimenum = int(pingtime)
6851
return pingtimenum
6952
else:
70-
print('Error while pinging!')
53+
filewrite(log_file, 'a', '\nE: Error while pinging!')
7154
return None
7255

7356
# Error in pinging
74-
except:
75-
print('Error while pinging!')
57+
except Exception as e:
58+
filewrite(log_file, 'a', '\nE: Error while pinging ' + e)
7659
return None
7760

7861

@@ -87,8 +70,9 @@ def net_status(net_status_server):
8770
return False
8871
if gpiomode:
8972
blink(redpin, 0.3, 2)
90-
except:
73+
except Exception as e:
9174
return False
75+
filewrite(log_file, 'a', '\nE: No Connection ' + e)
9276

9377

9478
def blink(pin, timeon, number):
@@ -117,13 +101,13 @@ def run(self):
117101
cplot = []
118102
n = 0
119103

104+
120105
if http_server:
121106
# add http handler
122107
Handler = http.server.SimpleHTTPRequestHandler
123108
# run http server on port
124109
HttpThread().start()
125-
print('see plot on http://localhost:' + str(http_port))
126-
110+
filewrite(log_file, 'a', '\nplot on http://localhost:' + str(http_port))
127111
while True:
128112
time.sleep(sleeptime)
129113

@@ -154,7 +138,7 @@ def run(self):
154138
blink(bluepin, 0.3, 1)
155139

156140
else:
157-
print('server type Error')
141+
filewrite(log_file, 'a', '\nE: Server type error ')
158142

159143
if n % plottime == 0:
160144
plotdata.clear()
@@ -178,7 +162,7 @@ def run(self):
178162

179163
# drow plot
180164
plot = plotly.offline.plot(plotinput, filename='index.html', auto_open=False)
181-
print('plot: ' + plot)
165+
filewrite(log_file, 'a', '\nPlot: ' + plot)
182166

183167
if n % reset == 0:
184168
for server in serverslist:

0 commit comments

Comments
 (0)