| 
									
										
										
										
											2009-10-28 13:49:37 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  | // If anybody has again the idea to implement the PHP internal library calls,
 | 
					
						
							|  |  |  | // be aware that it was tried and banned by lead dev Adam
 | 
					
						
							| 
									
										
										
										
											2012-05-16 13:25:50 +00:00
										 |  |  | //
 | 
					
						
							|  |  |  | // TRUE STORY. THAT SHIT IS WHACK. -- adama.
 | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-17 16:01:50 +00:00
										 |  |  | function string_to_oid($string) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   $oid = strlen($string); | 
					
						
							|  |  |  |   for($i = 0; $i != strlen($string); $i++) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |      $oid .= ".".ord($string[$i]); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return $oid; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-15 17:54:42 +00:00
										 |  |  | function snmp_get_multi($device, $oids, $options = "-OQUs", $mib = NULL, $mibdir = NULL) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   global $debug,$config,$runtime_stats,$mibs_loaded; | 
					
						
							| 
									
										
										
										
											2010-07-17 15:54:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   if (is_numeric($device['timeout']) && $device['timeout'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |      $timeout = $device['timeout']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['timeout'])) { | 
					
						
							|  |  |  |      $timeout =  $config['snmp']['timeout']; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-03-12 11:32:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   if (is_numeric($device['retries']) && $device['retries'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $retries = $device['retries']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['retries'])) { | 
					
						
							|  |  |  |     $retries =  $config['snmp']['retries']; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-03-04 17:51:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   if (!isset($device['transport'])) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $device['transport'] = "udp"; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-03-16 09:20:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-15 15:18:57 +00:00
										 |  |  |   $cmd  = $config['snmpget']; | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  |   $cmd .= snmp_gen_auth ($device); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-29 15:00:16 +00:00
										 |  |  |   if ($options) { $cmd .= " " . $options; } | 
					
						
							|  |  |  |   if ($mib) { $cmd .= " -m " . $mib; } | 
					
						
							|  |  |  |   if ($mibdir) { $cmd .= " -M " . $mibdir; } else { $cmd .= " -M ".$config['mibdir']; } | 
					
						
							| 
									
										
										
										
											2010-07-17 15:54:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-29 15:00:16 +00:00
										 |  |  |   if (isset($timeout)) { $cmd .= " -t " . $timeout; } | 
					
						
							|  |  |  |   if (isset($retries)) { $cmd .= " -r " . $retries; } | 
					
						
							| 
									
										
										
										
											2010-10-07 18:12:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  |   $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']; | 
					
						
							|  |  |  |   $cmd .= " ".$oids; | 
					
						
							| 
									
										
										
										
											2012-03-29 15:00:16 +00:00
										 |  |  |   if (!$debug) { $cmd .= " 2>/dev/null"; } | 
					
						
							|  |  |  |   $data = trim(external_exec($cmd)); | 
					
						
							|  |  |  |   $runtime_stats['snmpget']++; | 
					
						
							|  |  |  |   foreach (explode("\n", $data) as $entry) | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2012-05-10 15:02:57 +00:00
										 |  |  |     list($oid,$value) = explode("=", $entry); | 
					
						
							|  |  |  |     $oid = trim($oid); $value = trim($value); | 
					
						
							|  |  |  |     list($oid, $index) = explode(".", $oid); | 
					
						
							|  |  |  |     if (!strstr($value, "at this OID") && isset($oid) && isset($index)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       $array[$index][$oid] = $value; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   return $array; | 
					
						
							| 
									
										
										
										
											2010-02-13 21:45:39 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  | function snmp_get($device, $oid, $options = NULL, $mib = NULL, $mibdir = NULL) | 
					
						
							| 
									
										
										
										
											2011-03-02 15:03:05 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   global $debug,$config,$runtime_stats,$mibs_loaded; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['timeout']) && $device['timeout'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |      $timeout = $device['timeout']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['timeout'])) { | 
					
						
							|  |  |  |      $timeout =  $config['snmp']['timeout']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['retries']) && $device['retries'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $retries = $device['retries']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['retries'])) { | 
					
						
							|  |  |  |     $retries =  $config['snmp']['retries']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!isset($device['transport'])) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $device['transport'] = "udp"; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (strstr($oid,' ')) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2011-03-31 10:51:02 +00:00
										 |  |  |     echo("BUG: snmp_get called for multiple OIDs: $oid\n"); | 
					
						
							|  |  |  |     echo("Please report this to the Observium team."); | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-15 15:18:57 +00:00
										 |  |  |   $cmd  = $config['snmpget']; | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  |   $cmd .= snmp_gen_auth ($device); | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-29 15:00:16 +00:00
										 |  |  |   if ($options) { $cmd .= " " . $options; } | 
					
						
							|  |  |  |   if ($mib) { $cmd .= " -m " . $mib; } | 
					
						
							| 
									
										
										
										
											2012-05-15 15:18:57 +00:00
										 |  |  |   if ($mibdir) { $cmd .= " -M " . $mibdir; } | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  |     else { $cmd .= " -M ".$config['mibdir']; } | 
					
						
							| 
									
										
										
										
											2012-03-29 15:00:16 +00:00
										 |  |  |   if (isset($timeout)) { $cmd .= " -t " . $timeout; } | 
					
						
							|  |  |  |   if (isset($retries)) { $cmd .= " -r " . $retries; } | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  |   $cmd .= " " . $device['transport'].":".$device['hostname'].":".$device['port']; | 
					
						
							|  |  |  |   $cmd .= " " . $oid; | 
					
						
							| 
									
										
										
										
											2012-05-15 15:18:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-29 15:00:16 +00:00
										 |  |  |   if (!$debug) { $cmd .= " 2>/dev/null"; } | 
					
						
							|  |  |  |   $data = trim(external_exec($cmd)); | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   $runtime_stats['snmpget']++; | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (is_string($data) && (preg_match("/(No Such Instance|No Such Object|No more variables left|Authentication failure)/i", $data))) | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   { | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   elseif ($data) { return $data; } | 
					
						
							|  |  |  |   else { return false; } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-02-08 03:36:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  | function snmp_walk($device, $oid, $options = NULL, $mib = NULL, $mibdir = NULL) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   global $debug,$config,$runtime_stats; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['timeout']) && $device['timeout'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |      $timeout = $device['timeout']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['timeout'])) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |      $timeout =  $config['snmp']['timeout']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['retries']) && $device['retries'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $retries = $device['retries']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['retries'])) { | 
					
						
							|  |  |  |     $retries =  $config['snmp']['retries']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (!isset($device['transport'])) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $device['transport'] = "udp"; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $snmpcommand = $config['snmpwalk']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $snmpcommand = $config['snmpbulkwalk']; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-05-15 15:18:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  |   $cmd = $snmpcommand; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $cmd .= snmp_gen_auth ($device); | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if ($options) { $cmd .= " $options "; } | 
					
						
							|  |  |  |   if ($mib) { $cmd .= " -m $mib"; } | 
					
						
							|  |  |  |   if ($mibdir) { $cmd .= " -M " . $mibdir; } else { $cmd .= " -M ".$config['mibdir']; } | 
					
						
							|  |  |  |   if (isset($timeout)) { $cmd .= " -t " . $timeout; } | 
					
						
							|  |  |  |   if (isset($retries)) { $cmd .= " -r " . $retries; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!$debug) { $cmd .= " 2>/dev/null"; } | 
					
						
							| 
									
										
										
										
											2011-09-15 22:35:22 +00:00
										 |  |  |   $data = trim(external_exec($cmd)); | 
					
						
							| 
									
										
										
										
											2012-01-17 15:58:34 +00:00
										 |  |  |   $data = str_replace("\"", "", $data); | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (is_string($data) && (preg_match("/No Such (Object|Instance)/i", $data))) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $data = false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     if (preg_match("/No more variables left in this MIB View \(It is past the end of the MIB tree\)$/",$data))  { | 
					
						
							|  |  |  |     # Bit ugly :-(
 | 
					
						
							|  |  |  |     $d_ex = explode("\n",$data); | 
					
						
							|  |  |  |     unset($d_ex[count($d_ex)-1]); | 
					
						
							|  |  |  |     $data = implode("\n",$d_ex); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   $runtime_stats['snmpwalk']++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return $data; | 
					
						
							| 
									
										
										
										
											2009-10-28 13:49:37 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | function snmpwalk_cache_cip($device, $oid, $array, $mib = 0) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   global $config; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['timeout']) && $device['timeout'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |      $timeout = $device['timeout']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['timeout'])) { | 
					
						
							|  |  |  |      $timeout =  $config['snmp']['timeout']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['retries']) && $device['retries'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $retries = $device['retries']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['retries'])) { | 
					
						
							|  |  |  |     $retries =  $config['snmp']['retries']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!isset($device['transport'])) { $device['transport'] = "udp"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $snmpcommand = $config['snmpwalk']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $snmpcommand = $config['snmpbulkwalk']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  |   $cmd = $snmpcommand; | 
					
						
							|  |  |  |   $cmd .= snmp_gen_auth ($device); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-15 15:18:57 +00:00
										 |  |  |   $cmd .= " -O snQ"; | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   if ($mib) { $cmd .= " -m $mib"; } | 
					
						
							| 
									
										
										
										
											2012-03-26 13:34:49 +00:00
										 |  |  |   $cmd .= " -M ".$config['install_dir']."/mibs"; | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   if (isset($timeout)) { $cmd .= " -t " . $timeout; } | 
					
						
							|  |  |  |   if (isset($retries)) { $cmd .= " -r " . $retries; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!$debug) { $cmd .= " 2>/dev/null"; } | 
					
						
							| 
									
										
										
										
											2011-09-15 22:35:22 +00:00
										 |  |  |   $data = trim(external_exec($cmd)); | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   $device_id = $device['device_id']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   #echo("Caching: $oid\n");
 | 
					
						
							|  |  |  |   foreach (explode("\n", $data) as $entry) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     list ($this_oid, $this_value) = preg_split("/=/", $entry); | 
					
						
							|  |  |  |     $this_oid = trim($this_oid); | 
					
						
							|  |  |  |     $this_value = trim($this_value); | 
					
						
							|  |  |  |     $this_oid = substr($this_oid, 30); | 
					
						
							|  |  |  |     list($ifIndex,$dir,$a,$b,$c,$d,$e,$f) = explode(".", $this_oid); | 
					
						
							|  |  |  |     $h_a = zeropad(dechex($a)); | 
					
						
							|  |  |  |     $h_b = zeropad(dechex($b)); | 
					
						
							|  |  |  |     $h_c = zeropad(dechex($c)); | 
					
						
							|  |  |  |     $h_d = zeropad(dechex($d)); | 
					
						
							|  |  |  |     $h_e = zeropad(dechex($e)); | 
					
						
							|  |  |  |     $h_f = zeropad(dechex($f)); | 
					
						
							|  |  |  |     $mac = "$h_a$h_b$h_c$h_d$h_e$h_f"; | 
					
						
							|  |  |  |     if ($dir == "1") { $dir = "input"; } elseif ($dir == "2") { $dir = "output"; } | 
					
						
							|  |  |  |     if ($mac && $dir) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       $array[$ifIndex][$mac][$oid][$dir] = $this_value; | 
					
						
							| 
									
										
										
										
											2011-03-22 20:27:39 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-04-12 16:27:42 +00:00
										 |  |  |   return $array; | 
					
						
							| 
									
										
										
										
											2010-09-26 22:10:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function snmp_cache_ifIndex($device) | 
					
						
							| 
									
										
										
										
											2010-07-22 21:58:49 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-03-29 15:00:16 +00:00
										 |  |  |   // FIXME: this is not yet using our own snmp_*
 | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   global $config; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['timeout']) && $device['timeout'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |      $timeout = $device['timeout']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['timeout'])) { | 
					
						
							|  |  |  |      $timeout =  $config['snmp']['timeout']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['retries']) && $device['retries'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $retries = $device['retries']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['retries'])) { | 
					
						
							|  |  |  |     $retries =  $config['snmp']['retries']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!isset($device['transport'])) { $device['transport'] = "udp"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $snmpcommand = $config['snmpwalk']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $snmpcommand = $config['snmpbulkwalk']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  |   $cmd = $snmpcommand; | 
					
						
							|  |  |  |   $cmd .= snmp_gen_auth ($device); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-15 15:18:57 +00:00
										 |  |  |   $cmd .= " -O Qs"; | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  |   $cmd .= " -M " . $config['install_dir']."/mibs"; | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   $cmd .= " -m IF-MIB ifIndex"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (isset($timeout)) { $cmd .= " -t " . $timeout; } | 
					
						
							|  |  |  |   if (isset($retries)) { $cmd .= " -r " . $retries; } | 
					
						
							|  |  |  |   if (!$debug) { $cmd .= " 2>/dev/null"; } | 
					
						
							| 
									
										
										
										
											2011-09-15 22:35:22 +00:00
										 |  |  |   $data = trim(external_exec($cmd)); | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   $device_id = $device['device_id']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   foreach (explode("\n", $data) as $entry) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     list ($this_oid, $this_value) = preg_split("/=/", $entry); | 
					
						
							|  |  |  |     list ($this_oid, $this_index) = explode(".", $this_oid); | 
					
						
							|  |  |  |     $this_index = trim($this_index); | 
					
						
							|  |  |  |     $this_oid = trim($this_oid); | 
					
						
							|  |  |  |     $this_value = trim($this_value); | 
					
						
							|  |  |  |     if (!strstr($this_value, "at this OID") && $this_index) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       $array[] = $this_value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return $array; | 
					
						
							| 
									
										
										
										
											2009-10-28 13:49:37 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | function snmpwalk_cache_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL) | 
					
						
							| 
									
										
										
										
											2010-07-22 21:58:49 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   $data = snmp_walk($device, $oid, "-OQUs", $mib, $mibdir); | 
					
						
							|  |  |  |   foreach (explode("\n", $data) as $entry) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     list($oid,$value) = explode("=", $entry); | 
					
						
							|  |  |  |     $oid = trim($oid); $value = trim($value); | 
					
						
							| 
									
										
										
										
											2011-04-27 01:39:02 +00:00
										 |  |  |     list($oid, $index) = explode(".", $oid, 2); | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |     if (!strstr($value, "at this OID") && isset($oid) && isset($index)) | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |       $array[$index][$oid] = $value; | 
					
						
							| 
									
										
										
										
											2009-11-10 19:28:24 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   return $array; | 
					
						
							| 
									
										
										
										
											2009-11-10 19:28:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | function snmpwalk_cache_multi_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL) | 
					
						
							| 
									
										
										
										
											2010-07-22 21:58:49 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   global $cache; | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   if (!(is_array($cache['snmp'][$device['device_id']]) && array_key_exists($oid,$cache['snmp'][$device['device_id']]))) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $data = snmp_walk($device, $oid, "-OQUs", $mib, $mibdir); | 
					
						
							|  |  |  |     foreach (explode("\n", $data) as $entry) | 
					
						
							| 
									
										
										
										
											2010-11-24 12:00:09 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |       list($r_oid,$value) = explode("=", $entry); | 
					
						
							|  |  |  |       $r_oid = trim($r_oid); $value = trim($value); | 
					
						
							|  |  |  |       $oid_parts = explode(".", $r_oid); | 
					
						
							|  |  |  |       $r_oid = $oid_parts['0']; | 
					
						
							|  |  |  |       $index = $oid_parts['1']; | 
					
						
							|  |  |  |       if (isset($oid_parts['2'])) { $index .= ".".$oid_parts['2']; } | 
					
						
							|  |  |  |       if (isset($oid_parts['3'])) { $index .= ".".$oid_parts['3']; } | 
					
						
							|  |  |  |       if (isset($oid_parts['4'])) { $index .= ".".$oid_parts['4']; } | 
					
						
							|  |  |  |       if (isset($oid_parts['5'])) { $index .= ".".$oid_parts['5']; } | 
					
						
							|  |  |  |       if (isset($oid_parts['6'])) { $index .= ".".$oid_parts['6']; } | 
					
						
							|  |  |  |       if (!strstr($value, "at this OID") && isset($r_oid) && isset($index)) | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         $array[$index][$r_oid] = $value; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     $cache['snmp'][$device['device_id']][$oid] = $array; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return $cache['snmp'][$device['device_id']][$oid]; | 
					
						
							| 
									
										
										
										
											2010-02-21 05:08:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | function snmpwalk_cache_double_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL) | 
					
						
							| 
									
										
										
										
											2010-07-22 21:58:49 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   $data = snmp_walk($device, $oid, "-OQUs", $mib, $mibdir); | 
					
						
							| 
									
										
										
										
											2011-03-12 08:50:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   foreach (explode("\n", $data) as $entry) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     list($oid,$value) = explode("=", $entry); | 
					
						
							|  |  |  |     $oid = trim($oid); $value = trim($value); | 
					
						
							|  |  |  |     list($oid, $first, $second) = explode(".", $oid); | 
					
						
							|  |  |  |     if (!strstr($value, "at this OID") && isset($oid) && isset($first) && isset($second)) | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |       $double = $first.".".$second; | 
					
						
							|  |  |  |       $array[$double][$oid] = $value; | 
					
						
							| 
									
										
										
										
											2010-02-13 21:45:39 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   return $array; | 
					
						
							| 
									
										
										
										
											2010-02-13 21:45:39 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | function snmpwalk_cache_triple_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL) | 
					
						
							| 
									
										
										
										
											2010-07-22 21:58:49 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   $data = snmp_walk($device, $oid, "-OQUs", $mib, $mibdir); | 
					
						
							| 
									
										
										
										
											2011-03-15 15:27:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   foreach (explode("\n", $data) as $entry) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     list($oid,$value) = explode("=", $entry); | 
					
						
							|  |  |  |     $oid = trim($oid); $value = trim($value); | 
					
						
							|  |  |  |     list($oid, $first, $second, $third) = explode(".", $oid); | 
					
						
							|  |  |  |     if (!strstr($value, "at this OID") && isset($oid) && isset($first) && isset($second)) | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |       $index = $first.".".$second.".".$third; | 
					
						
							|  |  |  |       $array[$index][$oid] = $value; | 
					
						
							| 
									
										
										
										
											2010-02-15 21:02:07 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   return $array; | 
					
						
							| 
									
										
										
										
											2010-02-15 21:02:07 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | function snmpwalk_cache_twopart_oid($device, $oid, $array, $mib = 0) | 
					
						
							| 
									
										
										
										
											2010-07-22 21:58:49 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   global $config; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['timeout']) && $device['timeout'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |      $timeout = $device['timeout']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['timeout'])) { | 
					
						
							|  |  |  |      $timeout =  $config['snmp']['timeout']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['retries']) && $device['retries'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $retries = $device['retries']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['retries'])) { | 
					
						
							|  |  |  |     $retries =  $config['snmp']['retries']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!isset($device['transport'])) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $device['transport'] = "udp"; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $snmpcommand = $config['snmpwalk']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $snmpcommand = $config['snmpbulkwalk']; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   $cmd = $snmpcommand; | 
					
						
							|  |  |  |   $cmd .= snmp_gen_auth ($device); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $cmd .= " -O QUs"; | 
					
						
							| 
									
										
										
										
											2012-03-26 13:34:49 +00:00
										 |  |  |   $cmd .= " -M ".$config['install_dir']."/mibs"; | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   if ($mib) { $cmd .= " -m $mib"; } | 
					
						
							|  |  |  |   if (isset($timeout)) { $cmd .= " -t " . $timeout; } | 
					
						
							|  |  |  |   if (isset($retries)) { $cmd .= " -r " . $retries; } | 
					
						
							|  |  |  |   $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid; | 
					
						
							|  |  |  |   if (!$debug) { $cmd .= " 2>/dev/null"; } | 
					
						
							| 
									
										
										
										
											2011-09-15 22:35:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   $data = trim(external_exec($cmd)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   $device_id = $device['device_id']; | 
					
						
							|  |  |  |   foreach (explode("\n", $data) as $entry) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     list($oid,$value) = explode("=", $entry); | 
					
						
							|  |  |  |     $oid = trim($oid); $value = trim($value); $value = str_replace("\"", "", $value); | 
					
						
							|  |  |  |     list($oid, $first, $second) = explode(".", $oid); | 
					
						
							|  |  |  |     if (!strstr($value, "at this OID") && isset($oid) && isset($first) && isset($second)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       $array[$first][$second][$oid] = $value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return $array; | 
					
						
							| 
									
										
										
										
											2009-11-10 15:43:13 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | function snmpwalk_cache_threepart_oid($device, $oid, $array, $mib = 0) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   global $config, $debug; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['timeout']) && $device['timeout'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |      $timeout = $device['timeout']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['timeout'])) { | 
					
						
							|  |  |  |      $timeout =  $config['snmp']['timeout']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['retries']) && $device['retries'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $retries = $device['retries']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['retries'])) { | 
					
						
							|  |  |  |     $retries =  $config['snmp']['retries']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!isset($device['transport'])) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $device['transport'] = "udp"; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $snmpcommand = $config['snmpwalk']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $snmpcommand = $config['snmpbulkwalk']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  |   $cmd = $snmpcommand; | 
					
						
							|  |  |  |   $cmd .= snmp_gen_auth ($device); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $cmd .= " -O QUs"; | 
					
						
							| 
									
										
										
										
											2012-03-26 13:34:49 +00:00
										 |  |  |   $cmd .= " -M ".$config['install_dir']."/mibs"; | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   if ($mib) { $cmd .= " -m $mib"; } | 
					
						
							|  |  |  |   if (isset($timeout)) { $cmd .= " -t " . $timeout; } | 
					
						
							|  |  |  |   if (isset($retries)) { $cmd .= " -r " . $retries; } | 
					
						
							|  |  |  |   $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid; | 
					
						
							|  |  |  |   if (!$debug) { $cmd .= " 2>/dev/null"; } | 
					
						
							| 
									
										
										
										
											2011-09-15 22:35:22 +00:00
										 |  |  |   $data = trim(external_exec($cmd)); | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   $device_id = $device['device_id']; | 
					
						
							|  |  |  |   foreach (explode("\n", $data) as $entry) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     list($oid,$value) = explode("=", $entry); | 
					
						
							|  |  |  |     $oid = trim($oid); $value = trim($value); $value = str_replace("\"", "", $value); | 
					
						
							|  |  |  |     list($oid, $first, $second, $third) = explode(".", $oid); | 
					
						
							|  |  |  |     if ($debug) {echo("$entry || $oid || $first || $second || $third\n"); } | 
					
						
							|  |  |  |     if (!strstr($value, "at this OID") && isset($oid) && isset($first) && isset($second) && isset($third)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       $array[$first][$second][$third][$oid] = $value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return $array; | 
					
						
							| 
									
										
										
										
											2010-01-05 18:40:05 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | function snmp_cache_slotport_oid($oid, $device, $array, $mib = 0) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   global $config; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['timeout']) && $device['timeout'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |      $timeout = $device['timeout']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['timeout'])) { | 
					
						
							|  |  |  |      $timeout =  $config['snmp']['timeout']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['retries']) && $device['retries'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $retries = $device['retries']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['retries'])) { | 
					
						
							|  |  |  |     $retries =  $config['snmp']['retries']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!isset($device['transport'])) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $device['transport'] = "udp"; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $snmpcommand = $config['snmpwalk']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $snmpcommand = $config['snmpbulkwalk']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  |   $cmd = $snmpcommand; | 
					
						
							|  |  |  |   $cmd .= snmp_gen_auth ($device); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-15 15:18:57 +00:00
										 |  |  |   $cmd .= " -O QUs"; | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   if ($mib) { $cmd .= " -m $mib"; } | 
					
						
							| 
									
										
										
										
											2012-03-26 13:34:49 +00:00
										 |  |  |   $cmd .= " -M ".$config['install_dir']."/mibs"; | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   if (isset($timeout)) { $cmd .= " -t " . $timeout; } | 
					
						
							|  |  |  |   if (isset($retries)) { $cmd .= " -r " . $retries; } | 
					
						
							|  |  |  |   $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid; | 
					
						
							|  |  |  |   if (!$debug) { $cmd .= " 2>/dev/null"; } | 
					
						
							| 
									
										
										
										
											2011-09-15 22:35:22 +00:00
										 |  |  |   $data = trim(external_exec($cmd)); | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   $device_id = $device['device_id']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   foreach (explode("\n", $data) as $entry) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $entry = str_replace($oid.".", "", $entry); | 
					
						
							|  |  |  |     list($slotport, $value) = explode("=", $entry); | 
					
						
							|  |  |  |     $slotport = trim($slotport); $value = trim($value); | 
					
						
							|  |  |  |     if ($array[$slotport]['ifIndex']) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       $ifIndex = $array[$slotport]['ifIndex']; | 
					
						
							|  |  |  |       $array[$ifIndex][$oid] = $value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return $array; | 
					
						
							| 
									
										
										
										
											2009-10-29 09:28:43 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | function snmp_cache_oid($oid, $device, $array, $mib = 0) | 
					
						
							| 
									
										
										
										
											2010-07-22 21:58:49 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   $array = snmpwalk_cache_oid($device, $oid, $array, $mib); | 
					
						
							|  |  |  |   return $array; | 
					
						
							| 
									
										
										
										
											2009-10-28 13:49:37 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 18:03:49 +00:00
										 |  |  | function snmp_cache_port_oids($oids, $port, $device, $array, $mib=0) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   global $config; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['timeout']) && $device['timeout'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |      $timeout = $device['timeout']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['timeout'])) { | 
					
						
							|  |  |  |      $timeout =  $config['snmp']['timeout']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['retries']) && $device['retries'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $retries = $device['retries']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['retries'])) { | 
					
						
							|  |  |  |     $retries =  $config['snmp']['retries']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!isset($device['transport'])) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $device['transport'] = "udp"; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   foreach ($oids as $oid) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $string .= " $oid.$port"; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  |   $cmd = $config['snmpget']; | 
					
						
							|  |  |  |   $cmd .= snmp_gen_auth ($device); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-15 15:18:57 +00:00
										 |  |  |   $cmd .= " -O vq"; | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   if (isset($timeout)) { $cmd .= " -t " . $timeout; } | 
					
						
							|  |  |  |   if (isset($retries)) { $cmd .= " -r " . $retries; } | 
					
						
							| 
									
										
										
										
											2012-03-26 13:34:49 +00:00
										 |  |  |   $cmd .= " -M ".$config['install_dir']."/mibs"; | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   if ($mib) { $cmd .= " -m $mib"; } | 
					
						
							|  |  |  |   $cmd .= " -t " . $timeout . " -r " . $retries; | 
					
						
							|  |  |  |   $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$string; | 
					
						
							|  |  |  |   if (!$debug) { $cmd .= " 2>/dev/null"; } | 
					
						
							| 
									
										
										
										
											2011-09-15 22:35:22 +00:00
										 |  |  |   $data = trim(external_exec($cmd)); | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   $x=0; | 
					
						
							|  |  |  |   $values = explode("\n", $data); | 
					
						
							|  |  |  |   #echo("Caching: ifIndex $port\n");
 | 
					
						
							| 
									
										
										
										
											2011-09-20 14:37:54 +00:00
										 |  |  |   foreach ($oids as $oid) { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |     if (!strstr($values[$x], "at this OID")) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       $array[$port][$oid] = $values[$x]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     $x++; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return $array; | 
					
						
							| 
									
										
										
										
											2009-10-28 13:49:37 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 18:03:49 +00:00
										 |  |  | function snmp_cache_portIfIndex($device, $array) | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   global $config; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['timeout']) && $device['timeout'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |      $timeout = $device['timeout']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['timeout'])) { | 
					
						
							|  |  |  |      $timeout =  $config['snmp']['timeout']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['retries']) && $device['retries'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $retries = $device['retries']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['retries'])) { | 
					
						
							|  |  |  |     $retries =  $config['snmp']['retries']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!isset($device['transport'])) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $device['transport'] = "udp"; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  |   $cmd = $config['snmpwalk']; | 
					
						
							|  |  |  |   $cmd .= snmp_gen_auth ($device); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-15 15:18:57 +00:00
										 |  |  |   $cmd .= " -CI -m CISCO-STACK-MIB -O q"; | 
					
						
							| 
									
										
										
										
											2012-03-26 13:34:49 +00:00
										 |  |  |   $cmd .= " -M ".$config['install_dir']."/mibs"; | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   if (isset($timeout)) { $cmd .= " -t " . $timeout; } | 
					
						
							|  |  |  |   if (isset($retries)) { $cmd .= " -r " . $retries; } | 
					
						
							|  |  |  |   $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." portIfIndex"; | 
					
						
							| 
									
										
										
										
											2011-09-15 22:35:22 +00:00
										 |  |  |   $output = trim(external_exec($cmd)); | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   $device_id = $device['device_id']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   foreach (explode("\n", $output) as $entry) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $entry = str_replace("CISCO-STACK-MIB::portIfIndex.", "", $entry); | 
					
						
							|  |  |  |     list($slotport, $ifIndex) = explode(" ", $entry); | 
					
						
							| 
									
										
										
										
											2011-09-20 14:37:54 +00:00
										 |  |  |     if ($slotport && $ifIndex) { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |       $array[$ifIndex]['portIfIndex'] = $slotport; | 
					
						
							|  |  |  |       $array[$slotport]['ifIndex'] = $ifIndex; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return $array; | 
					
						
							| 
									
										
										
										
											2009-10-28 13:49:37 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 18:03:49 +00:00
										 |  |  | function snmp_cache_portName($device, $array) | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   global $config; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['timeout']) && $device['timeout'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |      $timeout = $device['timeout']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['timeout'])) { | 
					
						
							|  |  |  |      $timeout =  $config['snmp']['timeout']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (is_numeric($device['retries']) && $device['retries'] > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $retries = $device['retries']; | 
					
						
							|  |  |  |   } elseif (isset($config['snmp']['retries'])) { | 
					
						
							|  |  |  |     $retries =  $config['snmp']['retries']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!isset($device['transport'])) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $device['transport'] = "udp"; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  |   $cmd = $config['snmpwalk']; | 
					
						
							|  |  |  |   $cmd .= snmp_gen_auth ($device); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-15 15:18:57 +00:00
										 |  |  |   $cmd .= " -CI -m CISCO-STACK-MIB -O Qs"; | 
					
						
							| 
									
										
										
										
											2012-03-26 13:34:49 +00:00
										 |  |  |   $cmd .= " -M ".$config['install_dir']."/mibs"; | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   if (isset($timeout)) { $cmd .= " -t " . $timeout; } | 
					
						
							|  |  |  |   if (isset($retries)) { $cmd .= " -r " . $retries; } | 
					
						
							|  |  |  |   $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." portName"; | 
					
						
							| 
									
										
										
										
											2011-09-15 22:35:22 +00:00
										 |  |  |   $output = trim(external_exec($cmd)); | 
					
						
							| 
									
										
										
										
											2011-03-23 09:54:56 +00:00
										 |  |  |   $device_id = $device['device_id']; | 
					
						
							|  |  |  |   #echo("Caching: portName\n");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   foreach (explode("\n", $output) as $entry) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $entry = str_replace("portName.", "", $entry); | 
					
						
							|  |  |  |     list($slotport, $portName) = explode("=", $entry); | 
					
						
							|  |  |  |     $slotport = trim($slotport); $portName = trim($portName); | 
					
						
							|  |  |  |     if ($array[$slotport]['ifIndex']) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       $ifIndex = $array[$slotport]['ifIndex']; | 
					
						
							|  |  |  |       $array[$slotport]['portName'] = $portName; | 
					
						
							|  |  |  |       $array[$ifIndex]['portName'] = $portName; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return $array; | 
					
						
							| 
									
										
										
										
											2009-10-28 13:49:37 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-03-15 15:27:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  | function snmp_gen_auth (&$device) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   global $debug; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $cmd = ""; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ($device['snmpver'] === "v3") | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $cmd = " -v3 -n \"\" -l " . $device['authlevel']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ($device['authlevel'] === "noAuthNoPriv") | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       // We have to provide a username anyway (see Net-SNMP doc)
 | 
					
						
							|  |  |  |       $cmd .= " -u observium"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     elseif ($device['authlevel'] === "authNoPriv") | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       $cmd .= " -a " . $device['authalgo']; | 
					
						
							|  |  |  |       $cmd .= " -A \"" . $device['authpass'] . "\""; | 
					
						
							|  |  |  |       $cmd .= " -u " . $device['authname']; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     elseif ($device['authlevel'] === "authPriv") | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       $cmd .= " -a " . $device['authalgo']; | 
					
						
							|  |  |  |       $cmd .= " -A \"" . $device['authpass'] . "\""; | 
					
						
							|  |  |  |       $cmd .= " -u " . $device['authname']; | 
					
						
							|  |  |  |       $cmd .= " -x " . $device['cryptoalgo']; | 
					
						
							|  |  |  |       $cmd .= " -X \"" . $device['cryptopass'] . "\""; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if ($debug) { print "DEBUG: " . $device['snmpver'] ." : Unsupported SNMPv3 AuthLevel (wtf have you done ?)\n"; } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   elseif ($device['snmpver'] === "v2c" or $device['snmpver'] === "v1") | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     $cmd  = " -" . $device['snmpver']; | 
					
						
							|  |  |  |     $cmd .= " -c " . $device['community']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     if ($debug) { print "DEBUG: " . $device['snmpver'] ." : Unsupported SNMP Version (wtf have you done ?)\n"; } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ($debug) { print "DEBUG: SNMP Auth options = $cmd\n"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return $cmd; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-15 16:12:44 +00:00
										 |  |  | ?>
 |