接口B / W模块

时间:2013-07-02 07:31:52

标签: php linux networking ubuntu snmp

图片:http://i40.tinypic.com/2hodx55.png

我已经使用Php和SNMP构建了一个网络接口监视器,但现在当我在localhost上执行它时,我看到我的图形一次又一次地转到原点(0)(请看图像),并且Y轴上的速度是错误。有时它会进入Millons和Millions。

请有人告诉我下面代码中的问题是什么

    <?php
     $int="wlan0";


       session_start();

  $rx0 =snmpget('localhost','public','.1.3.6.1.2.1.2.2.1.10.3');

 $tx0 =snmpget('localhost','public','.1.3.6.1.2.1.2.2.1.16.3');

  sleep(5);

 $rx1 =snmpget('localhost','public','.1.3.6.1.2.1.2.2.1.10.3');

 $tx1 =snmpget('localhost','public','.1.3.6.1.2.1.2.2.1.16.3');


   $rx0 = substr($rx0, 11);
   $tx0 = substr($tx0, 11);
   $rx1 = substr($rx1, 11);
   $tx1 = substr($tx1, 11);

   $tBps = $tx1 - $tx0;
   $rBps = $rx1 - $rx0;

   $round_rx=$rBps;
   $round_tx=$tBps;



   $time=date("U")."000";


$_SESSION['rx'][] = "[$time, $round_rx]";   
 $_SESSION['tx'][] = "[$time, $round_tx]";
  $data['label'] = $int;
     $data['data'] = $_SESSION['rx'];

          if (count($_SESSION['rx'])>60)
    {
    $x = min(array_keys($_SESSION['rx']));
     unset($_SESSION['rx'][$x]);

    }




        echo '{"label":"'.$int.'","data":['.implode($_SESSION['rx'], ",").']}';


         ?>

1 个答案:

答案 0 :(得分:0)

我简化了代码段下半部分的代码。

  • 我认为$_SESSION['rx']初始化为某个数组。
  • $data已经消失了 - 我希望它不会被进一步使用。
  • $_SESSION['tx']已更新但未使用,就像在您的代码段中一样。

这应为rx生成干净的JSON输出。我没有看到您的版本或此版本中零值的来源。检查对此的输入(来自snmpget()的原始值)和输出JSON字符串。

$_SESSION['rx'][] = array($time, $round_rx);   
$_SESSION['tx'][] = array($time, $round_tx);

// retaining just the 60 most recent entries.
if (count($_SESSION['rx'])>60) {
  array_shift($_SESSION['rx']);
}

$rxdata = array("label"=>$int, "data"=>$_SESSION['rx']);
echo json_encode($rxdata);
相关问题