php 端口监控

[背景]原来对端口的监控是用nagios的,今天,看了php的fsockopen,就感觉用它可以做点事喽。。。。
[过程]
先给大家一张图

代码如下:
<?PHP
require_once('adodb/adodb.inc.php');
$db_type = 'mysql';
$db_host = 'localhost';
$db_user = 'cdnnode';
$db_pass = 'xxxxxxxxx';
$db_name = 'cdnode';
$GLOBALS['MY_SESS_CONN'] =& ADONewConnection($db_type);
$GLOBALS['MY_SESS_CONN']->Connect( $db_host, $db_user, $db_pass, $db_name);

if (! $MY_SESS_CONN){
        echo "Unable to connect to DB: " . mysql_error();
        exit;
}
   
    $sql = "SELECT ip,port from nodes";

    $result = mysql_query($sql);

    if (!$result) {
        echo "Could not successfully run query ($sql) from DB: " . mysql_error();
        exit;
    }

    if (mysql_num_rows($result) == 0) {
        echo "No rows found, nothing to print so am exiting";
        exit;
    }
?>
<html><head>
<meta http-equiv=refresh content="30">
<title>CDN NODES Monitors!</title>
</head>
<body>
<h1 align="center">CDN Nodes Status</h1>
<hr noshade>
<?php
echo "<table width=80% align='center' border='1'><tr>";   
$i=0;
while ($row = mysql_fetch_assoc($result)) {
      if($i%4 == 0){
          echo "</tr><tr>"; 
       }
       echo "<td><div align='center'>$row[ip]</td>";
       $res = @fsockopen($row[ip],$row[port],$errno,$errstr,2);
       if($res)
           {
                echo "<td><div align='center'><font color=\"#33CC66\"><b>OK!</b></font></div></td>";
           }
       else
           {
                echo "<td><div align='center'><font color=\"#FF0000\"><blink><b>Fail!</b></blink></font></div></td>";
           }

       $i+=1;
       }

echo "</tr></table>";

mysql_free_result($result);
?>
</body></html>

感觉如何,大家也试试吧!

你可能感兴趣的:(PHP,职场,休闲)