Python network monitor. Or Node Red...

Topics about the Software of Revolution Pi
Post Reply
User avatar
Truos
Posts: 28
Joined: 12 Jul 2018, 07:48
Answers: 0

Python network monitor. Or Node Red...

Post 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!
Last edited by Truos on 02 Jul 2019, 10:39, edited 1 time in total.
User avatar
RevPiModIO
KUNBUS
Posts: 322
Joined: 20 Jan 2017, 08:44
Answers: 0
Contact:

Re: Python network monitor.

Post 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
python3-RevPiModIO - https://revpimodio.org/ || Der RevPi ist das Beste, was passieren konnte!
User avatar
Truos
Posts: 28
Joined: 12 Jul 2018, 07:48
Answers: 0

Re: Python network monitor.

Post 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.
User avatar
Truos
Posts: 28
Joined: 12 Jul 2018, 07:48
Answers: 0

Re: Python network monitor.

Post 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
User avatar
Truos
Posts: 28
Joined: 12 Jul 2018, 07:48
Answers: 0

Re: Python network monitor.

Post by Truos »

The RPi crashes after a few hours of runtime.
I think I'll start investigating NodeRed as a ping system instead
MarcusMijsters
Posts: 2
Joined: 21 Jul 2017, 09:11
Answers: 0

Re: Python network monitor.

Post 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
User avatar
Truos
Posts: 28
Joined: 12 Jul 2018, 07:48
Answers: 0

Re: Python network monitor.

Post 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
error.jpg (707.43 KiB) Viewed 5774 times
User avatar
dirk
KUNBUS
Posts: 1926
Joined: 15 Dec 2016, 13:19
Answers: 4

Re: Python network monitor. Or Node Red...

Post by dirk »

Hi Truos, this seams serious. Please verify that you have the latest updates. Have a look at this post here.
Post Reply