Help....script check status if see something then send email

autorep -m bogus

Machine Name Max Load Current Load Factor O/S Status
___________ ________ ___________ ______ ________ ______
bogus --- --- 1.00 Sys Agent Online


Status
______
Online
Offline
Missing
Unqualified

The "Status" always "Online". I like create a script execute run 24/24 with command "break" and check if see not "Online" if see (Offline, Missing, Unqualified.....etc) then send out email. Can someone help on this code syntax....I can't make it work. Thanks

 
#!/bin/ksh

CHECK_MACHINE=`autorep -m bogus | awk '{print $NF}' | awk 'NR>3' | grep -v "Online"

##for file in ${CHECK_MACHINE}
##do
##if 

while true
do

CHECK_MACHINE=autorep -m bogus | awk '{print $NF}' | awk 'NR>3' | grep -v "Online"


if [ ${CHECK_MACHINE} = Offline|Missing|Unqualified] 
then
sleep 10; echo "Check the agent is [${CHECK_MACHINE}]" | 
    mailx -s "WARNING --The agent is [${CHECK_MACHINE}]" abc@test.com
break
fi
done
CHECK_MACHINE=autorep -m bogus | 
awk '{print $NF}' | awk 'NR>3' | egrep -q '(Offline|Missing|Unqualified)'
if [ $? -ne 0 ] ; then
sleep 10; echo "Check the agent is [${CHECK_MACHINE}]" | 
    mailx -s "WARNING --The agent is [${CHECK_MACHINE}]" abc@test.com

fi

try that - I left out break because the code is not in a loop. That I can see anyway.

cat abc.ksh

 
#!/bin/ksh
CHECK_MACHINE=autorep -m bogus | awk '{print $NF}' | awk 'NR>3' | egrep 'Offline|Missing|Unqualified'
if [ $? -ne 0 ] ; then
sleep 10; echo "Check the agent is [${CHECK_MACHINE}]" | mailx -s "WARNING --The agent is [${CHECK_MACHINE}]" abc@test.com
fi

Not sure what's cause run the code "-m: not found", but run every command line work fine.
cat abc.ksh
#!/bin/ksh
CHECK_MACHINE=autorep -m bogus | awk '{print $NF}' | awk 'NR>3' | egrep 'Offline|Missing|Unqualified'
if [ $? -ne 0 ] ; then
sleep 10; echo "Check the agent is [${CHECK_MACHINE}]" | mailx -s "WARNING --The agent is [${CHECK_MACHINE}]" abc@test.com
fi

./abc.ksh
./abc.ksh[3]: -m: not found

autorep -m bogus
Machine Name Max Load Current Load Factor O/S Status
________________________________________________________________________________ __________ ____________ _______ ___________ ______
bogus --- --- 1.00 Sys Agent Missing

autorep -m bogus | awk '{print $NF}'
Status
______
Missing

autorep -m bogus | awk '{print $NF}' | awk 'NR>3'
Missing

autorep -m bogus | awk '{print $NF}' | awk 'NR>3' | egrep 'Offline|Missing|Unqualified'
Missing

Finally I got this code work, but I want apply to multiple servers but seem not loop can somehelp check the syntax on this. Thanks

SERVERS="server1|server2|server3"

 
#!/bin/ksh
 
SERVERS="server1|server2|server3"
 
#for SER in $SERVERS
#do
 
CHECK_MACHINE=`autorep -m server1 | awk '{print $NF}' | awk 'NR>3' | egrep 'Offline|Missing|Unqualified'`
while true
do
CHECK_MACHINE=`autorep -m server1 | awk '{print $NF}' | awk 'NR>3' | egrep 'Offline|Missing|Unqualified'`
if [ $? = 0 ] ; then
sleep 10; echo "Check the agent is [${CHECK_MACHINE}]" | 
mailx -s "WARNING --The agent is [${CHECK_MACHINE}]" abc@test.com
exit 1
fi
done

If you want your script to loop, don't exit it. And, awk will return status 0 even if no pattern found, unless told otherwise. I can't test this, as I don't have autorep available, but try

SERVERS="server1 server2 server3"
while :
  do for S in $SERVERS
       do CM=$(autorep -m $S |
               awk  'NR<3                          {next}
                     /Offline|Missing|Unqualified/ {print $1, $NF; exit}
                                                   {exit 1}
                    ') &&     
          { echo "check the agent is $CM"
            echo "mailx -s Warning $CM abc@test.com"; } ||
          sleep 10
       done
  done

You may want to rephrase the echoed texts, and remove the echo in front of the mailx command...

With a list separated by pipes you'd need to change the shell's internal field separator to use it in a for loop.

$ SERVERS="svr1|svr2|svr3";  for SER in $SERVERS; do echo $SER; done
svr1|svr2|svr3
$ IFS='|'; SERVERS="svr1|svr2|svr3";  for SER in $SERVERS; do echo $SER; done; unset IFS
svr1
svr2
svr3

Thanks RudiC and CaloM,

But seem like not work with pipes or with space on autorep -m "bogus|mirage"

autorep -m bogus
Machine Name Max Load Current Load Factor O/S Status
________________________________________________________________________________ __________ ____________ _______ ___________ ______
bogus --- --- 1.00 Sys Agent Missing
autorep -m "bogus|mirage"
CAUAJM_E_50111 Invalid Machine Name: bogus|mirage
autorep -m mirage
Machine Name Max Load Current Load Factor O/S Status
________________________________________________________________________________ __________ ____________ _______ ___________ ______
mirage 100 0 1.00 Sys Agent Online

SERVERS="server1 server2 server3"
while :
do for S in $SERVERS
do CM=$(autorep -m $S |
awk 'NR<3 {next}
/Offline|Missing|Unqualified/ {print $1, $NF; exit}
{exit 1}
') &&
{ echo "check the agent is $CM"
echo "mailx -s Warning $CM abc@test.com"; } ||
sleep 10
done
done