PDA

View Full Version : help modifying check_services.php to run a custom command if any service is down


steve51184
21st February 2009, 17:52
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?

falko
22nd February 2009, 20:10
You can specify a command in the ISPConfig interface (Management > Server > Services > Monitoring).

steve51184
22nd February 2009, 20:24
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 &

/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 "

steve51184
23rd February 2009, 00:53
any ideas falko?

falko
23rd February 2009, 18:43
In that case I suggest you install monit: http://www.howtoforge.com/server_monitoring_with_munin_monit_debian_etch_p2

steve51184
23rd February 2009, 19:31
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

id10t
7th March 2009, 13:19
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.

steve51184
7th March 2009, 13:21
how do i run a a php script via php cli and will it work if apache2 is down?

falko
8th March 2009, 15:38
how do i run a a php script via php cli
php /path/to/php-script

and will it work if apache2 is down?How do you mean that?

steve51184
8th March 2009, 19:12
php /path/to/php-script

How do you mean that?

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

id10t
9th March 2009, 15:53
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.

steve51184
9th March 2009, 16:00
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 :(

id10t
10th March 2009, 03:57
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 ...


#!/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");
?>

steve51184
10th March 2009, 09:45
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 ...


#!/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)

id10t
10th March 2009, 15:01
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

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.