1

I'm trying to write a shell script for nagios plugin. I have a list of failed nodes where I have to trigger a CRITICAL alert to the nagios.

Input:

#cat file
 OFFLINE
 OFFLINE
 OFFLINE
 OFFLINE

All I want is to send exit status(i.e 2) for all the OFFLINE nodes. And, here is my code below so far.

CODE

while read -r output;
do
  STATE=$(echo $output)

  if [ $STATE == "OFFLINE" ]; then
        echo "CRITICAL: STATE is $STATE" 
        exit 2
  else
        echo  "OK " > /dev/null
  fi
done< <( cat file )

But the problem is the loop runs for one time and exits with 2 and alert goes for one node and for the remaining node I need to send the alert. Is there any way to write this script?

5
  • don't exit? but last I knew, Nagios only accepted one return code per system/check.
    – Jeff Schaller
    Commented May 25, 2016 at 15:06
  • If you issue an exit command, how do you expect your script to continue running ?
    – MelBurslan
    Commented May 25, 2016 at 15:08
  • @JeffSchaller is there any other way I can send alert to all offline node? Commented May 25, 2016 at 15:13
  • @MelBurslan Yeah i know but nagios expects only the exit code(0,1,2,3). if you know any other way to send the status code to nagios. It would be helpful for me. Commented May 25, 2016 at 15:14
  • Nagios triggers alerts for hosts on which a host or service check has failed. Last I knew, you couldn't tell Nagios "if a service check on host1 fails, alert for hosts 2, 3, and 4" You might be able to work around it with a service dependency.
    – Jeff Schaller
    Commented May 25, 2016 at 16:01

1 Answer 1

0

I am not familiar with Nagios and it s operation but if it takes only one exit code at a time, you will need to modify your script a little and run it multiple times.

output=$(head -1 file)  # read first line
sed -i -e '1,1d' file   # delete the first line

  STATE=$(echo $output)

  if [ $STATE == "OFFLINE" ]; then
        echo "CRITICAL: STATE is $STATE" 
        exit 2
  else
        echo  "OK " > /dev/null
  fi

If you can run this segment multiple times, you will receive multiple exit codes.

1
  • I used your idea to run the script multiple times using for loop in calling the above script. and It worked fine. Used two scripts and got the job done. Commented May 26, 2016 at 15:04

You must log in to answer this question.

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