On first sight, Nagios it doesn’t look like Nagios can be used to monitor websites hosted on the same server. All concepts in Nagios revolve around services and hosts. The whole “sub-part of service”, like a VirtualHosts in the HTTP service, doesn’t seem to fit. It is perfectly possible though.
Let’s start with a simple plugin to check whether a VirtualHost if functioning correctly (returning HTTP status 200, 301 or 302). This can’t be done with check_http, because it will only warn when a 404 is found.
#!/usr/bin/php
<?php
$headers = @get_headers($argv[1], 1);
if(!$header) { echo "Could not connect to $url"; exit(2); }
if(preg_match('/200/', $headers[0])) {
print_r($headers[0]);
exit(0);
} elseif(preg_match('/302/', $headers[0])) {
print_r($headers[0]);
exit(0);
} elseif(preg_match('/301/', $headers[0])) {
print_r($headers[0]);
exit(0);
} else {
print_r($headers[0]);
exit(2);
}
?>
Save this in /usr/lib/nagios/plugins/check_www and make the file executable.
Next, we have to create a command for Nagios to use. Paste the following in /etc/nagios-plugins/config/www.cfg
define command{
command_name check_www
command_line /usr/lib/nagios/plugins/check_www '$ARG1$'
}
Now finally, add service definitions like this to your Nagios configuration file.
define service {
host_name yourservername
service_description theurlyouwanttomonitor.com
check_command check_www!http://theurlyouwanttomonitor.com
use generic-service
}
Good luck!