- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Need Scripting Help - Script to send email if serv...
Categories
Company
Local Language
Forums
Discussions
Knowledge Base
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2001 07:47 AM
10-29-2001 07:47 AM
I have a (borne shell) script in cron that runs once a day and it monitors my global syslog. All my servers send a copy of their
syslog to this one server call GOAT.
It works great, I get an email and a pager email. The problem is I want it to run in cron
every 10 minutes. Well, I did that and I kept
getting emails telling me the same thing.
So I need to have an output file i.e. "sentmail" so it will not send me the same
email every 10 minutes.
Here's my script, I need help creating and
testing the sentemail file. I'm not sure how
the logic works:
!/usr/bin/ksh
# #
DATE=`date "+%b %d"`
HOST=`uname -n`
#ADMIN_PAGER=9131234420@messaging.sprintpcs.com
ADMIN_EMAIL=lakrumrey@statestreetkc.com
# Check to see if any of the servers are going down (shutdown or reboot)
if [ `grep -i "going down" /var/adm/syslog/syslog.log | wc -l` > 0 ]
then
grep "$DATE" /var/adm/syslog/syslog.log | grep "Going down" | mailx -s "Server Going Down" $ADMIN_EMAIL
fi
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2001 07:59 AM
10-29-2001 07:59 AM
SolutionWe run script every 10 minutes to check for n servers and if anyone is not reachable it sends me email.
Here is the loop without going through variable initilization.
for MACHINE in $MACHINES; do
RESULT=`$PING $MACHINE $BYTES $COUNT | grep "packet loss" | sed 's/%//g' | awk '
{print $7}'`
if [ "$RESULT" = "" ]; then
echo "Unexpected ping response from $MACHINE"
elif [ $RESULT -ge 100 ]; then
WASDOWN=`grep "^$MACHINE:" $OLDLIST | wc -l`
if [ $WASDOWN -gt 0 ]; then
grep "^$MACHINE:" $OLDLIST >> $NEWLIST
else
SENDMAIL=1
echo "$MACHINE: $DATE" >> $NEWLIST
echo "$MACHINE NOT RESPONDING" >> $MAILFILE
fi
else
WASDOWN=`grep "^$MACHINE:" $OLDLIST | wc -l`
if [ $WASDOWN -gt 0 ]; then
SENDMAIL=1
echo "$MACHINE now responding, down from `grep \"^$MACHINE:\" $OLDLIST`" >>
$MAILFILE
echo "`grep \"^$MACHINE:\" $OLDLIST` ---> $DATE" >> $LOGFILE
fi
fi
done
Sachin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2001 08:06 AM
10-29-2001 08:06 AM
Re: Need Scripting Help - Script to send email if server goes down
http://bb4.com
Also, since most of BB is shell scripts, its very platform independant. Hope this helps.
-Santosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2001 08:06 AM
10-29-2001 08:06 AM
Re: Need Scripting Help - Script to send email if server goes down
You can follow this logic .
1. Your script is valid for the first time. But what you need to do is to take the last line of the output and put the time stamp with the server name in a history file. tail -1 does the trick here.
2. Again, run your script. Now you verify this last line with that is already there in the history file. If they match, then don't send page. Else send page, and keep the new entry in the history file. You can use this history file later to calculate the downtimes. Or you can delete the entry if you don't want.
3. Repeat step 2 for all the entries.
The script would be like this. You may need to modify it.
#!/usr/bin/ksh
HIST=/var/adm/history
DATE=`date "+%b %d"`
HOST=`uname -n`
#ADMIN_PAGER=9131234420@messaging.sprintpcs.com
ADMIN_EMAIL=lakrumrey@statestreetkc.com
# Check to see if any of the servers are going down (shutdown or reboot)
LINE=`grep -i "going down" /var/adm/syslog | tail -1`
MYHOST=`echo $LINE|awk '{print $4}'`
MYTIME=`echo $LINE|awk '{print $1$2$3}'`
grep $MYHOST:$MYTIME $HIST > /dev/null 2>&1
if [ $? != 0 ]
then
echo "$MYHOST went down at $MYDATE" |mailx -s
"$MYHOST went down" $ADMIN_EMAIL
echo "$MYHOST:$MYTIME" >> $HIST
fi
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2001 08:07 AM
10-29-2001 08:07 AM
Re: Need Scripting Help - Script to send email if server goes down
There is also software that will leave you a voice mail, and with some phone systems you can have it page you when you get voice mail. They usually will continue paging you until you acknowledge it.
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2001 08:07 AM
10-29-2001 08:07 AM
Re: Need Scripting Help - Script to send email if server goes down
You can follow this logic .
1. Your script is valid for the first time. But what you need to do is to take the last line of the output and put the time stamp with the server name in a history file. tail -1 does the trick here.
2. Again, run your script. Now you verify this last line with that is already there in the history file. If they match, then don't send page. Else send page, and keep the new entry in the history file. You can use this history file later to calculate the downtimes. Or you can delete the entry if you don't want.
3. Repeat step 2 for all the entries.
The script would be like this. You may need to modify it.
#!/usr/bin/ksh
HIST=/var/adm/history
DATE=`date "+%b %d"`
HOST=`uname -n`
#ADMIN_PAGER=9131234420@messaging.sprintpcs.com
ADMIN_EMAIL=lakrumrey@statestreetkc.com
# Check to see if any of the servers are going down (shutdown or reboot)
LINE=`grep -i "going down" /var/adm/syslog | tail -1`
if [ $LINE ]
then
MYHOST=`echo $LINE|awk '{print $4}'`
MYTIME=`echo $LINE|awk '{print $1$2$3}'`
grep $MYHOST:$MYTIME $HIST > /dev/null 2>&1
if [ $? != 0 ]
then
echo "$MYHOST went down at $MYDATE" |mailx -s
"$MYHOST went down" $ADMIN_EMAIL
echo "$MYHOST:$MYTIME" >> $HIST
fi
fi
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2001 08:10 AM
10-29-2001 08:10 AM
Re: Need Scripting Help - Script to send email if server goes down
Try this page, you will be able to customize this software and it will provide you with alot of future functionality.
Enjoy,
Craig
http://www.cert.dfn.de/eng/logsurf/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2001 08:29 AM
10-29-2001 08:29 AM
Re: Need Scripting Help - Script to send email if server goes down
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2001 09:16 AM
10-30-2001 09:16 AM
Re: Need Scripting Help - Script to send email if server goes down
I am also using your ping script. Everything works when my server is down. How do I get it
to just send me one email and not keep repeating itself?
What did I miss?
for MACHINE in $MACHINE;
do
RESULT=`$PING $MACHINE $BYTES $COUNT | grep "packet loss" | sed 's/%//g' | awk '
{print $7}'`
if [ "$RESULT" = "" ]; then
echo "Unexpected ping response from $MACHINE"
elif [ $RESULT -ge 100 ]; then
WASDOWN=`grep "^$MACHINE:" $OLDLIST | wc -l`
if [ $WASDOWN -gt 0 ]; then
grep "^$MACHINE:" $OLDLIST >> $NEWLIST
echo "Cannot ping $MACHINE" | mailx -s "$MACHINE NOT RESPONDING" $ADMIN_EMAIL
fi
else
WASDOWN=`grep "^$MACHINE:" $OLDLIST | wc -l`
if [ $WASDOWN -gt 0 ]; then
SENDMAIL=1
echo "$MACHINE now responding, down from `grep \"^$MACHINE:\" $OLDLIST`" |mailx -s "$MYHOST Down" $ADMIN_EMAIL
echo "`grep \"^$MACHINE:\" $OLDLIST` ---> $DATE" >> $LOGFILE
fi
fi
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2001 09:37 AM
10-30-2001 09:37 AM
Re: Need Scripting Help - Script to send email if server goes down
One more option, if you have a central server that is consolidating syslog stuff, why not configuring syslog.conf on your server to send all its messages to that other server. Then you can set up a script on one server to look for errors/messages from all your servers. There are several good scripts out there search through a log file and filter out what you want.
That said, having some kind of real-time monitoring system as is mentioned above is still the best way to know when your server is down, as systems as known to crash or go down ungracefully without going through the proper shutdown sequence.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2001 10:38 AM
10-30-2001 10:38 AM
Re: Need Scripting Help - Script to send email if server goes down
You need simple trick.
#folloing .new and .old file will do that trick
NEWLIST="/tmp/server_ping.new"
OLDLIST="/tmp/server_ping.old"
MAILFILE="/tmp/server_ping.mail"
MAILUSERS="sachin"
SENDMAIL=0
BYTES=64
COUNT=4
DATE=`date`
PING=/etc/ping
DIRNAME=`dirname $0`
MACHINES="server1 server2 server3...."
LOGFILE="$DIRNAME/uptime.log"
# If there is no old list file create one
if [ ! -f $OLDLIST ]; then
echo "" > $OLDLIST
fi
echo "Current Time: $DATE" > $MAILFILE
echo "" >> $MAILFILE
echo "" > $NEWLIST
# Where mail is required
if [ $SENDMAIL -eq 1 ]; then
echo "" >> $MAILFILE
echo "Machines currently down:" >> $MAILFILE
cat $NEWLIST >> $MAILFILE
mailx -s "Server Uptime Change" $MAILUSERS < $MAILFILE
fi
#Update old list file
cp $NEWLIST $OLDLIST
Now you have $OLDLIST file and so it will not send you eamil again for that system. It will send you email when that system is up again or other system goes down.
Sachin