1756278 Members
2506 Online
108843 Solutions
New Discussion

Need to alert

 
allanm77
Frequent Advisor

Need to alert

Hi All,

 

Have the following piece of script which I need to modify so that if the count of down instances is more than 1 then it should alert otherwise should not send an email.

 

   PROD)
      export ENVIRONMENT=prod
      INSTANCES=`some-command | awk -F: '{print $2}'`
      PORT=`some-command | awk -F: '{print $3}'|head -1`
      count_errs=0
      for i in $INSTANCES
      do
         echo "" >> /tmp/var.tmp
         curl --connect-timeout 2 -s --retry 1 --retry-delay 20 --max-time 5 "http://$i:$PORT/dest" |sed 's/<[^>]*>//'g  | egrep -A1 "DOWN|DISABLE" >> /tmp/var.tmp
         status=$?
         if [ $status -eq 0 ]; then
            (( count_errs += 1 ))
         fi
      done
      egrep -B1 -A1 "DOWN|DISABLE" /tmp/var.tmp > /tmp/var.1.tmp
      if [ $count_errs -ne 0 ]; then
         (echo "Subject:$1:Instances down"; echo "To:${EMAIL}"; echo ""; cat /tmp/var.1.tmp; ) | /usr/sbin/sendmail -t
         rm -f /tmp/var.tmp /tmp/dirmon_var.1.tmp
      fi
   ;;

 Thanks,

Allan.

3 REPLIES 3
Dennis Handly
Acclaimed Contributor

Re: Need to alert

>if the count of down instances is more than 1 then it should alert otherwise should not send an email.

 

I assume you just change: if [ $count_errs -ne 0 ]; then

To: if [ $count_errs -gt 1 ]; then

 

Any reason you're using sendmail directly instead of mailx?

mailx -s "$1:Instances down" ${EMAIL} < /tmp/var.1.tmp

 

Hmm, is $count_errors the number of instances down or is $1?

Steve Post
Trusted Contributor

Re: Need to alert

It looks to me like you want to change

this text from   

 

if [ $count_errs -ne 0 ]

 

to

 

if [ $count_errs -gt 1 ]

 

 

 

Run the man page on "test".

 

I would also change that egrep  to grep -E.

I haven't used curl.

 

you can always look for the syntax of the the "test" command.  This

  if  [ condition  ] ; then 

 is the same as 

if test condition ; then

 

tests for integer fields: 

-eq, equal

-ne,notequal

-lt, lessthan

-le, less than or equal

-ge, greater than or equal

-gt,  greater than

 

So changing "-ne 0" to " -gt 1" is the same aschanging  "not equal to 0" to "greater than 1".

 

logic stuff:

! = not

-a   = and

-o = or

 

string tests?

-z   string      test if string is 0 length.  in other words it is empty.

-n  string      the opposite.  If the string has something in it.  Even a space counts.

string1!=string2  to test if the strings are different.

 

string1=string2  to test if the strings are the same.

 

Note NO spaces on either side of the equal sign.   This tests if string1 is the same as string2.   But what if there ARE spaces around that equal sign?   Well?  Then you are assigning string1 to variable string1.  And because the assignment was successful, it will alway be true.  (ahem as in TRUEly messed up).

 

Perhaps I am insulting your intelligence with this stuff.  Sorry about that.  I am probably rambling on too much.

 

 

Dennis Handly
Acclaimed Contributor

Re: Need to alert

>you can always look for the syntax of the the "test" command.


Unless you are using [[ ... ]].

Note that test(1) is an illusion, unless you are using the scummy C shell.  ;-)


>string1!=string2  to test if the strings are different.
>string1=string2  to test if the strings are the same.
>Note NO spaces on either side of the equal sign.

 

No, whitespace is required!  Otherwise string1=string2 is one long string, which is always true.

if [ abc=def ]; then echo T; else echo F; fi

Prints "T".

 

>But what if there ARE spaces around that equal sign?  Then you are assigning string1 to variable string1.

 

Not hardly, not for a real shell.