本文整理汇总了PHP中isSNMPable函数的典型用法代码示例。如果您正苦于以下问题:PHP isSNMPable函数的具体用法?PHP isSNMPable怎么用?PHP isSNMPable使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isSNMPable函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: discover_new_device_ip
function discover_new_device_ip($host)
{
global $config;
if (match_network($config['autodiscovery']['ip_nets'], $host)) {
if (isPingable($host)) {
echo "Pingable ";
foreach ($config['snmp']['community'] as $community) {
$device = deviceArray($host, $community, "v2c", "161", "udp", NULL);
print_message("Trying community {$community} ...");
if (isSNMPable($device)) {
echo "SNMPable ";
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
if (dbFetchCell("SELECT COUNT(device_id) FROM devices WHERE sysName = ?", array($snmphost)) == '0') {
$device_id = createHost($snmphost, $community, "v2c", "161", "udp");
$device = device_by_id_cache($device_id, 1);
array_push($GLOBALS['devices'], $device);
return $device_id;
} else {
echo "Already have host with sysName {$snmphost}\n";
}
}
}
}
}
}
示例2: discover_new_device_ip
/**
* Observium
*
* This file is part of Observium.
*
* @package observium
* @subpackage discovery
* @subpackage functions
* @author Adam Armstrong <adama@memetic.org>
* @copyright (C) 2006-2014 Adam Armstrong
*
*/
function discover_new_device_ip($host, $source = 'xdp', $protocol = NULL, $device = NULL, $port = NULL)
{
global $config;
print_debug("Discovering possible new device on {$host}");
if ($config['autodiscovery'][$source]) {
if (match_network($config['autodiscovery']['ip_nets'], $host)) {
$db = dbFetchRow("SELECT * FROM ipv4_addresses AS A, ports AS P, devices AS D WHERE A.ipv4_address = ? AND P.port_id = A.port_id AND D.device_id = P.device_id", array($host));
if (is_array($db)) {
print_debug("Already have {$host} on " . $db['hostname']);
} else {
if (isPingable($host)) {
echo "Pingable ";
foreach ($config['snmp']['community'] as $community) {
$newdevice = deviceArray($host, $community, "v2c", "161", "udp", NULL);
print_message("Trying community {$community} ...");
if (isSNMPable($newdevice)) {
echo "SNMPable ";
$snmphost = snmp_get($newdevice, "sysName.0", "-Oqv", "SNMPv2-MIB");
if (dbFetchCell("SELECT COUNT(device_id) FROM devices WHERE sysName = ?", array($snmphost)) == '0') {
$device_id = createHost($snmphost, $community, "v2c", "161", "udp");
$newdevice = device_by_id_cache($device_id, 1);
array_push($GLOBALS['devices'], $newdevice);
if (!$protocol) {
$protocol = strtoupper($source);
}
if ($port) {
humanize_port($port);
log_event("Device autodiscovered through {$protocol} on " . $device['hostname'] . " (port " . $port['label'] . ")", $remote_device_id, 'interface', $port['port_id']);
} else {
log_event("Device autodiscovered through {$protocol} on " . $device['hostname'], $remote_device_id);
}
return $device_id;
} else {
echo "Already have host with sysName {$snmphost}\n";
}
}
}
} else {
print_debug("IP not pingable.");
}
}
} else {
print_debug("Host does not match configured nets");
}
} else {
print_debug("Source {$source} disabled for autodiscovery!");
}
}
示例3: addHost
function addHost($host, $snmpver, $port = '161', $transport = 'udp', $quiet = '0', $poller_group = '0', $force_add = '0')
{
global $config;
list($hostshort) = explode(".", $host);
// Test Database Exists
if (dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = ?", array($host)) == '0') {
if ($config['addhost_alwayscheckip'] === TRUE) {
$ip = gethostbyname($host);
} else {
$ip = $host;
}
if (ip_exists($ip) === false) {
// Test reachability
if ($force_add == 1 || isPingable($host)) {
if (empty($snmpver)) {
// Try SNMPv2c
$snmpver = 'v2c';
$ret = addHost($host, $snmpver, $port, $transport, $quiet, $poller_group, $force_add);
if (!$ret) {
//Try SNMPv3
$snmpver = 'v3';
$ret = addHost($host, $snmpver, $port, $transport, $quiet, $poller_group, $force_add);
if (!$ret) {
// Try SNMPv1
$snmpver = 'v1';
return addHost($host, $snmpver, $port, $transport, $quiet, $poller_group, $force_add);
} else {
return $ret;
}
} else {
return $ret;
}
}
if ($snmpver === "v3") {
// Try each set of parameters from config
foreach ($config['snmp']['v3'] as $v3) {
$device = deviceArray($host, NULL, $snmpver, $port, $transport, $v3);
if ($quiet == '0') {
print_message("Trying v3 parameters " . $v3['authname'] . "/" . $v3['authlevel'] . " ... ");
}
if ($force_add == 1 || isSNMPable($device)) {
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
if (empty($snmphost) or $snmphost == $host || ($hostshort = $host)) {
$device_id = createHost($host, NULL, $snmpver, $port, $transport, $v3, $poller_group);
return $device_id;
} else {
if ($quiet == '0') {
print_error("Given hostname does not match SNMP-read hostname ({$snmphost})!");
}
}
} else {
if ($quiet == '0') {
print_error("No reply on credentials " . $v3['authname'] . "/" . $v3['authlevel'] . " using {$snmpver}");
}
}
}
} elseif ($snmpver === "v2c" or $snmpver === "v1") {
// try each community from config
foreach ($config['snmp']['community'] as $community) {
$device = deviceArray($host, $community, $snmpver, $port, $transport, NULL);
if ($quiet == '0') {
print_message("Trying community {$community} ...");
}
if ($force_add == 1 || isSNMPable($device)) {
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
if (empty($snmphost) || $snmphost && ($snmphost == $host || ($hostshort = $host))) {
$device_id = createHost($host, $community, $snmpver, $port, $transport, array(), $poller_group);
return $device_id;
} else {
if ($quiet == '0') {
print_error("Given hostname does not match SNMP-read hostname ({$snmphost})!");
}
}
} else {
if ($quiet == '0') {
print_error("No reply on community {$community} using {$snmpver}");
}
}
}
} else {
if ($quiet == '0') {
print_error("Unsupported SNMP Version \"{$snmpver}\".");
}
}
if (!$device_id) {
// Failed SNMP
if ($quiet == '0') {
print_error("Could not reach {$host} with given SNMP community using {$snmpver}");
}
}
} else {
// failed Reachability
if ($quiet == '0') {
print_error("Could not ping {$host}");
}
}
} else {
if ($quiet == 0) {
print_error("Already have host with this IP {$host}");
}
//.........这里部分代码省略.........
示例4: array
$v3 = array('authlevel' => mres($_POST['authlevel']), 'authname' => mres($_POST['authname']), 'authpass' => mres($_POST['authpass']), 'authalgo' => mres($_POST['authalgo']), 'cryptopass' => mres($_POST['cryptopass']), 'cryptoalgo' => mres($_POST['cryptoalgo']));
// FIXME needs better feedback
$update = array('community' => $community, 'snmpver' => $snmpver, 'port' => $port, 'transport' => $transport, 'poller_group' => $poller_group, 'port_association_mode' => $port_assoc_mode);
if ($_POST['timeout']) {
$update['timeout'] = $timeout;
} else {
$update['timeout'] = array('NULL');
}
if ($_POST['retries']) {
$update['retries'] = $retries;
} else {
$update['retries'] = array('NULL');
}
$update = array_merge($update, $v3);
$device_tmp = deviceArray($device['hostname'], $community, $snmpver, $port, $transport, $v3, $port_assoc_mode);
if (isSNMPable($device_tmp)) {
$rows_updated = dbUpdate($update, 'devices', '`device_id` = ?', array($device['device_id']));
$max_repeaters_set = false;
if (is_numeric($max_repeaters) && $max_repeaters != 0) {
set_dev_attrib($device, 'snmp_max_repeaters', $max_repeaters);
$max_repeaters_set = true;
} else {
del_dev_attrib($device, 'snmp_max_repeaters');
$max_repeaters_set = true;
}
if ($rows_updated > 0) {
$update_message = $rows_updated . ' Device record updated.';
$updated = 1;
} else {
if ($rows_updated = '-1') {
if ($max_repeaters_set === true) {
示例5: poll_device
function poll_device($device, $options)
{
global $config, $device, $polled_devices, $db_stats, $memcache;
$attribs = get_dev_attribs($device['device_id']);
$status = 0;
unset($array);
$device_start = utime();
// Start counting device poll time
echo $device['hostname'] . ' ' . $device['device_id'] . ' ' . $device['os'] . ' ';
if ($config['os'][$device['os']]['group']) {
$device['os_group'] = $config['os'][$device['os']]['group'];
echo '(' . $device['os_group'] . ')';
}
echo "\n";
unset($poll_update);
unset($poll_update_query);
unset($poll_separator);
$poll_update_array = array();
$update_array = array();
$host_rrd = $config['rrd_dir'] . '/' . $device['hostname'];
if (!is_dir($host_rrd)) {
mkdir($host_rrd);
echo "Created directory : {$host_rrd}\n";
}
$address_family = snmpTransportToAddressFamily($device['transport']);
$ping_response = isPingable($device['hostname'], $address_family, $attribs);
$device_perf = $ping_response['db'];
$device_perf['device_id'] = $device['device_id'];
$device_perf['timestamp'] = array('NOW()');
if (can_ping_device($attribs) === true && is_array($device_perf)) {
dbInsert($device_perf, 'device_perf');
}
$device['pingable'] = $ping_response['result'];
$ping_time = $ping_response['last_ping_timetaken'];
$response = array();
$status_reason = '';
if ($device['pingable']) {
$device['snmpable'] = isSNMPable($device);
if ($device['snmpable']) {
$status = '1';
$response['status_reason'] = '';
} else {
echo 'SNMP Unreachable';
$status = '0';
$response['status_reason'] = 'snmp';
}
} else {
echo 'Unpingable';
$status = '0';
$response['status_reason'] = 'icmp';
}
if ($device['status'] != $status) {
$poll_update .= $poll_separator . "`status` = '{$status}'";
$poll_separator = ', ';
dbUpdate(array('status' => $status, 'status_reason' => $response['status_reason']), 'devices', 'device_id=?', array($device['device_id']));
dbInsert(array('importance' => '0', 'device_id' => $device['device_id'], 'message' => 'Device is ' . ($status == '1' ? 'up' : 'down')), 'alerts');
log_event('Device status changed to ' . ($status == '1' ? 'Up' : 'Down'), $device, $status == '1' ? 'up' : 'down');
}
if ($status == '1') {
$graphs = array();
$oldgraphs = array();
if ($options['m']) {
foreach (explode(',', $options['m']) as $module) {
if (is_file('includes/polling/' . $module . '.inc.php')) {
include 'includes/polling/' . $module . '.inc.php';
}
}
} else {
foreach ($config['poller_modules'] as $module => $module_status) {
if ($attribs['poll_' . $module] || $module_status && !isset($attribs['poll_' . $module])) {
// TODO per-module polling stats
include 'includes/polling/' . $module . '.inc.php';
} else {
if (isset($attribs['poll_' . $module]) && $attribs['poll_' . $module] == '0') {
echo "Module [ {$module} ] disabled on host.\n";
} else {
echo "Module [ {$module} ] disabled globally.\n";
}
}
}
}
//end if
if (!$options['m']) {
// FIXME EVENTLOGGING -- MAKE IT SO WE DO THIS PER-MODULE?
// This code cycles through the graphs already known in the database and the ones we've defined as being polled here
// If there any don't match, they're added/deleted from the database.
// Ideally we should hold graphs for xx days/weeks/polls so that we don't needlessly hide information.
foreach (dbFetch('SELECT `graph` FROM `device_graphs` WHERE `device_id` = ?', array($device['device_id'])) as $graph) {
if (isset($graphs[$graph['graph']])) {
$oldgraphs[$graph['graph']] = true;
} else {
dbDelete('device_graphs', '`device_id` = ? AND `graph` = ?', array($device['device_id'], $graph['graph']));
}
}
foreach ($graphs as $graph => $value) {
if (!isset($oldgraphs[$graph])) {
echo '+';
dbInsert(array('device_id' => $device['device_id'], 'graph' => $graph), 'device_graphs');
}
echo $graph . ' ';
//.........这里部分代码省略.........
示例6: detect_device_snmpauth
function detect_device_snmpauth($hostname, $snmp_port = 161, $snmp_transport = 'udp', $detect_ip_version = FALSE)
{
global $config;
// Additional checks for IP version
if ($detect_ip_version) {
$ip_version = get_ip_version($hostname);
if (!$ip_version) {
$ip = gethostbyname6($hostname);
$ip_version = get_ip_version($ip);
}
// Detect snmp transport
if (stripos($snmp_transport, 'tcp') !== FALSE) {
$snmp_transport = $ip_version == 4 ? 'tcp' : 'tcp6';
} else {
$snmp_transport = $ip_version == 4 ? 'udp' : 'udp6';
}
}
// Detect snmp port
if (!is_numeric($snmp_port) || $snmp_port < 1 || $snmp_port > 65535) {
$snmp_port = 161;
} else {
$snmp_port = (int) $snmp_port;
}
// Here set default snmp version order
$i = 1;
$snmp_version_order = array();
foreach (array('v2c', 'v3', 'v1') as $tmp_version) {
if ($config['snmp']['version'] == $tmp_version) {
$snmp_version_order[0] = $tmp_version;
} else {
$snmp_version_order[$i] = $tmp_version;
}
$i++;
}
ksort($snmp_version_order);
foreach ($snmp_version_order as $snmp_version) {
if ($snmp_version === 'v3') {
// Try each set of parameters from config
foreach ($config['snmp']['v3'] as $snmp_v3) {
$device = build_initial_device_array($hostname, NULL, $snmp_version, $snmp_port, $snmp_transport, $snmp_v3);
print_message("Trying v3 parameters " . $device['snmp_authname'] . "/" . $device['snmp_authlevel'] . " ... ");
if (isSNMPable($device)) {
return $device;
} else {
print_warning("证书无应答 " . $device['snmp_authname'] . "/" . $device['snmp_authlevel'] . " using {$snmp_version}.");
}
}
} else {
// if ($snmp_version === "v2c" || $snmp_version === "v1")
// Try each community from config
foreach ($config['snmp']['community'] as $snmp_community) {
$device = build_initial_device_array($hostname, $snmp_community, $snmp_version, $snmp_port, $snmp_transport);
print_message("尝试 {$snmp_version} community {$snmp_community} ...");
if (isSNMPable($device)) {
return $device;
} else {
print_warning("Community无应答 {$snmp_community} 使用 {$snmp_version}.");
}
}
}
}
return FALSE;
}
示例7: poll_device
function poll_device($device, $options)
{
global $config, $device, $polled_devices, $memcache;
$attribs = get_dev_attribs($device['device_id']);
$device['snmp_max_repeaters'] = $attribs['snmp_max_repeaters'];
$device['snmp_max_oid'] = $attribs['snmp_max_oid'];
$status = 0;
unset($array);
$device_start = microtime(true);
// Start counting device poll time
echo 'Hostname: ' . $device['hostname'] . PHP_EOL;
echo 'Device ID: ' . $device['device_id'] . PHP_EOL;
echo 'OS: ' . $device['os'];
$ip = dnslookup($device);
if (!empty($ip) && $ip != inet6_ntop($device['ip'])) {
log_event('Device IP changed to ' . $ip, $device, 'system');
$db_ip = inet_pton($ip);
dbUpdate(array('ip' => $db_ip), 'devices', 'device_id=?', array($device['device_id']));
}
if ($config['os'][$device['os']]['group']) {
$device['os_group'] = $config['os'][$device['os']]['group'];
echo ' (' . $device['os_group'] . ')';
}
echo PHP_EOL . PHP_EOL;
unset($poll_update);
unset($poll_update_query);
unset($poll_separator);
$poll_update_array = array();
$update_array = array();
$host_rrd = $config['rrd_dir'] . '/' . $device['hostname'];
if ($config['norrd'] !== true && !is_dir($host_rrd)) {
mkdir($host_rrd);
echo "Created directory : {$host_rrd}\n";
}
$address_family = snmpTransportToAddressFamily($device['transport']);
$ping_response = isPingable($device['hostname'], $address_family, $attribs);
$device_perf = $ping_response['db'];
$device_perf['device_id'] = $device['device_id'];
$device_perf['timestamp'] = array('NOW()');
if (can_ping_device($attribs) === true && is_array($device_perf)) {
dbInsert($device_perf, 'device_perf');
}
$device['pingable'] = $ping_response['result'];
$ping_time = $ping_response['last_ping_timetaken'];
$response = array();
$status_reason = '';
if ($device['pingable']) {
$device['snmpable'] = isSNMPable($device);
if ($device['snmpable']) {
$status = '1';
$response['status_reason'] = '';
} else {
echo 'SNMP Unreachable';
$status = '0';
$response['status_reason'] = 'snmp';
}
} else {
echo 'Unpingable';
$status = '0';
$response['status_reason'] = 'icmp';
}
if ($device['status'] != $status) {
$poll_update .= $poll_separator . "`status` = '{$status}'";
$poll_separator = ', ';
dbUpdate(array('status' => $status, 'status_reason' => $response['status_reason']), 'devices', 'device_id=?', array($device['device_id']));
log_event('Device status changed to ' . ($status == '1' ? 'Up' : 'Down') . ' from ' . $response['status_reason'] . ' check.', $device, $status == '1' ? 'up' : 'down');
}
if ($status == '1') {
$graphs = array();
$oldgraphs = array();
// we always want the core module to be included
include 'includes/polling/core.inc.php';
$force_module = false;
if ($options['m']) {
$config['poller_modules'] = array();
foreach (explode(',', $options['m']) as $module) {
if (is_file('includes/polling/' . $module . '.inc.php')) {
$config['poller_modules'][$module] = 1;
$force_module = true;
}
}
}
foreach ($config['poller_modules'] as $module => $module_status) {
$os_module_status = $config['os'][$device['os']]['poller_modules'][$module];
d_echo("Modules status: Global" . (isset($module_status) ? $module_status ? '+ ' : '- ' : ' '));
d_echo("OS" . (isset($os_module_status) ? $os_module_status ? '+ ' : '- ' : ' '));
d_echo("Device" . (isset($attribs['poll_' . $module]) ? $attribs['poll_' . $module] ? '+ ' : '- ' : ' '));
if ($force_module === true || $attribs['poll_' . $module] || $os_module_status && !isset($attribs['poll_' . $module]) || $module_status && !isset($os_module_status) && !isset($attribs['poll_' . $module])) {
$module_start = 0;
$module_time = 0;
$module_start = microtime(true);
echo "\n#### Load poller module {$module} ####\n";
include "includes/polling/{$module}.inc.php";
$module_time = microtime(true) - $module_start;
printf("\n>> Runtime for poller module '%s': %.4f seconds\n", $module, $module_time);
echo "#### Unload poller module {$module} ####\n\n";
// save per-module poller stats
$tags = array('module' => $module, 'rrd_def' => 'DS:poller:GAUGE:600:0:U', 'rrd_name' => array('poller-perf', $module));
$fields = array('poller' => $module_time);
data_update($device, 'poller-perf', $tags, $fields);
//.........这里部分代码省略.........
示例8: poll_device
function poll_device($device, $options)
{
global $config, $device, $polled_devices, $db_stats, $exec_status, $alert_rules, $alert_table, $graphs, $attribs;
$alert_metrics = array();
$oid_cache = array();
$old_device_state = unserialize($device['device_state']);
$attribs = get_entity_attribs('device', $device['device_id']);
$pid_info = check_process_run($device);
// This just clear stalled DB entries
add_process_info($device);
// Store process info
$alert_rules = cache_alert_rules();
$alert_table = cache_device_alert_table($device['device_id']);
if (OBS_DEBUG > 1 && (count($alert_rules) || count($alert_table))) {
print_vars($alert_rules);
print_vars($alert_table);
}
$status = 0;
$device_start = utime();
// Start counting device poll time
print_cli_heading($device['hostname'] . " [" . $device['device_id'] . "]", 1);
print_cli_data("OS", $device['os'], 1);
if ($config['os'][$device['os']]['group']) {
$device['os_group'] = $config['os'][$device['os']]['group'];
print_cli_data("OS Group", $device['os_group'], 1);
}
if (is_numeric($device['last_polled_timetaken'])) {
print_cli_data("Last poll duration", $device['last_polled_timetaken'] . " seconds", 1);
}
print_cli_data("Last Polled", $device['last_polled'], 1);
print_cli_data("SNMP Version", $device['snmp_version'], 1);
//unset($poll_update); unset($poll_update_query); unset($poll_separator);
$update_array = array();
$host_rrd_dir = $config['rrd_dir'] . "/" . $device['hostname'];
if (!is_dir($host_rrd_dir)) {
mkdir($host_rrd_dir);
echo "Created directory : {$host_rrd_dir}\n";
}
$flags = OBS_DNS_ALL;
if ($device['snmp_transport'] == 'udp6' || $device['snmp_transport'] == 'tcp6') {
$flags = $flags ^ OBS_DNS_A;
}
$attribs['ping_skip'] = isset($attribs['ping_skip']) && $attribs['ping_skip'];
if ($attribs['ping_skip']) {
$flags = $flags | OBS_PING_SKIP;
// Add skip ping flag
}
$device['pingable'] = isPingable($device['hostname'], $flags);
if ($device['pingable']) {
$device['snmpable'] = isSNMPable($device);
if ($device['snmpable']) {
$ping_msg = $attribs['ping_skip'] ? '' : 'PING (' . $device['pingable'] . 'ms) and ';
print_cli_data("Device status", "Device is reachable by " . $ping_msg . "SNMP (" . $device['snmpable'] . "ms)", 1);
$status = "1";
$status_type = '';
} else {
print_cli_data("Device status", "Device is not responding to SNMP requests", 1);
$status = "0";
$status_type = 'snmp';
}
} else {
print_cli_data("Device status", "Device is not responding to PINGs", 1);
$status = "0";
$status_type = 'ping';
}
if ($device['status'] != $status) {
dbUpdate(array('status' => $status), 'devices', 'device_id = ?', array($device['device_id']));
// dbInsert(array('importance' => '0', 'device_id' => $device['device_id'], 'message' => "Device is " .($status == '1' ? 'up' : 'down')), 'alerts');
$event_msg = 'Device status changed to ';
if ($status == '1') {
// Device Up, Severity Warning (4)
$event_msg .= 'Up';
$event_severity = 4;
} else {
// Device Down, Severity Error (3)!
$event_msg .= 'Down';
$event_severity = 3;
}
if ($status_type != '') {
$event_msg .= ' (' . $status_type . ')';
}
log_event($event_msg, $device, 'device', $device['device_id'], $event_severity);
}
rrdtool_update_ng($device, 'status', array('status' => $status));
if (!$attribs['ping_skip']) {
// Ping response RRD database.
rrdtool_update_ng($device, 'ping', array('ping' => $device['pingable'] ? $device['pingable'] : 'U'));
}
// SNMP response RRD database.
rrdtool_update_ng($device, 'ping_snmp', array('ping_snmp' => $device['snmpable'] ? $device['snmpable'] : 'U'));
$alert_metrics['device_status'] = $status;
$alert_metrics['device_status_type'] = $status_type;
$alert_metrics['device_ping'] = $device['pingable'];
// FIXME, when ping skipped, here always 0.001
$alert_metrics['device_snmp'] = $device['snmpable'];
if ($status == "1") {
// Arrays for store and check enabled/disabled graphs
$graphs = array();
$graphs_db = array();
foreach (dbFetchRows("SELECT * FROM `device_graphs` WHERE `device_id` = ?", array($device['device_id'])) as $entry) {
//.........这里部分代码省略.........
示例9: addHost
function addHost($host, $snmpver, $port = '161', $transport = 'udp', $quiet = '0')
{
global $config;
list($hostshort) = explode(".", $host);
// Test Database Exists
if (dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = ?", array($host)) == '0') {
// Test if IP or Hostname
if (!inet_pton($host)) {
// Test reachability
if (isPingable($host)) {
$added = 0;
if (empty($snmpver)) {
// Try SNMPv2c
$snmpver = 'v2c';
$ret = addHost($host, $snmpver);
if (!$ret) {
//Try SNMPv3
$snmpver = 'v3';
$ret = addHost($host, $snmpver);
if (!$ret) {
// Try SNMPv1
$snmpver = 'v1';
return addHost($host, $snmpver);
} else {
return $ret;
}
} else {
return $ret;
}
}
if ($snmpver === "v3") {
// Try each set of parameters from config
foreach ($config['snmp']['v3'] as $v3) {
$device = deviceArray($host, NULL, $snmpver, $port, $transport, $v3);
if ($quiet == '0') {
print_message("Trying v3 parameters " . $v3['authname'] . "/" . $v3['authlevel'] . " ... ");
}
if (isSNMPable($device)) {
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
if (empty($snmphost) or $snmphost == $host || ($hostshort = $host)) {
$device_id = createHost($host, NULL, $snmpver, $port, $transport, $v3);
return $device_id;
} else {
if ($quiet == '0') {
print_error("Given hostname does not match SNMP-read hostname ({$snmphost})!");
}
}
} else {
if ($quiet == '0') {
print_error("No reply on credentials " . $v3['authname'] . "/" . $v3['authlevel'] . " using {$snmpver}");
}
}
}
} elseif ($snmpver === "v2c" or $snmpver === "v1") {
// try each community from config
foreach ($config['snmp']['community'] as $community) {
$device = deviceArray($host, $community, $snmpver, $port, $transport, NULL);
if ($quiet == '0') {
print_message("Trying community {$community} ...");
}
if (isSNMPable($device)) {
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
if ($snmphost == "" || $snmphost && ($snmphost == $host || ($hostshort = $host))) {
$device_id = createHost($host, $community, $snmpver, $port, $transport);
return $device_id;
} else {
if ($quiet == '0') {
print_error("Given hostname does not match SNMP-read hostname ({$snmphost})!");
}
}
} else {
if ($quiet == '0') {
print_error("No reply on community {$community} using {$snmpver}");
}
}
}
} else {
if ($quiet == '0') {
print_error("Unsupported SNMP Version \"{$snmpver}\".");
}
}
if (!$device_id) {
// Failed SNMP
if ($quiet == '0') {
print_error("Could not reach {$host} with given SNMP community using {$snmpver}");
}
}
} else {
// failed Reachability
if ($quiet == '0') {
print_error("Could not ping {$host}");
}
}
} else {
// Failed DNS lookup
if ($quiet == '0') {
print_error("{$host} looks like an IP address, please use FQDN");
}
}
} else {
//.........这里部分代码省略.........
示例10: print_message
}
}
if (!$where && !$options['u']) {
print_message("%n\nUSAGE:\n{$scriptname} [-dquV] [-i instances] [-n number] [-m module] [-h device]\n\nEXAMPLE:\n-h <device id> | <device hostname wildcard> Discover single device\n-h odd Discover odd numbered devices (same as -i 2 -n 0)\n-h even Discover even numbered devices (same as -i 2 -n 1)\n-h all Discover all devices\n-h new Discover all devices that have not had a discovery run before\n\n-i <instances> -n <number> Discover as instance <number> of <instances>\n Instances start at 0. 0-3 for -n 4\n\nOPTIONS:\n -h Device hostname, id or key odd/even/all/new.\n -i Discovery instance.\n -n Discovery number.\n -q Quiet output.\n -u Upgrade DB schema\n -V Show version and exit.\n\nDEBUGGING OPTIONS:\n -d Enable debugging output.\n -m Specify modules (separated by commas) to be run.\n\n%rInvalid arguments!%n", 'color', FALSE);
}
if ($config['version_check'] && ($options['h'] != 'new' || $options['u'])) {
include $config['install_dir'] . '/includes/versioncheck.inc.php';
}
if (!$where) {
exit;
}
$discovered_devices = 0;
foreach (dbFetchRows("SELECT * FROM `devices` WHERE `status` = 1 AND `disabled` = 0 {$where} ORDER BY `last_discovered_timetaken` ASC", $params) as $device) {
// Additional check if device SNMPable, because during
// discovery many devices (long time), the some device can be switched off
if ($options['h'] == 'new' || isSNMPable($device)) {
discover_device($device, $options);
if (function_exists('update_device_alert_table')) {
update_device_alert_table($device);
}
// not exist in 'community' edition
} else {
$string = "Device '" . $device['hostname'] . "' skipped, because switched off during runtime discovery process.";
print_debug($string);
logfile($argv[0] . ": {$string}");
}
}
$end = utime();
$run = $end - $start;
$proctime = substr($run, 0, 5);
if ($discovered_devices) {
示例11: add_device
function add_device($host, $snmpver = array(), $port = '161', $transport = 'udp', $error = FALSE)
{
global $config;
// If $error set as TRUE break recursive function execute
if ($error) {
return FALSE;
}
// Reset snmp timeout and retries options for speedup device adding
unset($config['snmp']['timeout'], $config['snmp']['retries']);
list($hostshort) = explode(".", $host);
// Test if host exists in database
if (dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = ?", array($host)) == '0') {
// Test DNS lookup.
if (gethostbyname6($host, TRUE)) {
// Test reachability
if (isPingable($host)) {
// Test directory exists in /rrd/
if (!$config['rrd_override'] && file_exists($config['rrd_dir'] . '/' . $host)) {
print_error("目录 rrd/{$host} 已经存在.");
return FALSE;
}
if (empty($snmpver)) {
foreach (array('v2c', 'v3', 'v1') as $snmpver) {
// Try SNMP v2c, v3 and v1
$ret = add_device($host, $snmpver, $port, $transport, $error);
if ($ret === FALSE) {
$error = TRUE;
} elseif (is_numeric($ret) && $ret > 0) {
return $ret;
}
}
}
if ($snmpver === "v3") {
// Try each set of parameters from config
foreach ($config['snmp']['v3'] as $v3) {
$device = deviceArray($host, NULL, $snmpver, $port, $transport, $v3);
print_message("Trying v3 parameters " . $v3['authname'] . "/" . $v3['authlevel'] . " ... ");
if (isSNMPable($device)) {
if (!check_device_duplicated($device)) {
$device_id = createHost($host, NULL, $snmpver, $port, $transport, $v3);
return $device_id;
}
} else {
print_warning("证书没有回应 " . $v3['authname'] . "/" . $v3['authlevel'] . " using {$snmpver}.");
}
}
} elseif ($snmpver === "v2c" || $snmpver === "v1") {
// Try each community from config
foreach ($config['snmp']['community'] as $community) {
$device = deviceArray($host, $community, $snmpver, $port, $transport, NULL);
print_message("尝试 {$snmpver} community {$community} ...");
if (isSNMPable($device)) {
if (!check_device_duplicated($device)) {
$device_id = createHost($host, $community, $snmpver, $port, $transport);
return $device_id;
}
} else {
print_warning("Community 没有应答 {$community} 使用 {$snmpver}.");
}
}
} else {
print_error("不支持的协议版本 \"{$snmpver}\".");
}
if (!$device_id) {
// Failed SNMP
print_error("不可到达的 {$host} 与给定 SNMP community 使用 {$snmpver}.");
}
} else {
// failed Reachability
print_error("无法 ping {$host}.");
}
} else {
// Failed DNS lookup
print_error("无法解析 {$host}.");
}
} else {
// found in database
print_error("已有设备 {$host}.");
}
return FALSE;
}
示例12: addHost
/**
* Add a device to LibreNMS
*
* @param string $host dns name or ip address
* @param string $snmp_version If this is empty, try v2c,v3,v1. Otherwise, use this specific version.
* @param string $port the port to connect to for snmp
* @param string $transport udp or tcp
* @param string $poller_group the poller group this device will belong to
* @param boolean $force_add add even if the device isn't reachable
* @param string $port_assoc_mode snmp field to use to determine unique ports
*
* @return int returns the device_id of the added device
*
* @throws HostExistsException This hostname already exists
* @throws HostIpExistsException We already have a host with this IP
* @throws HostUnreachableException We could not reach this device is some way
* @throws HostUnreachablePingException We could not ping the device
* @throws InvalidPortAssocModeException The given port association mode was invalid
* @throws SnmpVersionUnsupportedException The given snmp version was invalid
*/
function addHost($host, $snmp_version = '', $port = '161', $transport = 'udp', $poller_group = '0', $force_add = false, $port_assoc_mode = 'ifIndex')
{
global $config;
// Test Database Exists
if (host_exists($host) === true) {
throw new HostExistsException("Already have host {$host}");
}
// Valid port assoc mode
if (!is_valid_port_assoc_mode($port_assoc_mode)) {
throw new InvalidPortAssocModeException("Invalid port association_mode '{$port_assoc_mode}'. Valid modes are: " . join(', ', get_port_assoc_modes()));
}
// check if we have the host by IP
if ($config['addhost_alwayscheckip'] === true) {
$ip = gethostbyname($host);
} else {
$ip = $host;
}
if (ip_exists($ip)) {
throw new HostIpExistsException("Already have host with this IP {$host}");
}
// Test reachability
if (!$force_add) {
$address_family = snmpTransportToAddressFamily($transport);
$ping_result = isPingable($host, $address_family);
if (!$ping_result['result']) {
throw new HostUnreachablePingException("Could not ping {$host}");
}
}
// if $snmpver isn't set, try each version of snmp
if (empty($snmp_version)) {
$snmpvers = array('v2c', 'v3', 'v1');
} else {
$snmpvers = array($snmp_version);
}
$host_unreachable_exception = new HostUnreachableException("Could not connect, please check the snmp details and snmp reachability");
// try different snmp variables to add the device
foreach ($snmpvers as $snmpver) {
if ($snmpver === "v3") {
// Try each set of parameters from config
foreach ($config['snmp']['v3'] as $v3) {
$device = deviceArray($host, null, $snmpver, $port, $transport, $v3, $port_assoc_mode);
if ($force_add === true || isSNMPable($device)) {
if ($force_add !== true) {
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
}
$result = createHost($host, null, $snmpver, $port, $transport, $v3, $poller_group, $port_assoc_mode, $snmphost, $force_add);
if ($result !== false) {
return $result;
}
} else {
$host_unreachable_exception->addReason("SNMP {$snmpver}: No reply with credentials " . $v3['authname'] . "/" . $v3['authlevel']);
}
}
} elseif ($snmpver === "v2c" || $snmpver === "v1") {
// try each community from config
foreach ($config['snmp']['community'] as $community) {
$device = deviceArray($host, $community, $snmpver, $port, $transport, null, $port_assoc_mode);
if ($force_add === true || isSNMPable($device)) {
if ($force_add !== true) {
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
}
$result = createHost($host, $community, $snmpver, $port, $transport, array(), $poller_group, $port_assoc_mode, $snmphost, $force_add);
if ($result !== false) {
return $result;
}
} else {
$host_unreachable_exception->addReason("SNMP {$snmpver}: No reply with community {$community}");
}
}
} else {
throw new SnmpVersionUnsupportedException("Unsupported SNMP Version \"{$snmpver}\", must be v1, v2c, or v3");
}
}
throw $host_unreachable_exception;
}
示例13: poll_device
function poll_device($device, $options)
{
global $config, $debug, $device, $polled_devices, $db_stats, $memcache;
$old_device_state = unserialize($device['device_state']);
$attribs = get_dev_attribs($device['device_id']);
$status = 0;
unset($array);
$device_start = utime();
// Start counting device poll time
echo $device['hostname'] . " " . $device['device_id'] . " " . $device['os'] . " ";
if ($config['os'][$device['os']]['group']) {
$device['os_group'] = $config['os'][$device['os']]['group'];
echo "(" . $device['os_group'] . ")";
}
echo "\n";
unset($poll_update);
unset($poll_update_query);
unset($poll_separator);
$poll_update_array = array();
$host_rrd = $config['rrd_dir'] . "/" . $device['hostname'];
if (!is_dir($host_rrd)) {
mkdir($host_rrd);
echo "Created directory : {$host_rrd}\n";
}
$device['pingable'] = isPingable($device['hostname']);
if ($device['pingable']) {
$device['snmpable'] = isSNMPable($device);
if ($device['snmpable']) {
$status = "1";
$status_type = '';
} else {
echo "SNMP Unreachable";
$status = "0";
$status_type = ' (snmp)';
}
} else {
echo "Unpingable";
$status = "0";
$status_type = ' (ping)';
}
if ($device['status'] != $status) {
$poll_update .= $poll_separator . "`status` = '{$status}'";
$poll_separator = ", ";
dbUpdate(array('status' => $status), 'devices', 'device_id=?', array($device['device_id']));
dbInsert(array('importance' => '0', 'device_id' => $device['device_id'], 'message' => "Device is " . ($status == '1' ? 'up' : 'down')), 'alerts');
log_event('Device status changed to ' . ($status == '1' ? 'Up' : 'Down') . $status_type, $device, 'system');
notify($device, "Device " . ($status == '1' ? 'Up' : 'Down') . ": " . $device['hostname'] . $status_type, "Device " . ($status == '1' ? 'up' : 'down') . ": " . $device['hostname']);
}
$rrd = $config['rrd_dir'] . "/" . $device['hostname'] . "/status.rrd";
if (!is_file($rrd)) {
rrdtool_create($rrd, "DS:status:GAUGE:600:0:1 " . $config['rrd_rra']);
}
if ($status == "1" || $status == "0") {
rrdtool_update($rrd, "N:" . $status);
} else {
rrdtool_update($rrd, "N:U");
}
// Ping response RRD database.
$ping_rrd = $config['rrd_dir'] . '/' . $device['hostname'] . '/ping.rrd';
if (!is_file($ping_rrd)) {
rrdtool_create($ping_rrd, "DS:ping:GAUGE:600:0:65535 " . $config['rrd_rra']);
}
if ($device['pingable']) {
rrdtool_update($ping_rrd, "N:" . $device['pingable']);
} else {
rrdtool_update($ping_rrd, "N:U");
}
// SNMP response RRD database.
$ping_snmp_rrd = $config['rrd_dir'] . '/' . $device['hostname'] . '/ping_snmp.rrd';
if (!is_file($ping_snmp_rrd)) {
rrdtool_create($ping_snmp_rrd, "DS:ping_snmp:GAUGE:600:0:65535 " . $config['rrd_rra']);
}
if ($device['snmpable']) {
rrdtool_update($ping_snmp_rrd, "N:" . $device['snmpable']);
} else {
rrdtool_update($ping_snmp_rrd, "N:U");
}
if ($status == "1") {
$graphs = array();
$oldgraphs = array();
// Enable Ping graphs
$graphs['ping'] = TRUE;
// Enable SNMP graphs
$graphs['ping_snmp'] = TRUE;
if ($options['m']) {
foreach (explode(",", $options['m']) as $module) {
if (is_file("includes/polling/" . $module . ".inc.php")) {
include "includes/polling/" . $module . ".inc.php";
}
}
} else {
foreach ($config['poller_modules'] as $module => $module_status) {
if ($attribs['poll_' . $module] || $module_status && !isset($attribs['poll_' . $module])) {
if ($debug) {
echo "including: includes/polling/{$module}.inc.php\n";
}
include 'includes/polling/' . $module . '.inc.php';
} elseif (isset($attribs['poll_' . $module]) && $attribs['poll_' . $module] == "0") {
echo "Module [ {$module} ] disabled on host.\n";
} else {
//.........这里部分代码省略.........
示例14: poll_device
function poll_device($device, $options)
{
global $config, $debug, $device, $polled_devices, $db_stats, $memcache, $exec_status, $alert_rules, $alert_table;
$oid_cache = array();
$old_device_state = unserialize($device['device_state']);
$attribs = get_dev_attribs($device['device_id']);
$alert_rules = cache_alert_rules();
$alert_table = cache_device_alert_table($device['device_id']);
if ($debug && (count($alert_rules) || count($alert_table))) {
print_vars($alert_rules);
print_vars($alert_table);
}
$status = 0;
unset($array);
$device_start = utime();
// Start counting device poll time
echo $device['hostname'] . " " . $device['device_id'] . " " . $device['os'] . " ";
if ($config['os'][$device['os']]['group']) {
$device['os_group'] = $config['os'][$device['os']]['group'];
echo "(" . $device['os_group'] . ")";
}
echo "\n";
unset($poll_update);
unset($poll_update_query);
unset($poll_separator);
$poll_update_array = array();
$host_rrd = $config['rrd_dir'] . "/" . $device['hostname'];
if (!is_dir($host_rrd)) {
mkdir($host_rrd);
echo "Created directory : {$host_rrd}\n";
}
$device['pingable'] = isPingable($device['hostname']);
if ($device['pingable']) {
$device['snmpable'] = isSNMPable($device);
if ($device['snmpable']) {
$status = "1";
$status_type = '';
} else {
echo "SNMP Unreachable";
$status = "0";
$status_type = ' (snmp)';
}
} else {
echo "Unpingable";
$status = "0";
$status_type = ' (ping)';
}
if ($device['status'] != $status) {
$poll_update .= $poll_separator . "`status` = '{$status}'";
$poll_separator = ", ";
dbUpdate(array('status' => $status), 'devices', 'device_id=?', array($device['device_id']));
dbInsert(array('importance' => '0', 'device_id' => $device['device_id'], 'message' => "Device is " . ($status == '1' ? 'up' : 'down')), 'alerts');
log_event('Device status changed to ' . ($status == '1' ? 'Up' : 'Down') . $status_type, $device, 'system');
notify($device, "Device " . ($status == '1' ? 'Up' : 'Down') . ": " . $device['hostname'] . $status_type, "Device " . ($status == '1' ? 'up' : 'down') . ": " . $device['hostname']);
}
$rrd = $config['rrd_dir'] . "/" . $device['hostname'] . "/status.rrd";
if (!is_file($rrd)) {
rrdtool_create($rrd, "DS:status:GAUGE:600:0:1 ");
}
if ($status == "1" || $status == "0") {
rrdtool_update($rrd, "N:" . $status);
} else {
rrdtool_update($rrd, "N:U");
}
// Ping response RRD database.
$ping_rrd = $config['rrd_dir'] . '/' . $device['hostname'] . '/ping.rrd';
if (!is_file($ping_rrd)) {
rrdtool_create($ping_rrd, "DS:ping:GAUGE:600:0:65535 ");
}
if ($device['pingable']) {
rrdtool_update($ping_rrd, "N:" . $device['pingable']);
} else {
rrdtool_update($ping_rrd, "N:U");
}
// SNMP response RRD database.
$ping_snmp_rrd = $config['rrd_dir'] . '/' . $device['hostname'] . '/ping_snmp.rrd';
if (!is_file($ping_snmp_rrd)) {
rrdtool_create($ping_snmp_rrd, "DS:ping_snmp:GAUGE:600:0:65535 ");
}
if ($device['snmpable']) {
rrdtool_update($ping_snmp_rrd, "N:" . $device['snmpable']);
} else {
rrdtool_update($ping_snmp_rrd, "N:U");
}
if ($status == "1") {
$graphs = array();
$oldgraphs = array();
// Enable Ping graphs
$graphs['ping'] = TRUE;
// Enable SNMP graphs
$graphs['ping_snmp'] = TRUE;
// Run this base modules always and before all other modules!
$poll_modules = array('system', 'os');
if ($options['m']) {
foreach (explode(',', $options['m']) as $module) {
$module = trim($module);
if (in_array($module, $poll_modules)) {
continue;
}
// Skip already added modules
//.........这里部分代码省略.........
示例15: poll_device
function poll_device($device, $options)
{
global $config, $device, $polled_devices, $db_stats, $memcache, $exec_status, $alert_rules, $alert_table, $graphs, $attribs;
$alert_metrics = array();
$oid_cache = array();
$old_device_state = unserialize($device['device_state']);
$attribs = get_dev_attribs($device['device_id']);
$alert_rules = cache_alert_rules();
$alert_table = cache_device_alert_table($device['device_id']);
if (OBS_DEBUG > 1 && (count($alert_rules) || count($alert_table))) {
print_vars($alert_rules);
print_vars($alert_table);
}
$status = 0;
unset($array);
$device_start = utime();
// Start counting device poll time
echo $device['hostname'] . " " . $device['device_id'] . " " . $device['os'] . " ";
if ($config['os'][$device['os']]['group']) {
$device['os_group'] = $config['os'][$device['os']]['group'];
echo "(" . $device['os_group'] . ")";
}
echo "\n";
unset($poll_update);
unset($poll_update_query);
unset($poll_separator);
$poll_update_array = array();
$host_rrd_dir = $config['rrd_dir'] . "/" . $device['hostname'];
if (!is_dir($host_rrd_dir)) {
mkdir($host_rrd_dir);
echo "创建的目录 : {$host_rrd_dir}\n";
}
$try_a = !($device['snmp_transport'] == 'udp6' || $device['snmp_transport'] == 'tcp6');
// Use IPv6 only if transport 'udp6' or 'tcp6'
$device['pingable'] = isPingable($device['hostname'], $try_a);
if ($device['pingable']) {
$device['snmpable'] = isSNMPable($device);
if ($device['snmpable']) {
$status = "1";
$status_type = '';
} else {
echo "SNMP 无法访问";
$status = "0";
$status_type = 'snmp';
}
} else {
echo "Unpingable";
$status = "0";
$status_type = 'ping';
}
if ($device['status'] != $status) {
$poll_update .= $poll_separator . "`status` = '{$status}'";
$poll_separator = ", ";
dbUpdate(array('status' => $status), 'devices', 'device_id = ?', array($device['device_id']));
dbInsert(array('importance' => '0', 'device_id' => $device['device_id'], 'message' => "设备的 " . ($status == '1' ? 'up' : 'down')), 'alerts');
$event_msg = '设备状态变更为 ';
if ($status == '1') {
// Device Up, Severity Warning (4)
$event_msg .= 'Up';
$event_severity = 4;
} else {
// Device Down, Severity Error (3)!
$event_msg .= 'Down';
$event_severity = 3;
}
if ($status_type != '') {
$event_msg .= ' (' . $status_type . ')';
}
log_event($event_msg, $device, 'device', $device['device_id'], $event_severity);
}
$rrd_filename = "status.rrd";
rrdtool_create($device, $rrd_filename, "DS:status:GAUGE:600:0:1 ");
if ($status == "1" || $status == "0") {
rrdtool_update($device, $rrd_filename, "N:" . $status);
} else {
rrdtool_update($device, $rrd_filename, "N:U");
}
// Ping response RRD database.
$ping_rrd = 'ping.rrd';
rrdtool_create($device, $ping_rrd, "DS:ping:GAUGE:600:0:65535 ");
if ($device['pingable']) {
rrdtool_update($device, $ping_rrd, "N:" . $device['pingable']);
} else {
rrdtool_update($device, $ping_rrd, "N:U");
}
// SNMP response RRD database.
$ping_snmp_rrd = 'ping_snmp.rrd';
rrdtool_create($device, $ping_snmp_rrd, "DS:ping_snmp:GAUGE:600:0:65535 ");
if ($device['snmpable']) {
rrdtool_update($device, $ping_snmp_rrd, "N:" . $device['snmpable']);
} else {
rrdtool_update($device, $ping_snmp_rrd, "N:U");
}
$alert_metrics['device_status'] = $status;
$alert_metrics['device_status_type'] = $status_type;
$alert_metrics['device_ping'] = $device['pingable'];
$alert_metrics['device_snmp'] = $device['snmpable'];
if ($status == "1") {
// Arrays for store and check enabled/disabled graphs
$graphs = array();
//.........这里部分代码省略.........