User avatar
Truos
Topic Author
Posts: 10
Joined: 12 Jul 2018, 07:48

Python network monitor.

05 Apr 2019, 07:47

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!
 
User avatar
RevPiModIO
Posts: 182
Joined: 20 Jan 2017, 08:44
Contact:

Re: Python network monitor.

06 Apr 2019, 19:10

Hi Truos!

You can do this with this code. All you have to do is install RevPiModIO and maybe RevPiPyLoad to execute this script:

# -*- 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:

rpi.io[ip_io[ip]].value = exit_code

to

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
Topic Author
Posts: 10
Joined: 12 Jul 2018, 07:48

Re: Python network monitor.

18 Apr 2019, 13:20

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
Topic Author
Posts: 10
Joined: 12 Jul 2018, 07:48

Re: Python network monitor.

12 Jun 2019, 19:09

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

Who is online

Users browsing this forum: No registered users and 1 guest