PHP snmpwalkoid采集SNMP tcpConnTable的正确方法

之前使用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();
}
?>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值