PHP类采集SNMP tcpConnTable并解析TCP_Connections

<?php
error_reporting(0);
    class TCP_Connections
    {
        public function __construct($operating_system, $host_address)
        {
            $this->operating_system = $operating_system;
            $this->host_address     = $host_address;
            if($this->operating_system == 'windows')
            {
                $this->create_data_array_windows();
            }
            if($this->operating_system == 'linux')
            {
                $this->create_data_array_windows();
            }
        }
        private function create_data_array_windows()
        {
                    $tcp = array();
        $result = snmpwalk($this->host_address, "public", '1.3.6.1.2.1.6.13.1');
        $tcp = array_chunk($result,count($result)/5);
//        return $tcp;
            
//            $tcpConnState       = snmpwalkoid($this->host_address, "public", ".iso.org.dod.internet.mgmt.mib-2.tcp.tcpConnTable.tcpConnEntry.tcpConnState");
//            $tcpConnLocalAddress     = snmpwalkoid($this->host_address, "public", ".iso.org.dod.internet.mgmt.mib-2.tcp.tcpConnTable.tcpConnEntry.tcpConnLocalAddress");
//            $tcpConnLocalPort       = snmpwalkoid($this->host_address, "public", ".iso.org.dod.internet.mgmt.mib-2.tcp.tcpConnTable.tcpConnEntry.tcpConnLocalPort");
//            $tcpConnRemAddress = snmpwalkoid($this->host_address, "public", ".iso.org.dod.internet.mgmt.mib-2.tcp.tcpConnTable.tcpConnEntry.tcpConnRemAddress");
//            $tcpConnRemPort       = snmpwalkoid($this->host_address, "public", ".iso.org.dod.internet.mgmt.mib-2.tcp.tcpConnTable.tcpConnEntry.tcpConnRemPort");
            $tcpConnState               = $tcp[0];//snmpwalkoid($this->host_address, "public", ".iso.org.dod.internet.mgmt.mib-2.tcp.tcpConnTable.tcpConnEntry.tcpConnState");
            $tcpConnLocalAddress        = $tcp[1];//snmpwalkoid($this->host_address, "public", ".iso.org.dod.internet.mgmt.mib-2.tcp.tcpConnTable.tcpConnEntry.tcpConnLocalAddress");
            $tcpConnLocalPort           = $tcp[2];//snmpwalkoid($this->host_address, "public", ".iso.org.dod.internet.mgmt.mib-2.tcp.tcpConnTable.tcpConnEntry.tcpConnLocalPort");
            $tcpConnRemAddress          = $tcp[3];//snmpwalkoid($this->host_address, "public", ".iso.org.dod.internet.mgmt.mib-2.tcp.tcpConnTable.tcpConnEntry.tcpConnRemAddress");
            $tcpConnRemPort             = $tcp[4];//snmpwalkoid($this->host_address, "public", ".iso.org.dod.internet.mgmt.mib-2.tcp.tcpConnTable.tcpConnEntry.tcpConnRemPort");

            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();
                        
            //---------------------------------------------------------------------------------------
            $i = 0;
            foreach($tcpConnState as $key => $value)
            { 
                $data       = str_replace('INTEGER: ', '', $value);
                $State[$i] = $data;
                $i = $i + 1;
            }
            $tcpConnState = null;
            //---------------------------------------------------------------------------------------
                        
            $i = 0;
            foreach($tcpConnLocalAddress as $key => $value)
            {
                $data       = str_replace('IpAddress: ', '', $value);
                $LocalAddress[$i] = $data;
                $i = $i + 1;
            }
            $tcpConnLocalAddress = null;

 
                        //---------------------------------------------------------------------------------------
                        
            $i = 0;
            foreach($tcpConnLocalPort as $key => $value)
            {
                $data       = str_replace('INTEGER: ', '', $value);
                $LocalPort[$i] = $data;
                $i = $i + 1;
            }
            $tcpConnLocalPort = null;
            //---------------------------------------------------------------------------------------

                        //---------------------------------------------------------------------------------------
                        
            $i = 0;
            foreach($tcpConnRemAddress as $key => $value)
            {
                $data       = str_replace('IpAddress: ', '', $value);
                $RemAddress[$i] = $data;
                $i = $i + 1;
            }
            $tcpConnRemAddress = null;
            //---------------------------------------------------------------------------------------
                        //---------------------------------------------------------------------------------------
                        
            $i = 0;
            foreach($tcpConnRemPort as $key => $value)
            {
                $data       = str_replace('INTEGER: ', '', $value);
                $RemPort[$i] = $data;
                $i = $i + 1;
            }
            $tcpConnRemPort = null;
            //---------------------------------------------------------------------------------------
                                    
            $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 $data_array = array();
    }
?>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值