diff --git a/includes/snmp.inc.php b/includes/snmp.inc.php index ea31034aa..a0c9db043 100644 --- a/includes/snmp.inc.php +++ b/includes/snmp.inc.php @@ -5,68 +5,62 @@ // // TRUE STORY. THAT SHIT IS WHACK. -- adama. -function string_to_oid($string) -{ +function string_to_oid($string) { $oid = strlen($string); - for($i = 0; $i != strlen($string); $i++) - { + for($i = 0; $i != strlen($string); $i++) { $oid .= ".".ord($string[$i]); } return $oid; } -function mibdir($mibdir) -{ +function prep_snmp_setting($device, $setting) { + global $config; + + if (is_numeric($device[$setting]) && $device[$setting] > 0) { + return $device[$setting]; + } elseif (isset($config['snmp'][$setting])) { + return $config['snmp'][$setting]; + } +} + +function mibdir($mibdir) { global $config; return " -M " . ($mibdir ? $mibdir : $config['mibdir']); } -function snmp_get_multi($device, $oids, $options = "-OQUs", $mib = NULL, $mibdir = NULL) -{ +function snmp_get_multi($device, $oids, $options = "-OQUs", $mib = NULL, $mibdir = NULL) { global $debug,$config,$runtime_stats,$mibs_loaded; - if (is_numeric($device['timeout']) && $device['timeout'] > 0) - { - $timeout = $device['timeout']; - } elseif (isset($config['snmp']['timeout'])) { - $timeout = $config['snmp']['timeout']; - } + // populate timeout & retries values from configuration + $timeout = prep_snmp_setting($device, "timeout"); + $retries = prep_snmp_setting($device, "retries"); - if (is_numeric($device['retries']) && $device['retries'] > 0) - { - $retries = $device['retries']; - } elseif (isset($config['snmp']['retries'])) { - $retries = $config['snmp']['retries']; - } - - if (!isset($device['transport'])) - { - $device['transport'] = "udp"; - } + if (!isset($device['transport'])) $device['transport'] = "udp"; $cmd = $config['snmpget']; $cmd .= snmp_gen_auth ($device); - if ($options) { $cmd .= " " . $options; } - if ($mib) { $cmd .= " -m " . $mib; } - $cmd .= mibdir($mibdir); + if ($options) $cmd .= " " . $options; + if ($mib) $cmd .= " -m " . $mib; - if (isset($timeout)) { $cmd .= " -t " . $timeout; } - if (isset($retries)) { $cmd .= " -r " . $retries; } + $cmd .= mibdir($mibdir); - $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']; + $cmd .= isset($timeout) ? " -t " . $timeout : ""; + $cmd .= isset($retries) ? " -r " . $retries : ""; + + $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']; $cmd .= " ".$oids; - if (!$debug) { $cmd .= " 2>/dev/null"; } + + if (!$debug) $cmd .= " 2>/dev/null"; + $data = trim(external_exec($cmd)); $runtime_stats['snmpget']++; $array = array(); - foreach (explode("\n", $data) as $entry) - { + foreach (explode("\n", $data) as $entry) { 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)) - { + if (!strstr($value, "at this OID") && isset($oid) && isset($index)) { $array[$index][$oid] = $value; } } @@ -74,90 +68,58 @@ function snmp_get_multi($device, $oids, $options = "-OQUs", $mib = NULL, $mibdir return $array; } -function snmp_get($device, $oid, $options = NULL, $mib = NULL, $mibdir = NULL) -{ +function snmp_get($device, $oid, $options = NULL, $mib = NULL, $mibdir = NULL) { global $debug,$config,$runtime_stats,$mibs_loaded; - if (is_numeric($device['timeout']) && $device['timeout'] > 0) - { - $timeout = $device['timeout']; - } elseif (isset($config['snmp']['timeout'])) { - $timeout = $config['snmp']['timeout']; - } + $timeout = prep_snmp_setting($device, "timeout"); + $retries = prep_snmp_setting($device, "retries"); - if (is_numeric($device['retries']) && $device['retries'] > 0) - { - $retries = $device['retries']; - } elseif (isset($config['snmp']['retries'])) { - $retries = $config['snmp']['retries']; - } + if (!isset($device['transport'])) $device['transport'] = "udp"; - if (!isset($device['transport'])) - { - $device['transport'] = "udp"; - } - - if (strstr($oid,' ')) - { + if (strstr($oid,' ')) { echo(report_this_text("snmp_get called for multiple OIDs: $oid")); } $cmd = $config['snmpget']; $cmd .= snmp_gen_auth ($device); - if ($options) { $cmd .= " " . $options; } - if ($mib) { $cmd .= " -m " . $mib; } - $cmd .= mibdir($mibdir); - if (isset($timeout)) { $cmd .= " -t " . $timeout; } - if (isset($retries)) { $cmd .= " -r " . $retries; } - $cmd .= " " . $device['transport'].":".$device['hostname'].":".$device['port']; + if ($options) $cmd .= " " . $options; + if ($mib) $cmd .= " -m " . $mib; + + $cmd .= mibdir($mibdir); + + $cmd .= isset($timeout) ? " -t " . $timeout : ""; + $cmd .= isset($retries) ? " -r " . $retries : ""; + + $cmd .= " " . $device['transport'] . ":" . $device['hostname'] . ":" . $device['port']; $cmd .= " " . $oid; - if (!$debug) { $cmd .= " 2>/dev/null"; } + if (!$debug) $cmd .= " 2>/dev/null"; + $data = trim(external_exec($cmd)); $runtime_stats['snmpget']++; - if (is_string($data) && (preg_match("/(No Such Instance|No Such Object|No more variables left|Authentication failure)/i", $data))) - { + if (is_string($data) && (preg_match("/(No Such Instance|No Such Object|No more variables left|Authentication failure)/i", $data))) { + return false; + } elseif ($data) { + return $data; + } else { return false; } - elseif ($data) { return $data; } - else { return false; } } - - - -function snmp_walk($device, $oid, $options = NULL, $mib = NULL, $mibdir = NULL) -{ +function snmp_walk($device, $oid, $options = NULL, $mib = NULL, $mibdir = NULL) { global $debug,$config,$runtime_stats; - if (is_numeric($device['timeout']) && $device['timeout'] > 0) - { - $timeout = $device['timeout']; - } elseif (isset($config['snmp']['timeout'])) - { - $timeout = $config['snmp']['timeout']; - } + $timeout = prep_snmp_setting($device, "timeout"); + $retries = prep_snmp_setting($device, "retries"); - if (is_numeric($device['retries']) && $device['retries'] > 0) - { - $retries = $device['retries']; - } elseif (isset($config['snmp']['retries'])) { - $retries = $config['snmp']['retries']; - } - if (!isset($device['transport'])) - { - $device['transport'] = "udp"; - } + if (!isset($device['transport'])) $device['transport'] = "udp"; - if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) - { + if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) { $snmpcommand = $config['snmpwalk']; - } - else - { + } else { $snmpcommand = $config['snmpbulkwalk']; } @@ -165,25 +127,25 @@ function snmp_walk($device, $oid, $options = NULL, $mib = NULL, $mibdir = NULL) $cmd .= snmp_gen_auth ($device); - if ($options) { $cmd .= " $options "; } - if ($mib) { $cmd .= " -m $mib"; } - $cmd .= mibdir($mibdir); - if (isset($timeout)) { $cmd .= " -t " . $timeout; } - if (isset($retries)) { $cmd .= " -r " . $retries; } + if ($options) $cmd .= " $options "; + if ($mib) $cmd .= " -m $mib"; - $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid; + $cmd .= mibdir($mibdir); - if (!$debug) { $cmd .= " 2>/dev/null"; } - $data = trim(external_exec($cmd)); + $cmd .= isset($timeout) ? " -t " . $timeout : ""; + $cmd .= isset($retries) ? " -r " . $retries : ""; + + $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid; + + if (!$debug) $cmd .= " 2>/dev/null"; + + $data = trim(external_exec($cmd)); $data = str_replace("\"", "", $data); - if (is_string($data) && (preg_match("/No Such (Object|Instance)/i", $data))) - { + if (is_string($data) && (preg_match("/No Such (Object|Instance)/i", $data))) { $data = false; - } - else - { - if (preg_match("/No more variables left in this MIB View \(It is past the end of the MIB tree\)$/",$data)) { + } 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]); @@ -195,58 +157,44 @@ function snmp_walk($device, $oid, $options = NULL, $mib = NULL, $mibdir = NULL) return $data; } -function snmpwalk_cache_cip($device, $oid, $array = array(), $mib = 0) -{ +function snmpwalk_cache_cip($device, $oid, $array = array(), $mib = 0) { global $config; - if (is_numeric($device['timeout']) && $device['timeout'] > 0) - { - $timeout = $device['timeout']; - } elseif (isset($config['snmp']['timeout'])) { - $timeout = $config['snmp']['timeout']; + $timeout = prep_snmp_setting($device, "timeout"); + $retries = prep_snmp_setting($device, "retries"); + + if (!isset($device['transport'])) $device['transport'] = "udp"; + + if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) { + $snmpcommand = $config['snmpwalk']; + } else { + $snmpcommand = $config['snmpbulkwalk']; } - if (is_numeric($device['retries']) && $device['retries'] > 0) - { - $retries = $device['retries']; - } elseif (isset($config['snmp']['retries'])) { - $retries = $config['snmp']['retries']; - } - - if (!isset($device['transport'])) { $device['transport'] = "udp"; } - - if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) - { - $snmpcommand = $config['snmpwalk']; - } - else - { - $snmpcommand = $config['snmpbulkwalk']; - } - $cmd = $snmpcommand; $cmd .= snmp_gen_auth ($device); $cmd .= " -O snQ"; - if ($mib) { $cmd .= " -m $mib"; } + if ($mib) $cmd .= " -m $mib"; $cmd .= mibdir(null); - if (isset($timeout)) { $cmd .= " -t " . $timeout; } - if (isset($retries)) { $cmd .= " -r " . $retries; } - $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid; + $cmd .= isset($timeout) ? " -t " . $timeout : ""; + $cmd .= isset($retries) ? " -r " . $retries : ""; - if (!$debug) { $cmd .= " 2>/dev/null"; } - $data = trim(external_exec($cmd)); + $cmd .= " " . $device['transport'] . ":" . $device['hostname'] . ":" . $device['port'] . " " . $oid; + + if (!$debug) $cmd .= " 2>/dev/null"; + + $data = trim(external_exec($cmd)); $device_id = $device['device_id']; #echo("Caching: $oid\n"); - foreach (explode("\n", $data) as $entry) - { + foreach (explode("\n", $data) as $entry) { list ($this_oid, $this_value) = preg_split("/=/", $entry); $this_oid = trim($this_oid); $this_value = trim($this_value); $this_oid = substr($this_oid, 30); - list($ifIndex,$dir,$a,$b,$c,$d,$e,$f) = explode(".", $this_oid); + list($ifIndex, $dir, $a, $b, $c, $d, $e, $f) = explode(".", $this_oid); $h_a = zeropad(dechex($a)); $h_b = zeropad(dechex($b)); $h_c = zeropad(dechex($c)); @@ -254,45 +202,35 @@ function snmpwalk_cache_cip($device, $oid, $array = array(), $mib = 0) $h_e = zeropad(dechex($e)); $h_f = zeropad(dechex($f)); $mac = "$h_a$h_b$h_c$h_d$h_e$h_f"; - if ($dir == "1") { $dir = "input"; } elseif ($dir == "2") { $dir = "output"; } - if ($mac && $dir) - { + + if ($dir == "1") { + $dir = "input"; + } elseif ($dir == "2") { + $dir = "output"; + } + + if ($mac && $dir) { $array[$ifIndex][$mac][$oid][$dir] = $this_value; } } return $array; } -function snmp_cache_ifIndex($device) -{ +function snmp_cache_ifIndex($device) { // FIXME: this is not yet using our own snmp_* global $config; - if (is_numeric($device['timeout']) && $device['timeout'] > 0) - { - $timeout = $device['timeout']; - } elseif (isset($config['snmp']['timeout'])) { - $timeout = $config['snmp']['timeout']; + $timeout = prep_snmp_setting($device, "timeout"); + $retries = prep_snmp_setting($device, "retries"); + + if (!isset($device['transport'])) $device['transport'] = "udp"; + + if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) { + $snmpcommand = $config['snmpwalk']; + } else { + $snmpcommand = $config['snmpbulkwalk']; } - if (is_numeric($device['retries']) && $device['retries'] > 0) - { - $retries = $device['retries']; - } elseif (isset($config['snmp']['retries'])) { - $retries = $config['snmp']['retries']; - } - - if (!isset($device['transport'])) { $device['transport'] = "udp"; } - - if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) - { - $snmpcommand = $config['snmpwalk']; - } - else - { - $snmpcommand = $config['snmpbulkwalk']; - } - $cmd = $snmpcommand; $cmd .= snmp_gen_auth ($device); @@ -300,21 +238,21 @@ function snmp_cache_ifIndex($device) $cmd .= mibdir(null); $cmd .= " -m IF-MIB ifIndex"; - if (isset($timeout)) { $cmd .= " -t " . $timeout; } - if (isset($retries)) { $cmd .= " -r " . $retries; } - if (!$debug) { $cmd .= " 2>/dev/null"; } - $data = trim(external_exec($cmd)); + $cmd .= isset($timeout) ? " -t " . $timeout : ""; + $cmd .= isset($retries) ? " -r " . $retries : ""; + + if (!$debug) $cmd .= " 2>/dev/null"; + + $data = trim(external_exec($cmd)); $device_id = $device['device_id']; - foreach (explode("\n", $data) as $entry) - { + foreach (explode("\n", $data) as $entry) { 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); $this_value = trim($this_value); - if (!strstr($this_value, "at this OID") && $this_index) - { + if (!strstr($this_value, "at this OID") && $this_index) { $array[] = $this_value; } } @@ -322,16 +260,13 @@ function snmp_cache_ifIndex($device) return $array; } -function snmpwalk_cache_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL, $snmpflags = "-OQUs") -{ +function snmpwalk_cache_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL, $snmpflags = "-OQUs") { $data = snmp_walk($device, $oid, $snmpflags, $mib, $mibdir); - foreach (explode("\n", $data) as $entry) - { + foreach (explode("\n", $data) as $entry) { 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)) - { + if (!strstr($value, "at this OID") && isset($oid) && isset($index)) { $array[$index][$oid] = $value; } } @@ -343,35 +278,29 @@ function snmpwalk_cache_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL, // this is useful when the oid is indexed by the mac address and snmpwalk would // return periods (.) for non-printable numbers, thus making many different indexes appear // to be the same. -function snmpwalk_cache_oid_num($device, $oid, $array, $mib = NULL, $mibdir = NULL) -{ +function snmpwalk_cache_oid_num($device, $oid, $array, $mib = NULL, $mibdir = NULL) { return snmpwalk_cache_oid($device, $oid, $array, $mib, $mibdir, $snmpflags = "-OQUn"); } - -function snmpwalk_cache_multi_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL) -{ +function snmpwalk_cache_multi_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL) { global $cache; - if (!(is_array($cache['snmp'][$device['device_id']]) && array_key_exists($oid,$cache['snmp'][$device['device_id']]))) - { + if (!(is_array($cache['snmp'][$device['device_id']]) && array_key_exists($oid,$cache['snmp'][$device['device_id']]))) { $data = snmp_walk($device, $oid, "-OQUs", $mib, $mibdir); - foreach (explode("\n", $data) as $entry) - { + foreach (explode("\n", $data) as $entry) { 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)) - { - $array[$index][$r_oid] = $value; - } + if (isset($oid_parts['2'])) $index .= ".".$oid_parts['2']; + if (isset($oid_parts['3'])) $index .= ".".$oid_parts['3']; + if (isset($oid_parts['4'])) $index .= ".".$oid_parts['4']; + if (isset($oid_parts['5'])) $index .= ".".$oid_parts['5']; + if (isset($oid_parts['6'])) $index .= ".".$oid_parts['6']; + if (!strstr($value, "at this OID") && isset($r_oid) && isset($index)) { + $array[$index][$r_oid] = $value; + } } $cache['snmp'][$device['device_id']][$oid] = $array; } @@ -383,13 +312,11 @@ function snmpwalk_cache_double_oid($device, $oid, $array, $mib = NULL, $mibdir = { $data = snmp_walk($device, $oid, "-OQUs", $mib, $mibdir); - foreach (explode("\n", $data) as $entry) - { + foreach (explode("\n", $data) as $entry) { 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)) - { + if (!strstr($value, "at this OID") && isset($oid) && isset($first) && isset($second)) { $double = $first.".".$second; $array[$double][$oid] = $value; } @@ -398,17 +325,14 @@ function snmpwalk_cache_double_oid($device, $oid, $array, $mib = NULL, $mibdir = return $array; } -function snmpwalk_cache_triple_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL) -{ +function snmpwalk_cache_triple_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL) { $data = snmp_walk($device, $oid, "-OQUs", $mib, $mibdir); - foreach (explode("\n", $data) as $entry) - { + foreach (explode("\n", $data) as $entry) { 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)) - { + if (!strstr($value, "at this OID") && isset($oid) && isset($first) && isset($second)) { $index = $first.".".$second.".".$third; $array[$index][$oid] = $value; } @@ -417,35 +341,19 @@ function snmpwalk_cache_triple_oid($device, $oid, $array, $mib = NULL, $mibdir = return $array; } -function snmpwalk_cache_twopart_oid($device, $oid, $array, $mib = 0) -{ +function snmpwalk_cache_twopart_oid($device, $oid, $array, $mib = 0) { global $config; - if (is_numeric($device['timeout']) && $device['timeout'] > 0) - { - $timeout = $device['timeout']; - } elseif (isset($config['snmp']['timeout'])) { - $timeout = $config['snmp']['timeout']; - } + $timeout = prep_snmp_setting($device, "timeout"); + $retries = prep_snmp_setting($device, "retries"); - if (is_numeric($device['retries']) && $device['retries'] > 0) - { - $retries = $device['retries']; - } elseif (isset($config['snmp']['retries'])) { - $retries = $config['snmp']['retries']; - } - - if (!isset($device['transport'])) - { + if (!isset($device['transport'])) { $device['transport'] = "udp"; } - if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) - { + if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) { $snmpcommand = $config['snmpwalk']; - } - else - { + } else { $snmpcommand = $config['snmpbulkwalk']; } @@ -454,22 +362,26 @@ function snmpwalk_cache_twopart_oid($device, $oid, $array, $mib = 0) $cmd .= " -O QUs"; $cmd .= mibdir(null); - if ($mib) { $cmd .= " -m $mib"; } - if (isset($timeout)) { $cmd .= " -t " . $timeout; } - if (isset($retries)) { $cmd .= " -r " . $retries; } - $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid; - if (!$debug) { $cmd .= " 2>/dev/null"; } - $data = trim(external_exec($cmd)); + if ($mib) $cmd .= " -m $mib"; + + $cmd .= isset($timeout) ? " -t " . $timeout : ""; + $cmd .= isset($retries) ? " -r " . $retries : ""; + + $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid; + + if (!$debug) $cmd .= " 2>/dev/null"; + + $data = trim(external_exec($cmd)); $device_id = $device['device_id']; - foreach (explode("\n", $data) as $entry) - { + foreach (explode("\n", $data) as $entry) { list($oid,$value) = explode("=", $entry, 2); - $oid = trim($oid); $value = trim($value); $value = str_replace("\"", "", $value); + $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)) - { + if (!strstr($value, "at this OID") && isset($oid) && isset($first) && isset($second)) { $array[$first][$second][$oid] = $value; } } @@ -477,35 +389,19 @@ function snmpwalk_cache_twopart_oid($device, $oid, $array, $mib = 0) return $array; } -function snmpwalk_cache_threepart_oid($device, $oid, $array, $mib = 0) -{ +function snmpwalk_cache_threepart_oid($device, $oid, $array, $mib = 0) { global $config, $debug; - if (is_numeric($device['timeout']) && $device['timeout'] > 0) - { - $timeout = $device['timeout']; - } elseif (isset($config['snmp']['timeout'])) { - $timeout = $config['snmp']['timeout']; - } + $timeout = prep_snmp_setting($device, "timeout"); + $retries = prep_snmp_setting($device, "retries"); - if (is_numeric($device['retries']) && $device['retries'] > 0) - { - $retries = $device['retries']; - } elseif (isset($config['snmp']['retries'])) { - $retries = $config['snmp']['retries']; - } - - if (!isset($device['transport'])) - { + if (!isset($device['transport'])) { $device['transport'] = "udp"; } - if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) - { + if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) { $snmpcommand = $config['snmpwalk']; - } - else - { + } else { $snmpcommand = $config['snmpbulkwalk']; } @@ -514,22 +410,28 @@ function snmpwalk_cache_threepart_oid($device, $oid, $array, $mib = 0) $cmd .= " -O QUs"; $cmd .= mibdir(null); - if ($mib) { $cmd .= " -m $mib"; } - if (isset($timeout)) { $cmd .= " -t " . $timeout; } - if (isset($retries)) { $cmd .= " -r " . $retries; } - $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid; - if (!$debug) { $cmd .= " 2>/dev/null"; } - $data = trim(external_exec($cmd)); + if ($mib) $cmd .= " -m $mib"; + + $cmd .= isset($timeout) ? " -t " . $timeout : ""; + $cmd .= isset($retries) ? " -r " . $retries : ""; + + $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid; + + if (!$debug) $cmd .= " 2>/dev/null"; + + $data = trim(external_exec($cmd)); $device_id = $device['device_id']; - foreach (explode("\n", $data) as $entry) - { + foreach (explode("\n", $data) as $entry) { list($oid,$value) = explode("=", $entry, 2); - $oid = trim($oid); $value = trim($value); $value = str_replace("\"", "", $value); + $oid = trim($oid); + $value = trim($value); + $value = str_replace("\"", "", $value); list($oid, $first, $second, $third) = explode(".", $oid); - if ($debug) {echo("$entry || $oid || $first || $second || $third\n"); } - if (!strstr($value, "at this OID") && isset($oid) && isset($first) && isset($second) && isset($third)) - { + + if ($debug) echo("$entry || $oid || $first || $second || $third\n"); + + if (!strstr($value, "at this OID") && isset($oid) && isset($first) && isset($second) && isset($third)) { $array[$first][$second][$third][$oid] = $value; } } @@ -541,31 +443,16 @@ function snmp_cache_slotport_oid($oid, $device, $array, $mib = 0) { global $config; - if (is_numeric($device['timeout']) && $device['timeout'] > 0) - { - $timeout = $device['timeout']; - } elseif (isset($config['snmp']['timeout'])) { - $timeout = $config['snmp']['timeout']; - } + $timeout = prep_snmp_setting($device, "timeout"); + $retries = prep_snmp_setting($device, "retries"); - if (is_numeric($device['retries']) && $device['retries'] > 0) - { - $retries = $device['retries']; - } elseif (isset($config['snmp']['retries'])) { - $retries = $config['snmp']['retries']; - } - - if (!isset($device['transport'])) - { + if (!isset($device['transport'])) { $device['transport'] = "udp"; } - if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) - { + if ($device['snmpver'] == 'v1' || $config['os'][$device['os']]['nobulk']) { $snmpcommand = $config['snmpwalk']; - } - else - { + } else { $snmpcommand = $config['snmpbulkwalk']; } @@ -573,22 +460,24 @@ function snmp_cache_slotport_oid($oid, $device, $array, $mib = 0) $cmd .= snmp_gen_auth ($device); $cmd .= " -O QUs"; - if ($mib) { $cmd .= " -m $mib"; } + if ($mib) $cmd .= " -m $mib"; $cmd .= mibdir(null); - if (isset($timeout)) { $cmd .= " -t " . $timeout; } - if (isset($retries)) { $cmd .= " -r " . $retries; } - $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid; - if (!$debug) { $cmd .= " 2>/dev/null"; } - $data = trim(external_exec($cmd)); + + $cmd .= isset($timeout) ? " -t " . $timeout : ""; + $cmd .= isset($retries) ? " -r " . $retries : ""; + + $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid; + + if (!$debug) $cmd .= " 2>/dev/null"; + + $data = trim(external_exec($cmd)); $device_id = $device['device_id']; - foreach (explode("\n", $data) as $entry) - { + foreach (explode("\n", $data) as $entry) { $entry = str_replace($oid.".", "", $entry); list($slotport, $value) = explode("=", $entry, 2); $slotport = trim($slotport); $value = trim($value); - if ($array[$slotport]['ifIndex']) - { + if ($array[$slotport]['ifIndex']) { $ifIndex = $array[$slotport]['ifIndex']; $array[$ifIndex][$oid] = $value; } @@ -597,37 +486,22 @@ function snmp_cache_slotport_oid($oid, $device, $array, $mib = 0) return $array; } -function snmp_cache_oid($oid, $device, $array, $mib = 0) -{ +function snmp_cache_oid($oid, $device, $array, $mib = 0) { $array = snmpwalk_cache_oid($device, $oid, $array, $mib); return $array; } -function snmp_cache_port_oids($oids, $port, $device, $array, $mib=0) -{ +function snmp_cache_port_oids($oids, $port, $device, $array, $mib=0) { global $config; - if (is_numeric($device['timeout']) && $device['timeout'] > 0) - { - $timeout = $device['timeout']; - } elseif (isset($config['snmp']['timeout'])) { - $timeout = $config['snmp']['timeout']; - } + $timeout = prep_snmp_setting($device, "timeout"); + $retries = prep_snmp_setting($device, "retries"); - if (is_numeric($device['retries']) && $device['retries'] > 0) - { - $retries = $device['retries']; - } elseif (isset($config['snmp']['retries'])) { - $retries = $config['snmp']['retries']; - } - - if (!isset($device['transport'])) - { + if (!isset($device['transport'])) { $device['transport'] = "udp"; } - foreach ($oids as $oid) - { + foreach ($oids as $oid) { $string .= " $oid.$port"; } @@ -635,20 +509,24 @@ function snmp_cache_port_oids($oids, $port, $device, $array, $mib=0) $cmd .= snmp_gen_auth ($device); $cmd .= " -O vq"; - if (isset($timeout)) { $cmd .= " -t " . $timeout; } - if (isset($retries)) { $cmd .= " -r " . $retries; } - $cmd .= mibdir(null); - if ($mib) { $cmd .= " -m $mib"; } - $cmd .= " -t " . $timeout . " -r " . $retries; + + $cmd .= isset($timeout) ? " -t " . $timeout : ""; + $cmd .= isset($retries) ? " -r " . $retries : ""; + + $cmd .= mibdir(null); + if ($mib) $cmd .= " -m $mib"; + + $cmd .= " -t " . $timeout . " -r " . $retries; $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$string; - if (!$debug) { $cmd .= " 2>/dev/null"; } + + if (!$debug) $cmd .= " 2>/dev/null"; + $data = trim(external_exec($cmd)); $x=0; $values = explode("\n", $data); #echo("Caching: ifIndex $port\n"); foreach ($oids as $oid) { - if (!strstr($values[$x], "at this OID")) - { + if (!strstr($values[$x], "at this OID")) { $array[$port][$oid] = $values[$x]; } $x++; @@ -657,26 +535,13 @@ function snmp_cache_port_oids($oids, $port, $device, $array, $mib=0) return $array; } -function snmp_cache_portIfIndex($device, $array) -{ +function snmp_cache_portIfIndex($device, $array) { global $config; - if (is_numeric($device['timeout']) && $device['timeout'] > 0) - { - $timeout = $device['timeout']; - } elseif (isset($config['snmp']['timeout'])) { - $timeout = $config['snmp']['timeout']; - } + $timeout = prep_snmp_setting($device, "timeout"); + $retries = prep_snmp_setting($device, "retries"); - if (is_numeric($device['retries']) && $device['retries'] > 0) - { - $retries = $device['retries']; - } elseif (isset($config['snmp']['retries'])) { - $retries = $config['snmp']['retries']; - } - - if (!isset($device['transport'])) - { + if (!isset($device['transport'])) { $device['transport'] = "udp"; } @@ -685,14 +550,15 @@ function snmp_cache_portIfIndex($device, $array) $cmd .= " -CI -m CISCO-STACK-MIB -O q"; $cmd .= mibdir(null); - if (isset($timeout)) { $cmd .= " -t " . $timeout; } - if (isset($retries)) { $cmd .= " -r " . $retries; } - $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." portIfIndex"; + + $cmd .= isset($timeout) ? " -t " . $timeout : ""; + $cmd .= isset($retries) ? " -r " . $retries : ""; + + $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." portIfIndex"; $output = trim(external_exec($cmd)); $device_id = $device['device_id']; - foreach (explode("\n", $output) as $entry) - { + foreach (explode("\n", $output) as $entry) { $entry = str_replace("CISCO-STACK-MIB::portIfIndex.", "", $entry); list($slotport, $ifIndex) = explode(" ", $entry, 2); if ($slotport && $ifIndex) { @@ -704,26 +570,13 @@ function snmp_cache_portIfIndex($device, $array) return $array; } -function snmp_cache_portName($device, $array) -{ +function snmp_cache_portName($device, $array) { global $config; - if (is_numeric($device['timeout']) && $device['timeout'] > 0) - { - $timeout = $device['timeout']; - } elseif (isset($config['snmp']['timeout'])) { - $timeout = $config['snmp']['timeout']; - } + $timeout = prep_snmp_setting($device, "timeout"); + $retries = prep_snmp_setting($device, "retries"); - if (is_numeric($device['retries']) && $device['retries'] > 0) - { - $retries = $device['retries']; - } elseif (isset($config['snmp']['retries'])) { - $retries = $config['snmp']['retries']; - } - - if (!isset($device['transport'])) - { + if (!isset($device['transport'])) { $device['transport'] = "udp"; } @@ -732,20 +585,20 @@ function snmp_cache_portName($device, $array) $cmd .= " -CI -m CISCO-STACK-MIB -O Qs"; $cmd .= mibdir(null); - if (isset($timeout)) { $cmd .= " -t " . $timeout; } - if (isset($retries)) { $cmd .= " -r " . $retries; } - $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." portName"; + + $cmd .= isset($timeout) ? " -t " . $timeout : ""; + $cmd .= isset($retries) ? " -r " . $retries : ""; + + $cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." portName"; $output = trim(external_exec($cmd)); $device_id = $device['device_id']; #echo("Caching: portName\n"); - foreach (explode("\n", $output) as $entry) - { + foreach (explode("\n", $output) as $entry) { $entry = str_replace("portName.", "", $entry); list($slotport, $portName) = explode("=", $entry, 2); $slotport = trim($slotport); $portName = trim($portName); - if ($array[$slotport]['ifIndex']) - { + if ($array[$slotport]['ifIndex']) { $ifIndex = $array[$slotport]['ifIndex']; $array[$slotport]['portName'] = $portName; $array[$ifIndex]['portName'] = $portName; @@ -755,54 +608,43 @@ function snmp_cache_portName($device, $array) return $array; } -function snmp_gen_auth (&$device) -{ +function snmp_gen_auth (&$device) { global $debug; $cmd = ""; - if ($device['snmpver'] === "v3") - { + if ($device['snmpver'] === "v3") { $cmd = " -v3 -n \"\" -l " . $device['authlevel']; - if ($device['authlevel'] === "noAuthNoPriv") - { + if ($device['authlevel'] === "noAuthNoPriv") { // 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? $cmd .= " -u root"; - } - elseif ($device['authlevel'] === "authNoPriv") - { + } elseif ($device['authlevel'] === "authNoPriv") { $cmd .= " -a " . $device['authalgo']; $cmd .= " -A \"" . $device['authpass'] . "\""; $cmd .= " -u " . $device['authname']; - } - elseif ($device['authlevel'] === "authPriv") - { + } elseif ($device['authlevel'] === "authPriv") { $cmd .= " -a " . $device['authalgo']; $cmd .= " -A \"" . $device['authpass'] . "\""; $cmd .= " -u " . $device['authname']; $cmd .= " -x " . $device['cryptoalgo']; $cmd .= " -X \"" . $device['cryptopass'] . "\""; + } else { + if ($debug) { + print "DEBUG: " . $device['snmpver'] ." : Unsupported SNMPv3 AuthLevel (wtf have you done ?)\n"; + } } - else - { - if ($debug) { print "DEBUG: " . $device['snmpver'] ." : Unsupported SNMPv3 AuthLevel (wtf have you done ?)\n"; } - } - } - elseif ($device['snmpver'] === "v2c" or $device['snmpver'] === "v1") - { + } elseif ($device['snmpver'] === "v2c" or $device['snmpver'] === "v1") { $cmd = " -" . $device['snmpver']; $cmd .= " -c " . $device['community']; - } - else - { - if ($debug) { print "DEBUG: " . $device['snmpver'] ." : Unsupported SNMP Version (wtf have you done ?)\n"; } + } else { + if ($debug) print "DEBUG: " . $device['snmpver'] ." : Unsupported SNMP Version (wtf have you done ?)\n"; } - if ($debug) { print "DEBUG: SNMP Auth options = $cmd\n"; } + if ($debug) print "DEBUG: SNMP Auth options = $cmd\n"; - return $cmd; + return $cmd; } /* @@ -820,8 +662,7 @@ function snmp_gen_auth (&$device) * ::= { iso(1) org(3) dod(6) internet(1) private(4) enterprises(1) ruckusRootMIB(25053) ruckusObjects(1) ruckusZD(2) ruckusZDSystemModule(1) ruckusZDSystemMIB(1) ruckusZDSystemObjects(1) * ruckusZDSystemStats(15) 30 } */ -function snmp_mib_parse($oid, $mib, $module, $mibdir = null) -{ +function snmp_mib_parse($oid, $mib, $module, $mibdir = null) { global $debug; $fulloid = explode(".", $oid); @@ -879,8 +720,7 @@ function snmp_mib_parse($oid, $mib, $module, $mibdir = null) if (isset($result['syntax']) && isset($result['object_type'])) { $result['mib'] = $mib; return $result; - } - else { + } else { return null; } } @@ -898,8 +738,7 @@ function snmp_mib_parse($oid, $mib, $module, $mibdir = null) * .iso.org.dod.internet.private.enterprises.ruckusRootMIB.ruckusObjects.ruckusZD.ruckusZDSystemModule.ruckusZDSystemMIB.ruckusZDSystemObjects.ruckusZDSystemStats.ruckusZDSystemStatsNumSta * ... */ -function snmp_mib_walk($mib, $module, $mibdir = null) -{ +function snmp_mib_walk($mib, $module, $mibdir = null) { $cmd = "snmptranslate -Ts"; $cmd .= mibdir($mibdir); $cmd .= " -m ".$module; @@ -921,8 +760,7 @@ function snmp_mib_walk($mib, $module, $mibdir = null) * @return an array containing all of the mib objects, keyed by object-type; * returns an empty array if something goes wrong. */ -function snmp_mib_load($mib, $module, $mibdir = null) -{ +function snmp_mib_load($mib, $module, $mibdir = null) { $mibs = array(); foreach (snmp_mib_walk($mib, $module, $mibdir) as $obj) { $mibs[$obj['object_type']] = $obj; @@ -937,8 +775,7 @@ function snmp_mib_load($mib, $module, $mibdir = null) * snmptranslate -m all -M mibs .1.3.6.1.4.1.8072.3.2.10 2>/dev/null * NET-SNMP-TC::linux */ -function snmp_translate($oid, $module, $mibdir = null) -{ +function snmp_translate($oid, $module, $mibdir = null) { if ($module !== "all") { $oid = "$module::$oid"; } @@ -970,24 +807,24 @@ function oid_rrd_type($oid, $mibdef) { if (!isset($mibdef[$oid])) { return false; } + switch ($mibdef[$oid]['syntax']) { + case 'OCTET': + case 'IpAddress': + return false; - case 'OCTET': - case 'IpAddress': - return false; + case 'TimeTicks': + // Need to find a way to flag that this should be parsed + //return 'COUNTER'; + return false; - case 'TimeTicks': - // Need to find a way to flag that this should be parsed - //return 'COUNTER'; - return false; - - case 'Counter64': - return 'COUNTER:600:0:U'; - - case 'Unsigned32': - return 'GAUGE:600:U:U'; + case 'Counter64': + return 'COUNTER:600:0:U'; + case 'Unsigned32': + return 'GAUGE:600:U:U'; } + return false; } @@ -997,8 +834,7 @@ function oid_rrd_type($oid, $mibdef) { * Update the database with graph definitions as needed. * We don't include the index in the graph name - that is handled at display time. */ -function tag_graphs($mibname, $oids, $mibdef, &$graphs) -{ +function tag_graphs($mibname, $oids, $mibdef, &$graphs) { foreach ($oids as $index => $array) { foreach ($array as $oid => $val) { $graphname = $mibname."-".$mibdef[$oid]['shortname']; @@ -1010,8 +846,7 @@ function tag_graphs($mibname, $oids, $mibdef, &$graphs) /* * Ensure a graph_type definition exists in the database for the entities in this MIB */ -function update_mib_graph_types($mibname, $oids, $mibdef, $graphs) -{ +function update_mib_graph_types($mibname, $oids, $mibdef, $graphs) { $seengraphs = array(); // Get the list of graphs currently in the database @@ -1046,8 +881,7 @@ function update_mib_graph_types($mibname, $oids, $mibdef, $graphs) /* * Save all of the measurable oids for the device in their own RRDs. */ -function save_mibs($device, $mibname, $oids, $mibdef, &$graphs) -{ +function save_mibs($device, $mibname, $oids, $mibdef, &$graphs) { $usedoids = array(); foreach ($oids as $index => $array) { foreach ($array as $oid => $val) { @@ -1072,8 +906,7 @@ function save_mibs($device, $mibname, $oids, $mibdef, &$graphs) * Take a list of MIB name => module pairs. * Validate MIBs and poll based on the results. */ -function poll_mibs($list, $device, &$graphs) -{ +function poll_mibs($list, $device, &$graphs) { if (!is_dev_attrib_enabled($device, "poll_mib")) { d_echo("MIB module disabled for ".$device['hostname']."\n"); return; @@ -1093,8 +926,7 @@ function poll_mibs($list, $device, &$graphs) $oids = snmpwalk_cache_oid($device, $nam, array(), $mod, null, "-OQUsb"); d_print_r($oids); save_mibs($device, $nam, $oids, $mibdefs[$nam], $graphs); - } - else { + } else { d_echo("MIB: no match for $module::$name\n"); } }