Check your website with nagios from external testserver

06:00PM Aug 26, 2009 in category PHP by Alexander Pirsig

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.

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 2
After that make sure your restart the nrpe server and populate the check to your nagios configuration. Thats it!

Kommentare[0] Tags: php curl check_http external check nagios nrpe

PHP compilation error debian lenny (stable)

08:15AM Mai 26, 2009 in category PHP by Alexander Pirsig

Today I tried to recompile GD2-Lib on Debian Lenny (stable) to get the shared support working again. But during package build I had the following problem:
/usr/src/php5-5.2.6.dfsg.1/ext/imap/php_imap.c:4557: warning: pointer targets in assignment differ in signedness
/usr/src/php5-5.2.6.dfsg.1/ext/imap/php_imap.c:4557: warning: pointer targets in g argument 1 of ‘strlen’ differ in signedness
make[1]: *** [ext/imap/php_imap.lo] B??d 1
make[1]: Opuszczenie katalogu `/usr/src/php5-5.2.6/apache2-build'
make: *** [build-apache2-stamp] B??d 2
dpkg-buildpackage: niepowodzenie: debian/rules build zwróci? status b??du 2
I think there is something wrong with the php-imap and it's dependencies to a external lib. Since I don't need it for my server I removed the compile flags from debian rules.

Open your rules-set file in /usr/src/php5-5.2.6.dfsg.1/debian/rules
--with-imap=shared,/usr
--with-imap-ssl

After that rebuild package with:
dpkg-buildpackage -rfakeroot
That's it! :)

Kommentare[0] Tags: imap error recompile problem php5 lenny

Fetching Dilbert Comic stripts from rss feed

10:17PM Apr 17, 2009 in category PHP by Alexander Pirsig

This is a php script to fetch dilbert images out of a feedburner feed. Its a quick an dirty implementation it works and I'm sure there are bug's in it.

Enjoy! :D ... ah you can find the testside here -> testside or download it here
<\?php
/**
 * Make Sure XML is cached for couple of minutes
 */
$tmpfile = '/tmp/dilbert.rss';
$gap = 60*60*1; # 1 hour(s)

if(file_exists($tmpfile)===true && (filemtime($tmpfile)+$gap)>time())
{
  # Use locale cached dilbert Content
  $xmlStr = file_get_contents($tmpfile);
}
else
{
  # Reload Cached DilbertFeed
  if(!file_exists($tmpfile))
  {
    touch($tmpfile);
  }
  $xmlStr = file_get_contents('http://feeds2.feedburner.com/DilbertDailyStrip?format=rss');
  file_put_contents($tmpfile,$xmlStr);
}
$xml = new simpleXMLElement($xmlStr);
$content = $xml->xpath('//item');
\?>








  • ".$content[0]->title.""; ?> <\?php preg_match($expr,$content[0]->description,$matches); \?> enclosure['url'] = $matches[1]; \?> <\?php echo "".$content[0]->title.""; \?> <\?php echo "
    Fetched: ".date('Y-m-d H:i:s', filemtime($tmpfile)); \?>

Kommentare[0] Tags: php simple xml dilbert xpath coding labs rss

Quick Guide to use FirePHP + FireBug Console

09:30PM Apr 16, 2009 in category PHP by Alexander Pirsig

This is a quick guide to use FirePHP + Firebug to write debug output to firebug console. FirePHP is a Log-Appender for firebug console. With this piece of code you write php debug output directly to firebug console.

The requirements

So let's get started for beginning you need four things: After you've installed firephp and firebug in firefox, you have a "small bug icon" in the lower right corner that looks like this.

Netconsole settings

Next we're go to the test site and activate firephp. So type in http://labs.pirsig.net/firephp/firetest.php click on the Firebug icon. Next click on the Net-Tab and activate the "console" and "net" checkbox.

Net-Console Output

Next Click on "Console" and reload the site you should see some debug output in firebug console.

Testside PHP-Code

Enclosed is the php code for the testside
// Include FirePHP OOP Class
require_once("FirePHP.class.php");

// Define a Global DEBUG flag
// This should probly go to a central place
DEFINE(DEBUG,true);

// Get a instance the true automaticly create's
// an instance and makes sure there is only one
// registered (singleton)
$fb = FirePHP::getInstance(true);

// Array's with firephp configuration
//
// maxObjectDepth      Maximum depth to traverse objects.
// maxArrayDepth       Maximum depth to traverse arrays.
// useNativeJsonEncode Set to FALSE to use JSON encoder included
//                     with FirePHPCore instead of json_encode().
// includeLineNumbers  Include File and Line information in message.
// Defaults:
$options = array('maxObjectDepth' => 10,
                 'maxArrayDepth' => 20,
                 'useNativeJsonEncode' => true,
                 'includeLineNumbers' => true);

$fb->setOptions($options);

// Define to control global debug input output
$fb->setEnabled(DEBUG);

// Displays different kind of log output
$fb->log('Hello log');
$fb->info('Hello info');
$fb->warn('Hello warning');
$fb->error('Hello error');

echo "Normal site output";

Related sites

Kommentare[0] Tags: labs firephp javascript console debug php firebug error-handling