View Single Post
  #14  
Old 10th March 2009, 10:45
steve51184 steve51184 is offline
Member
 
Join Date: Mar 2008
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by id10t View Post
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");
?>
first off i want to say thank you very much for helping but i think you've misunderstood what i need

what your script does:
checks for a port on a host and email's IF it's not found (although i don't understand how it can email me)

what i need:
i need a way to run a php scrip if the check_services.php finds a service is down (or i can use yours as that seems nice and simple but i need it so IF a host is down it runs a php script)
Reply With Quote