function cacti_snmp_walk($hostname, $community, $oid, $version, $username, $password, $auth_proto, $priv_pass, $priv_proto, $context, $port = 161, $timeout = 500, $retries = 0, $max_oids = 10, $method = SNMP_VALUE_LIBRARY, $environ = SNMP_POLLER) { global $config, $banned_snmp_strings; $snmp_oid_included = true; $snmp_auth = ''; $snmp_array = array(); $temp_array = array(); /* determine default retries */ if ($retries == 0 || !is_numeric($retries)) { $retries = read_config_option('snmp_retries'); if ($retries == '') { $retries = 3; } } /* do not attempt to poll invalid combinations */ if ($version == 0 || !is_numeric($version) || !is_numeric($max_oids) || !is_numeric($port) || !is_numeric($retries) || !is_numeric($timeout) || $community == '' && $version != 3) { return array(); } $path_snmpbulkwalk = read_config_option('path_snmpbulkwalk'); if (snmp_get_method($version) == SNMP_METHOD_PHP && (!strlen($context) || $version != 3) && ($version == 1 || version_compare(phpversion(), '5.1') >= 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 */ cacti_oid_numeric_format(); snmp_set_quick_print(0); /* set the output format to numeric */ snmp_set_valueretrieval($method); 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]') { $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 = '-c ' . snmp_escape_string($community); /* v1/v2 - community string */ } elseif ($version == '2') { $snmp_auth = '-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]') { $proto = 'authNoPriv'; $priv_proto = ''; } else { $proto = 'authPriv'; } if (strlen($priv_pass)) { $priv_pass = '******' . 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 (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 cacti_snmp_walk($hostname, $community, $oid, $version, $username, $password, $auth_proto, $priv_pass, $priv_proto, $context, $port = 161, $timeout = 500, $retries = 0, $max_oids = 10, $environ = SNMP_POLLER) { global $config, $banned_snmp_strings; $snmp_oid_included = true; $snmp_auth = ''; $snmp_array = array(); $temp_array = array(); /* determine default retries */ if ($retries == 0 || !is_numeric($retries)) { $retries = read_config_option("snmp_retries"); if ($retries == "") { $retries = 3; } } /* determine default max_oids */ if ($max_oids == 0 || !is_numeric($max_oids)) { $max_oids = read_config_option("max_get_size"); if ($max_oids == "") { $max_oids = 10; } } /* do not attempt to poll invalid combinations */ if ($version == 0 || !is_numeric($version) || !is_numeric($max_oids) || !is_numeric($port) || !is_numeric($retries) || !is_numeric($timeout) || $community == "" && $version != 3) { return array(); } $path_snmpbulkwalk = read_config_option("path_snmpbulkwalk"); if (snmp_get_method($version) == SNMP_METHOD_PHP && (!strlen($context) || $version != 3) && ($version == 1 || version_compare(phpversion(), "5.1") >= 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 = "******" . 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 query_script_host($host_id, $snmp_query_id) { $script_queries = get_data_query_array($snmp_query_id); /* invalid xml check */ if ((!is_array($script_queries)) || (sizeof($script_queries) == 0)) { debug_log_insert("data_query", "Error parsing XML file into an array."); return false; } debug_log_insert("data_query", "XML file parsed ok."); if (isset($script_queries["script_server"])) { $script_queries["script_path"] = "|path_php_binary| -q " . $script_queries["script_path"]; } $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] . " ": "") . $script_queries["arg_index"], $script_queries["script_path"], $host_id); /* fetch specified index at specified OID */ $script_index_array = exec_into_array($script_path); debug_log_insert("data_query", "Executing script for list of indexes '$script_path'"); db_execute("delete from host_snmp_cache where host_id=$host_id and snmp_query_id=$snmp_query_id"); while (list($field_name, $field_array) = each($script_queries["fields"])) { if ($field_array["direction"] == "input") { $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] . " ": "") . $script_queries["arg_query"] . " " . $field_array["query_name"], $script_queries["script_path"], $host_id); $script_data_array = exec_into_array($script_path); debug_log_insert("data_query", "Executing script query '$script_path'"); for ($i=0;($i<sizeof($script_data_array));$i++) { if (preg_match("/(.*)" . preg_quote($script_queries["output_delimeter"]) . "(.*)/", $script_data_array[$i], $matches)) { $script_index = $matches[1]; $field_value = $matches[2]; db_execute("replace into host_snmp_cache (host_id,snmp_query_id,field_name,field_value,snmp_index,oid) values ($host_id,$snmp_query_id,'$field_name','$field_value','$script_index','')"); debug_log_insert("data_query", "Found item [$field_name='$field_value'] index: $script_index"); } } } } return true; }
function cacti_snmp_walk($hostname, $community, $oid, $version, $username, $password, $auth_proto, $priv_pass, $priv_proto, $context, $port = 161, $timeout = 500, $retries = 0, $max_oids = 10, $environ = SNMP_POLLER) { global $config, $banned_snmp_strings; $snmp_oid_included = false; $snmp_auth = ''; $snmp_array = array(); $temp_array = array(); /* determine default retries */ if (($retries == 0) || (!is_numeric($retries))) { $retries = read_config_option("snmp_retries"); if ($retries == "") $retries = 3; } $path_snmpbulkwalk = read_config_option("path_snmpbulkwalk"); if ((snmp_get_method($version) == SNMP_METHOD_PHP) && (!strlen($context) || ($version != 3)) && (($version == 1) || (version_compare(phpversion(), "5.1") >= 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); $snmp_oid_included = true; } 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]") { $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); } /* 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") ? cacti_escapeshellarg($community): "-c " . cacti_escapeshellarg($community); /* v1/v2 - community string */ }elseif ($version == "2") { $snmp_auth = (read_config_option("snmp_version") == "ucd-snmp") ? cacti_escapeshellarg($community): "-c " . cacti_escapeshellarg($community); /* v1/v2 - community string */ $version = "2c"; /* ucd/net snmp prefers this over '2' */ }elseif ($version == "3") { if ($priv_proto == "[None]") { $proto = "authNoPriv"; $priv_proto = ""; }else{ $proto = "authPriv"; } if (strlen($priv_pass)) { $priv_pass = "******" . cacti_escapeshellarg($priv_pass) . " -x " . cacti_escapeshellarg($priv_proto); }else{ $priv_pass = ""; } if (strlen($context)) { $context = "-n " . cacti_escapeshellarg($context); }else{ $context = ""; } $snmp_auth = trim("-u " . cacti_escapeshellarg($username) . " -l " . cacti_escapeshellarg($proto) . " -a " . cacti_escapeshellarg($auth_proto) . " -A " . cacti_escapeshellarg($password) . " " . $priv_pass . " " . $context); /* v3 - username/password */ } if (read_config_option("snmp_version") == "ucd-snmp") { $temp_array = exec_into_array(read_config_option("path_snmpwalk") . " -v$version -t $timeout -r $retries $hostname:$port $snmp_auth $oid"); }else { if (file_exists($path_snmpbulkwalk) && ($version > 1) && ($max_oids > 1)) { $temp_array = exec_into_array($path_snmpbulkwalk . " -O Qn $snmp_auth -v $version -t $timeout -r $retries -Cr$max_oids $hostname:$port $oid"); }else{ $temp_array = exec_into_array(read_config_option("path_snmpwalk") . " -O Qn $snmp_auth -v $version -t $timeout -r $retries $hostname:$port $oid"); } } /* 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 cacti_snmp_walk($hostname, $community, $oid, $version, $username, $password, $port = 161, $timeout = 500, $environ = SNMP_POLLER) { global $config; $snmp_array = array(); $temp_array = array(); /* determine default retries */ $retries = read_config_option("snmp_retries"); if ($retries == "") $retries = 3; if (snmp_get_method($version) == SNMP_METHOD_PHP) { /* make sure snmp* is verbose so we can see what types of data we are getting back */ snmp_set_quick_print(0); if (function_exists("snmp_set_valueretrieval")) { snmp_set_valueretrieval(SNMP_VALUE_PLAIN); } if ($version == "1") { $temp_array = @snmprealwalk("$hostname:$port", $community, $oid, ($timeout * 1000), $retries); } else { $temp_array = @snmp2_real_walk("$hostname:$port", $community, $oid, ($timeout * 1000), $retries); } $o = 0; for (@reset($temp_array); $i = @key($temp_array); next($temp_array)) { $snmp_array[$o]["oid"] = ereg_replace("^\.", "", $i); $snmp_array[$o]["value"] = format_snmp_string($temp_array[$i]); $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_CHARACTER . $community . SNMP_ESCAPE_CHARACTER : "-c " . SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER; /* v1/v2 - community string */ }elseif ($version == "2") { $snmp_auth = (read_config_option("snmp_version") == "ucd-snmp") ? SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER : "-c " . SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER; /* v1/v2 - community string */ $version = "2c"; /* ucd/net snmp prefers this over '2' */ }elseif ($version == "3") { $snmp_auth = "-u $username -l authPriv -a MD5 -A $password -x DES -X $password"; /* v3 - username/password */ } if (read_config_option("snmp_version") == "ucd-snmp") { $temp_array = exec_into_array(read_config_option("path_snmpwalk") . " -v$version -t $timeout -r $retries $hostname:$port $snmp_auth $oid"); }else { $temp_array = exec_into_array(read_config_option("path_snmpwalk") . " -O QfntUe $snmp_auth -v $version -t $timeout -r $retries $hostname:$port $oid"); } if ((sizeof($temp_array) == 0) || (substr_count($temp_array[0], "No Such Object"))) { return array(); } for ($i=0; $i < count($temp_array); $i++) { $snmp_array[$i]["oid"] = trim(ereg_replace("(.*) =.*", "\\1", $temp_array[$i])); $snmp_array[$i]["value"] = format_snmp_string($temp_array[$i]); } } return $snmp_array; }
function cacti_snmp_walk($hostname, $community, $oid, $version, $v3username, $v3password, $v3authproto = "", $v3privpassphrase = "", $v3privproto = "", $port = 161, $timeout = 500, $environ = SNMP_POLLER) { require_once(CACTI_BASE_PATH . "/lib/sys/exec.php"); $snmp_array = array(); $temp_array = array(); /* determine default retries */ $retries = read_config_option("snmp_retries"); if ($retries == "") $retries = 3; /* get rid of quotes in privacy passphrase */ $v3privpassphrase = str_replace("#space#", " ", $v3privpassphrase); if ($v3privproto == "[None]") { $v3privproto = ""; } $path_snmpbulkwalk = read_config_option("path_snmpbulkwalk"); if ((snmp_get_method($version) == SNMP_METHOD_PHP) && (($version == 1) || (version_compare(phpversion(), "5.1") >= 0) || (!file_exists($path_snmpbulkwalk)))) { /* make sure snmp* is verbose so we can see what types of data we are getting back */ snmp_set_quick_print(0); /* force php to return numeric oid's */ if (function_exists("snmp_set_oid_numeric_print")) { snmp_set_oid_numeric_print(1); } if ($version == "1") { $temp_array = @snmprealwalk("$hostname:$port", $community, trim($oid), ($timeout * 1000), $retries); }elseif ($version == "2") { $temp_array = @snmp2_real_walk("$hostname:$port", $community, trim($oid), ($timeout * 1000), $retries); }else{ $temp_array = @snmp3_real_walk("$hostname:$port", $v3username, snmp_get_v3authpriv($v3privproto), $v3authproto, $v3password, $v3privproto, $v3privpassphrase, trim($oid), ($timeout * 1000), $retries); } $o = 0; for (@reset($temp_array); $i = @key($temp_array); next($temp_array)) { $snmp_array[$o]["oid"] = ereg_replace("^\.", "", $i); $snmp_array[$o]["value"] = format_snmp_string($temp_array[$i]); $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_CHARACTER . $community . SNMP_ESCAPE_CHARACTER : "-c " . SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER; /* v1/v2 - community string */ }elseif ($version == "2") { $snmp_auth = (read_config_option("snmp_version") == "ucd-snmp") ? SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER : "-c " . SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER; /* v1/v2 - community string */ $version = "2c"; /* ucd/net snmp prefers this over '2' */ }elseif ($version == "3") { $snmp_auth = "-u $v3username -A $v3password -a $v3authproto -X $v3privpassphrase -x $v3privproto -l " . snmp_get_v3authpriv($v3privproto); /* v3 - username/password/etc... */ } if (read_config_option("snmp_version") == "ucd-snmp") { $temp_array = exec_into_array(read_config_option("path_snmpwalk") . " -v$version -t $timeout -r $retries $hostname:$port $snmp_auth $oid"); }else { if (strlen(trim($path_snmpbulkwalk)) && ($version > 1)) { $temp_array = exec_into_array(read_config_option("path_snmpbulkwalk") . " -O QfntUe $snmp_auth -v $version -t $timeout -r $retries -Cr50 $hostname:$port $oid"); }else{ $temp_array = exec_into_array(read_config_option("path_snmpwalk") . " -O QfntUe $snmp_auth -v $version -t $timeout -r $retries $hostname:$port $oid"); } } if ((sizeof($temp_array) == 0) || (substr_count($temp_array[0], "No Such Object")) || (substr_count($temp_array[0], "No more variables"))) { return array(); } for ($i=0; $i < count($temp_array); $i++) { $snmp_array[$i]["oid"] = trim(ereg_replace("(.*) =.*", "\\1", $temp_array[$i])); $snmp_array[$i]["value"] = format_snmp_string($temp_array[$i]); } } return $snmp_array; }
function query_script_host($host_id, $snmp_query_id) { $script_queries = get_data_query_array($snmp_query_id); /* invalid xml check */ if (!is_array($script_queries) || sizeof($script_queries) == 0) { debug_log_insert("data_query", "Error parsing XML file into an array."); return false; } debug_log_insert("data_query", "XML file parsed ok."); /* are we talking to script server? */ if (isset($script_queries["script_server"])) { $script_queries["script_path"] = "\"|path_php_binary|\" -q " . $script_queries["script_path"]; } if (!verify_index_order($script_queries)) { debug_log_insert("data_query", "Invalid field <index_order>" . $script_queries["index_order"] . "</index_order>"); debug_log_insert("data_query", "Must contain <direction>input</direction> fields only"); return false; } /* provide data for arg_num_indexes, if given */ if (isset($script_queries["arg_num_indexes"])) { $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] . " " : "") . $script_queries["arg_num_indexes"], $script_queries["script_path"], $host_id); /* fetch specified index at specified OID */ $script_num_index_array = exec_into_array($script_path); debug_log_insert("data_query", "Executing script for num of indexes" . " '{$script_path}'"); for ($i = 0; $i < sizeof($script_num_index_array); $i++) { debug_log_insert("data_query", "Found number of indexes: " . $script_num_index_array[$i]); } } else { if (isset($script_queries["script_server"])) { debug_log_insert("data_query", "<arg_num_indexes> missing in XML file, 'Index Count Changed' not supported"); } else { debug_log_insert("data_query", "<arg_num_indexes> missing in XML file, 'Index Count Changed' emulated by counting arg_index entries"); } } /* provide data for index, mandatory */ $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] . " " : "") . $script_queries["arg_index"], $script_queries["script_path"], $host_id); /* fetch specified index */ $script_index_array = exec_into_array($script_path); debug_log_insert("data_query", "Executing script for list of indexes" . " '{$script_path}' " . "Index Count: " . sizeof($script_index_array)); for ($i = 0; $i < sizeof($script_index_array); $i++) { debug_log_insert("data_query", "Found index: " . $script_index_array[$i]); } /* set an array to host all updates */ $output_array = array(); while (list($field_name, $field_array) = each($script_queries["fields"])) { if ($field_array["direction"] == "input") { $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] . " " : "") . $script_queries["arg_query"] . " " . $field_array["query_name"], $script_queries["script_path"], $host_id); $script_data_array = exec_into_array($script_path); debug_log_insert("data_query", "Executing script query '{$script_path}'"); for ($i = 0; $i < sizeof($script_data_array); $i++) { if (preg_match("/(.*)" . preg_quote($script_queries["output_delimeter"]) . "(.*)/", $script_data_array[$i], $matches)) { $script_index = $matches[1]; $field_value = $matches[2]; $output_array[] = data_query_format_record($host_id, $snmp_query_id, $field_name, $field_value, $script_index, ''); debug_log_insert("data_query", "Found item [{$field_name}='{$field_value}'] index: {$script_index}"); } } } } data_query_update_host_cache_from_buffer($host_id, $snmp_query_id, $output_array); return true; }
/** * execute a script query for a given device * @param int $device_id * @param int $snmp_query_id */ function query_script_device($device_id, $snmp_query_id) { $script_queries = get_data_query_array($snmp_query_id); /* invalid xml check */ if ((!is_array($script_queries)) || (sizeof($script_queries) == 0)) { debug_log_insert("data_query", __("Error parsing XML file into an array.")); return false; } debug_log_insert("data_query", __("XML file parsed ok.")); if (isset($script_queries["script_server"])) { $script_queries["script_path"] = "|path_php_binary| -q " . $script_queries["script_path"]; } $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] . " ": "") . $script_queries["arg_index"], $script_queries["script_path"], $device_id); /* fetch specified index at specified OID */ $script_index_array = exec_into_array($script_path); debug_log_insert("data_query", __("Executing script for list of indexes") . " '$script_path'"); /* prepare an output array */ $output_array = array(); while (list($field_name, $field_array) = each($script_queries["fields"])) { if ($field_array["direction"] == "input") { $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] . " ": "") . $script_queries["arg_query"] . " " . $field_array["query_name"], $script_queries["script_path"], $device_id); $script_data_array = exec_into_array($script_path); debug_log_insert("data_query", __("Executing script query") . " '$script_path'"); for ($i=0;($i<sizeof($script_data_array));$i++) { if (preg_match("/(.*)" . preg_quote($script_queries["output_delimeter"]) . "(.*)/", $script_data_array[$i], $matches)) { $script_index = $matches[1]; $field_value = $matches[2]; $output_array[] = data_query_format_record($device_id, $snmp_query_id, $field_name, $field_value, $script_index, ''); debug_log_insert("data_query", __("Found item [%s='%s'] index: %s", $field_name, $field_value, $script_index)); } } } } if (sizeof($output_array)) { data_query_update_device_cache_from_buffer($device_id, $snmp_query_id, $output_array); } return true; }
function cacti_snmp_walk($hostname, $community, $oid, $version, $username, $password, $port = 161, $timeout = 500, $retries = 0, $environ = SNMP_POLLER) { global $config; $snmp_array = array(); $temp_array = array(); /* determine default retries */ if (($retries == 0) || (!is_numeric($retries))) { $retries = read_config_option("snmp_retries"); if ($retries == "") $retries = 3; } $path_snmpbulkwalk = read_config_option("path_snmpbulkwalk"); if ((snmp_get_method($version) == SNMP_METHOD_PHP) && (($version == 1) || (version_compare(phpversion(), "5.1") >= 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); } 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{ $temp_array = @snmp3_real_walk("$hostname:$port", $username, "authNoPriv", "MD5", $password, "", "", $oid, ($timeout * 1000), $retries); } $o = 0; for (@reset($temp_array); $i = @key($temp_array); next($temp_array)) { $snmp_array[$o]["oid"] = ereg_replace("^\.", "", $i); $snmp_array[$o]["value"] = format_snmp_string($temp_array[$i]); $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_CHARACTER . $community . SNMP_ESCAPE_CHARACTER : "-c " . SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER; /* v1/v2 - community string */ }elseif ($version == "2") { $snmp_auth = (read_config_option("snmp_version") == "ucd-snmp") ? SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER : "-c " . SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER; /* v1/v2 - community string */ $version = "2c"; /* ucd/net snmp prefers this over '2' */ }elseif ($version == "3") { $snmp_auth = "-u $username -l authNoPriv -a MD5 -A $password"; /* v3 - username/password */ } if (read_config_option("snmp_version") == "ucd-snmp") { $temp_array = exec_into_array(read_config_option("path_snmpwalk") . " -v$version -t $timeout -r $retries $hostname:$port $snmp_auth $oid"); }else { if (file_exists($path_snmpbulkwalk) && ($version > 1)) { $temp_array = exec_into_array($path_snmpbulkwalk . " -O n $snmp_auth -v $version -t $timeout -r $retries -Cr50 $hostname:$port $oid"); }else{ $temp_array = exec_into_array(read_config_option("path_snmpwalk") . " -O n $snmp_auth -v $version -t $timeout -r $retries $hostname:$port $oid"); } } if ((sizeof($temp_array) == 0) || (substr_count($temp_array[0], "No Such Object")) || (substr_count($temp_array[0], "No more variables"))) { return array(); } for ($i=0; $i < count($temp_array); $i++) { $snmp_array[$i]["oid"] = trim(ereg_replace("(.*) =.*", "\\1", $temp_array[$i])); $snmp_array[$i]["value"] = format_snmp_string($temp_array[$i]); } } return $snmp_array; }
/* snmp; count items */ if (read_config_option('log_verbosity') >= POLLER_VERBOSITY_DEBUG) { cacti_log("Device[{$host_id}] RECACHE DQ[" . $index_item['data_query_id'] . '] OID Count: ' . $index_item['arg1'], $print_data_to_stdout); } $output = sizeof(cacti_snmp_walk($item['hostname'], $item['snmp_community'], $index_item['arg1'], $item['snmp_version'], $item['snmp_username'], $item['snmp_password'], $item['snmp_auth_protocol'], $item['snmp_priv_passphrase'], $item['snmp_priv_protocol'], $item['snmp_context'], $item['snmp_port'], $item['snmp_timeout'], read_config_option('snmp_retries'), SNMP_CMDPHP)); if (read_config_option('log_verbosity') >= POLLER_VERBOSITY_MEDIUM) { cacti_log("Device[{$host_id}] RECACHE DQ[" . $index_item['data_query_id'] . '] OID Count: ' . $index_item['arg1'] . ', output: ' . $output, $print_data_to_stdout); } break; case POLLER_ACTION_SCRIPT_COUNT: /* script (popen); count items */ if (read_config_option('log_verbosity') >= POLLER_VERBOSITY_DEBUG) { cacti_log("Device[{$host_id}] RECACHE DQ[" . $index_item['data_query_id'] . '] Script Count: ' . $index_item['arg1'], $print_data_to_stdout); } /* count items found */ $script_index_array = exec_into_array($index_item['arg1']); $output = sizeof($script_index_array); if (!validate_result($output)) { if (strlen($output) > 20) { $strout = 20; } else { $strout = strlen($output); } cacti_log("Device[{$host_id}] RECACHE DQ[" . $index_item['data_query_id'] . '] Warning: Result from Script not valid. Partial Result: ' . substr($output, 0, $strout), $print_data_to_stdout); } if (read_config_option('log_verbosity') >= POLLER_VERBOSITY_MEDIUM) { cacti_log("Device[{$host_id}] RECACHE DQ[" . $index_item['data_query_id'] . '] Script Count: ' . $index_item['arg1'] . ", output: {$output}", $print_data_to_stdout); } break; case POLLER_ACTION_SCRIPT_PHP_COUNT: /* script (php script server); count items */
function api_data_query_script_execute_field($host_id, $data_query_field_id, $script_path) { require_once(CACTI_BASE_PATH . "/include/data_query/data_query_constants.php"); require_once(CACTI_BASE_PATH . "/lib/data_query/data_query_info.php"); require_once(CACTI_BASE_PATH . "/lib/device/device_info.php"); /* fetch information about the data query field */ $data_query_field = api_data_query_field_get($data_query_field_id); /* query a list of values for the script query field */ $script_field_path = $script_path . " " . DATA_QUERY_SCRIPT_ARG_QUERY . " " . $data_query_field["source"]; debug_log_insert("data_query", sprintf(_("Executing script for list of values '%s'"), $script_field_path)); $script_field_array = exec_into_array($script_field_path); $values_array = array(); for ($i = 0; $i < sizeof($script_field_array); $i++) { /* parse each row into an index -> value pair */ if (preg_match("/(.*):(.*)/", $script_field_array[$i], $matches)) { $data_query_index = $matches[1]; $field_value = $matches[2]; $values_array[$data_query_index] = array("value" => $field_value); } } return $values_array; }
function cacti_snmp_walk($hostname, $community, $oid, $version, $username, $password, $port = 161, $timeout = 500, $environ = SNMP_POLLER) { global $config; $snmp_array = array(); $temp_array = array(); $retries = read_config_option("snmp_retries"); if ($retries == "") $retries = 3; /* always use SNMP version 1 for UI stuff */ if ($environ == SNMP_WEBUI) { $version = "1"; } if (($config["php_snmp_support"] == true) && ($version == "1")) { $temp_array = @snmpwalkoid("$hostname:$port", $community, $oid, ($timeout * 1000), $retries); $o = 0; for (@reset($temp_array); $i = @key($temp_array); next($temp_array)) { $snmp_array[$o]["oid"] = ereg_replace("^\.", "", $i); $snmp_array[$o]["value"] = format_snmp_string($temp_array[$i]); $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_CHARACTER . $community . SNMP_ESCAPE_CHARACTER : "-c " . SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER; /* v1/v2 - community string */ }elseif ($version == "2") { $snmp_auth = (read_config_option("snmp_version") == "ucd-snmp") ? SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER : "-c " . SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER; /* v1/v2 - community string */ $version = "2c"; /* ucd/net snmp prefers this over '2' */ }elseif ($version == "3") { $snmp_auth = "-u $username -X $password"; /* v3 - username/password */ } if (read_config_option("snmp_version") == "ucd-snmp") { $temp_array = exec_into_array(read_config_option("path_snmpwalk") . " -v$version -t $timeout -r $retries $hostname:$port $snmp_auth $oid"); }elseif (read_config_option("snmp_version") == "net-snmp") { $temp_array = exec_into_array(read_config_option("path_snmpwalk") . " $snmp_auth -v $version -t $timeout -r $retries $hostname:$port $oid"); } if (sizeof($temp_array) == 0) { return 0; } for ($i=0; $i < count($temp_array); $i++) { $snmp_array[$i]["oid"] = trim(ereg_replace("(.*) =.*", "\\1", $temp_array[$i])); $snmp_array[$i]["value"] = format_snmp_string($temp_array[$i]); } } return $snmp_array; }
/* snmp; count items */ if (read_config_option("log_verbosity") >= POLLER_VERBOSITY_DEBUG) { cacti_log("Host[{$host_id}] RECACHE DQ[" . $index_item["data_query_id"] . "] OID Count: " . $index_item["arg1"], $print_data_to_stdout); } $output = sizeof(cacti_snmp_walk($item["hostname"], $item["snmp_community"], $index_item["arg1"], $item["snmp_version"], $item["snmp_username"], $item["snmp_password"], $item["snmp_auth_protocol"], $item["snmp_priv_passphrase"], $item["snmp_priv_protocol"], $item["snmp_context"], $item["snmp_port"], $item["snmp_timeout"], read_config_option("snmp_retries"), SNMP_CMDPHP)); if (read_config_option("log_verbosity") >= POLLER_VERBOSITY_MEDIUM) { cacti_log("Host[{$host_id}] RECACHE DQ[" . $index_item["data_query_id"] . "] OID Count: " . $index_item["arg1"] . ", output: " . $output, $print_data_to_stdout); } break; case POLLER_ACTION_SCRIPT_COUNT: /* script (popen); count items */ if (read_config_option("log_verbosity") >= POLLER_VERBOSITY_DEBUG) { cacti_log("Host[{$host_id}] RECACHE DQ[" . $index_item["data_query_id"] . "] Script Count: " . $index_item["arg1"], $print_data_to_stdout); } /* count items found */ $script_index_array = exec_into_array($index_item["arg1"]); $output = sizeof($script_index_array); if (!validate_result($output)) { if (strlen($output) > 20) { $strout = 20; } else { $strout = strlen($output); } cacti_log("Host[{$host_id}] RECACHE DQ[" . $index_item["data_query_id"] . "] Warning: Result from Script not valid. Partial Result: " . substr($output, 0, $strout), $print_data_to_stdout); } if (read_config_option("log_verbosity") >= POLLER_VERBOSITY_MEDIUM) { cacti_log("Host[{$host_id}] RECACHE DQ[" . $index_item["data_query_id"] . "] Script Count: " . $index_item["arg1"] . ", output: {$output}", $print_data_to_stdout); } break; case POLLER_ACTION_SCRIPT_PHP_COUNT: /* script (php script server); count items */
function cacti_snmp_walk($hostname, $community, $oid, $version, $username, $password, $auth_proto, $priv_pass, $priv_proto, $port = 161, $timeout = 500, $retries = 0, $environ = SNMP_POLLER) { global $config; $snmp_array = array(); $temp_array = array(); /* determine default retries */ if ($retries == 0 || !is_numeric($retries)) { $retries = read_config_option("snmp_retries"); if ($retries == "") { $retries = 3; } } $path_snmpbulkwalk = read_config_option("path_snmpbulkwalk"); if (snmp_get_method($version) == SNMP_METHOD_PHP && ($version == 1 || version_compare(phpversion(), "5.1") >= 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); } 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 ($auth_proto == "[None]") { $proto = "authNoPriv"; } else { $proto = "authPriv"; } $temp_array = @snmp3_real_walk("{$hostname}:{$port}", $username, $proto, $auth_proto, "{$password}", "{$priv_pass}", $priv_proto, "{$oid}", $timeout * 1000, $retries); } $o = 0; for (@reset($temp_array); $i = @key($temp_array); next($temp_array)) { $snmp_array[$o]["oid"] = ereg_replace("^\\.", "", $i); $snmp_array[$o]["value"] = format_snmp_string($temp_array[$i]); $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_CHARACTER . $community . SNMP_ESCAPE_CHARACTER : "-c " . SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER; /* v1/v2 - community string */ } elseif ($version == "2") { $snmp_auth = read_config_option("snmp_version") == "ucd-snmp" ? SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER : "-c " . SNMP_ESCAPE_CHARACTER . $community . SNMP_ESCAPE_CHARACTER; /* v1/v2 - community string */ $version = "2c"; /* ucd/net snmp prefers this over '2' */ } elseif ($version == "3") { if ($auth_proto == "[None]") { $proto = "authNoPriv"; } else { $proto = "authPriv"; } if (strlen($priv_pass)) { $priv_pass = "******"{$priv_pass}\" -x {$priv_proto}"; } else { $priv_pass = ""; } $snmp_auth = "-u {$username} -l {$proto} -a {$auth_proto} -A {$password} {$priv_pass}"; /* v3 - username/password */ } if (read_config_option("snmp_version") == "ucd-snmp") { $temp_array = exec_into_array(read_config_option("path_snmpwalk") . " -v{$version} -t {$timeout} -r {$retries} {$hostname}:{$port} {$snmp_auth} {$oid}"); } else { if (file_exists($path_snmpbulkwalk) && $version > 1) { $temp_array = exec_into_array($path_snmpbulkwalk . " -O Qn {$snmp_auth} -v {$version} -t {$timeout} -r {$retries} -Cr50 {$hostname}:{$port} {$oid}"); } else { $temp_array = exec_into_array(read_config_option("path_snmpwalk") . " -O Qn {$snmp_auth} -v {$version} -t {$timeout} -r {$retries} {$hostname}:{$port} {$oid}"); } } if (sizeof($temp_array) == 0 || substr_count($temp_array[0], "No Such Object") || substr_count($temp_array[0], "No more variables") || substr_count($temp_array[0], "End of MIB") || substr_count($temp_array[0], "Wrong Type")) { return array(); } for ($i = 0; $i < count($temp_array); $i++) { $snmp_array[$i]["oid"] = trim(ereg_replace("(.*) =.*", "\\1", $temp_array[$i])); $snmp_array[$i]["value"] = format_snmp_string($temp_array[$i]); } } return $snmp_array; }