The default HTTP check in Nagios only checks for one URL, which is the IP address of the host. In a world where everyone is using name-based virtual hosts this is not very useful. Moreover, in some cases you may need to check different URLs of the same site to ensure that it’s working. This post will show you how to do that.
There are a few things that need to be glued together. You need (1) a script that takes a list of URLs, checks them and returns status codes as appropriate, (2) a Nagios command that uses the script, (3) a host definition that uses the command for checking.
The Script
This script take a comma-delimited list of URLs, each has the form of
example.com/home/index
(no http://
is needed or supported). Normally I
would make each URL a separate parameter, but Nagios doesn’t support argument
list so I’d live with this.
The host is considered “up” if check_http
on all URLs returns “OK”, otherwise
the first failure (warning or critical) will be the status of the host.
Save the script in a place you like, for example /etc/nagios3/check_urls
.
#!/bin/bash
# Split URLs by comma
urls=${1//,/ }
for url in $urls
do
hostname="${url%%/*}"
if [[ "$url" = */* ]]
then
# Domain plus path, like example.com/path
path="/${url#*/}"
else
# Just domain, like example.com
path=/
fi
result="$(/usr/lib/nagios/plugins/check_http --hostname "$hostname" --url "$path")"
status="$?"
if [[ "$status" -ne 0 ]]
then
# First warning is the final result
echo "$url: $result"
exit "$status"
fi
done
echo "All URLs OK: $urls"
The Command
Command definition can go anywhere that Nagios reads, for example
/etc/nagios3/conf.d/commands.cfg
. Remember to change the path to
check_urls
if you save it in another place.
define command {
command_name check_urls
command_line /etc/nagios3/check_urls $_HOSTURLS$
}
The Host
Again, this definition can go anywhere that Nagios reads, for example
/etc/nagios3/conf.d/hosts.cfg
.
define host {
host_name example-host
alias My Example
address 123.123.123.123
check_command check_urls
_urls my-blog.com,my-api.com/check
}
See the funny _urls
bit? It’s a custom macro.
Questions or comments can go to Google+ :)