Page 1 of 1

Writing custom plugin, curl request

Posted: Fri Mar 18, 2016 3:20 pm
by rkymtnhigh
I need to create a check that runs a curl request (that I have) and gives OK or CRITICAL based on the information returned.
I can run the curl request on the nagios server and it returns the values I am expecting.
Where I am running into the issue is figuring out how to tell nagios what fields to look at for the exit codes.

I found a snippet of code that seems to do this, but I need to tie the $CHECK variable to the correct field in the response.

Code: Select all

if [[ "$CHECK" == "OK" ]]; then
   echo "SERVICE OK"
   exit 0
elif [[ "$CHECK" == "Failed" ]]; then
   echo "Service not working: ${SERVER}"
   exit 2
else
   echo "Check failed"
   exit 3
fi

How would I go about doing that?

Thank you!

Re: Writing custom plugin, curl request

Posted: Sat Mar 19, 2016 1:39 pm
by gormank
What's the correct field in the response?
Something like this?

Code: Select all

if [[ "$CHECK" == "OK" ]]; then
   echo "$CHECK: SERVICE OK"
   exit 0
elif [[ "$CHECK" == "Failed" ]]; then
   echo "$CHECK: Service not working: ${SERVER}"
   exit 2
else
   echo "$CHECK: Check failed"

Re: Writing custom plugin, curl request

Posted: Sun Mar 20, 2016 6:56 am
by WillemDH
You should read through this:

https://nagios-plugins.org/doc/guidelines.html

Grtz

Re: Writing custom plugin, curl request

Posted: Mon Mar 21, 2016 10:09 am
by rkymtnhigh
So it returns info like this:

Code: Select all

{"ID":"XXX","StatusCode":"1","response":{"status":Successfull","StatusCode:"1","StatusMessage":"Success"}}
I need to define OK as StatusMessage:Success
And critical as any other value for StatusMessage.

I've looked thru that document but am having trouble making sense of what I need to do.

Thank you!

Re: Writing custom plugin, curl request

Posted: Mon Mar 21, 2016 10:31 am
by lmiltchev
You can try something like this:

Code: Select all

#!/bib/bash

CHECK="$(your check goes here)"

if [[ "$CHECK" == *"StatusMessage:Success"* ]]; then
   echo "SERVICE OK"
   exit 0
else
   echo "Check failed"
   exit 3
fi

Re: Writing custom plugin, curl request

Posted: Mon Mar 21, 2016 10:33 am
by rkennedy
Also throwing this out there, as @lmiltchev beat me to hitting submit.

You should be able to define with a 0 or 1 logic.

Code: Select all

check=$(curl 'page' | grep 'Success' | wc -l)
From there, $check will be equal to 1 if it finds the word, and 0 if it does not. Is that what you were looking for?

Re: Writing custom plugin, curl request

Posted: Mon Mar 21, 2016 10:49 am
by rkymtnhigh
I think so, let me give this a try. Thank you.

Re: Writing custom plugin, curl request

Posted: Mon Mar 21, 2016 11:17 am
by lmiltchev
Let us know if you need any more help.

Re: Writing custom plugin, curl request

Posted: Mon Mar 21, 2016 11:42 am
by rkymtnhigh
It works! Thank you guys!
I ended up modifying it a little:

Code: Select all

if [[ $CHECK =~ .*Success.* ]]; then
   echo "SERVICE OK"
   exit 0
else
   echo "Check failed"
   exit 3
fi
Thanks again!

Re: Writing custom plugin, curl request

Posted: Mon Mar 21, 2016 1:09 pm
by lmiltchev
Great! I am glad we were able to help! :)