Monitoring Networked Devices Powered Status

Monitoring Networked Devices Powered Status

3 Comments on Monitoring Networked Devices Powered Status

Introduction
Have you ever wanted an simple way to monitor whether a network connected device is on or off. This script may be useful if you wanted to know if your children have gone to bed at the right time or stayed up beyond their bed time. There are many other uses for this script like monitoring devices that wake on lan etc.

The Script
This is not a bash tutorial, I am just sharing a script that you might find useful.

In the log file, the script will show the initial powered state of the devices. It will then only log events of power state changes. I have the script saved as the filename monip.sh (Monitor IP).

#!/bin/bash

FILE="/home/user/monip.txt"

HOST_NAMES=(
    USER-TABLET
    USER-PC
    USER-XBOX)

IP_ADDRESSES=(
    192.168.1.75
    192.168.1.65
    192.168.1.70)

PING_RESULTS=("" "" "")

function save_result {
    # WRITE THE STATE TO A FILE WITH A TIMESTAMP
    echo "| $1 IS NOW $2" | awk '{ print strftime("%d/%m/%Y %H:%M:%S"), $0; fflush(); }' >> ${FILE}
}

# SET UP THE FILE
rm ${FILE} 2> /dev/null
touch ${FILE}

echo "------------------------------------" >> ${FILE}
echo "monip started at $(date +'%d/%m/%Y %H:%M:%S')" >> ${FILE}
echo "------------------------------------" >> ${FILE}

# CHECK ARRAY LENGTHS
if [ ${#HOST_NAMES[@]} -ne ${#IP_ADDRESSES[@]} ]
then
    echo "ERROR: Length of array HOST_NAMES does not equal length of array IP_ADDRESSES" >> ${FILE}
    exit -1
fi
if [ ${#HOST_NAMES[@]} -ne ${#PING_RESULTS[@]} ]
then
    echo "ERROR: Length of array HOST_NAMES does not equal length of array PING_RESULTS" >> ${FILE}
    exit -1
fi

# START THE MAIN LOOP
while true
do
    for (( I=0; I<=(( ${#HOST_NAMES[@]} - 1 )); I++ ))
    do
        # CHECK IF ONLINE
        if ping -q -c 1 ${IP_ADDRESSES[${I}]} > /dev/null
        then
            RVAL="ONLINE"
        else
            RVAL="OFFLINE"
        fi

        # GET INITIAL STATE
        if [ -z ${PING_RESULTS[${I}]} ]
        then
            PING_RESULTS[${I}]=${RVAL}
            save_result ${HOST_NAMES[${I}]} ${RVAL}
        else
            # CHECK IF THE ONLINE STATE HAS CHANGED
           if [ ${PING_RESULTS[${I}]} != ${RVAL} ]
           then
               PING_RESULTS[${I}]=${RVAL}
               save_result ${HOST_NAMES[${I}]} ${RVAL}
           fi
       fi
    done
    sleep 60 # SLEEP ONE MINUTE
done

Save the code above to a file called monip.sh and make the file executable by running the following command:

chmod +x monip.sh

The Script Variables
Edit the following variables to have it work on your network:

  • FILE The destination path and file of the log file.
  • HOST_NAMES is an array of device names that will be put in the log file. You can change, add or remove as many as you like.
  • IP_ADDRESSES is an array of IP addresses that the script will ping. This has to be the IP addresses of the HOST_NAMES and in the same order for the log file to be correct.
  • PING_RESULTS This is an array of empty strings to store the results of the pings. This has to be the same amount of empty strings as you have in HOST_NAMES and IP_ADDRESSES.

The script will test the lengths of all the arrays and will exit with an error in the log file if they are not equal.

Viewing The Logfile
You can view the output of the log file in realtime using the following command:

tail -f monip.txt

The log file can be viewed in any text editor but will not be updated in realtime. The file will look like the following except for the HOST_NAMES you entered into the script.

$ tail -f monip.txt 
------------------------------------
monip started at 13/07/2015 16:26:07
------------------------------------
13/07/2015 16:26:10 | BILL-XBOX IS NOW OFFLINE
13/07/2015 16:26:10 | BILL-TABLET IS NOW ONLINE
13/07/2015 16:26:13 | LIAM-TABLET IS NOW OFFLINE
13/07/2015 16:26:16 | LIAM-PC IS NOW OFFLINE
13/07/2015 16:26:16 | LIAM-XBOX IS NOW ONLINE
13/07/2015 16:26:16 | NAS-DRIVE IS NOW ONLINE
13/07/2015 16:48:19 | BILL-TABLET IS NOW OFFLINE
13/07/2015 16:49:29 | BILL-TABLET IS NOW ONLINE
13/07/2015 16:57:38 | LIAM-PC IS NOW ONLINE

Start As Daemon
To start as a background service, create another script called start.sh and put the following code in it. Remember to change the cd line to point to the directory you have put the monip.sh script. Make sure screen is installed.

#!/bin/bash
cd /home/user/monip
screen -fa -d -m -S monip ./monip.sh

Conclusion
I am not as good in Bash as I am with compiled languages such as C++. I will be creating another post in the future with a binary version on this script. Please let me know what you think of the script in the comments below.

About the author:

Bill Payne started working as a paid professional software developer at the young age of 12 years old developing simple games and other applications for pre-packaged computers. Bill has since developed software for many industries such as direct sales and the the stock market. Bill has now started sharing his many years of software development experience through a blog on the MPSHouse website and one on one lessons.

3 Comments

  1. daniel  - 14 July , 2015 - 5:25 am
    Reply /

    You’re back! 😀

    The server wasn’t responding at some point on this year. I thought the blog and my precious gtkmm tutorials were lost forever.

    • Bill  - 14 July , 2015 - 11:54 am
      Reply /

      Thanks, yes I will try and keep them online. There was a disagreement with Amazon Web Services bill one month and I have relocated the server now.

Leave a comment

Back to Top