コード例 #1
0
function mactrack_device_edit()
{
    global $config, $fields_mactrack_device_edit;
    /* ================= input validation ================= */
    get_filter_request_var('device_id');
    /* ==================================================== */
    if (!isempty_request_var('device_id')) {
        $device = db_fetch_row_prepared('SELECT * FROM mac_track_devices WHERE device_id = ?', array(get_request_var('device_id')));
        $header_label = __('MacTrack Devices [edit: %s]', $device['device_name']);
    } else {
        $device = array();
        $header_label = __('MacTrack Devices [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 */
        cacti_oid_numeric_format();
        $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";
        }
        ?>
					</span>
				</td>
			</tr>
		</table>
		<br>
		<?php 
    }
    form_start('mactrack_devices.php');
    html_start_box($header_label, '100%', '', '3', 'center', '');
    /* preserve the devices site id between refreshes via a GET variable */
    if (!isempty_request_var('site_id')) {
        $fields_host_edit['site_id']['value'] = get_request_var('site_id');
    }
    draw_edit_form(array('config' => array('no_form_tab' => true), 'fields' => inject_form_variables($fields_mactrack_device_edit, isset($device) ? $device : array())));
    html_end_box();
    form_save_button('mactrack_devices.php', 'return');
}
コード例 #2
0
ファイル: snmp.php プロジェクト: Cacti/plugin_hmib
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;
}
コード例 #3
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 */
    cacti_oid_numeric_format();
    /* 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_prepared('SELECT * from mac_track_snmp_items WHERE snmp_id = ? ORDER BY sequence', array($device['snmp_options']));
        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;
}