我为我们的服务器编写了(很好地从其他人的代码拼凑而成)一个非常简单的正常运行时间监视器 - 它只是一个 ICMP(ping)监视器,它适用于我们有限数量的服务器(20 台左右),而且速度非常快. 这是代码(我认为实际的 ping 测试函数基于 Birk Jensen 的工作(http://birk-jensen.dk/2010/09/php-ping/),我刚刚利用他的函数显示绿色当一切正常时圈出 PNG,每台关闭的服务器(如果有的话)圈出红色的。
<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);
/*-----------------------------------------------------------------------------------------*/
// Checksum calculation function
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){
// Making the package
$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); // Calculate the checksum
$package = $type.$code.$checksum.$identifier.$seqNumber.$data;
// And off to the sockets
$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){
//do nothing
}else{
$j++;
$errors[$j] = "$name --> $ip";
}
}
/*-----------------------------------------------------------------------------------------*/
//READ IN THE INI FILE INTO $filedata Array
$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);
// Create an array with each line of the file
$array1 = explode("\r\n", $filedata);
unset($filedata); //free up a bit of memory
foreach ($array1 as &$line) { // step through the array, line by 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”的方式有关。
由于大学承诺等原因,我已经很久没有在这个脚本上做过任何工作了,但我已经尽可能多地做谷歌研究,但我承认我充其量是 2 级或 3 级 PHP n00b。今天我发现了一些适用于交换机的解决方案,但它们有 5 或 6 秒的超时时间,这是不可接受的,因为我希望系统尽可能多地循环并且尽可能干净,并记录停机时间以便稍后绘制图表。
例如 - 我试过这个:
function ping($host, $timeout = 1) {
/* ICMP ping packet with a pre-calculated checksum */
$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(用于测试,就好像主机离线一样),在单个 IP 上超时大约需要 5 秒或更长时间,这对我的需要来说太慢了。
是否可以使用 pcntl_fork 甚至 curl 使用多线程来做到这一点?或多个 'exec' 调用或 AJAX 甚至(我愿意在这个阶段尝试任何东西)
或某种数据层(第 2 层)Mac 扫描代码也很棒 - 我不希望任何人编写完整的代码,但我敢肯定,以前做过这种事情的人会有一个好主意陷阱以及如何绕过它们。
总而言之 - 一个简单易行的解决方案会很好(我会继续做梦:-D),但非常感谢任何帮助或建议。
编辑 - 在 PEAR 中尝试 Net_Ping 的一些建议后,我得到了以下代码:
<?php
$time1=date('H:i:s');
echo "Last Refresh Time = $time1<br/><hr/>";
//not sure if still needed - error_reporting(0);
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){
//do nothing
}else{
$j++;
$errors[$j] = "$name --> $ip";
}
}
/*-----------------------------------------------------------------------------------*/
//READ IN THE INI FILE INTO $filedata Array
$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);
// Create an array with each line of the file
$array1 = explode("\r\n", $filedata);
unset($filedata); //free up a bit of memory
foreach ($array1 as &$line) { // step through the array, line by 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,但实际上我需要一些可以集成到我公司的 Intranet (PHP) 中的东西。