Check your website with nagios from external testserver
Couple a day's a go one of our loadbalancer failed during the weekend. The problem was that the servers behind the loadbalancer we're operating normal but due to the lb issues no external request reached the farm. Instead clients received "Connections refused".
Since I'm not able to place any checks on the loadbalancer (not under my administration) I decided to write a php script that calls the loadbalancer and checks for a string. If it receives a timeout or error message nagios will go on "Read Alert" and sends out notifications.
Since I'm not able to place any checks on the loadbalancer (not under my administration) I decided to write a php script that calls the loadbalancer and checks for a string. If it receives a timeout or error message nagios will go on "Read Alert" and sends out notifications.
Write the script
again this is a very basic check I'm sure improvements are possible.
<\?php
/**
* PhP2Nag
*
* Curl Script to check a site for a particular string
* if string is found. Returns OK else ERROR
*
* @author Alexander Pirsig
* @version 0.1
* @since 2009-08-19
*/
// Config Parameter
$url = "URL_TO_CHECK";
$userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1";
$pattern = "/.*(DocumentType).*/";
$timeout = 3;
// Start Request
$hits = array();
$url = str_replace( "&", "&", urldecode(trim($url)));
$ch = curl_init($url);
curl_setopt( $ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt( $ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
$output = curl_exec($ch);
// Search for pattern
preg_match_all($pattern, $output, $hits, PREG_SET_ORDER);
// Return Result
if(count($hits)>0) {
echo "OK";
} else {
echo "ERROR";
}
Wiring it up
Second step is to connect your php script with nagios. Since I use nrpe this is very easy used place a config entry in your nrpe config like this:command[nrpe_check_external_1]=/usr/lib/nagios/plugins/check_http -H YOUR_WEBSITE_URL -p 80 -u "/PATH_TO_YOUR/PHP_SCRIPT.php" -s "OK" -t 2After that make sure your restart the nrpe server and populate the check to your nagios configuration. Thats it!
Permalink Kommentare[0] Tags: php curl check_http external check nagios nrpe








