function Walk($ip, $ver, $cm, $oid, $t = 1000000, $r = 2) { global $debug, $comms; if ($ver == 3 and $comms[$cm]['pprot']) { if ($debug) { echo "<div class=\"textpad noti \">snmpwalk -v3 -c{$cm} " . $comms[$cm]['aprot'] . "/" . $comms[$cm]['pprot'] . " {$ip} {$oid} ({$t} usec * {$r})</div>"; } return snmp3_real_walk($ip, $cm, 'authPriv', $comms[$cm]['aprot'], $comms[$cm]['apass'], $comms[$cm]['pprot'], $comms[$cm]['ppass'], ".{$oid}", $t); } elseif ($ver == 3 and $comms[$cm]['aprot']) { if ($debug) { echo "<div class=\"textpad noti \">snmpwalk -v3 -c{$cm} " . $comms[$cm]['aprot'] . " {$ip} {$oid} ({$t} usec * {$r})</div>"; } return snmp3_real_walk($ip, $cm, 'authNoPriv', $comms[$cm]['aprot'], $comms[$cm]['apass'], 'DES', '', ".{$oid}", $t); } elseif ($ver == 2) { if ($debug) { echo "<div class=\"textpad noti \">snmpwalk -v2c -c{$cm} {$ip} {$oid} ({$t} usec * {$r})</div>"; } return snmp2_real_walk($ip, $cm, ".{$oid}", $t); } else { if ($debug) { echo "<div class=\"textpad noti \">snmpwalk -v1 -c{$cm} {$ip} {$oid} ({$t} usec * {$r})</div>"; } return snmprealwalk($ip, $cm, ".{$oid}", $t); } }
static function walk($host, $object_id, $cache_ttl = null) { if ($cache_ttl) { $cache_var = str_replace('.', '_', $host->ip . $object_id); $cache = new CacheAPC(); $resultCache = $cache->load($cache_var); if ($resultCache !== null) { return $resultCache; } } snmp_set_oid_output_format(self::$oid_format); $snmp = $host->snmpTemplate; if ($snmp instanceof SnmpTemplate) { switch ($snmp->version) { case "1": $result = @snmprealwalk($host->ip, $snmp->community, $object_id, $snmp->timeout, $snmp->retries); break; case "2": case "2c": $result = @snmp2_real_walk($host->ip, $snmp->community, $object_id, $snmp->timeout, $snmp->retries); break; case "3": $result = @snmp3_real_walk($host->ip, $snmp->security_name, $snmp->security_level, $snmp->auth_protocol, $snmp->auth_passphrase, $snmp->priv_protocol, $snmp->priv_passphrase, $object_id, $snmp->timeout, $snmp->retries); break; default: throw new Exception('SNMP Template not implemented yet'); } } if (is_array($result)) { if ($cache_var && $cache_ttl) { $cache->save($cache_var, $result, $cache_ttl); } return $result; } else { //throw new Exception("Sem resposta SNMP"); return array(); } }
<?php // copyright license/LICENSE //SEND MODIFYING STATE COMMAND include "outlet_values.php"; //DYNAMIC VALUES //$num = 1; //outlet port # (1-8) $num = $_REQUEST['outletnum']; //echo $num; //echo vs return seem to change whether it returns once or twice, also return fails to pass anything but "" //if ($num) { echo 1; } else { echo 0;} //$num = 8; $op = "." . $num; //outlet port # (1-8) //$cmd = 3; //state change: 1 immediateon, 2 immediateoff, 3 reboot $cmd = $_REQUEST['commandnum']; $oid = $pre . $det . $op; //FUNCTIONS //READ STATUS $status = snmp3_real_walk($ip, $sec_name, $sec_level, $auth_protocol, $auth_passphrase, $priv_protocol, $priv_passphrase, $oid); //CHANGE STATE $result = snmpset($ip, $comm, $oid, 'i', $cmd); //true/false or 1/0? //RETURN VALUES //echo "Outlet: ".$num." was ".$result." turned ".$cmd; //return $result; echo $result;
function walk($oid, $suffix_as_key = FALSE) { switch ($this->version) { case self::VERSION_1: if ($suffix_as_key) { $this->result = snmpwalk($this->host, $this->community, $oid); } else { $this->result = snmprealwalk($this->host, $this->community, $oid); } break; case self::VERSION_2C: case self::VERSION_2c: if ($suffix_as_key) { $this->result = snmp2_walk($this->host, $this->community, $oid); } else { $this->result = snmp2_real_walk($this->host, $this->community, $oid); } break; case self::VERSION_3: if ($suffix_as_key) { $this->result = snmp3_walk($this->host, $this->community, $this->sec_level, $this->auth_protocol, $this->auth_passphrase, $this->priv_protocol, $this->priv_passphrase, $oid); } else { $this->result = snmp3_real_walk($this->host, $this->community, $this->sec_level, $this->auth_protocol, $this->auth_passphrase, $this->priv_protocol, $this->priv_passphrase, $oid); } break; } return $this->result; }
$z = snmp3_walk($hostname, $username, 'authNoPriv', 'MD5', $auth_pass, '', '', '.1.3.6.1.2.1.1.1.0', $timeout, $retries); var_dump(gettype($z)); var_dump(count($z)); var_dump($z); echo "REALWALK single on single OID\n"; $z = snmp3_real_walk($hostname, $username, 'authNoPriv', 'MD5', $auth_pass, '', '', '.1.3.6.1.2.1.1.1.0', $timeout, $retries); var_dump(gettype($z)); var_dump(count($z)); var_dump($z); echo "WALK multiple on single OID\n"; $z = snmp3_walk($hostname, $username, 'authNoPriv', 'MD5', $auth_pass, '', '', '.1.3.6.1.2.1.1', $timeout, $retries); var_dump(gettype($z)); var_dump(count($z)); var_dump(key($z)); var_dump(array_shift($z)); echo "REALWALK multiple on single OID\n"; $z = snmp3_real_walk($hostname, $username, 'authNoPriv', 'MD5', $auth_pass, 'AES', '', '.1.3.6.1.2.1.1', $timeout, $retries); var_dump(gettype($z)); var_dump(count($z)); var_dump(key($z)); var_dump(array_shift($z)); echo "SET single OID\n"; $oid1 = 'SNMPv2-MIB::sysContact.0'; $oldvalue1 = snmp3_get($hostname, $rwuser, 'authPriv', 'MD5', $auth_pass, 'AES', $priv_pass, $oid1, $timeout, $retries); $newvalue1 = $oldvalue1 . '0'; $z = snmp3_set($hostname, $rwuser, 'authPriv', 'MD5', $auth_pass, 'AES', $priv_pass, $oid1, 's', $newvalue1, $timeout, $retries); var_dump($z); var_dump(snmp3_get($hostname, $rwuser, 'authPriv', 'MD5', $auth_pass, 'AES', $priv_pass, $oid1, $timeout, $retries) === $newvalue1); $z = snmp3_set($hostname, $rwuser, 'authPriv', 'MD5', $auth_pass, 'AES', $priv_pass, $oid1, 's', $oldvalue1, $timeout, $retries); var_dump($z); var_dump(snmp3_get($hostname, $rwuser, 'authPriv', 'MD5', $auth_pass, 'AES', $priv_pass, $oid1, $timeout, $retries) === $oldvalue1);
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 snmpwalkoid($oid) { return snmp3_real_walk($this->hostname, $this->snmpsetup['sec_name'], $this->snmpsetup['sec_level'], $this->snmpsetup['auth_protocol'], $this->snmpsetup['auth_passphrase'], $this->snmpsetup['priv_protocol'], $this->snmpsetup['priv_passphrase'], $oid); }
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; }
protected function snmpget($oid, $version = null, $community = null) { if (!$version) { $version = $this->getAgentVersion(); } if (!$community) { $community = $this->getAgentReadCommunity(); } switch ($version) { case 1: return @snmprealwalk($this->host, $community, $oid, $this->timeout * 1000000, $this->retries); case 2: return @snmp2_real_walk($this->host, $community, $oid, $this->timeout * 1000000, $this->retries); case 3: extract($this->version3); return @snmp3_real_walk($this->host, $secName, $secLevel, $authProtocol, $authPassphrase, $privProtocol, $privPassphrase, $oid, $this->timeout * 1000000, $this->retries); default: return false; } }
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; }
function my_snmp_real_walk($ip, $credentials, $oid) { $timeout = '30000000'; $retries = '0'; switch ($credentials->credentials->version) { case '1': $array = @snmprealwalk($ip, $credentials->credentials->community, $oid, $timeout, $retries); break; case '2': $array = @snmp2_real_walk($ip, $credentials->credentials->community, $oid, $timeout, $retries); break; case '3': $sec_name = $credentials->credentials->security_name ?: ''; $sec_level = $credentials->credentials->security_level ?: ''; $auth_protocol = $credentials->credentials->authentication_protocol ?: ''; $auth_passphrase = $credentials->credentials->authentication_passphrase ?: ''; $priv_protocol = $credentials->credentials->privacy_protocol ?: ''; $priv_passphrase = $credentials->credentials->privacy_passphrase ?: ''; $array = @snmp3_real_walk($ip, $sec_name, $sec_level, $auth_protocol, $auth_passphrase, $priv_protocol, $priv_passphrase, $oid, $timeout, $retries); break; default: return false; break; } if (!is_array($array)) { return false; } foreach ($array as $i => $value) { $array[$i] = trim($array[$i]); if ($array[$i] == '""') { $array[$i] = ''; } if (strpos($array[$i], '.') === 0) { $array[$i] = substr($array[$i], 1); } // remove the first and last characters if they are " if (substr($array[$i], 0, 1) == "\"") { $array[$i] = substr($array[$i], 1, strlen($array[$i])); } if (substr($array[$i], -1, 1) == "\"") { $array[$i] = substr($array[$i], 0, strlen($array[$i]) - 1); } // remove some return strings if (strpos(strtolower($array[$i]), '/etc/snmp') !== false or strpos(strtolower($array[$i]), 'no such instance') !== false or strpos(strtolower($array[$i]), 'no such object') !== false or strpos(strtolower($array[$i]), 'not set') !== false or strpos(strtolower($array[$i]), 'unknown value type') !== false) { $array[$i] = ''; } // remove any quotation marks $array[$i] = str_replace('"', ' ', $array[$i]); // replace any line breaks with spaces $array[$i] = str_replace(array("\r", "\n"), " ", $array[$i]); } return $array; }
/** * 获取操作系统某一个snmp对象的数据 * @param $objectid 欲获取的snmp对象(不填则获取全部) */ public function get($objectid = null) { switch ($this->version) { case 3: $snmp = snmp3_real_walk($this->host, $this->sec_name, $this->sec_level, $this->auth_protocol, $this->auth_passphrase, $this->priv_protocol, $this->priv_passphrase, $objectid); break; } return $snmp; }
/** * Proxy to the snmp2_real_walk command * * @param string $oid The OID to walk * @return array The results of the walk */ public function realWalk($oid) { $v1 = 'snmprealwalk'; $v2c = 'snmp2_real_walk'; switch ($this->getVersion()) { case 1: case '2c': return $this->_lastResult = @${"v" . $this->getVersion()}($this->getHost(), $this->getCommunity(), $oid, $this->getTimeout(), $this->getRetry()); break; case '3': return $this->_lastResult = @snmp3_real_walk($this->getHost(), $this->getSecName(), $this->getSecLevel(), $this->getAuthProtocol(), $this->getAuthPassphrase(), $this->getPrivProtocol(), $this->getPrivPassphrase(), $oid, $this->getTimeout(), $this->getRetry()); break; default: throw new Exception('Invalid SNMP version: ' . $this->getVersion()); } }
$timestamp_human = date("d-M-Y H:i:s", $timestamp); echo $message = "ALERT {$hostname} - Disk Utilization Threshold breached at {$timestamp_human}"; $subject = $message; ticket_post($smtp_email, $smtp_email, "28", "{$subject}", "{$message}", '1'); } } } } else { if ("Memory Utilization" == $param_name) { //end of else if dsk utilization snmp_set_quick_print(1); $snmp_result = ''; $memInfo = "hrStorageDescr"; if (strtolower($snmp_version) == 'v3') { //SNMP V3 is used $snmp_result = @snmp3_real_walk($hostname, $v3_user, "authNoPriv", "MD5", $v3_pwd, "DES", "", $memInfo, $timeout, $count); } else { $snmp_result = @snmprealwalk($hostname, $community_string, $memInfo, $timeout, $count); } if (count($snmp_result) < 1) { echo "Unable to query server {$hostname} for Memory Utilization"; } $mem_pct_used = 0; foreach ($snmp_result as $oid => $val) { if ("/" == substr($val, 0, 1)) { //ignore } else { $index = substr($oid, strrpos($oid, ".") + 1); $used = ''; $total = ''; if (strtolower($snmp_version) == 'v3') {
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 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, $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; }