Web Hosting Talk







View Full Version : [php] Monitoring services?


srok@
02-20-2006, 05:15 PM
How to do script to counting uptime (like siteuptime.com ).
I hear that only fsockopen() it isn`t good idea. How to do a better monitoring for services (specially for http). If someone can help me I`ll be glad for code for ftp too. :)

Real-Hosts
02-20-2006, 05:41 PM
Depends on the method for HTTP.

Standard monitoring services will 'ping' the port. This will tell you if a service exists and is running the port, but won't tell you if it's on, but not responding. So your best bet is to open a socket, and mimic the HTTP connect. When it recieves the "HTTP OK" then you know the server is OK (within reason) on HTTP with whatever webserver you are monitoring.

With FTP,
You could mimic the same as above, but for the FTP login, so as soon as it prompts for login, you can assume it's OK.

We run a Nagios/custom blend for our own internal monitoring. We find it highly effective, and since we monitor using our US servers (US monitor UK, UK monitor US) they also perform a tracert and email that contents, along with the errors and ping results. So we can tell if a hop has died (provider) and which hop it is, whether the server is on (even if broken, most servers tend to ping when on) and any errors in connecting to a service.

srok@
02-20-2006, 05:44 PM
this i know, but i don`t know how to do it in php :(

getweb
02-23-2006, 12:26 AM
You could do an fopen() to a special page on the site you want to monitor.

- Create a PHP page on the target server that connects to database and does a simple query, saying simply "DB OK" when it's done, or something else on failure.
- On the monitoring server, do an fopen() to that remote page and fread() the contents into a string variable.
- Check the string variable for the text "DB OK", and if it's not found, or if the fopen() fails completely, do whatever action you want :)

Most of the paid monitoring services offer something like this, to run a webpage and check for a certain string, sending a message to your pager if it can't find it.

Do a google search for "PHP ping" and you'll find lots of sample code.

Idealws.com
02-23-2006, 05:55 AM
Hello srok@,

Here is a little script I made up some time ago you can use and modify to
your liking. Here is a example of its output: http://idealws.com/status/status.php



<html>
<head>
<title>Service Status</title>
<style>
td { font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: xx-small; color: #000000;}
.title { font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: xx-small; font-weight: bold; color: #FFFFFF; background-color: #2F82A0;}
.titlelgt { font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: xx-small; font-weight: bold; color: #000000; background-color: #E9E9E9;}
</style>
</head>
<body>
<?php
/*
* Turn PHP Error Reporting Off
*/
error_reporting(0);
/*
* Enter list of servers and their ips in array
* Example: 'server.yourdomain.com'=>'196.122.0.1'
*/
$ips = array('server1.idealws.com'=>'209.67.210.74', 'idealwebspace.com'=>'idealwebspace.com');
/* Enter the ports you wish to scan
* Example: 'HTTP'=>'80', 'FTP'=>'21'
*/
$services = array('HTTP'=>'80', 'FTP'=>'21','MySQL'=>'3306',
'SMTP'=>'25', 'POP3'=>'110');
// DO NOT EDIT THIS LINE
foreach($ips as $server => $ip) {
?>
<table width="300" cellspacing="0" cellpadding="3" border="1" bordercolordark="#FFFFFF" bordercolorlight="#C0C0C0">
<tr>
<td colspan=2 class="title"><?php echo $server; ?> (<?php echo $ip; ?>)</td>
</tr>
<?php
// DO NOT EDIT THIS LINE
foreach($services as $service => $value) {
if($fp = fsockopen ($ip, $value, $errno, $errstr, 10)){
?>
<tr>
<td class="titlelgt" width="285"><?php echo $service; ?></td>
<td align="center" width="15"><img src="green_lt.gif" alt="<?php echo "$service is up"; ?>"></td>
</tr>
<?php
// DO NOT EDIT THIS LINE
}else{
?>
<tr>
<td class="titlelgt" width="285"><?php echo $service; ?></td>
<td align="center" width="15"><img src="red_lt.gif" alt="<?php echo "$service is down"; ?>"></td>
</tr>
<?php
// DO NOT EDIT THIS LINE
}
$fp = null;
}
?>
</table>
<br>
<?php
// DO NOT EDIT THIS LINE
}
?>
</body>
</html>


This is controled by an array and can have as many servers or ports scanned as
you would like.

Hope this helps.

Regards,
Ray

srok@
02-23-2006, 05:57 AM
i made my own script...
It working that if httpd server send HTTP header it is taking it as server up else as down.