1833386 Members
3308 Online
110052 Solutions
New Discussion

Re: Scripting

 
Darrell K Miracle
New Member

Scripting

I'm looking for some help with a basic script. I want to run a cron job to ping my servers and send out an email if the ping fails. Any help would be greately apprecieated.

Thanks,
Darrell
13 REPLIES 13
Jeff_Traigle
Honored Contributor

Re: Scripting

Not too difficult. Put the following in a file, make it executable (chmod u+x script_name), and schedule it in cron:

#!/usr/bin/sh

SERVER_LIST=$(cat server_list_file)

for SERVER in ${SERVER_LIST}
do
ping ${SERVER} -n 1 > /dev/null
RC=$?

if [ ${RC} -ne 0 ];
then
echo "Ping to ${SERVER} failed!"
fi
done
--
Jeff Traigle
Jeff_Traigle
Honored Contributor

Re: Scripting

Not too difficult. Put the following in a file, make it executable (chmod u+x script_name), and schedule it in cron:

#!/usr/bin/sh

SERVER_LIST=$(cat server_list_file)

for SERVER in ${SERVER_LIST}
do
/etc/ping ${SERVER} -n 1 > /dev/null
RC=$?

if [ ${RC} -ne 0 ];
then
echo "Ping to ${SERVER} failed!"
fi
done
--
Jeff Traigle
Jeff_Traigle
Honored Contributor

Re: Scripting

Darn system glitches... use the second one. Since you have a minimal PATH in the cron environment, you need to specify the full path for the ping command.
--
Jeff Traigle
Geoff Wild
Honored Contributor

Re: Scripting

For the mail part, just change the above line from:

echo "Ping to ${SERVER} failed!"

to

mailx -s "Ping to ${SERVER} failed!" you@yourdomain.com

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Geoff Wild
Honored Contributor

Re: Scripting

For the mail part, just change the above line from:

echo "Ping to ${SERVER} failed!"

to

mailx -s "Ping to ${SERVER} failed!" you@yourdomain.com
Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Darrell K Miracle
New Member

Re: Scripting

How does it get the IPs and how do I set it up to send email when it fails??
James R. Ferguson
Acclaimed Contributor

Re: Scripting

Hi Darrell:

Using Jeff's script, build a file in the directory in which your script resides. Now specify it's name as shown:

SERVER_LIST=$(cat /yourdir/server_list_file)

The file can contain either ipaddresses or hostnames (assuming that you have DNS configured) or both, like:

10.11.10.123
herhost
hishost
10.11.10.456

The script as written only looks at the first whitespace delimited field.

Geoff's post after Jeff's tells you how to send the email notice.

Regards!

...JRF...

Darrell K Miracle
New Member

Re: Scripting

Can multipal email addresses be entered as follows.......

darrell.miracle@us.army.mil; john.doe@us.army.mil; jan.doe@us.army.mil
James R. Ferguson
Acclaimed Contributor

Re: Scripting

Hi Darrell:

Stack you multiple email destinations like this:

darrell.miracle@us.army.mil john.doe@us.army.mil jan.doe@us.army.mil

For example:

# mailx -s "Test!" < /dev/null darrell.miracle@us.army.mil john.doe@us.army.mil jan.doe@us.army.mil

...would send the three recipients a message with the subject line "Test!" but an empty message body.

Regards!

...JRF...
Geoff Wild
Honored Contributor

Re: Scripting

Another thing I like to do, is create a file with a list of admins in it then call it like so:

mailx -s "Ping to ${SERVER} failed!" `cat /usr/local/mailadmin.list`

That way, if you have multiple scripts, instead of updating each of them when you have to add or remove an admin - you only need to update 1 file - the mailadmin.list.

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Mark Ellzey
Valued Contributor

Re: Scripting

Darrell,

Geoff uses a file to send to multiple email addresses; I use an email alias, like so:

In /etc/mail/aliases --

admins : mark_ellzey@myco.com, other_admin@myco.com, yetanother_admin@myco.com

Then you can do:

mailx -s "Ping results" admins < message

Regards,
Mark

Geoff Wild
Honored Contributor

Re: Scripting

Mark - I used to use mail aliases - but more work to maintain - with a file - I can keep 1 master on a single server - then scp that file to all my other servers - with aliases - you might not want the same one on all servers...and if you do, it just means executing a newaliases command each time you change it (I know, only 1 extra step...).

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Jeff_Traigle
Honored Contributor

Re: Scripting

Well, I was originally simplifying my assumption that the output from the echos would get emailed to the user running the script via cron. (stdout is emailed automatically.) Adding the mailx line to the echo will generate multiple emails if multiple failures occur in a single run. To reduce to a single email and be able to mail to any group of email addresses you want (comma separated in email_list_file in my example), you can modify it as follows. Tons of ways to do most things on UNIX, of course. Tkae your pick and go for it. :)

#!/usr/bin/sh

MSG_FILE=/var/tmp/ping_msg
SERVER_LIST=$(cat server_list_file)
EMAIL_LIST=$(cat email_list_file)

for SERVER in ${SERVER_LIST}
do
/etc/ping ${SERVER} -n 1 > /dev/null
RC=$?

if [ ${RC} -ne 0 ];
then
echo "Ping to ${SERVER} failed!" >> ${MSG_FILE}
fi
done

if [ -f ${MSG_FILE} ];
then
mailx -s "Ping failures" ${EMAIL_LIST} < ${MSG_FILE}
rm ${MSG_FILE}
fi
--
Jeff Traigle