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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help....script check status if see something then send email
# 1  
Old 10-14-2013
Help....script check status if see something then send email

Code:
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


Code:
 
#!/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


Last edited by jim mcnamara; 10-14-2013 at 04:37 PM..
# 2  
Old 10-14-2013
Code:
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.
# 3  
Old 10-14-2013
cat abc.ksh
Code:
 
#!/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

Last edited by dotran; 10-14-2013 at 06:33 PM..
# 4  
Old 10-18-2013
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"

Code:
 
#!/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

# 5  
Old 10-18-2013
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
Code:
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...

Last edited by RudiC; 10-18-2013 at 03:59 PM..
# 6  
Old 10-18-2013
With a list separated by pipes you'd need to change the shell's internal field separator to use it in a for loop.

Code:
$ 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

# 7  
Old 10-18-2013
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

Last edited by dotran; 10-18-2013 at 06:03 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Send Disk Space Usage Status via email

Hi Guys, Is there any way I can write a script that sends DISK SPACE USAGE STATUS via email once a week? Thanks, (5 Replies)
Discussion started by: g4v1n
5 Replies

2. Shell Programming and Scripting

Script to email status

Hi, I have few processes in the server continuous run few jobs, each of the process will generate a log file which detailing when its jobs are completed. the logfile will has the name something like this, result1.log, result2.log, result3.log,.... result10.log, result11.log, result12.log.......... (8 Replies)
Discussion started by: khchong
8 Replies

3. Shell Programming and Scripting

Help with Email Status shell script

I have written a bash script to to sort the data from logs i need some help in printing the outputs , i dont have much ideas in bah scripting. Sample script ----------------------- #!/bin/bash a=`date | cut -d " " -f2,2,3` cat /var/log/maillog |grep "$a" |grep -E -e 'deferred|bounced'... (9 Replies)
Discussion started by: unimaxlin
9 Replies

4. Shell Programming and Scripting

Script to send email after comparing the folder permissions to a certain permission & send email

Hello , I am trying to write a unix shell script to compare folder permission to say drwxr-x-wx and then send an email to my id in case the folders don't have the drwxr-x-wx permissions set for them . I have been trying to come up with a script for few days now , pls help me:( (2 Replies)
Discussion started by: nairshar
2 Replies

5. Shell Programming and Scripting

Grep Stop status from the output of script and send an eamil alert.

Hello Team, I have script which gives below output. Server clustServer11 is in a STARTED state Server clustServer12 is in a STOPPED state Server clustServer21 is in a STOPPED state I would like to prepare script which will grep stop word from the above output and send an email alert. (5 Replies)
Discussion started by: coolguyamy
5 Replies

6. Shell Programming and Scripting

Check space of directories and send email if it has reached threshold limit

Hi, I need help in writing unix script for checking space of some directories on the system and also send an email when it reaches the threshold limit. I have written the followng code; #!/bin/ksh ADMIN="me@somewhere.com" # set alert level 80% is default THRESHOLD=80 df | grep -E... (5 Replies)
Discussion started by: jmathew99
5 Replies

7. UNIX for Dummies Questions & Answers

unix script to check if rsh to box and send status mail

rshstatus=`rsh -n lilo /db/p2/oracle/names9208/restart_names.sh` if $rshstatus <>0 then errstatus=1 mailx -s "xirsol8dr" ordba@xxx.com >> $log_dr else if errstatus=0 echo "status to xirsol8dr successful" can anyone provide if this is t he correct way to do this or is there a better way? (1 Reply)
Discussion started by: bpm12
1 Replies

8. Shell Programming and Scripting

Script to check processes and send an email

I searched through the forum and couldn't quite find what I was looking for. I have a script that looks for a process "DW" to be running. If it is not it will email a notice to an account. I would like to add the functionality to have it also email a seperate notice if there is more that one of... (1 Reply)
Discussion started by: heprox
1 Replies

9. Shell Programming and Scripting

check the status and send an email with status

Hi, We have a text file which has the following data. ISA~00~ ~00~ ~ZZ~VISTN ~ZZ~U1CAD ~051227~183 7~U~00200~000011258~0~P~< GS~FA~EE05J~U1CAD~051227~1831~000011258~X~002002 ST~997~0001 AK1~SH~247 AK2~856~2470001 AK5~A AK2~856~2470002 AK5~A... (3 Replies)
Discussion started by: isingh786
3 Replies

10. UNIX for Dummies Questions & Answers

awk to find the status and send an email

Hi, I have a datafile which has the following data and it can have much more records. The data set is as follows: ISA~00~ ~00~ ~ZZ~F159B ~ZZ~U1CAD ~051215~184 3~U~00200~000011432~0~P~< GS~FA~TC11A~U1CAD~051215~1843~000011432~X~002002 ST~997~0001... (6 Replies)
Discussion started by: isingh786
6 Replies
Login or Register to Ask a Question