之前使用snmpwalk 采集.iso.org.dod.internet.mgmt.mib-2.tcp.tcpConnTable.tcpConnEntry,最近发现各数据段数据数量不一致,可能是tcpConnState(1) tcpConnLocalAddress(2) tcpConnLocalPort(3) tcpConnRemAddress(4) tcpConnRemPort(5) 的采集不是同一时间点数据,所以数据不一致,修改使用snmpwalkoid采集一个tcpConnState,并从对象 id中提取本地远程地址和端口信息,实现数据为同一时间,数据一致
示例代码
<?php
$tcp = snmpwalkoid("127.0.0.1", "public", ".iso.org.dod.internet.mgmt.mib-2.tcp.tcpConnTable.tcpConnEntry.tcpConnState");
print_r($tcp);
foreach ($tcp as $key => $value) {
$s = explode(".", trim($key, "TCP-MIB::tcpConnState."));
print ($s[0] . "." . $s[1] . "." . $s[2] . "." . $s[3] . "\n");
$tcpConnLocalAddress[] = $s[0] . "." . $s[1] . "." . $s[2] . "." . $s[3];
print ($s[4] . "\n");
$tcpConnLocalPort[] = $s[4];
print ($s[5] . "." . $s[6] . "." . $s[7] . "." . $s[8] . "\n");
$tcpConnRemAddress[] = $s[5] . "." . $s[6] . "." . $s[7] . "." . $s[8];
print ($s[9] . "\n");
$tcpConnRemPort[] = $s[4];
echo trim($value, "INTEGER: ") . "\n";
$tcpConnState[] = trim($value, "INTEGER: ");
}
print_r($tcpConnLocalAddress);
print_r($tcpConnLocalPort);
print_r($tcpConnRemAddress);
print_r($tcpConnRemPort);
print_r($tcpConnState);
?>
完整类
<?php
error_reporting(0);
class TCP_Connections {
public function __construct($operating_system, $host_address, $community) {
$this->operating_system = $operating_system;
$this->host_address = $host_address;
$this->community = $community;
if ($this->operating_system == 'windows') {
$this->create_data_array_windows();
}
if ($this->operating_system == 'linux') {
$this->create_data_array_windows();
}
if ($this->operating_system == 'aix') {
$this->create_data_array_windows();
}
if ($this->operating_system == 'esxi') {
$this->create_data_array_windows();
}
}
private function create_data_array_windows() {
$tcp = array();
$tcp = snmpwalkoid($this->host_address, $this->community, '.iso.org.dod.internet.mgmt.mib-2.tcp.tcpConnTable.tcpConnEntry.tcpConnState', 1000000, 3);
foreach ($tcp as $key => $value) {
$s = explode(".", trim($key, "TCP-MIB::tcpConnState."));
//print($s[0].".".$s[1].".".$s[2].".".$s[3]."\n");
$tcpConnLocalAddress[] = $s[0] . "." . $s[1] . "." . $s[2] . "." . $s[3];
//print($s[4]."\n");
$tcpConnLocalPort[] = $s[4];
//print($s[5].".".$s[6].".".$s[7].".".$s[8]."\n");
$tcpConnRemAddress[] = $s[5] . "." . $s[6] . "." . $s[7] . "." . $s[8];
//print($s[9]."\n");
$tcpConnRemPort[] = $s[9];
//echo trim($value,"INTEGER: ")."\n";
$tcpConnState[] = trim($value, "INTEGER: ");
}
//print_r ($tcpConnLocalAddress);
//print_r ($tcpConnLocalPort);
//print_r ($tcpConnRemAddress);
//print_r ($tcpConnRemPort);
//print_r ($tcpConnState);
if ($tcpConnState != FALSE and $tcpConnLocalAddress != FALSE and $tcpConnLocalPort != FALSE and $tcpConnRemAddress != FALSE and $tcpConnRemPort != FALSE) {
$length = count($tcpConnState);
$count = array();
$count[0] = count($tcpConnState);
$count[1] = count($tcpConnLocalAddress);
$count[2] = count($tcpConnLocalPort);
$count[3] = count($tcpConnRemAddress);
$count[4] = count($tcpConnRemPort);
for ($i = 0;$i < count($count);$i++) {
if ($count[$i] == $length) {
$this->data_fetched = 'yes';
} else {
$this->data_fetched = 'no';
}
}
} else {
$this->data_fetched = 'no';
}
if ($this->data_fetched == 'yes') {
$this->populate_data_array($tcpConnState, $tcpConnLocalAddress, $tcpConnLocalPort, $tcpConnRemAddress, $tcpConnRemPort);
}
}
private function populate_data_array($tcpConnState, $tcpConnLocalAddress, $tcpConnLocalPort, $tcpConnRemAddress, $tcpConnRemPort) {
$State = array();
$LocalAddress = array();
$LocalPort = array();
$RemAddress = array();
$RemPort = array();
$State = $tcpConnState;
$LocalAddress = $tcpConnLocalAddress;
$LocalPort = $tcpConnLocalPort;
$RemAddress = $tcpConnRemAddress;
$RemPort = $tcpConnRemPort;
$this->data_array[0] = $State;
$this->data_array[1] = $LocalAddress;
$this->data_array[2] = $LocalPort;
$this->data_array[3] = $RemAddress;
$this->data_array[4] = $RemPort;
}
public function get_data_array() {
return $this->data_array;
}
public function __destruct() {
/*
* No code needed here
*/
}
public $data_fetched;
private $operating_system;
private $host_address;
private $community;
private $data_array = array();
}
?>