Check_HTTP query

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
neworderfac33
Posts: 329
Joined: Fri Jul 24, 2015 11:04 am

Check_HTTP query

Post by neworderfac33 »

Good afternoon - the following service definition:

Code: Select all

define service{
        use                     generic-service
        host_name               MyServerID
        #hostgroup_name          url010d
        service_description     http://MyURL Port 8001
        check_command           check_http_url!http://MyURL -p 8001
        }
calling the following from commands,cfg:

Code: Select all

define command{
        command_name    check_http_url
        command_line    $USER1$/check_http -I $HOSTADDRESS$ -u $ARG1$
}
returns the following result in the browser interface:

Code: Select all

HTTP OK: HTTP/1.1 301 Moved Permanently - 442 bytes in 0.002 second response time 
However, I have been asked to check that the return code is 200, rather than 301 to show that no redirection has taken place.

I have also been asked if I can check for specific text in what is returned e.g. "Country of residence"

Are either of these possible?

As always, thanks in advance.

Pete
neworderfac33
Posts: 329
Joined: Fri Jul 24, 2015 11:04 am

Re: Check_HTTP query

Post by neworderfac33 »

OK - I got the first bit sussed:

Code: Select all

define service{
        use                     generic-service
        host_name               MyServer
        #hostgroup_name          url011u
        service_description     http://MyURL - port 8001 [200 return code]
        check_command           check_http_url!http://MyURL -p 8001 -expect=200
        }
Still struggling with the expected text bit.

Nearly hometime - yaay! :-)

Cheers
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Check_HTTP query

Post by mcapra »

peterooney wrote: However, I have been asked to check that the return code is 200, rather than 301 to show that no redirection has taken place.
The -f option is what you're looking for. From the check_http manual page:
https://www.monitoring-plugins.org/doc/ ... _http.html

Code: Select all

 -f, --onredirect=<ok|warning|critical|follow|sticky|stickyport>
    How to handle redirected pages. sticky is like follow but stick to the
    specified IP address. stickyport also ensures port stays the same.
So you could try -f warning to get a warning if the page is redirecting. --expect=200 should also work if the header's response is well formatted.
peterooney wrote: I have also been asked if I can check for specific text in what is returned e.g. "Country of residence"
From the same manual page:

Code: Select all

 -e, --expect=STRING
    Comma-delimited list of strings, at least one of them is expected in
    the first (status) line of the server response (default: HTTP/1.)
    If specified skips all other status line logic (ex: 3xx, 4xx, 5xx processing)
 -d, --header-string=STRING
    String to expect in the response headers
 -s, --string=STRING
    String to expect in the content
So essentially -s "Country of residence"
Former Nagios employee
https://www.mcapra.com/
neworderfac33
Posts: 329
Joined: Fri Jul 24, 2015 11:04 am

Re: Check_HTTP query

Post by neworderfac33 »

That's brilliant - thank you.

I'll give the -f parameter a try on Monday, as well as the -s example.

Have a good weekend

Pete
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Check_HTTP query

Post by mcapra »

You too Pete! Let us know if there's any developments or issues!
Former Nagios employee
https://www.mcapra.com/
neworderfac33
Posts: 329
Joined: Fri Jul 24, 2015 11:04 am

Re: Check_HTTP query

Post by neworderfac33 »

So, if I wanted to monitor for:
Port=8001
Status=200
Text (from web content) = "Country of Residence"

How would I achieve this?

Would I have to add additional arguments to my check_http_url command like this:

Code: Select all

define command{
        command_name    check_http_url
        command_line    $USER1$/check_http -I $HOSTADDRESS$ $ARG1$ $ARG2$ $ARG3$
}
in order to run this

Code: Select all

define service{
        use                     generic-service
        host_name               MyServer
        #hostgroup_name          url010d
        service_description     Check Port, Status and Text
        check_command           check_http_url!MyURL -p 8010 --expect=200 -s "Country of Residence"
        }
from services.cfg?

Thanks

Pete
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Check_HTTP query

Post by mcapra »

The way a Service definition passes $ARGn$ values to a Command definition is by separating them with the ! character. You can choose to address each argument individually, or you can pass all the arguments in one go. It's entirely up to you at that point. One solution might make more sense than another depending on your use case.

If you define your check_http_url command like this:

Code: Select all

define command{
        command_name    check_http_url
        command_line    $USER1$/check_http -I $HOSTADDRESS$ $ARG1$ $ARG2$ $ARG3$
}
Then that command is going to be expecting 3 arguments. However, in your service definition:

Code: Select all

check_command           check_http_url!MyURL -p 8010 --expect=200 -s "Country of Residence"
You are, as far as Nagios is concerned, only passing the check_http_url command one argument (since there's only one ! character). More info here:
https://assets.nagios.com/downloads/nag ... acros.html

Another discrepancy is that your check_command is trying to pass MyURL without the -u directive. This will probably upset check_http since MyURL is just floating out there.

Just a few possible options for this setup:

Option A (4 arguments required)

Code: Select all

define command{
        command_name    check_http_url
        command_line    $USER1$/check_http -I $HOSTADDRESS$ -u $ARG1$ -p $ARG2$ --expect=$ARG3$ -s $ARG4$
}

......

define service{
        use                     generic-service
        host_name               MyServer
        #hostgroup_name          url010d
        service_description     Check Port, Status and Text
        check_command           check_http_url!MyURL!8001!200!"Country of Residence"
        }
Option B (pass everything in 1 argument)

Code: Select all

define command{
        command_name    check_http_url
        command_line    $USER1$/check_http -I $HOSTADDRESS$ $ARG1$
}

......

define service{
        use                     generic-service
        host_name               MyServer
        #hostgroup_name          url010d
        service_description     Check Port, Status and Text
        check_command           check_http_url!-u MyURL -p 8001 --expect=200 -s "Country of Residence"
        }
Former Nagios employee
https://www.mcapra.com/
neworderfac33
Posts: 329
Joined: Fri Jul 24, 2015 11:04 am

Re: Check_HTTP query

Post by neworderfac33 »

This makes sense - I'll have a play around with it tomorrow and let you know how I get on.

Thanks for your help.

Pete
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: Check_HTTP query

Post by tmcdonald »

Keep us posted!
Former Nagios employee
neworderfac33
Posts: 329
Joined: Fri Jul 24, 2015 11:04 am

Re: Check_HTTP query

Post by neworderfac33 »

Chapter 94...

My customer has now asked me if I can monitor the URL without it having any connection to a specific host, but if i don't specify a host, i get an error during the verification stage.

I'm not sure if what he's asking me for even makes sense in Nagios - all advice gratefully received!

Code: Select all


define command{
        command_name    check_http_url
        command_line    $USER1$/check_http -I $HOSTADDRESS$ -u $ARG1$
}
define service{
        use                     generic-service
        host_name               MyServerID
        #hostgroup_name          url011u
        service_description     URL http://MyURL + Text [Country of Residence] + Status [301] + Port [8001]
        check_command           check_http_url!MyURL -s "Country of Residence" --expect=301 -p 8001
        #register                0
        }
Pete
Locked