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

Recompiling php5 with bundled gd lib support on debian etch

09:17PM Apr 19, 2009 in category Linux & Co by Alexander Pirsig

Today we're had a minor problem with the GD Lib. in PHP5-GD under Debian etch. The problem was that we've tried to you special functionality that is only available if you're using php own library. Instead of using the bundled library php provides the Debian and Ubuntu maintainers compile php5-gd package against the shared system library.

So what happens is that you're probably missing some extra functionality in your PHP gd calls for example there is no imagerotate() or imageantialias() functions. They are only available in the Bundled version that comes with PHP.

A typical error you get is this:


JpGraph Error The function imageantialias() is not available in your PHP installation. Use the GD version that comes with PHP and not the standalone version.

But solving the problem thanks to debian is quite easy and only took some compile time. ;) To solve the problem I did the following steps:

Make sure your have you source package urls in apt sources.list defined simple check it by enter:


vi /etc/apt/sources.list
# A line like the following should appear
# source urls starting with deb-src for example:
deb-src http://ftp.de.debian.org/debian/ etch main contrib non-free

Next we're installing needed packages.


# Install build tools, debian helpers and fakeroot
apt-get install build-essential debhelper fakeroot
# source code should reside in /usr/src
cd /usr/src
# Download PHP source
apt-get source php5
# Install all packages required to build PHP5
apt-get build-dep php5

Now we're altering the build path for gdlib and make php to complile against the bundled gd lib version.


cd /usr/src/[PHP_VERSION]/debian # In my case php5-5.2.0
vi rules
# Next where changing gd lib compiler flag
# REPLACE: --with-gd=shared,/usr --enable-gd-native-ttf WITH: --with-gd=shared --enable-gd-native-ttf
# save and close
cd /usr/src/[PHP_VERSION]
dpkg-buildpackage -rfakeroot
# This takes a while
cd /usr/src
# Install new php5-gd lib package in my case:
dpkg -i php5-gd_5.2.0-8+etch13_amd64.deb
# Restart your apache or what every
/etc/init.d/apache2 restart
# Happy GD-ing :)

Kommentare[6] Tags: gd ubuntu imageantialias php building problem debian recompile gd2

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