Esempio n. 1
0
 function _getpeersfromrouter($config)
 {
     if ($config['snmp_community'] == '') {
         die('no community value set');
     }
     if (!$config['hostname']) {
         die('no hostname value set');
     }
     snmp_set_oid_numeric_print(1);
     snmp_set_valueretrieval(SNMP_VALUE_PLAIN);
     if ($raw_snmp = snmpwalkoid($config['hostname'] . ':' . $config['port'], $config['snmp_community'], $this->base_oid)) {
         if ($rawfile = fopen($this->cachefile, 'w')) {
             fputs($rawfile, serialize($raw_snmp));
         } else {
             die('could not write to cache file! ' . $this->cachefile);
         }
     } else {
         die('connection to router failed!');
     }
 }
Esempio n. 2
0
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;
}
Esempio n. 3
0
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;
}
Esempio n. 4
0
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;
}
Esempio n. 5
0
function valid_snmp_device(&$device)
{
    global $config;
    include_once $config["base_path"] . "/plugins/mactrack/mactrack_actions.php";
    /* initialize variable */
    $host_up = FALSE;
    $device["snmp_status"] = HOST_DOWN;
    /* force php to return numeric oid's */
    if (function_exists("snmp_set_oid_numeric_print")) {
        snmp_set_oid_numeric_print(TRUE);
    }
    /* if the first read did not work, loop until found */
    $snmp_sysObjectID = @cacti_snmp_get($device["hostname"], $device["snmp_readstring"], ".1.3.6.1.2.1.1.2.0", $device["snmp_version"], $device["snmp_username"], $device["snmp_password"], $device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"], $device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], $device["snmp_retries"]);
    $snmp_sysObjectID = str_replace("enterprises", ".1.3.6.1.4.1", $snmp_sysObjectID);
    $snmp_sysObjectID = str_replace("OID: ", "", $snmp_sysObjectID);
    $snmp_sysObjectID = str_replace(".iso", ".1", $snmp_sysObjectID);
    if (strlen($snmp_sysObjectID) > 0 && !substr_count($snmp_sysObjectID, "No Such Object") && !substr_count($snmp_sysObjectID, "Error In")) {
        $snmp_sysObjectID = trim(str_replace("\"", "", $snmp_sysObjectID));
        $host_up = TRUE;
        $device["snmp_status"] = HOST_UP;
    } else {
        /* loop through the default and then other common for the correct answer */
        $snmp_options = db_fetch_assoc("SELECT * from mac_track_snmp_items WHERE snmp_id=" . $device["snmp_options"] . " ORDER BY sequence");
        if (sizeof($snmp_options)) {
            foreach ($snmp_options as $snmp_option) {
                # update $device for later db update via db_update_device_status
                $device["snmp_readstring"] = $snmp_option["snmp_readstring"];
                $device["snmp_version"] = $snmp_option["snmp_version"];
                $device["snmp_username"] = $snmp_option["snmp_username"];
                $device["snmp_password"] = $snmp_option["snmp_password"];
                $device["snmp_auth_protocol"] = $snmp_option["snmp_auth_protocol"];
                $device["snmp_priv_passphrase"] = $snmp_option["snmp_priv_passphrase"];
                $device["snmp_priv_protocol"] = $snmp_option["snmp_priv_protocol"];
                $device["snmp_context"] = $snmp_option["snmp_context"];
                $device["snmp_port"] = $snmp_option["snmp_port"];
                $device["snmp_timeout"] = $snmp_option["snmp_timeout"];
                $device["snmp_retries"] = $snmp_option["snmp_retries"];
                $snmp_sysObjectID = @cacti_snmp_get($device["hostname"], $device["snmp_readstring"], ".1.3.6.1.2.1.1.2.0", $device["snmp_version"], $device["snmp_username"], $device["snmp_password"], $device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"], $device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], $device["snmp_retries"]);
                $snmp_sysObjectID = str_replace("enterprises", ".1.3.6.1.4.1", $snmp_sysObjectID);
                $snmp_sysObjectID = str_replace("OID: ", "", $snmp_sysObjectID);
                $snmp_sysObjectID = str_replace(".iso", ".1", $snmp_sysObjectID);
                if (strlen($snmp_sysObjectID) > 0 && !substr_count($snmp_sysObjectID, "No Such Object") && !substr_count($snmp_sysObjectID, "Error In")) {
                    $snmp_sysObjectID = trim(str_replace("\"", "", $snmp_sysObjectID));
                    $device["snmp_readstring"] = $snmp_option["snmp_readstring"];
                    $device["snmp_status"] = HOST_UP;
                    $host_up = TRUE;
                    # update cacti device, if required
                    sync_mactrack_to_cacti($device);
                    # update to mactrack itself is done by db_update_device_status in mactrack_scanner.php
                    # TODO: if db_update_device_status would use api_mactrack_device_save, there would be no need to call sync_mactrack_to_cacti here
                    # but currently the parameter set doesn't match
                    mactrack_debug("Result found on Option Set (" . $snmp_option["snmp_id"] . ") Sequence (" . $snmp_option["sequence"] . "): " . $snmp_sysObjectID);
                    break;
                    # no need to continue if we have a match
                } else {
                    $device["snmp_status"] = HOST_DOWN;
                    $host_up = FALSE;
                }
            }
        }
    }
    if ($host_up) {
        $device["snmp_sysObjectID"] = $snmp_sysObjectID;
        /* get system name */
        $snmp_sysName = @cacti_snmp_get($device["hostname"], $device["snmp_readstring"], ".1.3.6.1.2.1.1.5.0", $device["snmp_version"], $device["snmp_username"], $device["snmp_password"], $device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"], $device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], $device["snmp_retries"]);
        if (strlen($snmp_sysName) > 0) {
            $snmp_sysName = trim(strtr($snmp_sysName, "\"", " "));
            $device["snmp_sysName"] = $snmp_sysName;
        }
        /* get system location */
        $snmp_sysLocation = @cacti_snmp_get($device["hostname"], $device["snmp_readstring"], ".1.3.6.1.2.1.1.6.0", $device["snmp_version"], $device["snmp_username"], $device["snmp_password"], $device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"], $device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], $device["snmp_retries"]);
        if (strlen($snmp_sysLocation) > 0) {
            $snmp_sysLocation = trim(strtr($snmp_sysLocation, "\"", " "));
            $device["snmp_sysLocation"] = $snmp_sysLocation;
        }
        /* get system contact */
        $snmp_sysContact = @cacti_snmp_get($device["hostname"], $device["snmp_readstring"], ".1.3.6.1.2.1.1.4.0", $device["snmp_version"], $device["snmp_username"], $device["snmp_password"], $device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"], $device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], $device["snmp_retries"]);
        if (strlen($snmp_sysContact) > 0) {
            $snmp_sysContact = trim(strtr($snmp_sysContact, "\"", " "));
            $device["snmp_sysContact"] = $snmp_sysContact;
        }
        /* get system description */
        $snmp_sysDescr = @cacti_snmp_get($device["hostname"], $device["snmp_readstring"], ".1.3.6.1.2.1.1.1.0", $device["snmp_version"], $device["snmp_username"], $device["snmp_password"], $device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"], $device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], $device["snmp_retries"]);
        if (strlen($snmp_sysDescr) > 0) {
            $snmp_sysDescr = trim(strtr($snmp_sysDescr, "\"", " "));
            $device["snmp_sysDescr"] = $snmp_sysDescr;
        }
        /* get system uptime */
        $snmp_sysUptime = @cacti_snmp_get($device["hostname"], $device["snmp_readstring"], ".1.3.6.1.2.1.1.3.0", $device["snmp_version"], $device["snmp_username"], $device["snmp_password"], $device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"], $device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], $device["snmp_retries"]);
        if (strlen($snmp_sysUptime) > 0) {
            $snmp_sysUptime = trim(strtr($snmp_sysUptime, "\"", " "));
            $device["snmp_sysUptime"] = $snmp_sysUptime;
        }
    }
    return $host_up;
}
<?php
# Program: Topology-Spanningtree.php
# Programmer: Remo Rickli

$printable = 1;

error_reporting(1);
snmp_set_quick_print(1);
snmp_set_oid_numeric_print(1);
snmp_set_valueretrieval(SNMP_VALUE_LIBRARY);

include_once ("inc/header.php");
include_once ("inc/libdev.php");
include_once ("inc/libsnmp.php");

$_GET = sanitize($_GET);
$dev = isset($_GET['dev']) ? $_GET['dev'] : "";
$shg = isset($_GET['shg']) ? "checked" : "";
$vln = isset($_GET['vln']) ? $_GET['vln'] : "";
?>
<h1>Spanningtree Tool</h1>
<form method="get" action="<?= $self ?>.php" name="stree">
<table class="content"><tr class="<?= $modgroup[$self] ?>1">
<th width="50"><a href="<?= $self ?>.php"><img src="img/32/<?= $selfi ?>.png"></a></th>
<th>
Device
<select size="1" name="dev" onchange="document.stree.vln.value=''">
<option value=""><?= $sellbl ?> ->
<?php
$link	= DbConnect($dbhost,$dbuser,$dbpass,$dbname);
$query	= GenQuery('devices','s','device,devip,services,snmpversion,readcomm,location,contact,cliport,icon','device','',array('services & 2','snmpversion'),array('=','!='),array('2','0'),array('AND') );
Esempio n. 7
0
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;
}
Esempio n. 8
0
function snmptable($host, $community, $oid)
{
    # This handy function was bought to you by scot at indievisible dot org
    # Found on the PHP.net documentation page for snmprealwalk.
    # The important thing about this function is that it fills in the blanks.
    # Regular SNMP walks leave out items so you can't blindly prod things into arrays any more.
    snmp_set_oid_numeric_print(TRUE);
    snmp_set_quick_print(TRUE);
    snmp_set_enum_print(TRUE);
    $retval = array();
    if (!($raw = snmp2_real_walk($host, $community, $oid))) {
        return false;
    }
    if (count($raw) == 0) {
        return false;
    }
    // no data
    $prefix_length = 0;
    $largest = 0;
    foreach ($raw as $key => $value) {
        if ($prefix_length == 0) {
            // don't just use $oid's length since it may be non-numeric
            $prefix_elements = count(explode('.', $oid));
            $tmp = '.' . strtok($key, '.');
            while ($prefix_elements > 1) {
                $tmp .= '.' . strtok('.');
                $prefix_elements--;
            }
            $tmp .= '.';
            $prefix_length = strlen($tmp);
        }
        $key = substr($key, $prefix_length);
        $index = explode('.', $key, 2);
        isset($retval[$index[1]]) or $retval[$index[1]] = array();
        if ($largest < $index[0]) {
            $largest = $index[0];
        }
        $retval[$index[1]][$index[0]] = $value;
    }
    if (count($retval) == 0) {
        return false;
    }
    // no data
    // fill in holes and blanks the agent may "give" you
    foreach ($retval as $k => $x) {
        for ($i = 1; $i <= $largest; $i++) {
            if (!isset($retval[$k][$i])) {
                $retval[$k][$i] = '';
            }
        }
        ksort($retval[$k]);
    }
    return $retval;
}
Esempio n. 9
0
function DevRoutes($ip, $rv, $rc, $vrfname)
{
    global $toumsg, $nonlbl;
    if (!empty($vrfname)) {
        $suffix = strlen($vrfname);
        $sufarr = str_split($vrfname);
        foreach ($sufarr as $char) {
            $suffix .= "." . ord($char);
        }
        foreach (Walk($ip, $rv, $rc, "1.3.6.1.3.118.1.4.1.1.8.{$suffix}") as $ix => $val) {
            $r = preg_replace('/.*\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.\\d+\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$1', $ix);
            $nho = preg_replace('/.*\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.\\d+\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$3', $ix);
            $msk = preg_replace('/.*\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.\\d+\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$2', $ix);
            $route[$r]['nho'] = $nho;
            $route[$r]['msk'] = $msk;
            $route[$r]['ifx'] = $val;
        }
        if (!empty($ix)) {
            #metric
            foreach (Walk($ip, $rv, $rc, "1.3.6.1.3.118.1.4.1.1.14.{$suffix}") as $ix => $val) {
                $r = preg_replace('/.*\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.\\d+\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$1', $ix);
                $route[$r]['me1'] = $val;
                $route[$r]['vrfname'] = $vrfname;
            }
            #protocol
            foreach (Walk($ip, $rv, $rc, "1.3.6.1.3.118.1.4.1.1.10.{$suffix}") as $ix => $val) {
                $r = preg_replace('/.*\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.\\d+\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$1', $ix);
                $route[$r]['pro'] = $val;
            }
            #age
            foreach (Walk($ip, $rv, $rc, "1.3.6.1.3.118.1.4.1.1.11.{$suffix}") as $ix => $val) {
                $r = preg_replace('/.*\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.\\d+\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$1', $ix);
                $route[$r]['age'] = $val;
            }
            return $route;
        } else {
            echo "<h4>(VRFs: {$nonlbl})</h4>";
        }
    }
    #snmp_set_oid_numeric_print(1); Doesn't work eveerywhere, so I use the preg_replace hack below to avoid problems
    # now we should try to get ipCidrRouteIfIndex oid. Full table in 4 queries if exists.
    foreach (Walk($ip, $rv, $rc, "1.3.6.1.2.1.4.24.4.1.5") as $ix => $val) {
        $r = preg_replace('/.*\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.\\d+\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$1', $ix);
        $msk = preg_replace('/.*\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.\\d+\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$2', $ix);
        $nho = preg_replace('/.*\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.\\d+\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$3', $ix);
        $route[$r]['nho'] = $nho;
        $route[$r]['msk'] = $msk;
        $route[$r]['ifx'] = $val;
    }
    if (!$ix) {
        # no ipCidrRouteIfIndex OID, lets try ipRouteIfIndex OID (old one, but very common). This one will complete in 6 SNMP queries.
        echo "<h4>(ipCidrRouteIfIndex: {$nonlbl})</h4>";
        foreach (Walk($ip, $rv, $rc, "1.3.6.1.2.1.4.21.1.2") as $ix => $val) {
            $r = preg_replace('/.*\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$1', $ix);
            $route[$r]['ifx'] = $val;
        }
        if (!$ix) {
            #no luck with generic OIDs, try even more exotic inetCidrRoute
            #walking for inetCidrRouteIfIndex
            #netmask in really CIDR now
            echo "<h4>(generic OIDs: {$nonlbl})</h4>";
            snmp_set_oid_numeric_print(TRUE);
            foreach (Walk($ip, $rv, $rc, "1.3.6.1.2.1.4.24.7.1.7") as $ix => $val) {
                $r = preg_replace('/.*\\.1\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.(\\d+)\\.\\d+\\.1\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$1', $ix);
                $mskcidr = preg_replace('/.*\\.1\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.(\\d+)\\.\\d+\\.1\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$2', $ix);
                $nho = preg_replace('/.*\\.1\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.(\\d+)\\.\\d+\\.1\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$3', $ix);
                $mskpfix = Masker($mskcidr);
                $route[$r]['nho'] = $nho;
                $route[$r]['msk'] = $mskpfix[1];
                $route[$r]['ifx'] = $val;
            }
            if (!$ix) {
                #no OIDs for route info, timeout or route do not exist.
                echo "</table><h4>{$toumsg}</h4>";
                if ($_SESSION['vol']) {
                    echo "<embed src=\"inc/enter2.mp3\" volume=\"{$_SESSION['vol']}\" hidden=\"true\">\n";
                }
                die;
            }
            #metric inetCidrRouteMetric1
            foreach (Walk($ip, $rv, $rc, "1.3.6.1.2.1.4.24.7.1.12") as $ix => $val) {
                $r = preg_replace('/.*\\.1\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.(\\d+)\\.\\d+\\.1\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$1', $ix);
                $route[$r]['me1'] = $val;
            }
            #protocol inetCidrRouteProto
            foreach (Walk($ip, $rv, $rc, "1.3.6.1.2.1.4.24.7.1.9") as $ix => $val) {
                $r = preg_replace('/.*\\.1\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.(\\d+)\\.\\d+\\.1\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$1', $ix);
                $route[$r]['pro'] = $val;
            }
            #age inetCidrRouteAge
            foreach (Walk($ip, $rv, $rc, "1.3.6.1.2.1.4.24.7.1.10") as $ix => $val) {
                $r = preg_replace('/.*\\.1\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.(\\d+)\\.\\d+\\.1\\.4\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$1', $ix);
                $route[$r]['age'] = $val;
            }
            snmp_set_oid_numeric_print(FALSE);
            return $route;
        }
        #metric ipRouteMetric1
        foreach (Walk($ip, $rv, $rc, "1.3.6.1.2.1.4.21.1.3") as $ix => $val) {
            $r = preg_replace('/.*\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$1', $ix);
            $route[$r]['me1'] = $val;
        }
        #nexthop ipRouteNextHop
        foreach (Walk($ip, $rv, $rc, "1.3.6.1.2.1.4.21.1.7") as $ix => $val) {
            $r = preg_replace('/.*\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$1', $ix);
            $route[$r]['nho'] = $val;
        }
        #proto ipRouteProto
        foreach (Walk($ip, $rv, $rc, "1.3.6.1.2.1.4.21.1.9") as $ix => $val) {
            $r = preg_replace('/.*\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$1', $ix);
            $route[$r]['pro'] = $val;
        }
        #age ipRouteAge
        foreach (Walk($ip, $rv, $rc, "1.3.6.1.2.1.4.21.1.10") as $ix => $val) {
            $r = preg_replace('/.*\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$1', $ix);
            $route[$r]['age'] = $val;
        }
        #netmask ipRouteMask
        foreach (Walk($ip, $rv, $rc, "1.3.6.1.2.1.4.21.1.11") as $ix => $val) {
            $r = preg_replace('/.*\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$1', $ix);
            $route[$r]['msk'] = $val;
        }
        return $route;
    }
    #metric ipCidrRouteMetric1
    foreach (Walk($ip, $rv, $rc, "1.3.6.1.2.1.4.24.4.1.11") as $ix => $val) {
        $r = preg_replace('/.*\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.[0-9]\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$1', $ix);
        $route[$r]['me1'] = $val;
    }
    #protocol ipCidrRouteProto
    foreach (Walk($ip, $rv, $rc, "1.3.6.1.2.1.4.24.4.1.7") as $ix => $val) {
        $r = preg_replace('/.*\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.[0-9]\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$1', $ix);
        $route[$r]['pro'] = $val;
    }
    #age ipCidrRouteAge
    foreach (Walk($ip, $rv, $rc, "1.3.6.1.2.1.4.24.4.1.8") as $ix => $val) {
        $r = preg_replace('/.*\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)\\.[0-9]\\.(\\d+\\.\\d+\\.\\d+\\.\\d+)$/', '$1', $ix);
        $route[$r]['age'] = $val;
    }
    return $route;
}
Esempio n. 10
0
 function WIFI_GetSignal($ip, $mac_node, $community)
 {
     $devices = array('mac' => $mac_node, 'rssi' => NULL, 'tx' => NULL, 'rx' => NULL);
     $mac = explode(':', $mac_node);
     $oid_rssi = '.1.3.6.1.4.1.14988.1.1.1.2.1.3';
     $oid_tx = '.1.3.6.1.4.1.14988.1.1.1.2.1.8';
     $oid_rx = '.1.3.6.1.4.1.14988.1.1.1.2.1.9';
     for ($x = 0; $x < 6; $x++) {
         $oid_rssi .= '.' . hexdec($mac[$x]);
         $oid_tx .= '.' . hexdec($mac[$x]);
         $oid_rx .= '.' . hexdec($mac[$x]);
     }
     @snmp_set_oid_numeric_print(TRUE);
     @snmp_set_quick_print(TRUE);
     @snmp_set_enum_print(TRUE);
     if ($sn = @snmpwalk($ip, $community, $oid_rssi)) {
         foreach ($sn as $a => $x) {
             $devices['rssi'] = $x;
         }
         $sntx = @snmpwalk($ip, $community, $oid_tx);
         foreach ($sntx as $a => $x) {
             $devices['tx'] = $x / 1000000;
         }
         $snrx = @snmpwalk($ip, $community, $oid_rx);
         foreach ($snrx as $a => $x) {
             $devices['rx'] = $x / 1000000;
         }
     }
     return $devices;
 }
Esempio n. 11
0
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 = 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;
        }
    }
    /* 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);
            $snmp_oid_included = true;
        }
        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'] = ereg_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]') {
                $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(ereg_replace('(.*) =.*', "\\1", $temp_array[$i]));
                $snmp_array[$i]['value'] = format_snmp_string($temp_array[$i], true);
            }
        }
    }
    return $snmp_array;
}
Esempio n. 12
0
function mactrack_device_edit() {
	global $colors, $config, $fields_mactrack_device_edit;

	/* ================= input validation ================= */
	input_validate_input_number(get_request_var("device_id"));
	/* ==================================================== */

	display_output_messages();

	if (!empty($_GET["device_id"])) {
		$device = db_fetch_row("select * from mac_track_devices where device_id=" . $_GET["device_id"]);
		$header_label = "[edit: " . $device["device_name"] . "]";
	}else{
		$header_label = "[new]";
	}

	if (!empty($device["device_id"])) {
		?>
		<table width="100%" align="center">
			<tr>
				<td class="textInfo" colspan="2">
					<?php print $device["device_name"];?> (<?php print $device["hostname"];?>)
				</td>
			</tr>
			<tr>
				<td class="textHeader">
					SNMP Information<br>

					<span style="font-size: 10px; font-weight: normal; font-family: monospace;">
					<?php
					/* force php to return numeric oid's */
					if (function_exists("snmp_set_oid_numeric_print")) {
						snmp_set_oid_numeric_print(TRUE);
					}

					$snmp_system = cacti_snmp_get($device["hostname"], $device["snmp_readstring"], ".1.3.6.1.2.1.1.1.0", $device["snmp_version"], $device["snmp_username"], $device["snmp_password"], $device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"], $device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], $device["snmp_retries"], SNMP_WEBUI);

					if ($snmp_system == "") {
						print "<span style='color: #ff0000; font-weight: bold;'>SNMP error</span>\n";
					}else{
						$snmp_uptime = cacti_snmp_get($device["hostname"], $device["snmp_readstring"], ".1.3.6.1.2.1.1.3.0", $device["snmp_version"], $device["snmp_username"], $device["snmp_password"], $device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"], $device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], $device["snmp_retries"], SNMP_WEBUI);
						$snmp_hostname = cacti_snmp_get($device["hostname"], $device["snmp_readstring"], ".1.3.6.1.2.1.1.5.0", $device["snmp_version"], $device["snmp_username"], $device["snmp_password"], $device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"], $device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], $device["snmp_retries"], SNMP_WEBUI);
						$snmp_objid = cacti_snmp_get($device["hostname"], $device["snmp_readstring"], ".1.3.6.1.2.1.1.2.0", $device["snmp_version"], $device["snmp_username"], $device["snmp_password"], $device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"], $device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], $device["snmp_retries"], SNMP_WEBUI);

						$snmp_objid = str_replace("enterprises", ".1.3.6.1.4.1", $snmp_objid);
						$snmp_objid = str_replace("OID: ", "", $snmp_objid);
						$snmp_objid = str_replace(".iso", ".1", $snmp_objid);

						print "<strong>System:</strong> $snmp_system<br>\n";
						print "<strong>Uptime:</strong> $snmp_uptime<br>\n";
						print "<strong>Hostname:</strong> $snmp_hostname<br>\n";
						print "<strong>ObjectID:</strong> $snmp_objid<br>\n";
						if (isset($_GET["scan"]))
							print $_GET["scan"]==1 ? "<span style='color:#088A08'><strong>Port Scanning:</strong> Complete</span><br>\n" : "<span style='color:#ff0000'><strong>Port Scanning:</strong> Fail</span><br>\n";
					}
					?>
					</span>
				</td>
			</tr>
		</table>
		<br>
		<?php
	}

	html_start_box("<strong>MacTrack Devices</strong> $header_label", "100%", $colors["header"], "3", "center", "");

	/* preserve the devices site id between refreshes via a GET variable */
	if (!empty($_GET["site_id"])) {
		$fields_host_edit["site_id"]["value"] = $_GET["site_id"];
	}

	draw_edit_form(array(
		"config" => array("form_name" => "chk"),
		"fields" => inject_form_variables($fields_mactrack_device_edit, (isset($device) ? $device : array()))
		));

	html_end_box();

	if (isset($device)) {
		mactrack_save_button($config["url_path"] . "plugins/mactrack/mactrack_devices.php", "save", "", "device_id");
	}else{
		mactrack_save_button("cancel", "save", "", "device_id");
	}

	print "<script type='text/javascript' src='" . URL_PATH . "plugins/mactrack/mactrack_snmp.js'></script>";
}
Esempio n. 13
0
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;
}