= 0) || (!file_exists($path_snmpbulkwalk)))) { /* make sure snmp* is verbose so we can see what types of data we are getting back */ /* force php to return numeric oid's */ if (function_exists("snmp_set_oid_numeric_print")) { snmp_set_oid_numeric_print(TRUE); } if (function_exists("snmprealwalk")) { $snmp_oid_included = false; } snmp_set_quick_print(0); if ($version == "1") { $temp_array = @snmprealwalk("$hostname:$port", "$community", "$oid", ($timeout * 1000), $retries); }elseif ($version == "2") { $temp_array = @snmp2_real_walk("$hostname:$port", "$community", "$oid", ($timeout * 1000), $retries); }else{ if ($priv_proto == "[None]" || $priv_pass == '') { $proto = "authNoPriv"; $priv_proto = ""; }else{ $proto = "authPriv"; } $temp_array = @snmp3_real_walk("$hostname:$port", "$username", $proto, $auth_proto, "$password", $priv_proto, "$priv_pass", "$oid", ($timeout * 1000), $retries); } if ($temp_array === false) { cacti_log("WARNING: SNMP Walk Timeout for Host:'$hostname', and OID:'$oid'", false); } /* check for bad entries */ if (is_array($temp_array) && sizeof($temp_array)) { foreach($temp_array as $key => $value) { foreach($banned_snmp_strings as $item) { if(strstr($value, $item) != "") { unset($temp_array[$key]); continue 2; } } } } $o = 0; for (@reset($temp_array); $i = @key($temp_array); next($temp_array)) { if ($temp_array[$i] != "NULL") { $snmp_array[$o]["oid"] = preg_replace("/^\./", "", $i); $snmp_array[$o]["value"] = format_snmp_string($temp_array[$i], $snmp_oid_included); } $o++; } }else{ /* ucd/net snmp want the timeout in seconds */ $timeout = ceil($timeout / 1000); if ($version == "1") { $snmp_auth = (read_config_option("snmp_version") == "ucd-snmp") ? snmp_escape_string($community): "-c " . snmp_escape_string($community); /* v1/v2 - community string */ }elseif ($version == "2") { $snmp_auth = (read_config_option("snmp_version") == "ucd-snmp") ? snmp_escape_string($community): "-c " . snmp_escape_string($community); /* v1/v2 - community string */ $version = "2c"; /* ucd/net snmp prefers this over '2' */ }elseif ($version == "3") { if ($priv_proto == "[None]" || $priv_pass == '') { $proto = "authNoPriv"; $priv_proto = ""; }else{ $proto = "authPriv"; } if (strlen($priv_pass)) { $priv_pass = "-X " . snmp_escape_string($priv_pass) . " -x " . snmp_escape_string($priv_proto); }else{ $priv_pass = ""; } if (strlen($context)) { $context = "-n " . snmp_escape_string($context); }else{ $context = ""; } $snmp_auth = trim("-u " . snmp_escape_string($username) . " -l " . snmp_escape_string($proto) . " -a " . snmp_escape_string($auth_proto) . " -A " . snmp_escape_string($password) . " " . $priv_pass . " " . $context); /* v3 - username/password */ } if (read_config_option("snmp_version") == "ucd-snmp") { /* escape the command to be executed and vulnerable parameters * numeric parameters are not subject to command injection * snmp_auth is treated seperately, see above */ $temp_array = exec_into_array(cacti_escapeshellcmd(read_config_option("path_snmpwalk")) . " -v$version -t $timeout -r $retries " . cacti_escapeshellarg($hostname) . ":$port $snmp_auth " . cacti_escapeshellarg($oid)); }else { if (file_exists($path_snmpbulkwalk) && ($version > 1) && ($max_oids > 1)) { $temp_array = exec_into_array(cacti_escapeshellcmd($path_snmpbulkwalk) . " -O Qn $snmp_auth -v $version -t $timeout -r $retries -Cr$max_oids " . cacti_escapeshellarg($hostname) . ":$port " . cacti_escapeshellarg($oid)); }else{ $temp_array = exec_into_array(cacti_escapeshellcmd(read_config_option("path_snmpwalk")) . " -O Qn $snmp_auth -v $version -t $timeout -r $retries " . cacti_escapeshellarg($hostname) . ":$port " . cacti_escapeshellarg($oid)); } } if (substr_count(implode(" ", $temp_array), "Timeout:")) { cacti_log("WARNING: SNMP Walk Timeout for Host:'$hostname', and OID:'$oid'", false); } /* check for bad entries */ if (is_array($temp_array) && sizeof($temp_array)) { foreach($temp_array as $key => $value) { foreach($banned_snmp_strings as $item) { if(strstr($value, $item) != "") { unset($temp_array[$key]); continue 2; } } } } for ($i=0; $i < count($temp_array); $i++) { if ($temp_array[$i] != "NULL") { $snmp_array[$i]["oid"] = trim(preg_replace("/(.*) =.*/", "\\1", $temp_array[$i])); $snmp_array[$i]["value"] = format_snmp_string($temp_array[$i], true); } } } return $snmp_array; } function format_snmp_string($string, $snmp_oid_included) { global $banned_snmp_strings; $string = preg_replace(REGEXP_SNMP_TRIM, "", trim($string)); if (substr($string, 0, 7) == "No Such") { return ""; } if ($snmp_oid_included) { /* strip off all leading junk (the oid and stuff) */ $string_array = explode("=", $string); if (sizeof($string_array) == 1) { /* trim excess first */ $string = trim($string); }else if ((substr($string, 0, 1) == ".") || (strpos($string, "::") !== false)) { /* drop the OID from the array */ array_shift($string_array); $string = trim(implode("=", $string_array)); }else { $string = trim(implode("=", $string_array)); } } /* return the easiest value */ if ($string == "") { return $string; } /* now check for the second most obvious */ if (is_numeric($string)) { return trim($string); } /* remove ALL quotes, and other special delimiters */ $string = str_replace("\"", "", $string); $string = str_replace("'", "", $string); $string = str_replace(">", "", $string); $string = str_replace("<", "", $string); $string = str_replace("\\", "", $string); $string = str_replace("\n", " ", $string); $string = str_replace("\r", " ", $string); /* account for invalid MIB files */ if (substr_count($string, "Wrong Type")) { $string = strrev($string); if ($position = strpos($string, ":")) { $string = trim(strrev(substr($string, 0, $position))); }else{ $string = trim(strrev($string)); } } /* Remove invalid chars */ $k = strlen($string); for ($i=0; $i < $k; $i++) { if ((ord($string[$i]) <= 31) || (ord($string[$i]) >= 127)) { $string[$i] = " "; } } $string = trim($string); if ((substr_count($string, "Hex-STRING:")) || (substr_count($string, "Hex-")) || (substr_count($string, "Hex:"))) { /* strip of the 'Hex-STRING:' */ $string = preg_replace("/Hex-STRING: ?/i", "", $string); $string = preg_replace("/Hex: ?/i", "", $string); $string = preg_replace("/Hex- ?/i", "", $string); $string_array = explode(" ", $string); /* loop through each string character and make ascii */ $string = ""; $hexval = ""; $ishex = false; for ($i=0;($i= 127)) { if ((($i+1) == sizeof($string_array)) && ($string_array[$i] == 0)) { /* do nothing */ }else{ $ishex = true; } } } } if ($ishex) $string = $hexval; }elseif (preg_match("/(hex:\?)?([a-fA-F0-9]{1,2}(:|\s)){5}/i", $string)) { $octet = ""; /* strip off the 'hex:' */ $string = preg_replace("/hex: ?/i", "", $string); /* split the hex on the delimiter */ $octets = preg_split("/\s|:/", $string); /* loop through each octet and format it accordingly */ for ($i=0;($i