Writing custom plugin, curl request

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
rkymtnhigh
Posts: 95
Joined: Tue May 12, 2015 11:53 am

Writing custom plugin, curl request

Post 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!
gormank
Posts: 1114
Joined: Tue Dec 02, 2014 12:00 pm

Re: Writing custom plugin, curl request

Post 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"
User avatar
WillemDH
Posts: 2320
Joined: Wed Mar 20, 2013 5:49 am
Location: Ghent
Contact:

Re: Writing custom plugin, curl request

Post by WillemDH »

You should read through this:

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

Grtz
Nagios XI 5.8.1
https://outsideit.net
rkymtnhigh
Posts: 95
Joined: Tue May 12, 2015 11:53 am

Re: Writing custom plugin, curl request

Post 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!
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Writing custom plugin, curl request

Post 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
Be sure to check out our Knowledgebase for helpful articles and solutions!
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Writing custom plugin, curl request

Post 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?
Former Nagios Employee
rkymtnhigh
Posts: 95
Joined: Tue May 12, 2015 11:53 am

Re: Writing custom plugin, curl request

Post by rkymtnhigh »

I think so, let me give this a try. Thank you.
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Writing custom plugin, curl request

Post by lmiltchev »

Let us know if you need any more help.
Be sure to check out our Knowledgebase for helpful articles and solutions!
rkymtnhigh
Posts: 95
Joined: Tue May 12, 2015 11:53 am

Re: Writing custom plugin, curl request

Post 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!
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Writing custom plugin, curl request

Post by lmiltchev »

Great! I am glad we were able to help! :)
Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked