Location: PHPKode > projects > Stelib > stelib-1.2.1/stelib_snmp.php
<?php
/*
	snmp.php - Libreria snmp
	Stefano Sangalli
	01 novembre 2004
*/

//!  snmp(): oggetto per la costruzione di un'array bidimensionale contenente gli oggetti snmp.
/*!
	Questo oggetto permette di costruire un'array bidimensionale (myArray[x][y]) contenente gli oggetti snmp richiesti da un un agent
*/

class snmp extends Page{
		var $AGENT_HOST;
		var $AGENT_COMM;
		
		var $OBJECT_SET;
		
		
		
		var $OBJ_SYSTEM;
		var $OBJ_INTERFACES;
		var $OBJ_AT;
		var $OBJ_IP;
		var $OBJ_IP_ADDR_TABLE;
		var $OBJ_IP_ROUTING_TABLE;
		var $OBJ_IP_NET_TO_MEDIA_TABLE;
		var $OBJ_ICMP;
		var $OBJ_TCP;
		var $OBJ_TCP_CONN_TABLE;
		var $OBJ_TCP_IN_OUT;
		var $OBJ_UDP;
		var $OBJ_EGP;
		var $OBJ_EGP_NEIGH_TABLE;
		var $OBJ_SNMP;
	function snmp (
				/// Indirizzo IP o hostname dell'agent
				$agent_host,
				/// Community. Di default `public`
				$agent_comm) {
		if ($agent_host) 	{ $this->AGENT_HOST=$agent_host; } else { $this->AGENT_HOST="127.0.0.1"; 	}
		if ($agent_comm) 	{ $this->AGENT_COMM=$agent_comm; } else { $this->AGENT_COMM="public"; 		}
		return true;		
	}
	function getObject($object) {		
		if ($a=snmpwalk($this->AGENT_HOST,$this->AGENT_COMM,$object)) {
			return $a;
		}
	}
	function getSystem($name,$value) {
		if (!$name) 	{ $name="NAME"; }
		if (!$value) 	{ $value="VALUE"; }		
		$this->OBJ_SYSTEM=array();
		# titoli
		$this->OBJ_SYSTEM[0]=array();
			$this->OBJ_SYSTEM[0][0]=$name;
			$this->OBJ_SYSTEM[0][1]=$value;
		# valori
		$this->OBJ_SYSTEM[1]=array();
			$this->OBJ_SYSTEM[1][$name]="<B>sysDescr</B>";
			$this->OBJ_SYSTEM[1][$value]=snmpget($this->AGENT_HOST,$this->AGENT_COMM,"system.sysDescr.0");
		$this->OBJ_SYSTEM[2]=array();
			$this->OBJ_SYSTEM[2][$name]="<B>sysObjectID</B>";
			$this->OBJ_SYSTEM[2][$value]=snmpget($this->AGENT_HOST,$this->AGENT_COMM,"system.sysObjectID.0");
		$this->OBJ_SYSTEM[3]=array();
			$this->OBJ_SYSTEM[3][$name]="<B>sysUpTime</B>";
			$this->OBJ_SYSTEM[3][$value]=snmpget($this->AGENT_HOST,$this->AGENT_COMM,"system.sysUpTime.0");
		$this->OBJ_SYSTEM[4]=array();
			$this->OBJ_SYSTEM[4][$name]="<B>sysContact</B>";
			$this->OBJ_SYSTEM[4][$value]=snmpget($this->AGENT_HOST,$this->AGENT_COMM,"system.sysContact.0");		
		$this->OBJ_SYSTEM[5]=array();
			$this->OBJ_SYSTEM[5][$name]="<B>sysName</B>";
			$this->OBJ_SYSTEM[5][$value]=snmpget($this->AGENT_HOST,$this->AGENT_COMM,"system.sysName.0");
		$this->OBJ_SYSTEM[6]=array();
			$this->OBJ_SYSTEM[6][$name]="<B>sysLocation</B>";
			$this->OBJ_SYSTEM[6][$value]=snmpget($this->AGENT_HOST,$this->AGENT_COMM,"system.sysLocation.0");
		$this->OBJ_SYSTEM[7]=array();
			$this->OBJ_SYSTEM[7][$name]="<B>sysServices</B>";
			$this->OBJ_SYSTEM[7][$value]=snmpget($this->AGENT_HOST,$this->AGENT_COMM,"system.sysServices.0");
		return $this->OBJ_SYSTEM;
	}
	/// Questa funzione restituisce un'array bidimensionale (myArray[x][y]) contenente gli oggetti snmp richiesti da un un agent, dato un array di oggetti snmp
	function getArray(
		/// array contente gli oggetti da estrapolare. Exempio: array("system.sysDescr.0","interfaces.ifTable.ifEntry.ifIndex")
		$object_list) {
		$this->OBJECT_SET=array();		
		$this->OBJECT_SET[0]=array();	
		for ($i=0;$i<count($object_list);$i++) {
			# titoli
			$tmp_array=explode(".",$object_list[$i]);
 			$this->OBJECT_SET[0][$i]=$tmp_array[count($tmp_array)-1];
			# dati
 			$a=snmpwalk($this->AGENT_HOST,$this->AGENT_COMM,$object_list[$i]);
			for ($j=0;$j<count($a);$j++) {									
				if ($i==0) {
					$this->OBJECT_SET[$j+1]=array();
				}				
				$tmp_string=$a[$j];
				$tmp_string=eregi_replace("STRING: ","",$tmp_string);
				$tmp_string=eregi_replace("INTEGER: ","",$tmp_string);
				$tmp_string=eregi_replace("INTEGER32: ","",$tmp_string);
				$tmp_string=eregi_replace("COUNTER32: ","",$tmp_string);
				$tmp_string=eregi_replace("COUNTER64: ","",$tmp_string);
 				$tmp_string=eregi_replace("GAUGE32: ","",$tmp_string);

								
				$tmp_string=ereg_replace("OID: ","",$tmp_string);
				$this->OBJECT_SET[$j+1][$tmp_array[count($tmp_array)-1]]=$tmp_string;
			}
		}		
		return $this->OBJECT_SET;
	}

}
?>
Return current item: Stelib