PHP Server Monitor – Add Ping Functionality

Adding Ping (ICMP) to PHP Server MonitorPHP Monitor ICMP

I love PHP Server Monitor, it is an amazing tool for my business to have a simple, reliable, practical way to ensure which services or devices are online. The only complaint I have with it is that it does not support ping monitors, only services (query Port 3389 for example).

Major thanks to Michele Mariotti and insuman on the PHP Server Monitor forums (Post Link) for writing up some code that allows ICMP functionality, as long as the service is Port 1.

Log in to your server via Putty, make a backup of the StatusUpdater function definitions, and replace the old code that will allow you to use ICMP.

 

# For me, PHP Server Monitor it installed under /var/www/html, your path may be different.
cd /var/www/html/src/psm/Util/Server/Updater

cp StatusUpdater.class.php StatusUpdater.class.php.bak

sudo nano StatusUpdater.class.php

 

#Find the function
Ctrl + W (search)

function updateService

<Enter>

 Old Code in StatusUpdater.class.php

protected function updateService($max_runs, $run = 1) {
    $errno = 0;
    // save response time
    $starttime = microtime(true);

    $fp = fsockopen ($this->server['ip'], $this->server['port'], $errno, $this->error, 10);

    $status = ($fp === false) ? false : true;
    $this->rtime = (microtime(true) - $starttime);

    if(is_resource($fp)) {
      fclose($fp);
    }

    // check if server is available and rerun if asked.
    if(!$status && $run < $max_runs) {
      return $this->updateService($max_runs, $run + 1);
    }

    return $status;
  }

 

Change to New Code in StatusUpdater.class.php

This is hardcoding TCP Port 1 in PHP Monitor, to use ICMP/Ping. The default timeout is 5 seconds, adjust the number 5  in the timeout variable ($timeout) to whatever time in seconds you want.

You can use Ctrl+K in nano to delete an entire line at once, rather than holding the Backspace or Delete keys.

###NEW CODE###
protected function updateService($max_runs, $run = 1) {

        if (($this->server['port']) == 1) {
            /* timeout min: 5 sec */
            $timeout = ($this->server['timeout'] < 5 ? 5 : $this->server['timeout']);
            /* save response time */
            $starttime = microtime(true);
            /* ICMP ping packet with a pre-calculated checksum */
            $package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";

            $socket = socket_create(AF_INET, SOCK_RAW, 1);
            socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
            socket_connect($socket, $this->server['ip'], null);
            $ts = microtime(true);
            socket_send($socket, $package, strLen($package), 0);

            if (socket_read($socket, 255)) {
                $status = true;
            } else {
                /* store error reason */
                $this->error = socket_last_error() .' '. socket_strerror(socket_last_error());
                $status = false; 
            }
            $this->rtime = (microtime(true) - $starttime);
            socket_close($socket);
        } else
        //rest of code
        { 
            $errno = 0;
            // save response time
            $starttime = microtime(true);

            $fp = fsockopen ($this->server['ip'], $this->server['port'], $errno, $this->error, 10);

            $status = ($fp === false) ? false : true;
            $this->rtime = (microtime(true) - $starttime);

            if(is_resource($fp)) {
                fclose($fp);
            }
        }

Ping Monitor Example

PHP Monitor ICMP Server Example

6 thoughts on “PHP Server Monitor – Add Ping Functionality

  1. Hello,
    Congratulations for this tutorial and thank you.
    I am all the same confronted with a problem on my php server monitor.
    when I want to do an update. it puts me an error.

    “Status off 1 Operation not permitted”

    Have you an idea of the problem?

    ps: I’m french, sorry for the bad translation.
    thank you very much

    1. Hi Blin, sorry, just now seeing your post. Are you trying to update the entire program PHPServerMonitor itself? Do you need to run it as administrator/sudo?

      1. Hi Dan Kruse, no problem .
        The version I use is PHP Server Monitor v3.1.1 . The installation is made on a debian with the root account.

        1. I still don’t understand what you are trying to do. 3.1.1 is the most recent release already, from 2014. It is highly unlikely there will be another update, the main programmer has moved on to other projects. From some quick googling, that is a python library error. If this server only runs PHPServerMonitor, just back up your database and data and rebuild the OS. My guess is you are updating the Debian OS and a python update broke php monitor. Trying to fix anything with Linux libraries is always a nightmare, just go with an Ubuntu server build, and import your data or just recreate the entries.

  2. Howdy! I sіmpolу would likе to offer you a huge thumbs up for
    your great info үоu have got hеre on this post.

    I’lⅼ be returning tоo youyr web site for more soon.

Leave a Reply

Your email address will not be published. Required fields are marked *