I wrote (well-compiled from other people's code) a very simple time monitor for our servers - this is just an ICMP (ping) monitor, and it works great for our limited number of servers (about 20) and very fast. Here's the code (the actual ping verification functions, which I think are based on the work of Jensen Birk ( http://birk-jensen.dk/2010/09/php-ping/ ), and I just used its functions to display green a PNG mug when everything is inserted, and red for each server that is omitted (if any).
<html>
<head>
<style type='text/css'>
*{
font-family:verdana,tahoma,arial;
font-size:17px;
}
.light{width:30px;}
h1{
font-size:25px;
}
</style>
<meta http-equiv="refresh" content="30">
</head>
<body>
<?php
$time1=date('H:i:s');
echo "Last Refresh Time = $time1<br/><hr/>";
error_reporting(0);
function icmpChecksum($data)
{
if (strlen($data)%2)
$data .= "\x00";
$bit = unpack('n*', $data);
$sum = array_sum($bit);
while ($sum >> 16)
$sum = ($sum >> 16) + ($sum & 0xffff);
return pack('n*', ~$sum);
}
function PingTry1($pingaddress){
$type= "\x08";
$code= "\x00";
$checksum= "\x00\x00";
$identifier = "\x00\x00";
$seqNumber = "\x00\x00";
$data= "testing123";
$package = $type.$code.$checksum.$identifier.$seqNumber.$data;
$checksum = icmpChecksum($package);
$package = $type.$code.$checksum.$identifier.$seqNumber.$data;
$socket = socket_create(AF_INET, SOCK_RAW, 1);
socket_set_option ( $socket, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>1, "usec"=>0) );
socket_connect($socket, $pingaddress, null);
$startTime = microtime(true);
socket_send($socket, $package, strLen($package), 0);
if (socket_read($socket, 255)) {
return true;
}
else{
return false;
}
socket_close($socket);
}
function DoTheCheck($name,$ip){
global $errors;
global $j;
if (PingTry1($ip)==1){
}else{
$j++;
$errors[$j] = "$name --> $ip";
}
}
$myFile1="hosts.ini";
$filehandle1 = fopen($myFile1, 'r') or die("Couldn't open file [$myFile1]");
$number1=count(file($myFile1));;
$filedata = fread($filehandle1, filesize($myFile1));
fclose($filehandle1);
$array1 = explode("\r\n", $filedata);
unset($filedata);
foreach ($array1 as &$line) {
if (!empty($line)){
list ($name,$ip)=split(",",$line);
DoTheCheck($name,$ip);
}
}
if ($errors){
echo 'The Following Hosts are down - <br/><br/><table>';
foreach ($errors as &$value) {
$k++;
echo '<tr><td><img class="light" src="red.png" /></td><td>'.$errors[$k].'</td></tr>';
}
echo '</tr></table>';
}
else{echo '<img class="light" src="green.png" /><h1>ALL IPS ARE UP!</h1>';}
?>
</body>
</html>
, , , Cisco - , , "ping" .
script - .., , Google, , , 2 3 PHP n00b .
, , 5 6 , , , , .
- :
function ping($host, $timeout = 1) {
$package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
$socket = socket_create(AF_INET, SOCK_RAW, 1);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
socket_connect($socket, $host, null);
$ts = microtime(true);
socket_send($socket, $package, strLen($package), 0);
if (socket_read($socket, 255))
$result = microtime(true) - $ts;
else $result = false;
socket_close($socket);
return $result;
}
:
$url = '192.168.1.1';
$socket = ( bool )false;
$error = ( bool )false;
$socket = @fsockopen( $url, 23, $errno, $errstr, 1 ) or $error = ( bool )true;
if ( ( $socket ) && ( !$error ) )
{
echo "bound";
/* socket is bound - do something */
}
else
{
echo "not bound , [$errstr]";
/* socket is dead - errors are in $errno & $errstr */
}
if ($socket)fclose($socket);
, , IP-, ( , ), 5 , IP, .
, pcntl_fork ? "exec" AJAX ( - )
- Mac Layer (layer 2) - , - , , -, , , .
, , ( Dreaming:-D), .
EDIT - Net_Ping PEAR :
<?php
$time1=date('H:i:s');
echo "Last Refresh Time = $time1<br/><hr/>";
require_once "Net/Ping.php";
$ping = Net_Ping::factory();
$ping->setArgs(array('count' => 2, 'ttl' => 50, 'timeout' => 1));
function DoPing($ip)
{
global $ping;
$results = $ping->ping($ip);
if ($results->_loss==0) {return true;}else{return false;}
}
function DoTheCheck($name,$ip){
global $errors;
global $j;
if (DoPing($ip)==1){
}else{
$j++;
$errors[$j] = "$name --> $ip";
}
}
$myFile1="hosts.ini";
$filehandle1 = fopen($myFile1, 'r') or die("Couldn't open file [$myFile1]");
$number1=count(file($myFile1));;
$filedata = fread($filehandle1, filesize($myFile1));
fclose($filehandle1);
$array1 = explode("\r\n", $filedata);
unset($filedata);
foreach ($array1 as &$line) {
if ( (!empty($line)) && (!strstr($line,'##')) ) {
list ($name,$ip)=split(",",$line);
DoTheCheck($name,$ip);
}
}
if ($errors){
echo 'The Following Hosts are down - <br/><br/><table>';
foreach ($errors as &$value) {
$k++;
echo '<tr><td><img class="light" src="red.png" /></td><td>'.$errors[$k].'</td></tr>';
}
echo '</tr></table>';
}
else{echo '<img class="light" src="green.png" /><h1>ALL IPS ARE UP!</h1>';}
?>
... , 20 10 . 100 , . . , . , , Munin, -, (PHP).