Skip to content

Pingman PHP Script

Published: at 04:20 AM

The following PHP script is used to monitor the status of a server by pinging it and logging its status (either “up” or “down”) to a file. If the status changes (for example, the server goes down or comes back up), the script sends an email notification to the specified address.

While this article focuses on the PHP code, here’s is an explanation of how to set it up.

Script Code

<?php
function ping_log($server, $port, $timeout, $db, $email) {
    $current_status = ping($server, $port, $timeout);

    if (file_exists($db)):
        $previous_status = file_get_contents($db, true);
    else:
        file_put_contents($db, "up");
        $previous_status = "up";
    endif;

    if ($current_status == "down"):
        //echo "Server is down! ";
        file_put_contents($db, "down");
        if ($previous_status == "down"):
            mail($email, "Server down:" + $server, "Your server is down.");
            //echo "Email sent.";
        endif;
    else:
        //echo "Server is up! ";
        file_put_contents($db, "up");
        if ($previous_status == "down"):
            mail($email, "Server back up:" + $server, "Your server is back up.");
            //echo "Email sent.";
        endif;
    endif;
}

function ping($host, $port, $timeout){
    $tB = microtime(true);
    $fP = fSockOpen($host, $port, $errno, $errstr, $timeout);
    if (!$fP) { return "down"; }
    $tA = microtime(true);
    return round((($tA - $tB) * 1000), 0)." ms";
}
?>

Explanation of Code

This script performs the task of monitoring the status of a server by checking whether it is “up” (responsive) or “down” (not responding). If the status changes, it sends an email notification.

1. Ping Log Function: ping_log()

The ping_log() function is the core of the script, responsible for checking the server status and logging the result.

function ping_log($server, $port, $timeout, $db, $email) {
    $current_status = ping($server, $port, $timeout);

The ping_log() function checks the current status of the server by calling the ping() function and compares it with the previous status stored in the $db file.

2. Checking for Previous Status

If the status file ($db) exists, it reads the previous status from the file. If the file doesn’t exist, it creates the file and sets the initial status as “up”.

if (file_exists($db)):
    $previous_status = file_get_contents($db, true);
else:
    file_put_contents($db, "up");
    $previous_status = "up";
endif;

3. Status Change Logic

if ($current_status == "down"):
    file_put_contents($db, "down");
    if ($previous_status == "down"):
        mail($email, "Server down:" + $server, "Your server is down.");
    endif;
else:
    file_put_contents($db, "up");
    if ($previous_status == "down"):
        mail($email, "Server back up:" + $server, "Your server is back up.");
    endif;
endif;

4. Ping Function: ping()

The ping() function sends a network request to the specified server and port and measures the round-trip time in milliseconds. If the server is reachable, it returns the response time in milliseconds. Otherwise, it returns "down".

function ping($host, $port, $timeout){
    $tB = microtime(true);
    $fP = fSockOpen($host, $port, $errno, $errstr, $timeout);
    if (!$fP) { return "down"; }
    $tA = microtime(true);
    return round((($tA - $tB) * 1000), 0)." ms";
}

5. Mail Notifications

The mail() function is used to send email notifications. If the server’s status changes, an email is sent to the specified address with a message about the server’s status.

Summary

Use Case

This script is useful for monitoring the uptime of websites or services, ensuring that administrators are notified via email when their servers experience downtime or are back online.

For more information about PHP mail functions, check the PHP documentation.


Previous Post
EF with SP using ExecuteSP
Next Post
.htaccess Configuration