
|
View Full Version : PHP server status script
Eiolon 02-23-2007, 07:52 PM I am looking for a server status script. There are lots of them out there but I don't want any of the fancy features or designs. I just want a plain page with the following:
Server: [name]
Status: [up or down]
That's it. The less code the better. Does anyone know of a script out there like this? Thanks!
tobiasly 02-24-2007, 12:43 AM The less code the better you say? How about this?
<h1>Host Status</h1>
<?php
// put your host names or addresses in this array
$hosts = array('host1.example.com', 'host2.example.com', '192.168.0.1');
// max seconds to wait for a response from each host
$timeout = 10;
foreach($hosts as $host) {
$cmd = "/bin/ping -q -c 1 -W $timeout $host";
exec($cmd, $out, $result);
?>
<p>
<b>Server:</b> <?php echo($host); ?></b><br />
<b>Status:</b> <?php echo($result? "Down" : "Up"); ?></b>
</p>
<?php
}
linux-tech 02-24-2007, 12:52 AM $cmd = "/bin/ping -q -c 1 -W $timeout $host";
Bad code!
$fp = fsockopen("$host", $port, $errno, $errstr, $timeout);
if (!$fp) {
echo "ERROR: $errno - $errstr<br />\n";
} else {
fwrite($fp, "\n");
echo fread($fp, 26);
fclose($fp);
}
ALWAYS use something like fsockopen, so that you're not forcing individuals to accept ICMP data which should be disabled by default.
hitman3266 02-24-2007, 01:15 AM is there any script similar to this however it writes the results in database, so like XX uptime today, and XX uptime yesterday, so more of an uptime script for windows utilizing mysql?
Eiolon 02-24-2007, 01:34 AM Either of that code will do just fine as it is only internal I am using it on. Thanks for your help!
innova 02-27-2007, 05:37 PM You can also attempt to open socket connections to each port if you want to test if Apache (80), FTP (21), SSH (22) and the like are open and responding to connections. That would be easy to add without a lot of extra complexity.
is there any script similar to this however it writes the results in database, so like XX uptime today, and XX uptime yesterday, so more of an uptime script for windows utilizing mysql?
I am sure there are. This is also easy to implement though.
You would need to run it from CRON every X minutes, and have database connection details in. Have fields for ID, a datetime field, the machine IP, and the service name. Query the service in the manner described above, then record the results into the database. You would then have a separate script that would query the DB and show you your reports of uptime by day, week, month, etc.
Then, after you write all that, you will probably just download a premade script :)
linux-tech 02-27-2007, 08:23 PM Then, after you write all that, you will probably just download a premade script
Not always
Depending on what you're looking for, you will most likely be more flexible with your own code, not with something that's "already written".
In the original case, I would go with fsockopen, as that IS compatable with everything , and does NOT require the system() command which may or may not be available on all servers.
Eiolon 02-28-2007, 05:57 PM I have a question for the fsockopen. If I want to use it behind a proxy server, what do I need to add to the script? Thanks!
yourihost 02-28-2007, 07:05 PM Thanks to the people who contributed.
tobiasly 03-01-2007, 12:48 AM I have a question for the fsockopen. If I want to use it behind a proxy server, what do I need to add to the script? Thanks! You can't use fsockopen "through" a proxy. That function opens a "raw" socket connection and it is up to you to pass the data back and forth. A proxy forwards the HTTP conversation which occurs "on top" of the network connection. If there is a proxy in the way then fsockopen will not allow you to test the status of a remote host.
If you say it's only internal, why do you want to forward through a proxy? Or are you testing external hosts also?
VPSBite 03-01-2007, 12:51 AM I dont use 1 i just use whmcs it has it already
its very good
Eiolon 03-01-2007, 10:47 AM You can't use fsockopen "through" a proxy. That function opens a "raw" socket connection and it is up to you to pass the data back and forth. A proxy forwards the HTTP conversation which occurs "on top" of the network connection. If there is a proxy in the way then fsockopen will not allow you to test the status of a remote host.
If you say it's only internal, why do you want to forward through a proxy? Or are you testing external hosts also?
It is for internal use only but I was curious and tried it on an external server. Is there no way to have a server status script work if you are behind a proxy?
tobiasly 03-01-2007, 11:40 AM It is for internal use only but I was curious and tried it on an external server. Is there no way to have a server status script work if you are behind a proxy? Just to be sure we're clear on terminology, a proxy takes HTTP (among other) requests and forwards them on to another server, then listens for the response from that server and hands them back to you. It operates on a "higher level" than a firewall or router, which looks at the actual packets. So no, you can't really test a server's status by going through a proxy.
I suppose you could still issue an HTTP GET through the proxy for the external host, and see if the proxy can contact the external host and return a response (of course if you actually wanted to do that you should use an existing library such as HTTP_Request (http://pear.php.net/manual/en/package.http.http-request.proxy-auth.php)) but that's not very useful since a problem with the proxy would give you a false negative.
However, even if all port 80 traffic goes through a proxy you may have other ports available to test. For example you could probably attempt a port 25 connection if the external host is running an SMTP server. (I'm assuming these external hosts are your own, or ones you are familiar with the configuration of, because making a port 25 or any other connection to "test" and then dropping it w/o sending data will make you look an awful lot like you are probing the server and you may get blacklisted).
So basically what it boils down to is figuring out how your network and/or proxy are configured and figuring out or configuring the external host's services to be able to respond to your request. There are many ways you can do this, either with ping or with a socket connection, but you just want to make sure you aren't opening any ports or services unnecessarily. If you want to enable ping on the external host but are worried about having your host pingable by everyone (again, that issue is far from agreed upon; do a bit of Googling on the subject for pros and cons) then adjust its firewall to only allow pings from your status-checking host.
Eiolon 03-01-2007, 03:40 PM Okay, that's what I wanted to know. Thanks!
|