Pingstat!

Printer-friendly versionSend to friend

What is Pingstat ?

Pingstat is small Linux Bash Script which can log the results of PINGING a particular IP in a LOGFILE if specified OR output to STDOUT in a user-friendly format -

[time] PING [IP-ADDR] SUCCESS/FAILURE

 

LOGFILE by default is $HOME/pingstat.log

The script has a inbuilt default IP variable called $DEFAULTIP which is pinged if no IP is given as the first command line argument..

The script takes another argument which can have values of 1, YES, Yes, Y, TRUE, True  and when any of the values are found it will remove $LOGFILE if it exists.

It has two internal variables called $PINGCOUNT and $TIMEOUT (Their names say what they mean).

It has one more variable called $PINGCMD i.e. the name (or path) of the UNIX `ping` command ... in most cases its gonna be `ping`

So here's the script-

Feedback and Suggestions would be appreciated

#!/bin/bash

#PING AND LOG STATUS

#First argument which can be passed is the IP-Address
#If IP Address is not specified on the command line arguments,
#Then $DEFAULTIP will be used as declared below

#Second argument which can be specified is to remove the old log file
#Second argument can have values of - 1, TRUE, Yes, YES, True

#If second argument is not given then result is appended to ..
#existing logfile

#SOME DEFAULT SETTINGS .... CHANGE AS YOU WISH                

#The default IP to PING if no IP was given as the first argument

DEFAULTIP='121.246.205.1' 

#The PING command: Usually `ping`

PINGCMD='ping' 

#Number of packaets to send.... 
#Keep this small < 5 if you are pinging frequestly using Cron job to avoid 
#conflicts with the next ping

PINGCOUNT=2 

#Timeout in seconds if no response is recieved.....

#Keep this small < 5 if you are pinging frequently using Cron job to avoid
#conflicts with the next ping

TIMEOUT=5

#The LOG File PATH to save the result -

#Format of the LOG File will be Like this - 
#[time] PING $IP SUCCESS/FALIURER
#If LOGFILE is not specified, then output will be STDOUT

LOGFILE=$HOME/pingstat.log

#END CONFIGURATION SECTION

rmf()

{

        rm -f $LOGFILE

}


if [ $1 ]; then

        IP=$1

        else

                if [ ! $DEFAULTIP ]; then 

                echo -e "\nNO IP ADDRESS DEFINED\n"; 

                exit 1;

                else

                IP=$DEFAULTIP

                fi

fi

REMOLDLOG=$2

if [ -f $LOGFILE ]; then

        case "$REMOLDLOG" in

                        1)
                                rmf
                                ;;
                        TRUE)
                                rmf
                                ;;
                        YES)
                                rmf
                                ;;
                        Yes)
                                rmf
                                ;;
                        Y)
                                rmf
                                ;;
                        True)
                                rmf
                                ;;
        esac

fi

if [ ! $TIMEOUT ]; then

TIMEOUT=5

fi

if [ ! $PINGCOUNT ]; then

PINGCOUNT=2

fi

if [ ! $PINGCMD ]; then

PINGCMD='ping'

fi

if [ ! $LOGFILE ]; then

LOGFILE="/dev/stdout"

fi

$PINGCMD -c$PINGCOUNT -W$TIMEOUT $IP &> /dev/null

if [ $? -eq 0 ]; then

        echo -e "[$(date)] - PING $IP SUCCESS \n" >> $LOGFILE

else

        echo -e "[$(date)] - PING $IP FAILURE \n" >> $LOGFILE

fi

 


Nilesh Govindrajan

Site & Server Administrator
iTech7

Syndicate content