上代码
1 <?php 2 function getstr1($strall,$str1,$str2,$html_charset='utf-8'){ 3 $i1=mb_strpos($strall,$str1,0,$html_charset); 4 if(!is_int($i1)){return '';} 5 $i1R=$i1+mb_strlen($str1,$html_charset); 6 $i2=mb_strpos($strall,$str2,$i1+mb_strlen($str1,$html_charset),$html_charset); 7 if(!is_int($i2)){return '';} 8 return(mb_substr($strall,$i1R,$i2-$i1R,$html_charset)); 9 } 10 class traffic{ 11 private $lastArr=array(); 12 function __construct(){ 13 $this->lastArr['time']=0; 14 $this->lastArr['out']=0; 15 $this->lastArr['in']=0; 16 } 17 function doinfo($time,$in,$out){ 18 $timeI=$time-$this->lastArr['time']; 19 $outI=$out-$this->lastArr['out']; 20 if($outI<0)$outI+=pow(2,32); 21 $inI=$in-$this->lastArr['in']; 22 if($inI<0)$inI+=pow(2,32); 23 $strOut=number_format($outI/$timeI/1024/1024,2); 24 $strIn=number_format($inI/$timeI/1024/1024,2); 25 echo 'Out: '.$strOut.'MB/s, In: '.$strIn."MB/s\n"; 26 $this->lastArr['time']=$time; 27 $this->lastArr['out']=$out; 28 $this->lastArr['in']=$in; 29 } 30 } 31 $session = new SNMP(SNMP::VERSION_2C, "10.105.1.1", "public"); 32 $arr=array(); 33 $arr[]='iso.3.6.1.2.1.1.3.0'; 34 $n=6; 35 $arr[]='iso.3.6.1.2.1.2.2.1.10.'.$n;//in 36 $arr[]='iso.3.6.1.2.1.2.2.1.16.'.$n;//out 37 $traffic=new traffic(); 38 while(1){ 39 $p=$session->get($arr); 40 $timeticks=$p['iso.3.6.1.2.1.1.3.0']; 41 $timeticks=getstr1($timeticks,'(',')'); 42 $timeticks=$timeticks/100; 43 $iIn=$p['iso.3.6.1.2.1.2.2.1.10.6']; 44 $iIn=explode(': ',$iIn)[1]; 45 $iIn=floatval($iIn); 46 $iOut=$p['iso.3.6.1.2.1.2.2.1.16.6']; 47 $iOut=explode(': ',$iOut)[1]; 48 $iOut=floatval($iOut); 49 $traffic->doinfo($timeticks,$iIn,$iOut); 50 sleep(2); 51 } 52 $session->close();