Knowledge Base

Publishing Monitoring Results using a PHP Script (Alternative 2)

This article explains how you can access the TEXTFILE interface of IPCheck Server Monitoring using PHP to publish the current monitoring results using an external webserver. This way you can e.g. apply your own look&feel or do your own user accounting.

In some situations the features for monitoring status publishing of IPCheck Server Monitor may not be flexible enough. Using the TEXTLIST interface (which is also used by the Windows GUI) you can access the current monitoring status via HTTP using an external script.

This sample shows a PHP script that requests the monitoring data for one user account from the IPCheck server and publishes the data into a simple HTML file.

Note: There is also another script available in article 7

This screenshot shows the output of both scripts (left: original, right: alternate). Click the image to zoom in.

IPCheck user Daniel Netz has published his alternate PHP script (thanks Daniel!)

Alternate IPCheck Monitoring Status PHP script by Daniel Netz


		<?php
		
/*
    * Paessler IPCheck PHP Interface
    *
    * - reads data from the textlist and displays all sensors on one screen
    *
    * date: 2006-07-11
    * author: Daniel Netz
    * requires: PHP 5
    */

    /* user configuration */
$paessler_server    = '127.0.0.1';
$paessler_user        = 'ipcheck@mydomain.com';
$paessler_passwd    = 'mypassword';
$update_freq        = 45;
$show_num_messages    = 5;
$date_format        = "y-m-d h:ia";         //see http://se.php.net/manual/en/function.date.php
$timezone            = "UTC";
/* end of user configuration */

class paessler
{
public $lines;
public $messages;
public $groups;
public $servers;
public $sensors;

public function __construct($server, $user, $password)
        {
$this->lines = file("http://$server/textlist?login=$user&pass=$password");

            if ($this->lines !== FALSE)
$this->__process_data();
            else
error_msg("could not get textlist");
        }

/* divide text data into objects */
private function __process_data()
        {
            foreach($this->lines as $line)
            {
$parts = explode(',', substr($line, 3, 999)); 

                  if (substr($line, 0,3) == 'Gr:')
$this->groups[] = new paessler_group($parts[0]);

                 if (substr($line, 0,3) == 'Sr:')
$this->servers[] = new paessler_server(end($this->groups), $parts[0]);

                if (substr($line, 0,3) == 'Sn:')
$this->sensors[] = new paessler_sensor(end($this->servers), $parts[0], $parts[2], $parts[3], $parts[4], $parts[5], $parts[6]);

                if (substr($line, 0, 3) == 'Me:')
$this->messages[] = new paessler_message($line);
            }

$this->messages = array_reverse($this->messages);

$this->lines = array();
        }
    }

    class paessler_group
{
public $name;

public function __construct($name)
        {
$this->name = $name;
        }
    }

    class paessler_server
{
public $name;
public $parent;

public function __construct($parent, $name)
        {
$this->name = $name;      
$this->parent = &$parent;
        }
    }

    class paessler_sensor
{
public $parent;
public $name;
public $type;
public $status;
public $interval;
public $last_scan;
public $result_comment;

public function __construct($parent, $name, $type, $status, $result_comment, $interval, $last_scan)
        {
$this->name = $name;
$this->parent = &$parent;
$this->type = $type;
$this->last_scan = $last_scan;
$this->result_comment = $result_comment;
$this->interval = $interval;
$this->status = strtoupper($status);
        }
    }

    class paessler_message
{
public $text;
public $date;

public function __construct($line)
        {
              global $date_format; 

$parts = explode(',', substr($line, 3, 999));

$this->text = $parts[3];
$this->date = date($date_format,($parts[0]+$parts[1]/10000000-35065)*24*60*60+mktime(0, 0, 0, 1, 1, 1996));
        }
    }

    function error_msg($text)
    {
        die($text);
    }
?>
<html>
    <head>
        <meta http-equiv="refresh" content="<?php print $update_freq;?>">
        <title>IPCheck Server Monitor on <?php print $paessler_server;?></title>
    </head>
    <body>
        <style>
            body { background-color: #eee; font-family: verdana; font-size: 11px; }
            .warning { background-color: orange; }
            .error { background-color: #ff3333; }
            .normal { background-color: lightgreen; }
            .paused { background-color: #3366ff; }
            .odd { background-color: #eee; }
            table { width: 99%; padding: 2px; margin: 2px; background-color: #fefefe; }
            th { background-color: #104E8B; color: white; font-weight: bold; }
            td { font-size: 10px; }
            h1 { padding: 0px; margin: 0px; margin-top: 8px; font-size: 20px; font-weight: bold; }
            a { text-decoration: none; color: black;}
        </style>
<?php 
if (isset($_GET['messages']) AND $_GET['messages'] == '1')
$messages = 0;
            else
$messages = 1;
?>
        <h1><a href="?messages=<?php print $messages;?>">Messages</a></h1>
        <table>
            <tr><th>date</th><th>message</th></tr>
<?php
            date_default_timezone_set($timezone);

$paessler = new paessler($paessler_server, $paessler_user, $paessler_passwd);

/*** messages ***/
foreach($paessler->messages as $message)
            {
                  if ($show_num_messages < 1 AND $messages == 1)
                      break;
                  else
$show_num_messages--;

                  if (strpos($message->text,'DOWN') > 0)
$class = 'error';
                else if (strpos($message->text,'WARNING') > 0)
$class = 'warning';
                else if (strpos($message->text,'UP') > 0)
$class = 'normal';
                else
$class = 'odd';

                print "<tr><td class=\"$class\">$message->date</td><td class=\"$class\">$message->text</td></tr>\n";
            }
?>
        </table>
        <h1>Sensors not UP</h1>
        <table>
            <table><tr><th>Sensor</th><th>Type</th><th>Status</th><th>Result</th><th>Last Scan</th></tr>
<?php
/*** not UP sensors ***/
foreach($paessler->sensors as $sensor)
            {
                  if ($sensor->status !== 'UP')
                  {
                        switch($sensor->status)
                      {
                        case 'UP':        $class = 'normal';    break;
                        case 'DOWN':    $class = 'error';    break;
                        case 'PAUSED':    $class = 'paused';    break;
                        case 'WARNING':    $class = 'warning';    break;
                    }

                        print "<tr><td>$sensor->name on ".$sensor->parent->name."</td>";
                        print "<td>$sensor->type</td>";
                        print "<td class=\"$class\">$sensor->status</td>";
                        print "<td>$sensor->result_comment</td>";
                        print "<td>".date($date_format, time() - $sensor->last_scan)."</td></tr>\n";
                }    
            }
?>
        </table>
        <h1>All Sensors</h1>
        <table>
            <tr><th>Server</th><th colspan="10">Sensors</th></tr>
<?php
            $server_class = '';

/*** all sensors ***/
foreach($paessler->servers as $server)
            {
$server_class == '' ? $server_class = 'odd' : $server_class = '';

                print "<tr><td class=\"$server_class\" nowrap width=\"1%\">$server->name</td>";

                foreach($paessler->sensors as $sensor)
                {
                      switch($sensor->status)
                      {
                        case 'UP':        $class = 'normal';    break;
                        case 'DOWN':    $class = 'error';    break;
                        case 'PAUSED':    $class = 'paused';    break;
                        case 'WARNING':    $class = 'warning';    break;
                    }

                    if ($sensor->parent == $server)
                        print "<td class=\"$class\">$sensor->name</td>";
                }

                print "</tr>\n";
            }

?>
        </tr></table>
    </body>
</html>