1.采集数据
主要通过snmp2_real_walk()函数进行采集,它是一次性采集所有tcp.tcpConnTable的数据。
因为要取十次,所以要周期性地执行采集的代码,通过下面设置周期运行的时间及次数。
<?php
ignore_user_abort(); //即使Client断开(如关掉浏览器),PHP脚本也可以继续执行.
set_time_limit(0); // 执行时间为无限制,php默认执行时间是30秒,可以让程序无限制的执行下去
$interval=10; // 每隔10s运行一次
$i=0;
while($i<10)
{
sleep($interval); // 按设置的时间等待一小时循环执行
$i++;
$code=date("Ymd-H:i:s");
$filename="TcpConnTable"."$code";
$fn=fopen("$filename","w+") or die("can't open the file");
$snmprealwalkvalue=snmp2_real_walk("127.0.0.1","bjfu18","tcp.tcpConnTable");
foreach($snmprealwalkvalue as $oid=>$value){
$stringwalk="$oid"."$value \n";
fwrite($fn,"$stringwalk");
}
fclose($fn);
echo "$i"; //测验
}
?>
采集的十次数据
具体内容
2.转为页面的Table
这里我实现的是读数据+拆分五个列表+编写Table
主要代码
<?php
class tcpcollect
{
private $hostAddress;
private $community;
private $snmpobject;
function __construct(){
$args_num = func_num_args();
$this->hostAddress=func_get_arg(0);
$this->community=func_get_arg(1);
}
public function Walk($where){
$results = snmp2_real_walk($this->hostAddress,$this->community,$where);
$values = array();
$cnt = 0;
foreach ($results as $key => $value) {
$values[$cnt++] = substr(strrchr($value,": "), 2);
}
return $values;
}
}
$tcpobject = new tcpCollect("127.0.0.1","bjfu18");
$tcpConnLocalAddress = array();
$tcpConnLocalPort = array();
$tcpConnRemAddress = array();
$tcpConnRemPort = array();
$tcpConnState = array();
$tcpConnLocalAddress = $tcpobject->Walk("tcpConnLocalAddress");
$tcpConnLocalPort = $tcpobject->Walk("tcpConnLocalPort");
$tcpConnRemAddress = $tcpobject->Walk("tcpConnRemAddress");
$tcpConnRemPort = $tcpobject->Walk("tcpConnRemPort");
$tcpConnState = $tcpobject->Walk("tcpConnState");
$tcpConnNumber=count($tcpConnState);
//$memcache_obj=memcache_connect("localhost",113113);
echo"
<table class=\"table\">
<tr class=\"success\">
<td>tcpConnLocalAddress</td>
<td>tcpConnLocalPort</td>
<td>tcpConnRemAddress</td>
<td>tcpConnRemPort</td>
<td>tcpConnState</td>
</tr>";
for($i=0;$i<$tcpConnNumber;$i++){
echo"<tr class=\"info\">";
echo"<td>".$tcpConnLocalAddress[$i]."</td>";
echo"<td>".$tcpConnLocalPort[$i]."</td>";
echo"<td>".$tcpConnRemAddress[$i]."</td>";
echo"<td>".$tcpConnRemPort[$i]."</td>";
echo"<td>".$tcpConnState[$i]."</td>";
echo"</tr>";
}
echo"</table>";;
?>
页面显示Table
3.memcache存key value到内存
主要通过memcache_set()方法,这里我直接是手动看10个文件中各有多少条连接,自己设置的,应该是要读每个文件然后通过字符串处理得到连接Count,偷了一下懒。这里的value为数组。
<?php
$memcache_obj=memcache_connect('localhost',113113);
$CountArray=array();
for($i=0;$i<10;$i++)
{
$CountArray[$i]=11;
}
$CountArray[7]=10;
$CountArray[9]=12;
$CountArray[4]=9;
memcache_set($memcache_obj,'Count_Array',$CountArray,0,20);
$var=memcache_get($memcache_obj,'Count_Array');
for($i=0;$i<10;$i++)
{
echo "$var[$i]\n";
}
?>
4.memcache按key取value
主要通过memcache_get()方法。注意这里要把取出的数组变成json格式,然后给echats.js的数据接口。
<script type="text/javascript">
<?php
$memcache_obj=memcache_connect('localhost',113113);
$a=memcache_get($memcache_obj,'Count_Array');
$arr=json_encode($a);
?>
var dom = document.getElementById("container");
var myChart = echarts.init(dom);
var app = {};
var option;
option = {
xAxis: {
type: 'category',
data: ['20:14:54', '20:15:04', '20:15:14', '20:15:24', '20:15:34', '20:15:44', '20:15:54', '20:16:04', '20:16:14', '20:16:24']
},
yAxis: {
type: 'value'
},
series: [{
data: <?php echo $arr;?>,
type: 'line'
}]
};
if (option && typeof option === 'object') {
myChart.setOption(option);
}
</script>