1753830 Members
8686 Online
108806 Solutions
New Discussion

Script help

 
SOLVED
Go to solution
allanm77
Frequent Advisor

Script help

Hi All!

Have the following piece of script , there are atleast 5 URLs that it goes through and sends 5 alerts, I need to have the script send just 1 alert instead of 5. The problem is that if some app is inaccessible from the frontend URLs then I get 5 alerts instead of 1.

 

 PROD)
    export ENV1=prod1
    export URL1=http://tools.zzz.com:5555/apache/ww
    INSTANCES=`showappinst -app FrontEndApache | awk -F: '{print $1}'`
    mon=`showappinst -app FrontEndApache | awk -F: '{print $2}'|head -1`
    for i in $INSTANCES
    do
       curl --connect-timeout 2 -s --retry 3 --retry-delay 10 --max-time 3 "http://$i:$PORT/dest" | egrep "DOWN|INACCESSIBLE"
       curl --connect-timeout 2 -s --retry 3 --retry-delay 10 --max-time 3 "http://$i:$PORT/dest" |egrep -A1 "DOWN|INACCESSIBLE" > /tmp/mon_prod1.tmp
       if [ `echo $?` -eq 0 ]
       then
          echo "<html> <head> <title>destinations list</title> </head> <body> <table cellspacing="2" bgcolor="black"> <tr bgcolor="#a0a0a0"> <table> <tr>" > /tmp/mon_${ENV1}.html
          echo $i >> /tmp/mon_${ENV1}.html
          cat /tmp/mon_prod1.tmp >> /tmp/mon_${ENV1}.html
          echo "</tr> </table> </body> </html>" >> /tmp/mon_${ENV1}.html
          (echo "Subject:$1:Instances inaccessible or down in FrontEndApache"; echo "To:alert@zzz.com"; echo "Content-Type: text/html"; echo "Content-Disposition: inline"; cat /tmp/mon_${ENV1}.html; ) | /usr/sbin/sendmail -t
       fi
       echo -n "" > /tmp/mon_prod1.tmp > /tmp/monalert_${ENV1}.html
    done
 ;;

 
Thanks,
Allan.

2 REPLIES 2
Dennis Handly
Acclaimed Contributor
Solution

Re: Script help

>I need to have the script send just 1 alert instead of 5.

 

You need to send the message after your loop.

 

>mon=`showappinst -app FrontEndApache | awk -F: '{print $2}'|head -1`

 

You can optimize this to:

mon=$(showappinst -app FrontEndApache | awk -F: '{print $2; exit}')

 

typeset -i count_errors=0

for ...

 

I'm not sure why you are doing two curl commands?  Combine to one.

curl --connect-timeout 2 -s --retry 3 --retry-delay 10 --max-time 3 "http://$i:$PORT/dest" | egrep -A1 "DOWN|INACCESSIBLE" > /tmp/mon_prod1.tmp

status=$?

 

grep -e DOWN -e INACCESSIBLE /tmp/mon_prod1.tmp  # error output for user

 

Nor why you use echo for $?  ?

 

if [ $status -eq 0 ]; then

   (( count_errors += 1 ))

   if [ $count_errors -eq 1 ]; then  # first time header

      echo "<html> <head> <title>destinations list</title> </head> <body> <table cellspacing="2" bgcolor="black"> <tr bgcolor="#a0a0a0"> <table> <tr>" > /tmp/mon_${ENV1}.html

   fi

   echo $i >> /tmp/mon_${ENV1}.html

fi

 

I don't think you can redirect to two files??

echo -n "" > /tmp/mon_prod1.tmp > /tmp/monalert_${ENV1}.html

 

Also, "echo -n" isn't valid in a real shell.

 

And after the end of the loop check to send message:

if [ $count_errors -ne 0 ]; then

   cat /tmp/mon_prod1.tmp >> /tmp/mon_${ENV1}.html

   echo "</tr> </table> </body> </html>" >> /tmp/mon_${ENV1}.html

   (echo "Subject:$1:Instances inaccessible or down in FrontEndApache"; echo "To:alert@zzz.com"; echo "Content-Type: text/html"; echo "Content-Disposition: inline"; cat /tmp/mon_${ENV1}.html; ) | /usr/sbin/sendmail -t

   rm -f /tmp/mon_${ENV1}.html # cleanup

fi

allanm77
Frequent Advisor

Re: Script help

Thanks Dennis