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 . ' '; } } //end if $device_end = utime(); $device_run = $device_end - $device_start; $device_time = substr($device_run, 0, 5); // TODO: These should be easy converts to rrd_create_update() // Poller performance rrd $poller_rrd = $config['rrd_dir'] . '/' . $device['hostname'] . '/poller-perf.rrd'; if (!is_file($poller_rrd)) { rrdtool_create($poller_rrd, 'DS:poller:GAUGE:600:0:U ' . $config['rrd_rra']); } if (!empty($device_time)) { $fields = array('poller' => $device_time); rrdtool_update($poller_rrd, $fields); } // Ping response rrd if (can_ping_device($attribs) === true) { $ping_rrd = $config['rrd_dir'] . '/' . $device['hostname'] . '/ping-perf.rrd'; if (!is_file($ping_rrd)) { rrdtool_create($ping_rrd, 'DS:ping:GAUGE:600:0:65535 ' . $config['rrd_rra']); } if (!empty($ping_time)) { $fields = array('ping' => $ping_time); rrdtool_update($ping_rrd, $fields); } $update_array['last_ping'] = array('NOW()'); $update_array['last_ping_timetaken'] = $ping_time; } $update_array['last_polled'] = array('NOW()'); $update_array['last_polled_timetaken'] = $device_time; // echo("$device_end - $device_start; $device_time $device_run"); echo "Polled in {$device_time} seconds\n"; d_echo('Updating ' . $device['hostname'] . "\n"); d_echo($update_array); $updated = dbUpdate($update_array, 'devices', '`device_id` = ?', array($device['device_id'])); if ($updated) { echo "UPDATED!\n"; } unset($storage_cache); // Clear cache of hrStorage ** MAYBE FIXME? ** unset($cache); // Clear cache (unify all things here?) } //end if }
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); // remove old rrd $oldrrd = rrd_name($device['hostname'], array('poller', $module, 'perf')); if (is_file($oldrrd)) { unlink($oldrrd); } } elseif (isset($attribs['poll_' . $module]) && $attribs['poll_' . $module] == '0') { echo "Module [ {$module} ] disabled on host.\n\n"; } elseif (isset($os_module_status) && $os_module_status == '0') { echo "Module [ {$module} ] disabled on os.\n\n"; } else { echo "Module [ {$module} ] disabled globally.\n\n"; } } // Update device_groups UpdateGroupsForDevice($device['device_id']); if (!isset($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 . ' '; } } //end if $device_end = microtime(true); $device_run = $device_end - $device_start; $device_time = substr($device_run, 0, 5); // Poller performance if (!empty($device_time)) { $tags = array('rrd_def' => 'DS:poller:GAUGE:600:0:U', 'module' => 'ALL'); $fields = array('poller' => $device_time); data_update($device, 'poller-perf', $tags, $fields); } // Ping response if (can_ping_device($attribs) === true && !empty($ping_time)) { $tags = array('rrd_def' => 'DS:ping:GAUGE:600:0:65535'); $fields = array('ping' => $ping_time); $update_array['last_ping'] = array('NOW()'); $update_array['last_ping_timetaken'] = $ping_time; data_update($device, 'ping-perf', $tags, $fields); } $update_array['last_polled'] = array('NOW()'); $update_array['last_polled_timetaken'] = $device_time; // echo("$device_end - $device_start; $device_time $device_run"); echo "Polled in {$device_time} seconds\n"; d_echo('Updating ' . $device['hostname'] . "\n"); $updated = dbUpdate($update_array, 'devices', '`device_id` = ?', array($device['device_id'])); if ($updated) { echo "UPDATED!\n"; } unset($storage_cache); // Clear cache of hrStorage ** MAYBE FIXME? ** unset($cache); // Clear cache (unify all things here?) } //end if }
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 (host_exists($host) === false) { if ($config['addhost_alwayscheckip'] === TRUE) { $ip = gethostbyname($host); } else { $ip = $host; } if (ip_exists($ip) === false) { // Test reachability $address_family = snmpTransportToAddressFamily($transport); if ($force_add == 1 || isPingable($host, $address_family)) { 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}"); } } } else { // found in database if ($quiet == '0') { print_error("Already got host {$host}"); } } return 0; }
/** * 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; }