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?

  • don't exit? but last I knew, Nagios only accepted one return code per system/check. – Jeff Schaller May 25 '16 at 15:06
  • If you issue an exit command, how do you expect your script to continue running ? – MelBurslan May 25 '16 at 15:08
  • @JeffSchaller is there any other way I can send alert to all offline node? – Prasanna Ranganthan May 25 '16 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. – Prasanna Ranganthan May 25 '16 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 May 25 '16 at 16:01
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.

  • 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. – Prasanna Ranganthan May 26 '16 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.