function cacti_snmp_get($hostname, $community, $oid, $version, $username, $password, $port = 161, $timeout = 500, $retries = 0, $environ = SNMP_POLLER) { global $config; /* 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 || $community == "" && $version != 3) { return "U"; } 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 ($version == "1") { $snmp_value = @snmpget("{$hostname}:{$port}", "{$community}", "{$oid}", $timeout * 1000, $retries); } elseif ($version == "2") { $snmp_value = @snmp2_get("{$hostname}:{$port}", "{$community}", "{$oid}", $timeout * 1000, $retries); } else { $snmp_value = @snmp3_get("{$hostname}:{$port}", $username, "authNoPriv", "MD5", $password, "", "", $oid, $timeout * 1000, $retries); } } 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 */ } /* no valid snmp version has been set, get out */ if (empty($snmp_auth)) { return; } if (read_config_option("snmp_version") == "ucd-snmp") { exec(read_config_option("path_snmpget") . " -O vt -v{$version} -t {$timeout} -r {$retries} {$hostname}:{$port} {$snmp_auth} {$oid}", $snmp_value); } else { exec(read_config_option("path_snmpget") . " -O fntev {$snmp_auth} -v {$version} -t {$timeout} -r {$retries} {$hostname}:{$port} {$oid}", $snmp_value); } } if (isset($snmp_value)) { /* fix for multi-line snmp output */ if (is_array($snmp_value)) { $snmp_value = implode(" ", $snmp_value); } } /* strip out non-snmp data */ $snmp_value = format_snmp_string($snmp_value); return $snmp_value; }
function Get($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 \">snmpget -v3 -c{$cm} " . $comms[$cm]['aprot'] . "/" . $comms[$cm]['pprot'] . " {$ip} {$oid} ({$t} usec * {$r})</div>"; } return snmp3_get($ip, $cm, 'authPriv', $comms[$cm]['aprot'], $comms[$cm]['apass'], $comms[$cm]['pprot'], $comms[$cm]['ppass'], ".{$oid}", $t, $r); } elseif ($ver == 3 and $comms[$cm]['aprot']) { if ($debug) { echo "<div class=\"textpad noti \">snmpget -v3 -c{$cm} " . $comms[$cm]['aprot'] . " {$ip} {$oid} ({$t} usec * {$r})</div>"; } return snmp3_get($ip, $cm, 'authNoPriv', $comms[$cm]['aprot'], $comms[$cm]['apass'], 'DES', '', ".{$oid}", $t, $r); } elseif ($ver == 2) { if ($debug) { echo "<div class=\"textpad noti \">snmpget -v2c -c{$cm} {$ip} {$oid} ({$t} usec * {$r})</div>"; } return snmp2_get($ip, $cm, ".{$oid}", $t, $r); } else { if ($debug) { echo "<div class=\"textpad noti \">snmpget -v1 -c{$cm} {$ip} {$oid} ({$t} usec * {$r})</div>"; } return snmpget($ip, $cm, ".{$oid}", $t, $r); } }
static function get($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 = @snmpget($host->ip, $snmp->community, $object_id, $snmp->timeout, $snmp->retries); break; case "2": case "2c": $result = @snmp2_get($host->ip, $snmp->community, $object_id, $snmp->timeout, $snmp->retries); break; case "3": $result = @snmp3_get($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 ($result) { // retira 'STRING: ' do inicio do texto $result = trim(preg_replace('/^[^:]+: ?/', '', $result)); if ($cache_var && $cache_ttl) { $cache->save($cache_var, $result, $cache_ttl); } return $result; } else { //throw new Exception("Sem resposta SNMP"); return null; } }
private function __snmpget($object_id) { $retval = FALSE; switch ($this->version) { case self::VERSION_1: $retval = snmpget($this->host, $this->community, $object_id); break; case self::VERSION_2C: case self::VERSION_2c: $retval = snmp2_get($this->host, $this->community, $object_id); break; case self::VERSION_3: $retval = snmp3_get($this->host, $this->community, $this->sec_level, $this->auth_protocol, $this->auth_passphrase, $this->priv_protocol, $this->priv_passphrase, $object_id); break; } return $retval; }
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_get($hostname, $community, $oid, $version, $username, $password, $auth_proto, $priv_pass, $priv_proto, $context, $port = 161, $timeout = 500, $retries = 0, $environ = SNMP_POLLER) { global $config; /* 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($port) || !is_numeric($retries) || !is_numeric($timeout) || $community == "" && $version != 3) { return "U"; } if (snmp_get_method($version) == SNMP_METHOD_PHP && (!strlen($context) || $version != 3)) { /* make sure snmp* is verbose so we can see what types of data we are getting back */ snmp_set_quick_print(0); if ($version == "1") { $snmp_value = @snmpget("{$hostname}:{$port}", "{$community}", "{$oid}", $timeout * 1000, $retries); } elseif ($version == "2") { $snmp_value = @snmp2_get("{$hostname}:{$port}", "{$community}", "{$oid}", $timeout * 1000, $retries); } else { if ($priv_proto == "[None]" || $priv_pass == '') { $proto = "authNoPriv"; $priv_proto = ""; } else { $proto = "authPriv"; } $snmp_value = @snmp3_get("{$hostname}:{$port}", "{$username}", $proto, $auth_proto, "{$password}", $priv_proto, "{$priv_pass}", "{$oid}", $timeout * 1000, $retries); } if ($snmp_value === false) { cacti_log("WARNING: SNMP Get Timeout for Host:'{$hostname}', and OID:'{$oid}'", false); } } else { $snmp_value = ''; /* 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 */ } /* no valid snmp version has been set, get out */ if (empty($snmp_auth)) { return; } 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 */ exec(cacti_escapeshellcmd(read_config_option("path_snmpget")) . " -O vt -v{$version} -t {$timeout} -r {$retries} " . cacti_escapeshellarg($hostname) . ":{$port} {$snmp_auth} " . cacti_escapeshellarg($oid), $snmp_value); } else { exec(cacti_escapeshellcmd(read_config_option("path_snmpget")) . " -O fntev " . $snmp_auth . " -v {$version} -t {$timeout} -r {$retries} " . cacti_escapeshellarg($hostname) . ":{$port} " . cacti_escapeshellarg($oid), $snmp_value); } /* fix for multi-line snmp output */ if (is_array($snmp_value)) { $snmp_value = implode(" ", $snmp_value); } } /* fix for multi-line snmp output */ if (isset($snmp_value)) { if (is_array($snmp_value)) { $snmp_value = implode(" ", $snmp_value); } } if (substr_count($snmp_value, "Timeout:")) { cacti_log("WARNING: SNMP Get Timeout for Host:'{$hostname}', and OID:'{$oid}'", false); } /* strip out non-snmp data */ $snmp_value = format_snmp_string($snmp_value, false); return $snmp_value; }
$this_MemPhysSize = snmp3_get('localhost', $snmpUser, 'authPriv', 'MD5', $snmpPass, 'DES', $snmpPass, "HOST-RESOURCES-MIB::hrStorageSize.1"); $this_MemPhysUsed = snmp3_get('localhost', $snmpUser, 'authPriv', 'MD5', $snmpPass, 'DES', $snmpPass, "HOST-RESOURCES-MIB::hrStorageUsed.1"); $this_MemBuffUsed = snmp3_get('localhost', $snmpUser, 'authPriv', 'MD5', $snmpPass, 'DES', $snmpPass, "HOST-RESOURCES-MIB::hrStorageUsed.6"); $this_MemCachUsed = snmp3_get('localhost', $snmpUser, 'authPriv', 'MD5', $snmpPass, 'DES', $snmpPass, "HOST-RESOURCES-MIB::hrStorageUsed.7"); $this_MemPhysSize = preg_replace('/INTEGER\\: /', '', $this_MemPhysSize); $this_MemPhysUsed = preg_replace('/INTEGER\\: /', '', $this_MemPhysUsed); $this_MemBuffUsed = preg_replace('/INTEGER\\: /', '', $this_MemBuffUsed); $this_MemCachUsed = preg_replace('/INTEGER\\: /', '', $this_MemCachUsed); $UsedMemory = $this_MemPhysUsed - ($this_MemBuffUsed + $this_MemCachUsed); $thisColor = AutoColorScale($UsedMemory, $this_MemPhysSize * 0.6, 0, NULL); echo "<button class='UButton' style='background-color: " . $thisColor . ";'>Mem Use<br/>" . round($UsedMemory / 1024 / 1024, 2) . " GB</button>"; if (isset($_SESSION['eth0LastIn'])) { $this_eth0in = snmp3_get('localhost', $snmpUser, 'authPriv', 'MD5', $snmpPass, 'DES', $snmpPass, "IF-MIB::ifInOctets.2"); $this_eth0in = preg_replace('/Counter32\\: /', '', $this_eth0in); $this_eth0in_diff = $this_eth0in - $_SESSION['eth0LastIn']; $thisColor = AutoColorScale($this_eth0in_diff, 4096000, -10000, NULL); echo "<button class='UButton' style='background-color: " . $thisColor . ";'>eth0 Rx<br/>" . round($this_eth0in_diff / 1024 / 5, 1) . " KB</button>"; $_SESSION['eth0LastIn'] = $this_eth0in; } else { $_SESSION['eth0LastIn'] = 0; } if (isset($_SESSION['eth0LastOut'])) { $this_eth0out = snmp3_get('localhost', $snmpUser, 'authPriv', 'MD5', $snmpPass, 'DES', $snmpPass, "IF-MIB::ifOutOctets.2"); $this_eth0out = preg_replace('/Counter32\\: /', '', $this_eth0out); $this_eth0out_diff = $this_eth0out - $_SESSION['eth0LastOut']; $thisColor = AutoColorScale($this_eth0out_diff, 4096000, -10000, NULL); echo "<button class='UButton' style='background-color: " . $thisColor . ";'>eth0 Tx<br/>" . round($this_eth0out_diff / 1024 / 5, 1) . " KB</button>"; $_SESSION['eth0LastOut'] = $this_eth0out; } else { $_SESSION['eth0LastOut'] = 0; }
$snmp_port = 161; $snmp_timeout = 500; $snmp_retries = 3; $max_oids = 1; # required for SNMP V3 $snmp_auth_username = "******"; $auth_password = "******"; $auth_protocol = "SHA"; $priv_passphrase = "cisco123"; $priv_protocol = "AES"; $snmp_context = ""; $sec_level = "AuthPriv"; $host_address = "ipv6:[" . $node_ip . "]"; $node_type = @snmp3_get($host_address, $snmp_auth_username, $sec_level, $auth_protocol, $auth_password, $priv_protocol, $priv_passphrase, $oids['node_type'], $snmp_timeout * 1000, $snmp_retries); $node_type = format_snmp_string($node_type, $oids['node_type']); $node_info = @snmp3_get($host_address, $snmp_auth_username, $sec_level, $auth_protocol, $auth_password, $priv_protocol, $priv_passphrase, $oids['node_info'], $snmp_timeout * 1000, $snmp_retries); $node_info = format_snmp_string($node_info, $oids['node_info']); $node_info = str_replace("%", "<br/>", $node_info); if (empty($node_type) || empty($node_ip) || empty($node_lat) || empty($node_lon)) { echo "Failed to add node"; } else { mysql_select_db($cacti_db, $cacti_con); exec(" php -q add_cacti.php --host=" . $host_address . " --template=9 --desc=node" . $fabfi_number); $cacti_index = mysql_fetch_array(mysql_query("select `id` from `cacti`.`host` where `hostname`='" . $host_address . "'")); $cacti_index = $cacti_index["id"]; exec("php -q /usr/share/cacti/cli/add_tree.php --type=node --node-type=host --tree-id=1 --host-id=" . $cacti_index . " --host-group-style=1"); mysql_select_db($map_db, $con); mysql_query("INSERT INTO `meshmib`.`node` (`fabfi_number`,`ipv6_address`,`type`,`latitude`,`longitude`,`cacti_index`,`node_info`,`timestamp`) VALUES ('" . $fabfi_number . "','" . $node_ip . "','" . $node_type . "','" . $node_lat . "','" . $node_lon . "','" . $cacti_index . "','" . $node_info . "','" . $timestamp . "')"); echo "Node Added"; } } else {
function snmpget($oid) { return snmp3_get($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_get($hostname, $community, $oid, $version, $v3username, $v3password, $v3authproto = "", $v3privpassphrase = "", $v3privproto = "", $port = 161, $timeout = 500, $environ = SNMP_POLLER) { /* determine default retries */ $retries = read_config_option("snmp_retries"); if ($retries == "") $retries = 3; /* do not attempt to poll invalid combinations */ if (($version == 0) || (($community == "") && ($version != 3))) { return "U"; } /* get rid of quotes in privacy passphrase */ $v3privpassphrase = str_replace("#space#", " ", $v3privpassphrase); if ($v3privproto == "[None]") { $v3privproto = ""; } 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 ($version == "1") { $snmp_value = @snmpget("$hostname:$port", $community, trim($oid), ($timeout * 1000), $retries); }elseif ($version == "2") { $snmp_value = @snmp2_get("$hostname:$port", $community, $oid, ($timeout * 1000), $retries); }else{ $snmp_value = @snmp3_get("$hostname:$port", $v3username, snmp_get_v3authpriv($v3privproto), $v3authproto, $v3password, $v3privproto, $v3privpassphrase, trim($oid), ($timeout * 1000), $retries); } }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... */ } /* no valid snmp version has been set, get out */ if (empty($snmp_auth)) { return; } if (read_config_option("snmp_version") == "ucd-snmp") { exec(read_config_option("path_snmpget") . " -O vt -v$version -t $timeout -r $retries $hostname:$port $snmp_auth $oid", $snmp_value); }else { exec(read_config_option("path_snmpget") . " -O fntev $snmp_auth -v $version -t $timeout -r $retries $hostname:$port $oid", $snmp_value); } } if (isset($snmp_value)) { /* fix for multi-line snmp output */ if (is_array($snmp_value)) { $snmp_value = implode(" ", $snmp_value); } } /* strip out non-snmp data */ $snmp_value = format_snmp_string($snmp_value); return $snmp_value; }
function my_snmp_get($ip, $credentials, $oid) { $timeout = '3000000'; $retries = '0'; if (empty($credentials->credentials->version) or $credentials->credentials->version != 1 or $credentials->credentials->version != 2 or $credentials->credentials->version != 3) { $credentials->credentials->version = 2; } switch ($credentials->credentials->version) { case '1': $string = @snmpget($ip, $credentials->credentials->community, $oid, $timeout, $retries); break; case '2': $string = @snmp2_get($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 ?: ''; $string = @snmp3_get($ip, $sec_name, $sec_level, $auth_protocol, $auth_passphrase, $priv_protocol, $priv_passphrase, $oid, $timeout, $retries); break; default: return false; break; } if (empty($string) and $string != '0') { return false; } if ($string == '""') { $string = ''; } $string = trim($string); # if the first character is a '.', remove it. if (strpos($string, '.') === 0) { $string = substr($string, 1); } // remove the first and last characters if they are " if (substr($string, 0, 1) == "\"") { $string = substr($string, 1, strlen($string)); } if (substr($string, -1, 1) == "\"") { $string = substr($string, 0, strlen($string) - 1); } // remove some return strings if (strpos(strtolower($string), '/etc/snmp') !== false or strpos(strtolower($string), 'no such instance') !== false or strpos(strtolower($string), 'no such object') !== false or strpos(strtolower($string), 'not set') !== false or strpos(strtolower($string), 'unknown value type') !== false) { $string = ''; } // remove any quotation marks $string = str_replace('"', ' ', $string); // replace any line breaks with spaces $string = str_replace(array("\r", "\n"), " ", $string); return (string) $string; }
<?php require_once dirname(__FILE__) . '/snmp_include.inc'; echo "Checking error handling\n"; //int snmp3_get(string host, string sec_name, string sec_level, string auth_protocol, // string auth_passphrase, string priv_protocol, string priv_passphrase, // string object_id [, int timeout [, int retries]]); var_dump(snmp3_get($hostname, $community, '', '', '', '', '')); var_dump(snmp3_get($hostname, $community, '', '', '', '', '', '.1.3.6.1.2.1.1.1.0')); var_dump(snmp3_get($hostname, $community, 'bugusPriv', '', '', '', '', '.1.3.6.1.2.1.1.1.0')); var_dump(snmp3_get($hostname, $community, 'authNoPriv', 'TTT', '', '', '', '.1.3.6.1.2.1.1.1.0')); var_dump(snmp3_get($hostname, $community, 'authNoPriv', 'MD5', '', '', '', '.1.3.6.1.2.1.1.1.0')); var_dump(snmp3_get($hostname, $community, 'authNoPriv', 'MD5', 'te', '', '', '.1.3.6.1.2.1.1.1.0')); var_dump(snmp3_get($hostname, $community, 'authPriv', 'MD5', $auth_pass, 'BBB', '', '.1.3.6.1.2.1.1.1.0')); var_dump(snmp3_get($hostname, $community, 'authPriv', 'MD5', $auth_pass, 'AES', '', '.1.3.6.1.2.1.1.1.0')); var_dump(snmp3_get($hostname, $community, 'authPriv', 'MD5', $auth_pass, 'AES', 'ty', '.1.3.6.1.2.1.1.1.0')); var_dump(snmp3_get($hostname, 'somebogususer', 'authPriv', 'MD5', $auth_pass, 'AES', $priv_pass, '.1.3.6.1.2.1.1.1.0', $timeout, $retries)); var_dump(snmp3_set($hostname, $community, 'authPriv', 'MD5', $auth_pass, 'AES', $priv_pass, '', 's')); var_dump(snmp3_set($hostname, $rwuser, 'authPriv', 'MD5', $auth_pass, 'AES', $priv_pass, '.1.3.6.777...7.5.3', 's', 'ttt', $timeout, $retries)); var_dump(snmp3_set($hostname, $rwuser, 'authPriv', 'MD5', $auth_pass, 'AES', $priv_pass, '.1.3.6.777.7.5.3', array('s'), 'yyy', $timeout, $retries));
/** * Get a single SNMP value * * @throws \OSS_SNMP\Exception On *any* SNMP error, warnings are supressed and a generic exception is thrown * @param string $oid The OID to get * @return mixed The resultant value */ public function get($oid) { if ($this->cache() && ($rtn = $this->getCache()->load($oid)) !== null) { return $rtn; } $v1 = 'snmpget'; $v2c = 'snmp2_get'; switch ($this->getVersion()) { case 1: case '2c': $this->_lastResult = @${"v" . $this->getVersion()}($this->getHost(), $this->getCommunity(), $oid, $this->getTimeout(), $this->getRetry()); break; case '3': $this->_lastResult = @snmp3_get($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()); } if ($this->_lastResult === false) { throw new Exception('Could not perform walk for OID ' . $oid); } return $this->getCache()->save($oid, $this->parseSnmpValue($this->_lastResult)); }
if ($snmp_version == '') { $snmp_version = 'v2'; } $v3_user = $snmprow['v3_user']; $v3_pwd = $snmprow['v3_pwd']; $snmplocal_sent = microtime(true); $timezone = $snmprow['timezone']; $timezone = number_format((double) $timezone, 1, '.', ''); $tzDelta = $timezone * 3600 * 1000; $timeMIB = 'hrSystemDate.0'; snmp_set_quick_print(1); $timeInfo = ''; $snmpTime = ''; if (strtolower($snmp_version) == 'v3') { //SNMP V3 is used $timeInfo = @snmp3_get($hostname, $v3_user, "authNoPriv", "MD5", $v3_pwd, "DES", "", $timeMIB, $timeout, $count); } else { $timeInfo = @snmpget($hostname, $community_string, $timeMIB, $timeout, $count); } $regex = "/^(?P<YYYY>\\d\\d\\d\\d)-(?P<MM>\\d+)-(?P<DD>\\d+),(?P<HH>\\d+):(?P<mm>\\d+):(?P<ss>\\d+)\\.(?P<millisec>\\d+),(?P<plusminus>[+-])(?P<TZHH>\\d+):(?P<TZMM>\\d+)\$/"; preg_match($regex, $timeInfo, $matches); $snmpTime = $matches['YYYY'] . '-' . sprintf("%02s", $matches['MM']) . '-' . sprintf("%02s", $matches['DD']) . ' ' . sprintf("%02s", $matches['HH']) . ':' . sprintf("%02s", $matches['mm']) . ':' . sprintf("%02s", $matches['ss']); $snmpTime = strtotime($snmpTime); $server_time_formatted = date('Y-m-d H:i:s', $snmpTime); $snmpTime = round($snmpTime * 1000 + $matches['millisec'] * 1000); // Time from microsec in ms /* Following code was for linux snmp reads. //Now convert to UTC $tzDelta = (($matches['TZHH']*3600) + ($matches['TZMM'] * 60))*1000; if ($matches['plusminus'] == '+'){ //Ahead... so subtract to get UTC time $snmpTime = $snmpTime - $tzDelta;
} 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') { //SNMP V3 is used $used = @snmp3_get($hostname, $v3_user, "authNoPriv", "MD5", $v3_pwd, "DES", "", "hrStorageUsed.{$index}", $timeout, $count); $total = @snmp3_get($hostname, $v3_user, "authNoPriv", "MD5", $v3_pwd, "DES", "", "hrStorageSize.{$index}", $timeout, $count); } else { $used = @snmpget($hostname, $community_string, "hrStorageUsed.{$index}", $timeout, $count); $total = @snmpget($hostname, $community_string, "hrStorageSize.{$index}", $timeout, $count); } echo "Used: {$used};;;;;Total: {$total}\n"; if (!ereg("^[0-9]+\$", $used)) { echo "Unable to determine used space due to failed snmp query"; } if (!ereg("^[0-9]+\$", $total)) { echo "Unable to determine size due to failed snmp query"; } $mem_pct_used = 0; if ($total > 0) { $mem_pct_used = round($used / $total * 100, 0); }
function cacti_snmp_get($hostname, $community, $oid, $version, $username, $password, $auth_proto, $priv_pass, $priv_proto, $context, $port = 161, $timeout = 500, $retries = 0, $environ = SNMP_POLLER) { global $config; /* 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) || (($community == "") && ($version != 3))) { return "U"; } if ((snmp_get_method($version) == SNMP_METHOD_PHP) && (!strlen($context) || ($version != 3))) { /* make sure snmp* is verbose so we can see what types of data we are getting back */ snmp_set_quick_print(0); if ($version == "1") { $snmp_value = @snmpget("$hostname:$port", "$community", "$oid", ($timeout * 1000), $retries); }elseif ($version == "2") { $snmp_value = @snmp2_get("$hostname:$port", "$community", "$oid", ($timeout * 1000), $retries); }else{ if ($priv_proto == "[None]") { $proto = "authNoPriv"; $priv_proto = ""; }else{ $proto = "authPriv"; } $snmp_value = @snmp3_get("$hostname:$port", "$username", $proto, $auth_proto, "$password", $priv_proto, "$priv_pass", "$oid", ($timeout * 1000), $retries); } }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 */ } /* no valid snmp version has been set, get out */ if (empty($snmp_auth)) { return; } if (read_config_option("snmp_version") == "ucd-snmp") { exec(read_config_option("path_snmpget") . " -O vt -v$version -t $timeout -r $retries $hostname:$port $snmp_auth $oid", $snmp_value); }else { exec(read_config_option("path_snmpget") . " -O fntev $snmp_auth -v $version -t $timeout -r $retries $hostname:$port $oid", $snmp_value); } } if (isset($snmp_value)) { /* fix for multi-line snmp output */ if (is_array($snmp_value)) { $snmp_value = implode(" ", $snmp_value); } } /* strip out non-snmp data */ $snmp_value = format_snmp_string($snmp_value, false); return $snmp_value; }
function cacti_snmp_get($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; /* 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($port) || !is_numeric($retries) || !is_numeric($timeout) || $community == '' && $version != 3) { return 'U'; } if (snmp_get_method($version) == SNMP_METHOD_PHP && (!strlen($context) || $version != 3)) { /* make sure snmp* is verbose so we can see what types of data we are getting back */ snmp_set_quick_print(0); /* set the output format to numeric */ snmp_set_valueretrieval($method); if ($version == '1') { $snmp_value = @snmpget("{$hostname}:{$port}", "{$community}", "{$oid}", $timeout * 1000, $retries); } elseif ($version == '2') { $snmp_value = @snmp2_get("{$hostname}:{$port}", "{$community}", "{$oid}", $timeout * 1000, $retries); } else { if ($priv_proto == '[None]') { $proto = 'authNoPriv'; $priv_proto = ''; } else { $proto = 'authPriv'; } $snmp_value = @snmp3_get("{$hostname}:{$port}", "{$username}", $proto, $auth_proto, "{$password}", $priv_proto, "{$priv_pass}", "{$oid}", $timeout * 1000, $retries); } if ($snmp_value === false) { cacti_log("WARNING: SNMP Get Timeout for Host:'{$hostname}', and OID:'{$oid}'", false); } } 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 */ } /* no valid snmp version has been set, get out */ if (empty($snmp_auth)) { return; } exec(cacti_escapeshellcmd(read_config_option('path_snmpget')) . ' -O fntev ' . $snmp_auth . " -v {$version} -t {$timeout} -r {$retries} " . cacti_escapeshellarg($hostname) . ":{$port} " . cacti_escapeshellarg($oid), $snmp_value); /* fix for multi-line snmp output */ if (is_array($snmp_value)) { $snmp_value = implode(' ', $snmp_value); } } /* fix for multi-line snmp output */ if (isset($snmp_value)) { if (is_array($snmp_value)) { $snmp_value = implode(' ', $snmp_value); } } if (substr_count($snmp_value, 'Timeout:')) { cacti_log("WARNING: SNMP Get Timeout for Host:'{$hostname}', and OID:'{$oid}'", false); } /* strip out non-snmp data */ $snmp_value = format_snmp_string($snmp_value, false); return $snmp_value; }
/** * Méthode d'interrogation snmp. * Comparée au snmpget elle gère les type compteurs * * @param $oid string oid de la donnée * @param $time_derived boolean optionnel : false par défaut, le compteur doit il être divisé par le temp * @return string */ protected final function _snmpGet($oid) { trigger_error('start', E_USER_NOTICE); trigger_error("oid={$oid}", E_USER_NOTICE); switch ($this->getInput('version')) { case 'auto': $valeur = $this->_makeValueClean(snmpget($this->getInput('host') . ":" . $this->getInput('port'), $this->getInput('community'), $oid, $this->getInput('timeout') * 1000000, $this->getInput('retries'))); break; case '2': $valeur = $this->_makeValueClean(snmp2_get($this->getInput('host') . ":" . $this->getInput('port'), $this->getInput('community'), $oid, $this->getInput('timeout') * 1000000, $this->getInput('retries'))); break; case '3': $valeur = $this->_makeValueClean(snmp3_get($this->getInput('host') . ":" . $this->getInput('port'), $this->getInput('community'), $oid, $this->getInput('timeout') * 1000000, $this->getInput('retries'))); break; } trigger_error('end', E_USER_NOTICE); return $valeur; }