Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > ISPConfig 2 > Tips/Tricks/Mods

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #11  
Old 9th March 2009, 15:53
id10t id10t is offline
Senior Member
 
Join Date: Nov 2008
Posts: 240
Thanks: 2
Thanked 22 Times in 22 Posts
Default

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.
Reply With Quote
Sponsored Links
  #12  
Old 9th March 2009, 16:00
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
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.
i have no idea how to do any of that
Reply With Quote
  #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
  #14  
Old 10th March 2009, 09: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
  #15  
Old 10th March 2009, 15:01
id10t id10t is offline
Senior Member
 
Join Date: Nov 2008
Posts: 240
Thanks: 2
Thanked 22 Times in 22 Posts
 
Default

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.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
dns server error Hellbound General 7 2nd August 2007 09:18
Email not going out! cybereatl Installation/Configuration 12 28th February 2007 14:02
This is %#@*&^$# embarrassing! domino Smalltalk 34 5th February 2007 21:57
UebiMiau login puntjuh Installation/Configuration 13 14th March 2006 18:24


All times are GMT +2. The time now is 19:07.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.