| 
									
										
										
										
											2009-10-28 13:49:37 +00:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2015-07-20 14:08:23 +10:00
										 |  |  | /* | 
					
						
							|  |  |  |  * LibreNMS - SNMP Functions | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Original Observium code by: Adam Armstrong, Tom Laermans | 
					
						
							|  |  |  |  * Copyright (c) 2010-2012 Adam Armstrong. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Additions for LibreNMS by Paul Gear | 
					
						
							|  |  |  |  * Copyright (c) 2014-2015 Gear Consulting Pty Ltd <http://libertysys.com.au/> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software: you can redistribute it and/or modify it | 
					
						
							|  |  |  |  * under the terms of the GNU General Public License as published by the | 
					
						
							|  |  |  |  * Free Software Foundation, either version 3 of the License, or (at your | 
					
						
							|  |  |  |  * option) any later version.  Please see LICENSE.txt at the top level of | 
					
						
							|  |  |  |  * the source code distribution for details. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-10-28 13:49:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | function string_to_oid($string) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     $oid = strlen($string); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     for ($i = 0; $i != strlen($string); $i++) { | 
					
						
							|  |  |  |         $oid .= '.'.ord($string[$i]); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     return $oid; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | }//end string_to_oid()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-17 16:01:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | function prep_snmp_setting($device, $setting) { | 
					
						
							|  |  |  |     global $config; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (is_numeric($device[$setting]) && $device[$setting] > 0) { | 
					
						
							|  |  |  |         return $device[$setting]; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (isset($config['snmp'][$setting])) { | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |         return $config['snmp'][$setting]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | }//end prep_snmp_setting()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | function mibdir($mibdir) { | 
					
						
							| 
									
										
										
										
											2015-06-02 23:55:45 +10:00
										 |  |  |     global $config; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     return ' -M '.($mibdir ? $mibdir : $config['mibdir']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }//end mibdir()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 23:55:45 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | function snmp_get_multi($device, $oids, $options='-OQUs', $mib=null, $mibdir=null) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     global $debug,$config,$runtime_stats,$mibs_loaded; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     // populate timeout & retries values from configuration
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $timeout = prep_snmp_setting($device, 'timeout'); | 
					
						
							|  |  |  |     $retries = prep_snmp_setting($device, 'retries'); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (!isset($device['transport'])) { | 
					
						
							|  |  |  |         $device['transport'] = 'udp'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $cmd  = $config['snmpget']; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= snmp_gen_auth($device); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if ($options) { | 
					
						
							|  |  |  |         $cmd .= ' '.$options; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ($mib) { | 
					
						
							|  |  |  |         $cmd .= ' -m '.$mib; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $cmd .= mibdir($mibdir); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= isset($timeout) ? ' -t '.$timeout : ''; | 
					
						
							|  |  |  |     $cmd .= isset($retries) ? ' -r '.$retries : ''; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= ' '.$device['transport'].':'.$device['hostname'].':'.$device['port']; | 
					
						
							|  |  |  |     $cmd .= ' '.$oids; | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (!$debug) { | 
					
						
							|  |  |  |         $cmd .= ' 2>/dev/null'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     $data = trim(external_exec($cmd)); | 
					
						
							|  |  |  |     $runtime_stats['snmpget']++; | 
					
						
							|  |  |  |     $array = array(); | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     foreach (explode("\n", $data) as $entry) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         list($oid,$value)  = explode('=', $entry, 2); | 
					
						
							|  |  |  |         $oid               = trim($oid); | 
					
						
							|  |  |  |         $value             = trim($value); | 
					
						
							|  |  |  |         list($oid, $index) = explode('.', $oid, 2); | 
					
						
							|  |  |  |         if (!strstr($value, 'at this OID') && isset($oid) && isset($index)) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |             $array[$index][$oid] = $value; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return $array; | 
					
						
							| 
									
										
										
										
											2010-02-13 21:45:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | }//end snmp_get_multi()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function snmp_get($device, $oid, $options=null, $mib=null, $mibdir=null) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     global $debug,$config,$runtime_stats,$mibs_loaded; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $timeout = prep_snmp_setting($device, 'timeout'); | 
					
						
							|  |  |  |     $retries = prep_snmp_setting($device, 'retries'); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (!isset($device['transport'])) { | 
					
						
							|  |  |  |         $device['transport'] = 'udp'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (strstr($oid, ' ')) { | 
					
						
							|  |  |  |         echo report_this_text("snmp_get called for multiple OIDs: $oid"); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $cmd  = $config['snmpget']; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= snmp_gen_auth($device); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if ($options) { | 
					
						
							|  |  |  |         $cmd .= ' '.$options; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ($mib) { | 
					
						
							|  |  |  |         $cmd .= ' -m '.$mib; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $cmd .= mibdir($mibdir); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= isset($timeout) ? ' -t '.$timeout : ''; | 
					
						
							|  |  |  |     $cmd .= isset($retries) ? ' -r '.$retries : ''; | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= ' '.$device['transport'].':'.$device['hostname'].':'.$device['port']; | 
					
						
							|  |  |  |     $cmd .= ' '.$oid; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (!$debug) { | 
					
						
							|  |  |  |         $cmd .= ' 2>/dev/null'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     $data = trim(external_exec($cmd)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $runtime_stats['snmpget']++; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (is_string($data) && (preg_match('/(No Such Instance|No Such Object|No more variables left|Authentication failure)/i', $data))) { | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-10-14 20:33:59 +02:00
										 |  |  |     elseif ($data || $data === '0') { | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |         return $data; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-02-08 03:36:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | }//end snmp_get()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function snmp_walk($device, $oid, $options=null, $mib=null, $mibdir=null) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     global $debug,$config,$runtime_stats; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $timeout = prep_snmp_setting($device, 'timeout'); | 
					
						
							|  |  |  |     $retries = prep_snmp_setting($device, 'retries'); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (!isset($device['transport'])) { | 
					
						
							|  |  |  |         $device['transport'] = 'udp'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         $snmpcommand = $config['snmpwalk']; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         $snmpcommand = $config['snmpbulkwalk']; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $cmd = $snmpcommand; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= snmp_gen_auth($device); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ($options) { | 
					
						
							|  |  |  |         $cmd .= " $options "; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if ($mib) { | 
					
						
							|  |  |  |         $cmd .= " -m $mib"; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $cmd .= mibdir($mibdir); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= isset($timeout) ? ' -t '.$timeout : ''; | 
					
						
							|  |  |  |     $cmd .= isset($retries) ? ' -r '.$retries : ''; | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= ' '.$device['transport'].':'.$device['hostname'].':'.$device['port'].' '.$oid; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (!$debug) { | 
					
						
							|  |  |  |         $cmd .= ' 2>/dev/null'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     $data = trim(external_exec($cmd)); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $data = str_replace('"', '', $data); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (is_string($data) && (preg_match('/No Such (Object|Instance)/i', $data))) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         $data = false; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     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); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     $runtime_stats['snmpwalk']++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return $data; | 
					
						
							| 
									
										
										
										
											2009-10-28 13:49:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | }//end snmp_walk()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function snmpwalk_cache_cip($device, $oid, $array=array(), $mib=0) { | 
					
						
							| 
									
										
										
										
											2015-12-06 18:07:51 +10:00
										 |  |  |     global $config, $debug; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $timeout = prep_snmp_setting($device, 'timeout'); | 
					
						
							|  |  |  |     $retries = prep_snmp_setting($device, 'retries'); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (!isset($device['transport'])) { | 
					
						
							|  |  |  |         $device['transport'] = 'udp'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) { | 
					
						
							|  |  |  |         $snmpcommand = $config['snmpwalk']; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |         $snmpcommand = $config['snmpbulkwalk']; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd  = $snmpcommand; | 
					
						
							|  |  |  |     $cmd .= snmp_gen_auth($device); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $cmd .= ' -O snQ'; | 
					
						
							|  |  |  |     if ($mib) { | 
					
						
							|  |  |  |         $cmd .= " -m $mib"; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $cmd .= mibdir(null); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= isset($timeout) ? ' -t '.$timeout : ''; | 
					
						
							|  |  |  |     $cmd .= isset($retries) ? ' -r '.$retries : ''; | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= ' '.$device['transport'].':'.$device['hostname'].':'.$device['port'].' '.$oid; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (!$debug) { | 
					
						
							|  |  |  |         $cmd .= ' 2>/dev/null'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $data      = trim(external_exec($cmd)); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     // echo("Caching: $oid\n");
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     foreach (explode("\n", $data) as $entry) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         list ($this_oid, $this_value) = preg_split('/=/', $entry); | 
					
						
							|  |  |  |         $this_oid   = trim($this_oid); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         $this_value = trim($this_value); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         $this_oid   = substr($this_oid, 30); | 
					
						
							|  |  |  |         list($ifIndex, $dir, $a, $b, $c, $d, $e, $f) = explode('.', $this_oid); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         $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"; | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         if ($dir == '1') { | 
					
						
							|  |  |  |             $dir = 'input'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if ($dir == '2') { | 
					
						
							|  |  |  |             $dir = 'output'; | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($mac && $dir) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |             $array[$ifIndex][$mac][$oid][$dir] = $this_value; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     }//end foreach
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     return $array; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | }//end snmpwalk_cache_cip()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-26 22:10:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | function snmp_cache_ifIndex($device) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     // FIXME: this is not yet using our own snmp_*
 | 
					
						
							| 
									
										
										
										
											2015-12-06 18:07:51 +10:00
										 |  |  |     global $config, $debug; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $timeout = prep_snmp_setting($device, 'timeout'); | 
					
						
							|  |  |  |     $retries = prep_snmp_setting($device, 'retries'); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (!isset($device['transport'])) { | 
					
						
							|  |  |  |         $device['transport'] = 'udp'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) { | 
					
						
							|  |  |  |         $snmpcommand = $config['snmpwalk']; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |         $snmpcommand = $config['snmpbulkwalk']; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd  = $snmpcommand; | 
					
						
							|  |  |  |     $cmd .= snmp_gen_auth($device); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= ' -O Qs'; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     $cmd .= mibdir(null); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= ' -m IF-MIB ifIndex'; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= isset($timeout) ? ' -t '.$timeout : ''; | 
					
						
							|  |  |  |     $cmd .= isset($retries) ? ' -r '.$retries : ''; | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (!$debug) { | 
					
						
							|  |  |  |         $cmd .= ' 2>/dev/null'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $data      = trim(external_exec($cmd)); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-04 18:34:24 +02:00
										 |  |  |     $array = array(); | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     foreach (explode("\n", $data) as $entry) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         list ($this_oid, $this_value) = preg_split('/=/', $entry); | 
					
						
							|  |  |  |         list ($this_oid, $this_index) = explode('.', $this_oid, 2); | 
					
						
							|  |  |  |         $this_index                   = trim($this_index); | 
					
						
							|  |  |  |         $this_oid   = trim($this_oid); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         $this_value = trim($this_value); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         if (!strstr($this_value, 'at this OID') && $this_index) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |             $array[] = $this_value; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return $array; | 
					
						
							| 
									
										
										
										
											2009-10-28 13:49:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | }//end snmp_cache_ifIndex()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function snmpwalk_cache_oid($device, $oid, $array, $mib=null, $mibdir=null, $snmpflags='-OQUs') { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     $data = snmp_walk($device, $oid, $snmpflags, $mib, $mibdir); | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     foreach (explode("\n", $data) as $entry) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         list($oid,$value)  = explode('=', $entry, 2); | 
					
						
							|  |  |  |         $oid               = trim($oid); | 
					
						
							|  |  |  |         $value             = trim($value); | 
					
						
							|  |  |  |         list($oid, $index) = explode('.', $oid, 2); | 
					
						
							|  |  |  |         if (!strstr($value, 'at this OID') && isset($oid) && isset($index)) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |             $array[$index][$oid] = $value; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-11-10 19:28:24 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     return $array; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | }//end snmpwalk_cache_oid()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 19:28:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-21 18:17:23 +00:00
										 |  |  | // just like snmpwalk_cache_oid except that it returns the numerical oid as the index
 | 
					
						
							|  |  |  | // this is useful when the oid is indexed by the mac address and snmpwalk would
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | // return periods (.) for non-printable numbers, thus making many different indexes appear
 | 
					
						
							| 
									
										
										
										
											2012-05-21 18:17:23 +00:00
										 |  |  | // to be the same.
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | function snmpwalk_cache_oid_num($device, $oid, $array, $mib=null, $mibdir=null) { | 
					
						
							|  |  |  |     return snmpwalk_cache_oid($device, $oid, $array, $mib, $mibdir, $snmpflags = '-OQUn'); | 
					
						
							| 
									
										
										
										
											2012-05-21 18:17:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | }//end snmpwalk_cache_oid_num()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function snmpwalk_cache_multi_oid($device, $oid, $array, $mib=null, $mibdir=null) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     global $cache; | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02: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); | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |         foreach (explode("\n", $data) as $entry) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |             list($r_oid,$value) = explode('=', $entry, 2); | 
					
						
							|  |  |  |             $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)) { | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |                 $array[$index][$r_oid] = $value; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         }//end foreach
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         $cache['snmp'][$device['device_id']][$oid] = $array; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     }//end if
 | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return $cache['snmp'][$device['device_id']][$oid]; | 
					
						
							| 
									
										
										
										
											2010-02-21 05:08:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | }//end snmpwalk_cache_multi_oid()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function snmpwalk_cache_double_oid($device, $oid, $array, $mib=null, $mibdir=null) { | 
					
						
							|  |  |  |     $data = snmp_walk($device, $oid, '-OQUs', $mib, $mibdir); | 
					
						
							| 
									
										
										
										
											2011-03-12 08:50:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     foreach (explode("\n", $data) as $entry) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         list($oid,$value) = explode('=', $entry, 2); | 
					
						
							|  |  |  |         $oid              = trim($oid); | 
					
						
							|  |  |  |         $value            = trim($value); | 
					
						
							|  |  |  |         list($oid, $first, $second) = explode('.', $oid); | 
					
						
							|  |  |  |         if (!strstr($value, 'at this OID') && isset($oid) && isset($first) && isset($second)) { | 
					
						
							|  |  |  |             $double               = $first.'.'.$second; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |             $array[$double][$oid] = $value; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-02-13 21:45:39 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     return $array; | 
					
						
							| 
									
										
										
										
											2010-02-13 21:45:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | }//end snmpwalk_cache_double_oid()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function snmpwalk_cache_triple_oid($device, $oid, $array, $mib=null, $mibdir=null) { | 
					
						
							|  |  |  |     $data = snmp_walk($device, $oid, '-OQUs', $mib, $mibdir); | 
					
						
							| 
									
										
										
										
											2011-03-15 15:27:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     foreach (explode("\n", $data) as $entry) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         list($oid,$value) = explode('=', $entry, 2); | 
					
						
							|  |  |  |         $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)) { | 
					
						
							|  |  |  |             $index               = $first.'.'.$second.'.'.$third; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |             $array[$index][$oid] = $value; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-02-15 21:02:07 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-03-08 17:12:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     return $array; | 
					
						
							| 
									
										
										
										
											2010-02-15 21:02:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | }//end snmpwalk_cache_triple_oid()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function snmpwalk_cache_twopart_oid($device, $oid, $array, $mib=0) { | 
					
						
							| 
									
										
										
										
											2015-12-06 18:07:51 +10:00
										 |  |  |     global $config, $debug; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $timeout = prep_snmp_setting($device, 'timeout'); | 
					
						
							|  |  |  |     $retries = prep_snmp_setting($device, 'retries'); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     if (!isset($device['transport'])) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         $device['transport'] = 'udp'; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         $snmpcommand = $config['snmpwalk']; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         $snmpcommand = $config['snmpbulkwalk']; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd  = $snmpcommand; | 
					
						
							|  |  |  |     $cmd .= snmp_gen_auth($device); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= ' -O QUs'; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     $cmd .= mibdir(null); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if ($mib) { | 
					
						
							|  |  |  |         $cmd .= " -m $mib"; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= isset($timeout) ? ' -t '.$timeout : ''; | 
					
						
							|  |  |  |     $cmd .= isset($retries) ? ' -r '.$retries : ''; | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= ' '.$device['transport'].':'.$device['hostname'].':'.$device['port'].' '.$oid; | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (!$debug) { | 
					
						
							|  |  |  |         $cmd .= ' 2>/dev/null'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $data = trim(external_exec($cmd)); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     foreach (explode("\n", $data) as $entry) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         list($oid,$value) = explode('=', $entry, 2); | 
					
						
							|  |  |  |         $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)) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |             $array[$first][$second][$oid] = $value; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return $array; | 
					
						
							| 
									
										
										
										
											2009-11-10 15:43:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | }//end snmpwalk_cache_twopart_oid()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function snmpwalk_cache_threepart_oid($device, $oid, $array, $mib=0) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     global $config, $debug; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $timeout = prep_snmp_setting($device, 'timeout'); | 
					
						
							|  |  |  |     $retries = prep_snmp_setting($device, 'retries'); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     if (!isset($device['transport'])) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         $device['transport'] = 'udp'; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         $snmpcommand = $config['snmpwalk']; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         $snmpcommand = $config['snmpbulkwalk']; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd  = $snmpcommand; | 
					
						
							|  |  |  |     $cmd .= snmp_gen_auth($device); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= ' -O QUs'; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     $cmd .= mibdir(null); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if ($mib) { | 
					
						
							|  |  |  |         $cmd .= " -m $mib"; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= isset($timeout) ? ' -t '.$timeout : ''; | 
					
						
							|  |  |  |     $cmd .= isset($retries) ? ' -r '.$retries : ''; | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= ' '.$device['transport'].':'.$device['hostname'].':'.$device['port'].' '.$oid; | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (!$debug) { | 
					
						
							|  |  |  |         $cmd .= ' 2>/dev/null'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $data = trim(external_exec($cmd)); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     foreach (explode("\n", $data) as $entry) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         list($oid,$value) = explode('=', $entry, 2); | 
					
						
							|  |  |  |         $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"; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         if (!strstr($value, 'at this OID') && isset($oid) && isset($first) && isset($second) && isset($third)) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |             $array[$first][$second][$third][$oid] = $value; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return $array; | 
					
						
							| 
									
										
										
										
											2010-01-05 18:40:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | }//end snmpwalk_cache_threepart_oid()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function snmp_cache_slotport_oid($oid, $device, $array, $mib=0) { | 
					
						
							| 
									
										
										
										
											2015-12-06 18:07:51 +10:00
										 |  |  |     global $config, $debug; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $timeout = prep_snmp_setting($device, 'timeout'); | 
					
						
							|  |  |  |     $retries = prep_snmp_setting($device, 'retries'); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     if (!isset($device['transport'])) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         $device['transport'] = 'udp'; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         $snmpcommand = $config['snmpwalk']; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         $snmpcommand = $config['snmpbulkwalk']; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd  = $snmpcommand; | 
					
						
							|  |  |  |     $cmd .= snmp_gen_auth($device); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $cmd .= ' -O QUs'; | 
					
						
							|  |  |  |     if ($mib) { | 
					
						
							|  |  |  |         $cmd .= " -m $mib"; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $cmd .= mibdir(null); | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= isset($timeout) ? ' -t '.$timeout : ''; | 
					
						
							|  |  |  |     $cmd .= isset($retries) ? ' -r '.$retries : ''; | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= ' '.$device['transport'].':'.$device['hostname'].':'.$device['port'].' '.$oid; | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (!$debug) { | 
					
						
							|  |  |  |         $cmd .= ' 2>/dev/null'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $data      = trim(external_exec($cmd)); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     foreach (explode("\n", $data) as $entry) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         $entry                  = str_replace($oid.'.', '', $entry); | 
					
						
							|  |  |  |         list($slotport, $value) = explode('=', $entry, 2); | 
					
						
							|  |  |  |         $slotport               = trim($slotport); | 
					
						
							|  |  |  |         $value                  = trim($value); | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |         if ($array[$slotport]['ifIndex']) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |             $ifIndex               = $array[$slotport]['ifIndex']; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |             $array[$ifIndex][$oid] = $value; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return $array; | 
					
						
							| 
									
										
										
										
											2009-10-29 09:28:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | }//end snmp_cache_slotport_oid()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function snmp_cache_oid($oid, $device, $array, $mib=0) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     $array = snmpwalk_cache_oid($device, $oid, $array, $mib); | 
					
						
							|  |  |  |     return $array; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | }//end snmp_cache_oid()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-28 13:49:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | function snmp_cache_port_oids($oids, $port, $device, $array, $mib=0) { | 
					
						
							| 
									
										
										
										
											2015-12-06 18:07:51 +10:00
										 |  |  |     global $config, $debug; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $timeout = prep_snmp_setting($device, 'timeout'); | 
					
						
							|  |  |  |     $retries = prep_snmp_setting($device, 'retries'); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     if (!isset($device['transport'])) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         $device['transport'] = 'udp'; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     foreach ($oids as $oid) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         $string .= " $oid.$port"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd  = $config['snmpget']; | 
					
						
							|  |  |  |     $cmd .= snmp_gen_auth($device); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= ' -O vq'; | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= isset($timeout) ? ' -t '.$timeout : ''; | 
					
						
							|  |  |  |     $cmd .= isset($retries) ? ' -r '.$retries : ''; | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $cmd .= mibdir(null); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if ($mib) { | 
					
						
							|  |  |  |         $cmd .= " -m $mib"; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= ' -t '.$timeout.' -r '.$retries; | 
					
						
							|  |  |  |     $cmd .= ' '.$device['transport'].':'.$device['hostname'].':'.$device['port'].' '.$string; | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (!$debug) { | 
					
						
							|  |  |  |         $cmd .= ' 2>/dev/null'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $data   = trim(external_exec($cmd)); | 
					
						
							|  |  |  |     $x      = 0; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     $values = explode("\n", $data); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     // echo("Caching: ifIndex $port\n");
 | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     foreach ($oids as $oid) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         if (!strstr($values[$x], 'at this OID')) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |             $array[$port][$oid] = $values[$x]; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         $x++; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return $array; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | }//end snmp_cache_port_oids()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-28 13:49:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | function snmp_cache_portIfIndex($device, $array) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     global $config; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $timeout = prep_snmp_setting($device, 'timeout'); | 
					
						
							|  |  |  |     $retries = prep_snmp_setting($device, 'retries'); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     if (!isset($device['transport'])) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         $device['transport'] = 'udp'; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd  = $config['snmpwalk']; | 
					
						
							|  |  |  |     $cmd .= snmp_gen_auth($device); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= ' -CI -m CISCO-STACK-MIB -O q'; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     $cmd .= mibdir(null); | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= isset($timeout) ? ' -t '.$timeout : ''; | 
					
						
							|  |  |  |     $cmd .= isset($retries) ? ' -r '.$retries : ''; | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd      .= ' '.$device['transport'].':'.$device['hostname'].':'.$device['port'].' portIfIndex'; | 
					
						
							|  |  |  |     $output    = trim(external_exec($cmd)); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     foreach (explode("\n", $output) as $entry) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         $entry                    = str_replace('CISCO-STACK-MIB::portIfIndex.', '', $entry); | 
					
						
							|  |  |  |         list($slotport, $ifIndex) = explode(' ', $entry, 2); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         if ($slotport && $ifIndex) { | 
					
						
							|  |  |  |             $array[$ifIndex]['portIfIndex'] = $slotport; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |             $array[$slotport]['ifIndex']    = $ifIndex; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return $array; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | }//end snmp_cache_portIfIndex()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-28 13:49:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | function snmp_cache_portName($device, $array) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     global $config; | 
					
						
							| 
									
										
										
										
											2011-03-15 15:27:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $timeout = prep_snmp_setting($device, 'timeout'); | 
					
						
							|  |  |  |     $retries = prep_snmp_setting($device, 'retries'); | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     if (!isset($device['transport'])) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         $device['transport'] = 'udp'; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd  = $config['snmpwalk']; | 
					
						
							|  |  |  |     $cmd .= snmp_gen_auth($device); | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= ' -CI -m CISCO-STACK-MIB -O Qs'; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     $cmd .= mibdir(null); | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= isset($timeout) ? ' -t '.$timeout : ''; | 
					
						
							|  |  |  |     $cmd .= isset($retries) ? ' -r '.$retries : ''; | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd      .= ' '.$device['transport'].':'.$device['hostname'].':'.$device['port'].' portName'; | 
					
						
							|  |  |  |     $output    = trim(external_exec($cmd)); | 
					
						
							|  |  |  |     // echo("Caching: portName\n");
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     foreach (explode("\n", $output) as $entry) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         $entry = str_replace('portName.', '', $entry); | 
					
						
							|  |  |  |         list($slotport, $portName) = explode('=', $entry, 2); | 
					
						
							|  |  |  |         $slotport                  = trim($slotport); | 
					
						
							|  |  |  |         $portName                  = trim($portName); | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |         if ($array[$slotport]['ifIndex']) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |             $ifIndex = $array[$slotport]['ifIndex']; | 
					
						
							|  |  |  |             $array[$slotport]['portName'] = $portName; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |             $array[$ifIndex]['portName']  = $portName; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return $array; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | }//end snmp_cache_portName()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function snmp_gen_auth(&$device) { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |     global $debug; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd = ''; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if ($device['snmpver'] === 'v3') { | 
					
						
							| 
									
										
										
										
											2015-08-20 19:19:15 +00:00
										 |  |  |         $cmd = " -v3 -n '' -l '".$device['authlevel']."'"; | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         if ($device['authlevel'] === 'noAuthNoPriv') { | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |             // We have to provide a username anyway (see Net-SNMP doc)
 | 
					
						
							|  |  |  |             // FIXME: There are two other places this is set - why are they ignored here?
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |             $cmd .= ' -u root'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if ($device['authlevel'] === 'authNoPriv') { | 
					
						
							| 
									
										
										
										
											2015-08-20 19:19:15 +00:00
										 |  |  |             $cmd .= " -a '".$device['authalgo']."'"; | 
					
						
							|  |  |  |             $cmd .= " -A '".$device['authpass']."'"; | 
					
						
							|  |  |  |             $cmd .= " -u '".$device['authname']."'"; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else if ($device['authlevel'] === 'authPriv') { | 
					
						
							| 
									
										
										
										
											2015-08-20 19:19:15 +00:00
										 |  |  |             $cmd .= " -a '".$device['authalgo']."'"; | 
					
						
							|  |  |  |             $cmd .= " -A '".$device['authpass']."'"; | 
					
						
							|  |  |  |             $cmd .= " -u '".$device['authname']."'"; | 
					
						
							|  |  |  |             $cmd .= " -x '".$device['cryptoalgo']."'"; | 
					
						
							|  |  |  |             $cmd .= " -X '".$device['cryptopass']."'"; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |             if ($debug) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |                 print 'DEBUG: '.$device['snmpver']." : Unsupported SNMPv3 AuthLevel (wtf have you done ?)\n"; | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-07-08 14:24:59 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     else if ($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"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }//end if
 | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if ($debug) { | 
					
						
							|  |  |  |         print "DEBUG: SNMP Auth options = $cmd\n"; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     return $cmd; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | }//end snmp_gen_auth()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-09 16:18:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2015-11-15 17:38:26 +10:00
										 |  |  |  * Translate the given MIB into a PHP array.  Each keyword becomes an array index. | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |  * Example: | 
					
						
							|  |  |  |  * snmptranslate -Td -On -M mibs -m RUCKUS-ZD-SYSTEM-MIB RUCKUS-ZD-SYSTEM-MIB::ruckusZDSystemStatsNumSta | 
					
						
							|  |  |  |  * .1.3.6.1.4.1.25053.1.2.1.1.1.15.30 | 
					
						
							|  |  |  |  * ruckusZDSystemStatsAllNumSta OBJECT-TYPE | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  |  *   -- FROM    RUCKUS-ZD-SYSTEM-MIB | 
					
						
							|  |  |  |  *     SYNTAX   Unsigned32 | 
					
						
							|  |  |  |  *     MAX-ACCESS       read-only | 
					
						
							|  |  |  |  *     STATUS   current | 
					
						
							|  |  |  |  *     DESCRIPTION      "Number of All client devices" | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |  *   ::= { iso(1) org(3) dod(6) internet(1) private(4) enterprises(1) ruckusRootMIB(25053) ruckusObjects(1) ruckusZD(2) ruckusZDSystemModule(1) ruckusZDSystemMIB(1) ruckusZDSystemObjects(1) | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |  *           ruckusZDSystemStats(15) 30 } | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | function snmp_mib_parse($oid, $mib, $module, $mibdir=null) { | 
					
						
							|  |  |  |     $fulloid  = explode('.', $oid); | 
					
						
							| 
									
										
										
										
											2015-06-16 09:01:13 +10:00
										 |  |  |     $lastpart = end($fulloid); | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd  = 'snmptranslate -Td -On'; | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |     $cmd .= mibdir($mibdir); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd .= ' -m '.$module.' '.$module.'::'; | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |     $cmd .= $lastpart; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $result = array(); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $lines  = preg_split('/\n+/', trim(shell_exec($cmd))); | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |     foreach ($lines as $l) { | 
					
						
							|  |  |  |         $f = preg_split('/\s+/', trim($l)); | 
					
						
							|  |  |  |         // first line is all numeric
 | 
					
						
							|  |  |  |         if (preg_match('/^[\d.]+$/', $f[0])) { | 
					
						
							|  |  |  |             $result['oid'] = $f[0]; | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |         // then the name of the object type
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         if ($f[1] && $f[1] == 'OBJECT-TYPE') { | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  |             $result['object_type'] = $f[0]; | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |         // then the other data elements
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         if ($f[0] == '--' && $f[1] == 'FROM') { | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  |             $result['module'] = $f[2]; | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($f[0] == 'MAX-ACCESS') { | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  |             $result['max_access'] = $f[1]; | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($f[0] == 'STATUS') { | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |             $result[strtolower($f[0])] = $f[1]; | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($f[0] == 'SYNTAX') { | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |             $result[strtolower($f[0])] = $f[1]; | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($f[0] == 'DESCRIPTION') { | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |             $desc = explode('"', $l); | 
					
						
							|  |  |  |             if ($desc[1]) { | 
					
						
							|  |  |  |                 $str = preg_replace('/^[\s.]*/', '', $desc[1]); | 
					
						
							|  |  |  |                 $str = preg_replace('/[\s.]*$/', '', $str); | 
					
						
							|  |  |  |                 $result[strtolower($f[0])] = $str; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     }//end foreach
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-09 00:12:43 +10:00
										 |  |  |     // The main mib entry doesn't have any useful data in it - only return items that have the syntax specified.
 | 
					
						
							|  |  |  |     if (isset($result['syntax']) && isset($result['object_type'])) { | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |         $result['mib'] = $mib; | 
					
						
							|  |  |  |         return $result; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-11-15 18:18:13 +10:00
										 |  |  | } // snmp_mib_parse
 | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  |  * Walks through the given MIB module, looking for the given MIB. | 
					
						
							|  |  |  |  * NOTE: different from snmp walk - this doesn't touch the device. | 
					
						
							|  |  |  |  * NOTE: There's probably a better way to do this with snmptranslate. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |  * Example: | 
					
						
							|  |  |  |  * snmptranslate -Ts -M mibs -m RUCKUS-ZD-SYSTEM-MIB | grep ruckusZDSystemStats | 
					
						
							|  |  |  |  * .iso.org.dod.internet.private.enterprises.ruckusRootMIB.ruckusObjects.ruckusZD.ruckusZDSystemModule.ruckusZDSystemMIB.ruckusZDSystemObjects.ruckusZDSystemStats | 
					
						
							|  |  |  |  * .iso.org.dod.internet.private.enterprises.ruckusRootMIB.ruckusObjects.ruckusZD.ruckusZDSystemModule.ruckusZDSystemMIB.ruckusZDSystemObjects.ruckusZDSystemStats.ruckusZDSystemStatsNumAP | 
					
						
							|  |  |  |  * .iso.org.dod.internet.private.enterprises.ruckusRootMIB.ruckusObjects.ruckusZD.ruckusZDSystemModule.ruckusZDSystemMIB.ruckusZDSystemObjects.ruckusZDSystemStats.ruckusZDSystemStatsNumSta | 
					
						
							|  |  |  |  * ... | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-16 22:07:48 +10:00
										 |  |  | function snmp_mib_walk($mib, $module, $mibdir=null) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd    = 'snmptranslate -Ts'; | 
					
						
							|  |  |  |     $cmd   .= mibdir($mibdir); | 
					
						
							|  |  |  |     $cmd   .= ' -m '.$module; | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |     $result = array(); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $data   = preg_split('/\n+/', shell_exec($cmd)); | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |     foreach ($data as $oid) { | 
					
						
							|  |  |  |         // only include oids which are part of this mib
 | 
					
						
							|  |  |  |         if (strstr($oid, $mib)) { | 
					
						
							|  |  |  |             $obj = snmp_mib_parse($oid, $mib, $module, $mibdir); | 
					
						
							|  |  |  |             if ($obj) { | 
					
						
							|  |  |  |                 $result[] = $obj; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  |     return $result; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-15 18:18:13 +10:00
										 |  |  | } // snmp_mib_walk
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 23:56:09 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-16 22:09:17 +10:00
										 |  |  | function quote_column($a) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return '`'.$a.'`'; | 
					
						
							| 
									
										
										
										
											2015-11-15 18:18:13 +10:00
										 |  |  | } // quote_column
 | 
					
						
							| 
									
										
										
										
											2015-08-16 22:09:17 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-15 20:12:49 +10:00
										 |  |  | function join_array($a, $b) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-16 22:09:17 +10:00
										 |  |  |     return quote_column($a).'='.$b; | 
					
						
							| 
									
										
										
										
											2015-11-15 18:18:13 +10:00
										 |  |  | } // join_array
 | 
					
						
							| 
									
										
										
										
											2015-07-15 20:12:49 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-16 22:09:17 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2015-07-15 20:12:49 +10:00
										 |  |  |  * Update the given table in the database with the given row & column data. | 
					
						
							|  |  |  |  * @param tablename The table to update | 
					
						
							|  |  |  |  * @param columns   An array of column names | 
					
						
							|  |  |  |  * @param numkeys   The number of columns which are in the primary key of the table; these primary keys must be first in the list of columns | 
					
						
							|  |  |  |  * @param rows      Row data to insert, an array of arrays with column names as the second-level keys | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-07-15 20:12:49 +10:00
										 |  |  | function update_db_table($tablename, $columns, $numkeys, $rows) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-16 22:09:17 +10:00
										 |  |  |     dbBeginTransaction(); | 
					
						
							| 
									
										
										
										
											2015-07-15 20:12:49 +10:00
										 |  |  |     foreach ($rows as $nothing => $obj) { | 
					
						
							|  |  |  |         // create a parameter list based on the columns
 | 
					
						
							|  |  |  |         $params = array(); | 
					
						
							|  |  |  |         foreach ($columns as $column) { | 
					
						
							|  |  |  |             $params[] = $obj[$column]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $column_placeholders = array_fill(0, count($columns), '?'); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-15 20:12:49 +10:00
										 |  |  |         // build the "ON DUPLICATE KEY" part
 | 
					
						
							|  |  |  |         $non_key_columns = array_slice($columns, $numkeys); | 
					
						
							|  |  |  |         $non_key_placeholders = array_slice($column_placeholders, $numkeys); | 
					
						
							|  |  |  |         $update_definitions = array_map("join_array", $non_key_columns, $non_key_placeholders); | 
					
						
							|  |  |  |         $non_key_params = array_slice($params, $numkeys); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-16 22:09:17 +10:00
										 |  |  |         $sql = 'INSERT INTO `' . $tablename . '` (' . | 
					
						
							|  |  |  |             implode(',', array_map("quote_column", $columns)) . | 
					
						
							| 
									
										
										
										
											2015-07-15 20:12:49 +10:00
										 |  |  |             ') VALUES (' . implode(',', $column_placeholders) . | 
					
						
							|  |  |  |             ') ON DUPLICATE KEY UPDATE ' . implode(',', $update_definitions); | 
					
						
							|  |  |  |         $result = dbQuery($sql, array_merge($params, $non_key_params)); | 
					
						
							|  |  |  |         d_echo("Result: $result\n"); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-16 22:09:17 +10:00
										 |  |  |     dbCommitTransaction(); | 
					
						
							| 
									
										
										
										
											2015-11-15 18:18:13 +10:00
										 |  |  | } // update_db_table
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-15 20:12:49 +10:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2015-07-20 14:07:23 +10:00
										 |  |  |  * Load the given MIB into the database. | 
					
						
							|  |  |  |  * @return count of objects loaded | 
					
						
							| 
									
										
										
										
											2015-07-15 20:12:49 +10:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-07-20 14:07:23 +10:00
										 |  |  | function snmp_mib_load($mib, $module, $included_by, $mibdir = null) | 
					
						
							| 
									
										
										
										
											2015-07-15 20:12:49 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  |     $mibs = array(); | 
					
						
							|  |  |  |     foreach (snmp_mib_walk($mib, $module, $mibdir) as $obj) { | 
					
						
							|  |  |  |         $mibs[$obj['object_type']] = $obj; | 
					
						
							| 
									
										
										
										
											2015-07-20 14:07:23 +10:00
										 |  |  |         $mibs[$obj['object_type']]['included_by'] = $included_by; | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-02 09:42:00 +10:00
										 |  |  |     d_print_r($mibs); | 
					
						
							| 
									
										
										
										
											2015-07-20 14:07:23 +10:00
										 |  |  |     // NOTE: `last_modified` omitted due to being automatically maintained by MySQL
 | 
					
						
							|  |  |  |     $columns = array('module', 'mib', 'object_type', 'oid', 'syntax', 'description', 'max_access', 'status', 'included_by'); | 
					
						
							| 
									
										
										
										
											2015-07-15 20:12:49 +10:00
										 |  |  |     update_db_table('mibdefs', $columns, 3, $mibs); | 
					
						
							| 
									
										
										
										
											2015-07-20 14:07:23 +10:00
										 |  |  |     return count($mibs); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-15 18:18:13 +10:00
										 |  |  | } // snmp_mib_load
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * Turn the given oid (name or numeric value) into a MODULE::mib name. | 
					
						
							|  |  |  |  * @return an array consisting of the module and mib names, or null if no matching MIB is found. | 
					
						
							| 
									
										
										
										
											2015-06-14 17:32:21 +10:00
										 |  |  |  * Example: | 
					
						
							|  |  |  |  * snmptranslate -m all -M mibs .1.3.6.1.4.1.8072.3.2.10 2>/dev/null | 
					
						
							|  |  |  |  * NET-SNMP-TC::linux | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-07-15 20:12:49 +10:00
										 |  |  | function snmp_translate($oid, $module, $mibdir = null) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if ($module !== 'all') { | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  |         $oid = "$module::$oid"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $cmd  = 'snmptranslate'.mibdir($mibdir); | 
					
						
							|  |  |  |     $cmd .= " -m $module $oid"; | 
					
						
							|  |  |  |     // load all the MIBs looking for our object
 | 
					
						
							|  |  |  |     $cmd .= ' 2>/dev/null'; | 
					
						
							|  |  |  |     // ignore invalid MIBs
 | 
					
						
							| 
									
										
										
										
											2015-06-14 09:09:54 +10:00
										 |  |  |     $lines = preg_split('/\n+/', external_exec($cmd)); | 
					
						
							| 
									
										
										
										
											2015-06-16 08:42:05 +10:00
										 |  |  |     if (empty($lines)) { | 
					
						
							| 
									
										
										
										
											2015-06-13 22:54:40 +10:00
										 |  |  |         d_echo("No results from snmptranslate\n"); | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $matches = array(); | 
					
						
							|  |  |  |     if (!preg_match('/(.*)::(.*)/', $lines[0], $matches)) { | 
					
						
							| 
									
										
										
										
											2015-06-13 22:54:40 +10:00
										 |  |  |         d_echo("This doesn't look like a MIB: $lines[0]\n"); | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-13 22:54:40 +10:00
										 |  |  |     d_echo("SNMP translated: $module::$oid -> $matches[1]::$matches[2]\n"); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     return array( | 
					
						
							|  |  |  |         $matches[1], | 
					
						
							|  |  |  |         $matches[2], | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-15 18:18:13 +10:00
										 |  |  | } // snmp_translate
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							| 
									
										
										
										
											2015-06-08 14:58:22 +10:00
										 |  |  |  * check if the type of the oid is a numeric type, and if so, | 
					
						
							|  |  |  |  * @return the name of RRD type that is best suited to saving it | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-07-15 20:12:49 +10:00
										 |  |  | function oid_rrd_type($oid, $mibdef) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-09 00:12:43 +10:00
										 |  |  |     if (!isset($mibdef[$oid])) { | 
					
						
							| 
									
										
										
										
											2015-06-08 14:58:22 +10:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  |     switch ($mibdef[$oid]['syntax']) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     case 'OCTET': | 
					
						
							|  |  |  |     case 'IpAddress': | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2015-06-08 14:58:22 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     case 'TimeTicks': | 
					
						
							| 
									
										
										
										
											2015-07-20 14:08:23 +10:00
										 |  |  |         // FIXME
 | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     case 'INTEGER': | 
					
						
							|  |  |  |     case 'Integer32': | 
					
						
							|  |  |  |         // FIXME
 | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     case 'Counter32': | 
					
						
							|  |  |  |         // FIXME
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2015-06-08 14:58:22 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     case 'Counter64': | 
					
						
							|  |  |  |         return 'COUNTER:600:0:U'; | 
					
						
							| 
									
										
										
										
											2015-06-08 14:58:22 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     case 'Unsigned32': | 
					
						
							|  |  |  |         return 'GAUGE:600:U:U'; | 
					
						
							| 
									
										
										
										
											2015-06-08 14:58:22 +10:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-08 15:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-08 14:58:22 +10:00
										 |  |  |     return false; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-15 18:18:13 +10:00
										 |  |  | } // oid_rrd_type
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-08 14:58:22 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							| 
									
										
										
										
											2015-06-09 00:12:43 +10:00
										 |  |  |  * Construct a graph names for use in the database. | 
					
						
							|  |  |  |  * Tag each as in use on this device in &$graphs. | 
					
						
							|  |  |  |  * Update the database with graph definitions as needed. | 
					
						
							|  |  |  |  * We don't include the index in the graph name - that is handled at display time. | 
					
						
							| 
									
										
										
										
											2015-06-08 14:58:22 +10:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-07-15 20:12:49 +10:00
										 |  |  | function tag_graphs($mibname, $oids, $mibdef, &$graphs) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-09 00:12:43 +10:00
										 |  |  |     foreach ($oids as $index => $array) { | 
					
						
							|  |  |  |         foreach ($array as $oid => $val) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |             $graphname          = $mibname.'-'.$mibdef[$oid]['shortname']; | 
					
						
							| 
									
										
										
										
											2015-06-09 00:12:43 +10:00
										 |  |  |             $graphs[$graphname] = true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-06-08 14:58:22 +10:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-15 18:18:13 +10:00
										 |  |  | } // tag_graphs
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-09 00:12:43 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * Ensure a graph_type definition exists in the database for the entities in this MIB | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-07-15 20:12:49 +10:00
										 |  |  | function update_mib_graph_types($mibname, $oids, $mibdef, $graphs) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-09 00:12:43 +10:00
										 |  |  |     $seengraphs = array(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Get the list of graphs currently in the database
 | 
					
						
							|  |  |  |     // FIXME: there's probably a more efficient way to do this
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     foreach (dbFetch('SELECT DISTINCT `graph_subtype` FROM `graph_types` WHERE `graph_subtype` LIKE ?', array("$mibname-%")) as $graph) { | 
					
						
							| 
									
										
										
										
											2015-06-09 00:12:43 +10:00
										 |  |  |         $seengraphs[$graph['graph_subtype']] = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     foreach ($oids as $index => $array) { | 
					
						
							|  |  |  |         $i = 1; | 
					
						
							|  |  |  |         foreach ($array as $oid => $val) { | 
					
						
							|  |  |  |             $graphname = "$mibname-".$mibdef[$oid]['shortname']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // add the graph if it's not in the database already
 | 
					
						
							|  |  |  |             if ($graphs[$graphname] && !$seengraphs[$graphname]) { | 
					
						
							|  |  |  |                 // construct a graph definition based on the MIB definition
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |                 $graphdef                  = array(); | 
					
						
							|  |  |  |                 $graphdef['graph_type']    = 'device'; | 
					
						
							| 
									
										
										
										
											2015-06-09 00:12:43 +10:00
										 |  |  |                 $graphdef['graph_subtype'] = $graphname; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |                 $graphdef['graph_section'] = 'mib'; | 
					
						
							|  |  |  |                 $graphdef['graph_descr']   = $mibdef[$oid]['description']; | 
					
						
							|  |  |  |                 $graphdef['graph_order']   = $i++; | 
					
						
							| 
									
										
										
										
											2015-06-09 00:12:43 +10:00
										 |  |  |                 // TODO: add colours, unit_text, and ds
 | 
					
						
							|  |  |  |                 // add graph to the database
 | 
					
						
							|  |  |  |                 dbInsert($graphdef, 'graph_types'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-11-15 18:18:13 +10:00
										 |  |  |     } | 
					
						
							|  |  |  | } // update_mib_graph_types
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-08 14:58:22 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * Save all of the measurable oids for the device in their own RRDs. | 
					
						
							| 
									
										
										
										
											2015-08-16 22:09:17 +10:00
										 |  |  |  * Save the current value of all the oids in the database. | 
					
						
							| 
									
										
										
										
											2015-06-08 14:58:22 +10:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-07-15 20:12:49 +10:00
										 |  |  | function save_mibs($device, $mibname, $oids, $mibdef, &$graphs) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-09 00:12:43 +10:00
										 |  |  |     $usedoids = array(); | 
					
						
							| 
									
										
										
										
											2015-08-16 22:09:17 +10:00
										 |  |  |     $deviceoids = array(); | 
					
						
							| 
									
										
										
										
											2015-06-08 14:58:22 +10:00
										 |  |  |     foreach ($oids as $index => $array) { | 
					
						
							| 
									
										
										
										
											2015-08-16 22:09:17 +10:00
										 |  |  |         foreach ($array as $obj => $val) { | 
					
						
							|  |  |  |             // build up the device_oid row for saving into the database
 | 
					
						
							|  |  |  |             $numvalue = preg_match('/^\d+$/', $val) ? $val : null; | 
					
						
							|  |  |  |             $deviceoids[] = array( | 
					
						
							|  |  |  |                 'device_id'     => $device['device_id'], | 
					
						
							|  |  |  |                 'oid'           => $mibdef[$obj]['oid'].".".$index, | 
					
						
							|  |  |  |                 'module'        => $mibdef[$obj]['module'], | 
					
						
							|  |  |  |                 'mib'           => $mibdef[$obj]['mib'], | 
					
						
							|  |  |  |                 'object_type'   => $obj, | 
					
						
							|  |  |  |                 'value'         => $val, | 
					
						
							|  |  |  |                 'numvalue'      => $numvalue, | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $type = oid_rrd_type($obj, $mibdef); | 
					
						
							| 
									
										
										
										
											2015-06-16 08:42:05 +10:00
										 |  |  |             if ($type === false) { | 
					
						
							| 
									
										
										
										
											2015-06-08 14:58:22 +10:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-16 22:09:17 +10:00
										 |  |  |             $usedoids[$index][$obj] = $val; | 
					
						
							| 
									
										
										
										
											2015-11-15 18:01:09 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // if there's a file from the previous version of MIB-based polling, rename it
 | 
					
						
							| 
									
										
										
										
											2016-01-10 19:31:39 +10:00
										 |  |  |             $old = array($mibname, $mibdef[$obj]['object_type'], $index); | 
					
						
							|  |  |  |             $new = array($mibname, $mibdef[$obj]['shortname'], $index); | 
					
						
							|  |  |  |             rrd_file_rename($device, $old, $new); | 
					
						
							| 
									
										
										
										
											2015-11-15 18:01:09 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-10 19:31:39 +10:00
										 |  |  |             $tags = array( | 
					
						
							|  |  |  |                 'rrd_def' => array("DS:mibval:$type"), | 
					
						
							|  |  |  |                 'rrd_name' => $new, | 
					
						
							| 
									
										
										
										
											2015-06-08 14:58:22 +10:00
										 |  |  |             ); | 
					
						
							| 
									
										
										
										
											2016-01-10 19:31:39 +10:00
										 |  |  |             data_update($device, 'mibval', $tags, $val); | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-09 00:12:43 +10:00
										 |  |  |     tag_graphs($mibname, $usedoids, $mibdef, $graphs); | 
					
						
							|  |  |  |     update_mib_graph_types($mibname, $usedoids, $mibdef, $graphs); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-16 22:09:17 +10:00
										 |  |  |     // update database
 | 
					
						
							|  |  |  |     $columns = array('device_id', 'oid', 'module', 'mib', 'object_type', 'value', 'numvalue'); | 
					
						
							|  |  |  |     update_db_table('device_oids', $columns, 2, $deviceoids); | 
					
						
							| 
									
										
										
										
											2015-11-15 18:18:13 +10:00
										 |  |  | } // save_mibs
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-20 14:07:23 +10:00
										 |  |  | /* | 
					
						
							|  |  |  |  * @return an array of MIB objects matching $module, $name, keyed by object_type | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function load_mibdefs($module, $name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     $params = array($module, $name); | 
					
						
							|  |  |  |     $result = array(); | 
					
						
							|  |  |  |     $object_types = array(); | 
					
						
							|  |  |  |     foreach (dbFetchRows("SELECT * FROM `mibdefs` WHERE `module` = ? AND `mib` = ?", $params) as $row) { | 
					
						
							|  |  |  |         $mib = $row['object_type']; | 
					
						
							|  |  |  |         $object_types[] = $mib; | 
					
						
							|  |  |  |         $result[$mib] = $row; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // add shortname to each element
 | 
					
						
							|  |  |  |     $prefix = longest_matching_prefix($name, $object_types); | 
					
						
							|  |  |  |     foreach ($result as $mib => $m) { | 
					
						
							|  |  |  |         $result[$mib]['shortname'] = str_replace($prefix, '', $m['object_type']); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     d_print_r($result); | 
					
						
							|  |  |  |     return $result; | 
					
						
							| 
									
										
										
										
											2015-11-15 18:18:13 +10:00
										 |  |  | } // load_mibdefs
 | 
					
						
							| 
									
										
										
										
											2015-07-20 14:07:23 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * @return an array of MIB names and modules for $device from the database | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function load_device_mibs($device) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     $params = array($device['device_id']); | 
					
						
							|  |  |  |     $result = array(); | 
					
						
							|  |  |  |     foreach (dbFetchRows("SELECT `mib`, `module` FROM device_mibs WHERE device_id = ?", $params) as $row) { | 
					
						
							|  |  |  |         $result[$row['mib']] = $row['module']; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return $result; | 
					
						
							| 
									
										
										
										
											2015-11-15 18:18:13 +10:00
										 |  |  | } // load_device_mibs
 | 
					
						
							| 
									
										
										
										
											2015-07-20 14:07:23 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-20 14:07:23 +10:00
										 |  |  | /* | 
					
						
							|  |  |  |  * Run MIB-based polling for $device.  Update $graphs with the results. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function poll_mibs($device, &$graphs) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!is_mib_poller_enabled($device)) { | 
					
						
							| 
									
										
										
										
											2015-06-06 21:17:24 +10:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-20 14:07:23 +10:00
										 |  |  |     echo 'MIB: polling '; | 
					
						
							| 
									
										
										
										
											2015-06-14 09:09:54 +10:00
										 |  |  |     d_echo("\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-20 14:07:23 +10:00
										 |  |  |     foreach (load_device_mibs($device) as $name => $module) { | 
					
						
							|  |  |  |         echo "$name "; | 
					
						
							|  |  |  |         d_echo("\n"); | 
					
						
							|  |  |  |         $oids = snmpwalk_cache_oid($device, $name, array(), $module, null, "-OQUsb"); | 
					
						
							|  |  |  |         d_print_r($oids); | 
					
						
							|  |  |  |         save_mibs($device, $name, $oids, load_mibdefs($module, $name), $graphs); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     echo "\n"; | 
					
						
							| 
									
										
										
										
											2015-11-15 18:18:13 +10:00
										 |  |  | } // poll_mibs
 | 
					
						
							| 
									
										
										
										
											2015-07-20 14:07:23 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * Take a list of MIB name => module pairs. | 
					
						
							|  |  |  |  * Validate MIBs and store the device->mib mapping in the database. | 
					
						
							|  |  |  |  * See includes/discovery/os/ruckuswireless.inc.php for an example of usage. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function register_mibs($device, $mibs, $included_by) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!is_mib_poller_enabled($device)) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     echo "MIB: registering\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     foreach ($mibs as $name => $module) { | 
					
						
							| 
									
										
										
										
											2015-06-08 14:58:22 +10:00
										 |  |  |         $translated = snmp_translate($name, $module); | 
					
						
							|  |  |  |         if ($translated) { | 
					
						
							| 
									
										
										
										
											2015-07-20 14:07:23 +10:00
										 |  |  |             $mod = $translated[0]; | 
					
						
							|  |  |  |             $nam = $translated[1]; | 
					
						
							|  |  |  |             echo "     $mod::$nam\n"; | 
					
						
							|  |  |  |             if (snmp_mib_load($nam, $mod, $included_by) > 0) { | 
					
						
							|  |  |  |                 // NOTE: `last_modified` omitted due to being automatically maintained by MySQL
 | 
					
						
							|  |  |  |                 $columns = array('device_id', 'module', 'mib', 'included_by'); | 
					
						
							|  |  |  |                 $rows = array(); | 
					
						
							|  |  |  |                 $rows[] = array( | 
					
						
							|  |  |  |                     'device_id'   => $device['device_id'], | 
					
						
							|  |  |  |                     'module'      => $mod, | 
					
						
							|  |  |  |                     'mib'         => $nam, | 
					
						
							|  |  |  |                     'included_by' => $included_by, | 
					
						
							|  |  |  |                 ); | 
					
						
							|  |  |  |                 update_db_table('device_mibs', $columns, 3, $rows); | 
					
						
							| 
									
										
										
										
											2015-07-15 20:12:49 +10:00
										 |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 echo("MIB: Could not load definition for $mod::$nam\n"); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2015-07-20 14:07:23 +10:00
										 |  |  |             echo("MIB: Could not find $module::$name\n"); | 
					
						
							| 
									
										
										
										
											2015-06-08 14:58:22 +10:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-15 20:12:49 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-20 14:07:23 +10:00
										 |  |  |     echo "\n"; | 
					
						
							| 
									
										
										
										
											2015-11-15 18:18:13 +10:00
										 |  |  | } // register_mibs
 |