Page 1 of 1
Python network monitor. Or Node Red...
Posted: 05 Apr 2019, 07:47
by Truos
Hi,
I'm looking to build a networkmonitor in python that pings all my devices at home and set DO on the RevPi DIO module depending on reply.
Does anyone have any pointers of where to look and read up on to make this a reality.
I have a RevPi Connect and a RevPi DIO.
Thank you and have a great day!
Re: Python network monitor.
Posted: 06 Apr 2019, 19:10
by RevPiModIO
Hi Truos!
You can do this with this code. All you have to do is install
RevPiModIO and maybe
RevPiPyLoad to execute this script:
Code: Select all
# -*- coding: utf-8 -*-
"""Ping IP addresses and set an IO to True on failure."""
__author__ = "Sven Sager"
__copyright__ = "Copyright (C) 2019 Sven Sager"
__license__ = "GPLv3"
import revpimodio2
from os import system
from threading import Event
# Event to exit the while loop
evt_exit = Event()
# RevPiModIO to communicate to process image
rpi = revpimodio2.RevPiModIO(autorefresh=True)
rpi.handlesignalend(evt_exit.set)
# This is our IP - Output-List
# TODO: CHANGE THIS TO YOUR NEEDS !!!
ip_io = {
"127.0.0.1": "O_12",
"192.168.1.1": "O_13",
"192.168.1.55": "O_14",
}
# Our mainloop - Executed every 5 seconds - Exit with Ctrl+C
while not evt_exit.wait(5):
# This is our ip check
for ip in ip_io:
# Do the Ping - Return code 0 = success
exit_code = system("ping -c 1 {0}".format(ip))
# Bool value of IO will the set to True on error
rpi.io[ip_io[ip]].value = exit_code
# Clean up process image
rpi.cleanup()
Important! You have to configure the IOs in piCtory and set the Names of IOs on your DIO in the "ip_io" directory.
This Program will switch on the IO if the ping fails. If you like to switch Output on if ping is successful just change:
Code: Select all
rpi.io[ip_io[ip]].value = exit_code
to
Code: Select all
rpi.io[ip_io[ip]].value != exit_code
Regards, Sven
Re: Python network monitor.
Posted: 18 Apr 2019, 13:20
by Truos
Thank you Sven!
Could I extend this and use it to instead write to a modbus register in pictory?
In that case I might get it to work with my HMI at home that I use for modbus already to read other data.
Re: Python network monitor.
Posted: 12 Jun 2019, 19:09
by Truos
hi I've been playing around with your script and changed it to set "Output_1" for Modbus and it shows value 256.
Is it possible to change this?
Also in the prompt it show the ping commands running isn't that going to be a problem if I run it to monitor say 42 hosts?
--- 192.168.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.567/0.567/0.567/0.000 ms
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.071 ms
--- 127.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.071/0.071/0.071/0.000 ms
PING 192.168.1.40 (192.168.1.40) 56(84) bytes of data.
64 bytes from 192.168.1.40: icmp_seq=1 ttl=64 time=2.48 ms
Re: Python network monitor.
Posted: 28 Jun 2019, 09:07
by Truos
The RPi crashes after a few hours of runtime.
I think I'll start investigating NodeRed as a ping system instead
Re: Python network monitor.
Posted: 28 Jun 2019, 09:49
by MarcusMijsters
Hello
Please have a look at Fping, its a free configurable variant to Ping, making it able to gain a lot of processing speed.
Works both on Linux and Windows
Re: Python network monitor.
Posted: 02 Jul 2019, 10:39
by Truos
I've setup Node Red instead and so far not involved any revolution pi specifics.
It's running on a RevPi Connect and I've also set it up on a RPi 3A+
It works fine for days on the RPi 3A+ and crashed with the screen attached after a few hours on the RevPi Connect
Have anyone else encountered this?
I'm reflashing a second RevPi Connect to set them off simultaneously.
- error.jpg (707.43 KiB) Viewed 9675 times
Re: Python network monitor. Or Node Red...
Posted: 04 Jul 2019, 13:35
by dirk
Hi Truos, this seams serious. Please verify that you have the latest updates. Have a look at
this post here.