Operating System - HP-UX
1833758 Members
3215 Online
110063 Solutions
New Discussion

Need Looping Script Capability - Only works for one box per day

 
SOLVED
Go to solution
Laurie A. Krumrey
Regular Advisor

Need Looping Script Capability - Only works for one box per day

Hi All,

My Script works fine, for only one of my servers going down in any given day. But if
I have 2 or more of my servers go down in one
day, I will only get an email for the first
one.

I need some looping help. I have tried do whiles, do untils, for in. But what I really
think I need is a way to repeat the command
to search for what I want (see INFO in script).
I want to increment and I don't know how.

Or tell it to read till end of file.

Script:

# Look in syslog for any servers going down with today's date
INFO=`grep -i "Going down" va/adm/admin/testsyslog |grep "$MYDATE"`

# Check to see the last server that went down that we sent email about
LAST=`tail -1 $EMAILLOG`

if [ "$INFO" != "" ]; then
MYHOST=`echo $INFO|awk '{print $4}'`
MYTIME=`echo $INFO|awk '{print $1,$2,$3}'`

# Did you already send an email ?
if [ "$MYHOST:$MYTIME" != "$LAST" ]; then

grep $MYHOST:$MYTIME $EMAILLOG > /dev/null 2>&1
echo "$MYHOST went down on $MYTIME" | mailx -s "$MYHOST Down" $ADMIN_EMAIL
echo "$MYHOST:$MYTIME" >> $EMAILLOG
fi
fi

Happiness is a choice
4 REPLIES 4
Sachin Patel
Honored Contributor
Solution

Re: Need Looping Script Capability - Only works for one box per day

Hi Laurie,
for loop for multiple systems

$MACHINE="server1 server2 server3"
for MACHINE in $MACHINES; do
codes......
done
Is photography a hobby or another way to spend $
Sachin Patel
Honored Contributor

Re: Need Looping Script Capability - Only works for one box per day

Hi Laurie,
If you use for loop from one system to get info from remote systems then you have to use remsh
For example

for MACHINE in $MACHINES;do
# Look in syslog for any servers going down with today's date
INFO=`remsh $MACHINE grep -i "Going down" va/adm/admin/testsyslog |grep "$MYDATE"`

# Check to see the last server that went down that we sent email about
LAST=`remsh $MACHINE tail -1 $EMAILLOG`

if [ "$INFO" != "" ]; then
MYHOST=`echo $INFO|awk '{print $4}'`
MYTIME=`echo $INFO|awk '{print $1,$2,$3}'`

# Did you already send an email ?
if [ "$MYHOST:$MYTIME" != "$LAST" ]; then

remsh grep $MYHOST:$MYTIME $EMAILLOG > /dev/null 2>&1
echo "$MYHOST went down on $MYTIME" | mailx -s "$MYHOST Down" $ADMIN_EMAIL
echo "$MYHOST:$MYTIME" >> $EMAILLOG
fi
fi
done

Sachin
Is photography a hobby or another way to spend $
Sridhar Bhaskarla
Honored Contributor

Re: Need Looping Script Capability - Only works for one box per day

Laurie,

This should work. You can verify it by removing the history file.


//start

#!/usr/bin/ksh
INFO=/tmp/info$$
HIST=history
grep -i "Going down" log >> $INFO
SYSLIST=`cat $INFO |awk '{print $4}'|sort |uniq`
ADMIN_EMAIL=some_address


if [ `wc -l $INFO|awk '{print $1}'` -ne 0 ]
then
for SYS in $SYSLIST
do
MYHOST=`grep $SYS $INFO |tail -1 |awk '{print $4}'`
MYTIME=`grep $SYS $INFO |tail -1 |awk '{print $1$2$3}'`
grep $MYHOST:$MYTIME $HIST > /dev/null 2>&1
if [ $? != 0 ]
then
echo "$MYHOST went down on $MYTIME" | mailx -s "$MYHOST Down" $ADMIN_EMAIL
echo "$MYHOST:$MYTIME" >> $HIST
fi
done
fi
//END

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: Need Looping Script Capability - Only works for one box per day

Hi (again) Laurie,

Danger..danger..danger...

Keep one line at the end. I am sorry.

rm $INFO

otherwise you will end up filling your /tmp with temp files.

Also, you need to replace HIST,log, ADMIN_EMAIL with your values.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try