help modifying check_services.php to run a custom command if any service is down

Discussion in 'Tips/Tricks/Mods' started by steve51184, Feb 21, 2009.

  1. steve51184

    steve51184 New Member

    hi all i need help modifying check_services.php to run a custom command if any service is down

    the file i want to run is a php script that sends me an sms and i thought as ispconfig has it's own install of php that even if apache is down i'll get an sms wherever i am in the world.. so how do i edit check_services.php to run the command?
     
  2. falko

    falko Super Moderator ISPConfig Developer

    You can specify a command in the ISPConfig interface (Management > Server > Services > Monitoring).
     
  3. steve51184

    steve51184 New Member

    is that in the "Offline" bit?

    also this is the command i'm trying to run but it won't work as it has special characters like the &

    Code:
    /root/ispconfig/php/php "/var/www/web1/web/sms/index.php?sendto=1&message=xxx&submit=Submit" &> /dev/null
    also i've tried with and without the "
     
    Last edited: Feb 22, 2009
  4. steve51184

    steve51184 New Member

    any ideas falko?
     
  5. falko

    falko Super Moderator ISPConfig Developer

  6. steve51184

    steve51184 New Member

    monit sounds cool but how is this any different from the service monitoring that ispconfig or even webmin has built in?

    also i'm wanting to run that above command if/when a service is down and as far as i know monit won't do that but ispconfig will
     
  7. id10t

    id10t Member

    Of course, ISPConfig may be down as well.

    I would take a look at the check service php file in ISPConfig, and make a copy of it to work on. Then modify to check services and ispconfig up, send mail/sms/etc, and make it run under PHP cli. Then schedule as a cron job.
     
  8. steve51184

    steve51184 New Member

    how do i run a a php script via php cli and will it work if apache2 is down?
     
  9. falko

    falko Super Moderator ISPConfig Developer

    Code:
    php /path/to/php-script
    How do you mean that?
     
  10. steve51184

    steve51184 New Member

    well i'm wanting to run a php script that will sms me if apache2 goes down so i'm asking can i still use php if apache is down... that why i was originally wanting to use ispconfigs copy of php
     
  11. id10t

    id10t Member

    Sure, with the php cli which ispconfig requires to be installed anyway (at least, in the debian server setup).

    Since it is on a command line, it will work as long as your file system can be read, etc. just like a bash script, perl script, etc.

    As a bonus, the 30 second time limit and memory limits don't apply.

    You may want/need to make a copy of the script and specify php as the handler, remove HTML output, etc. instead of just running the script as-is

    Remember that cron will send any output/errors to local email, so you may want to have no output and just let the php script send/call.
     
  12. steve51184

    steve51184 New Member

    i have no idea how to do any of that :(
     
  13. id10t

    id10t Member

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

    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: Mar 10, 2009
  14. steve51184

    steve51184 New Member

    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)
     
  15. id10t

    id10t Member

    I recommend doing it as the command line version because the webserver may be down, ISPConfig may be down, etc. Also this lets you check any host you like... on any port you like.

    At the very very bottom of the script look for
    Code:
    if(!$isup){
        print($time." - Service ".$service_name[$i]." on host ".$service_host[$i].":".$service_port[$i]." is down\n\r");
      }
    
    Replace the print function with whatever you want to do... after all, it is GPL so you are free to modify it... have it grab a URL via curl or fopen, use the php mail() function or do a system call out to a mail utility like mail or mailm, etc.
     

Share This Page