SUMMARY(2): Simple "ping" script

From: Mike's List <mikelist_at_sky.net>
Date: Mon Jan 20 2003 - 12:21:26 EST
Below are some more scripts (for multiple servers) --verus running one
command/cron at a time.  Too many for me to tests but hopefully, one of
the below will help some on the list.  (Note: www.nagios.org was mentioned,
seems to be a very detailed monitoring software).  If you need assistance
with one of the script, let me know and I'll notify the author.

Thanks to Luc, Dhari, Ramiro, Marc, Dilip, and Jim (order of arrivals).


- Mike


Luc
---
#!/bin/sh
#*
#* File         :       Ping Check
#* Description  :       Check list ot hosts if they are responding to ping
#* Author       :       Luc I. Suryo (luc@suryo.com)
#* Version      :       1.0
#* Date         :       Jan 17, 2003
#* History      :       First release
#*

FROMMAIL="mikelist@sky.net"
TOMAIL="mikelist@sky.net"
MAILFILE=/tmp/`basename ${0}`.$$

HOSTLIST="host1 host2 host3"
PROBLIST=""

SentPage() {

        DATE=`date "+%H:%m %Y-%m-%d"`
        HOST=`uname -n`

        echo "From: ${FROMMAIL}"                                > ${MAILFILE}
        echo "To: ${TOMAIL}"                                    >> ${MAILFILE}
        echo "Subject: [HOST DOWN ALERT FROM: ${HOST}]."        >> ${MAILFILE}
        echo "Content-Type: text/plain; charset=us-ascii"       >> ${MAILFILE}
        echo "Content-Transfer-Encoding: 7bit\n\n"              >> ${MAILFILE}

        echo " "                                                >> ${MAILFILE}
        echo "Time stamp: ${DATE}."                             >> ${MAILFILE}
        echo " "                                                >> ${MAILFILE}

        echo "Following host(s) has problems:"                  >> ${MAILFILE}
        echo ${PROBLIST}                                        >> ${MAILFILE}

        echo " "                                                >> ${MAILFILE}
        echo "-----------"                                      >> ${MAILFILE}

        /usr/lib/sendmail -oi -t -oem < ${MAILFILE}
        rm -f ${MAILFILE}
}

CheckHosts() {

        for hst in `echo ${HOSTLIST}`
        do
                /usr/sbin/ping -t 1 $hst 1 1  > /dev/null 2>&1
                if [ $? -ne 0 ] ; then
                        PROBLIST="${PROBLIST} ${hst}"
                fi
        done
}

main() {

        CheckHosts

        if [ "${PROBLIST}" != "" ] ; then
                SentPage
        fi
}

main


Dhari
-----
#!/bin/ksh
#
HOSTS=`/usr/bin/cat /var/tmp/host_list`
#
for i in $HOSTS
do
/usr/sbin/ping $i > /dev/null
 if [ $? -ne 0 ]
 then
/usr/bin/echo " $i is not pingable"|/usr/bin/mailx 1234567890@somepager.com
fi
done
exit


Ramiro
------
#!/bin/sh
# This script make a ping to arg 1
# if fails sends an email to arg 2
#
# Script basics
PN=`basename "$0"`      # Program name
VER=`echo '$Revision: 1 $' | cut -d' ' -f2`
#
# Definning the Usage
Usage () {
         echo >&2 "$PN - pingit, $VER (hs '2003) usage: $PN [name@domain]"
         exit 1
         }
#
# Testing if you pass the right number
# of args to the script
#
if [ $# -lt 2 ]
   then
   Usage
fi
#
# Set the environment
#
PING=/usr/sbin/ping
MAILX=/usr/bin/mailx
EMAIL=`echo $2`
#
# Script function
$PING $1 > /dev/null 2>&1
#
if [ $? -ne 0 ]
   then
   echo "unable to ping $1" | $MAILX -s "ping failed" $EMAIL
   else
   echo
fi


Marc
----
#!/bin/csh

# This is a ping script that will
# ping the Sun servers looking
# for any to go down.  If one does
# go down, it will send an epage
# to the desired address.
#
# By: R. Marc Baldus
# email: 1234567890@somepager.com

#################################################################
# Rev 0.9  08/12/2001  ---  First build. Only pings one server. #
# Rev 1.0  05/16/2002  ---  Set to ping a list of servers.      #
#################################################################

# This is a comma seperated list of email addresses
set epage = "root"

# This is the Subject line of the email
set subj = "Server Down"

# This is the list of servers you want pinged
set servers = ( 10.1.1.3 10.1.1.4 )

# This are program variables
set count = $#servers
set count2 = 0
set a = 1
set body = ""
set oldbody = ""

# This is the main loop
# $a will always be 1

while ( $a == 1 )


        # Clearing some program variables
        set count2 = 0
        set body = ""

        foreach i ( $servers )
                set count2 = `expr $count2 + 1`
                ping $i 1 > /dev/null

                # If the ping fails
                if ( $status == 1 ) then

                        # This is what is sent in the email body
                        set body = $body$servers[$count2]"\n"
                endif
        end

        # If we have text in the email body
        if !( -z "$body" ) then

                # And the email body has changed from last email
                if ( "$oldbody" != "$body" ) then

                        # Send out the email
                        printf $body | mailx -s "$subj" "$epage"
                endif
        endif

# Save the email body for comparison
set oldbody = $body

# Sleep for 5 minutes and run again
sleep 300
end


Dilip
-----
#!/bin/ksh
# Script Name:  monitor_server
# Description:  To monitor all server. If server is down, send page to SA.
The
host list is come from
#               monitor_server.list file
# Usage:        monitor_server
# History:
# Date:         Author                  Description
# 05/29/2002    DRaj                    Initially Created
###############################################################
#       Set Up Environment
################################################################

ADMINDIR=/backup/scripts
while read -r IP SRVNM
do
    if test `/usr/sbin/ping $IP | grep -c "is alive"` -eq 0; then
        # Wait 5 minutes before checking again
 sleep 120
     if test `/usr/sbin/ping $IP | grep -c "is alive"` -eq 0; then

mail    acc@motorola.com << EOF
From:   acc@motorola.com
To:     acc@motorola.com
Subject:From `uname -n`::: Machine $SRVNM is Down !!

Machine $SRVNM is not responding.

EOF
 fi
    fi

done < $ADMINDIR/monitor_server.list
exit 0



# Contents of monitor_server.list  List of all your servers

192.168.70.101  XXX
192.168.70.102  ttt
192.168.68.1    ppp


Jim
---
PINGLIST="server1 server2 server3"
RECIPIENTS="foo@bar bar@foo"

for SERVER in $PINGLIST
do
   ! ping $SERVER > /dev/null 2>&1 && mailx -s "$SERVER is dead!"
$RECIPIENTS
done
_______________________________________________
sunmanagers mailing list
sunmanagers@sunmanagers.org
http://www.sunmanagers.org/mailman/listinfo/sunmanagers
Received on Mon Jan 20 12:27:42 2003

This archive was generated by hypermail 2.1.8 : Thu Mar 03 2016 - 06:43:01 EST