1

I have the below script and basically what I want it to do is connect to the various servers. See how many open files there are, and sent an email if its over 50,000. But the last server I need this check for over 400,000. I have a problem with the OR which is not working.

If I run this current script as it is I get the alert email even though none of the limits have been hit. Please help me with my OR statement.

My output from in command line shows:

Alert Server Issue 
AlertError S1:6326 S2:6355 S3:6242 S4:7171 S5:4694 S6:5284 S7:3973 S8:308432

The script:

#!/bin/bash
S1_IP=72.15.97.13
S2_IP=72.15.97.14
S3_IP=72.15.97.15
S4_IP=72.15.97.16
S5_IP=72.15.97.17
S6_IP=72.15.97.18
S7_IP=72.15.97.19
S8_IP=72.15.97.20

RESULT1=$(ssh test@$S1_IP lsof | wc -l)
RESULT2=$(ssh test@$S2_IP lsof | wc -l)
RESULT3=$(ssh test@$S3_IP lsof | wc -l)
RESULT4=$(ssh test@$S4_IP lsof | wc -l)
RESULT5=$(ssh test@$S5_IP lsof | wc -l)
RESULT6=$(ssh test@$S6_IP lsof | wc -l)
RESULT7=$(ssh test@$S7_IP lsof | wc -l)
RESULT8=$(ssh test@$S8_IP lsof | wc -l)

ERROR_COUNT=0

if [[ $RESULT1 || $RESULT2 || $RESULT3 || $RESULT4 || $RESULT5 || $RESULT6 || $RESULT7 -gt 50000 ]] || [[ $RESULT8 -gt 400000 ]]
then

    ERRORS[$ERROR_COUNT]="AlertError"
    ERROR_COUNT=$(($ERROR_COUNT+1))
fi
if [ $ERROR_COUNT -gt 0 ]
then
    [email protected]
    SUBJECT="Over 50,000"
    BODY='Alert Server Issue'
    CNT=0
    while [ "$CNT" != "$ERROR_COUNT" ]
    do
        BODY="$BODY ${ERRORS[$CNT]}
S1:$RESULT1
S2:$RESULT2
S3:$RESULT3
S4:$RESULT4
S5:$RESULT5
S6:$RESULT6
S7:$RESULT7
S8:$RESULT8"
        CNT=$(($CNT+1))
    done
    echo $SUBJECT
    echo $BODY
    echo $BODY | mail -s "$SUBJECT" -a "From: [email protected]" $EMAIL
else
    echo "I can handle it
S1:$RESULT1
S2:$RESULT2
S3:$RESULT3
S4:$RESULT4
S5:$RESULT5
S6:$RESULT6
S7:$RESULT7
S8:$RESULT8"
fi
3
  • We are not a script writing service. If you can improve this question, show us what you did, and what you got, instead of just telling us, then not only might this question not get closed, it might even get an answer!
    – MadHatter
    Dec 14, 2015 at 9:41
  • What more do you want me to add? I have explained what the current output is when I run the script. An email is sent even though the values in the OR statement are not breached.... Also I know this isn't a script writing service hence I already have the script. Did someone get out the wrong side of bed this Monday morning? (Welcome to serverfault.com......)
    – jaseUK
    Dec 14, 2015 at 9:46
  • 1
    Exactly my point: don't tell us what you get, show us what you get. STDOUT, error messages, odd files left in $cwd. Anything you see, show us. Add a set -vx to the top of the script, show us what you get then. Do anything you can think of to get us more pertinent information.
    – MadHatter
    Dec 14, 2015 at 9:51

2 Answers 2

4

From info coreutils 'test invocation'

If EXPRESSION is a single argument, 'test' returns false if the
argument is null and true otherwise

so, having the following in your code evaluates to true.

if [[ $RESULT1 || ...

You want to specify explicitly $RESULT1 -gt 50000 for all the arguments and not only for the last one.

2
  • Beat me to it - nicely done.
    – MadHatter
    Dec 14, 2015 at 10:00
  • Thanks NKMS for your help, I have it working now. A nice and simple solution to my question.
    – jaseUK
    Dec 14, 2015 at 10:30
3

With my hat off to nkms I would suggest the following instead:

#!/bin/bash

RESULT[1]=2
RESULT[2]=4
RESULT[3]=8
RESULT[4]=16
RESULT[5]=99999
RESULT[6]=32
RESULT[7]=64
RESULT[8]=128
RESULT[9]=256

for result in ${RESULT[*]}
do
    if [[ $result -gt 50000 ]]; then
        echo big result $result
    fi
done

Repeating -gt 50000 over and over again is error prone. Even if you get it right once what happens when somebody comes along and tries to add another result? A loop is easier to maintain and scale. And if you're going to loop over each of the results, why not put them into an array?

If you want to extend this to the rest of your script you can put your IP addresses into an array as well.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .