Tuesday August 22, 2017

Basic Port Uptime Script

Date: 03/17/2005
Author: Wayne Eggert


This is a rather simple script to demonstrate an application that many server administrators could probably find useful -- an uptime notifcation script. As a server administrator, you are often in charge of keeping watch on a large number of services on servers. This script checks to see if it can successfully initate a connection to defined ports on a host, and if it's unable to, emails the designated recipients. You can of course define a mobile phone text messaging address & it will send notifications there as well. It could probably be modified to pull back a response from the a port & compare it to a message that the service should respond with, rather than just check if the port is up as it is currently.

// Host to check
$host "www.yourdomain.com"// *** CHANGE THIS TO YOUR DOMAIN ***

// Define the ports we'll be checking
$ports = array(21=>"FTP",25=>"Sendmail",80=>"Apache Web Server",3306=>"MySQL Server");

// Initially assume there isn't a problem with the services
$problem 0;

// Check to see if a socket can be opened to each of the ports in $ports
foreach($ports as $port => $service){
$fp fsockopen($host,$port,$errno,$errstr,10); 
$portmsg.="Port ".$port." - ".$service."\n";

// Notify the intended recipients if there is a problem
if($problem == 1){
// send full email notifications of service outage
$recipients "youremail@domain.com";  // *** CHANGE THIS TO YOUR EMAIL ***
$msg date("M d, Y h:i:s",time())."\n\n";
$msg.= "The following service(s) were unreachable and may require immediate attention:\n\n";
$msg.= $portmsg
$subject 'Service Unreachable!';
$headers .= "From: Server Status <root@localhost>\r\n";
$headers .= "X-Sender: <root@localhost>\r\n";
$headers .= "Content-Type: text; charset=iso-8859-1\r\n";
mail($recipients$subject$msg$headers) or die("Problem sending mail.");


  • The first few lines of the script define the host, ports to check & the services running on the ports, and set the variable indicating a problem (cleverly named $problem) to false.
  • The foreach loop runs through all the defined ports and attempts to make a connection using PHP's fsockopen function, which will return false if the script is unable to connect to the port.
  • Upon finding an issue with any of the ports being checked, the $problem variable will be set to true.
  • Finally, the script emails a notification to the user if any of the ports could not be reached.

Adding to CRON
This script is intended to be run periodically on a server or (for redudancy) several severs. You'd likely want to set it to run every 5-10 minutes, but you could set it to run more often depending upon how much of a priority the services you're checking are.

To add to CRON add the following lines to /etc/crontab or root's crontab:
*/10 * * * * root lynx -dump http://www.yourdomain.com/portcheck.php 1> /dev/null 2> /dev/null
*Be sure to replace www.yourdomain.com with the appropriate http location of your script.

You could also run this using php's cgi executable, if you have it installed on your system (likely in /usr/bin/php).

While this script is pretty basic, even without checking port responses it's still valuable to a system admin that just wants to check if the ports are responding. Why pay a 3rd-party company to notify you if a few ports are down when you can utilize your own resources to check your servers? I don't advocate using this for life-or-death port monitoring situations, but for many users it should be more than adequate to get a heads up when a problem arises.

No comments have yet been made.