View Single Post
  #13  
Old 10th March 2009, 03:57
id10t id10t is offline
Senior Member
 
Join Date: Nov 2008
Posts: 240
Thanks: 2
Thanked 22 Times in 22 Posts
Default

Then I guess you are luck that I do...

Quick and sloppy, simply opens a connection to the destination port and returns an error or an OK. Licensed under GPL v. 2

Copy/paste this to a file, save it on a machine that has php-cli installed and has network access to whatever servers and ports you want to check. Edit it so there is a definition of service name, service host, and service port. Run it manually as a test, then if all is good set it up as a cron job - when you do, redirect stderr to /dev/null and let cron handle sending you email with the stdout ...

Code:
#!/usr/bin/php
<?php

// copyright 2009 gruv.org
// this code is licensed for use under the GPL v. 2


//****************************************************************
// List of services and hosts to check
// Each service needs a name (whatever you like),
// the network port to connect to
// and the hostname to connect to
// Remeber - the machine running this script must
// be able to connect to these services on these hosts
//****************************************************************

$service_name[]="ISPConfig";
$service_port[]="81";
$service_host[]="www.example.com";

$service_name[]="IMAP";
$service_port[]="143";
$service_host[]="imap.example.com";

$service_name[]="WWW";
$service_port[]="80";
$service_host[]="www.example.com";

$service_name[]="MySQL";
$service_port[]="3306";
$service_host[]="localhost";

$service_name[]="WWW-SSL";
$service_port[]="443";
$service_host[]="www.example.com";

$service_name[]="SSH";
$service_port[]="22";
$service_host[]="example.com";

//****************************************************************
// DO NOT EDIT BELOW THIS LINE
//****************************************************************

function chkServer($host, $port){  
    $hostip = @gethostbyname($host); 
    if ($hostip == $host){
        echo "Server ".$host." is down or does not exist\n\r";
	return false;
    }
    else{
        if (!$x = @fsockopen($hostip, $port, $errno, $errstr, 5)){
            return false;
        }
        else{
            if ($x){
                @fclose($x); //close connection
            }
	    return true;
        } 
    }
}

if((count($service_name)!=count($service_port))||
   (count($service_name)!=count($service_host))||
   (count($service_host)!=count($service_port))){
    print("\n\n
Your host array is incorrect - each service needs a 
description, host name, and port.\n\n");
  exit;
}

for($i=0;$i<count($service_name);$i++){
  $isup=chkServer($service_host[$i],$service_port[$i]);
  $time=date("H:i n-j-y");
  if(!$isup){
    print($time." - Service ".$service_name[$i]." on host ".$service_host[$i].":".$service_port[$i]." is down\n\r");
  }
}
print("Service check complete.\n\r\n\r");
?>

Last edited by id10t; 10th March 2009 at 04:03.
Reply With Quote