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 function accepts five parameters:
$server
: The target server (e.g.,www.example.com
).$port
: The port to check (e.g.,80
for HTTP).$timeout
: The timeout period for the ping request.$db
: The file to store the server’s status (up
ordown
).$email
: The email address to send notifications to.
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 the current status is “down” and the previous status was also “down”, it sends an email notification stating the server is down.
- If the server’s status changes from “down” to “up”, it sends an email stating the server is back up.
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
- Ping Log Functionality: This script regularly checks the status of servers by pinging them and logs the result in a file. If there is a status change, it triggers an email notification.
- Email Notifications: The script sends email notifications when the server is down or comes back up.
- Ping Mechanism: Uses
fsockopen()
to simulate a “ping” and determine if the server is reachable. - File Handling: The server status is stored in a file (
IRS.txt
,PA.txt
) to track previous statuses.
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.