PHP类采集SNMP udpTable并解析UDP_Connections

<?php
error_reporting(0);
    class UDP_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()
        {
            $udp = array();
            $result = snmpwalk($this->host_address, "public", '.iso.org.dod.internet.mgmt.mib-2.udp.udpTable.udpEntry');
            $udp = array_chunk($result,count($result)/2);
        
            $udpLocalAddress        = $udp[0];//snmpwalkoid($this->host_address, "public", ".iso.org.dod.internet.mgmt.mib-2.udp.udpTable.udpEntry.udpLocalAddress");
            $udpLocalPort           = $udp[1];//snmpwalkoid($this->host_address, "public", ".iso.org.dod.internet.mgmt.mib-2.udp.udpTable.udpEntry.udpLocalPort");
            if(
                $udpLocalAddress       != FALSE and
                $udpLocalPort != FALSE
            )
            {
                
                $length   = count($udpLocalAddress);
                $count    = array();
                $count[0] = count($udpLocalAddress);
                $count[1] = count($udpLocalPort);
                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($udpLocalAddress, $udpLocalPort);
            }
        }
        private function populate_data_array($udpLocalAddress, $udpLocalPort)
        {
            $LocalAddress     = array();
            $LocalPort       = array();
                        
                        //---------------------------------------------------------------------------------------
            $i = 0;
            foreach($udpLocalAddress as $key => $value)
            {
                $data       = str_replace('IpAddress: ', '', $value);
                $LocalAddress[$i] = $data;
                $i = $i + 1;
            }
            $udpLocalAddress = null;

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

                                    
            $this->data_array[0] = $LocalAddress;
            $this->data_array[1] = $LocalPort;
                                                
        }
        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();
    }
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值