refactor: Centralise device up/down check and use in disco #5862 (#5897)

* refactor: Centralise device up/down check and use in disco #5862

* Remove extra indentation
This commit is contained in:
Neil Lathwood
2017-02-15 15:22:09 +00:00
committed by GitHub
parent 3fd56d7297
commit 38168832a6
3 changed files with 54 additions and 46 deletions

View File

@@ -102,6 +102,12 @@ function discover_device($device, $options = null)
// Start counting device poll time // Start counting device poll time
echo $device['hostname'] . ' ' . $device['device_id'] . ' ' . $device['os'] . ' '; echo $device['hostname'] . ' ' . $device['device_id'] . ' ' . $device['os'] . ' ';
$response = device_is_up($device, true);
if ($response['status'] !== '1') {
return;
}
if ($device['os'] == 'generic') { if ($device['os'] == 'generic') {
// verify if OS has changed from generic // verify if OS has changed from generic
$device['os'] = getHostOS($device); $device['os'] = getHostOS($device);
@@ -138,14 +144,15 @@ function discover_device($device, $options = null)
if ($force_module === true || if ($force_module === true ||
$attribs['discover_' . $module] || $attribs['discover_' . $module] ||
($os_module_status && !isset($attribs['discover_' . $module])) || ($os_module_status && !isset($attribs['discover_' . $module])) ||
($module_status && !isset($os_module_status) && !isset($attribs['discover_' . $module]))) { ($module_status && !isset($os_module_status) && !isset($attribs['discover_' . $module]))
) {
$module_start = microtime(true); $module_start = microtime(true);
$start_memory = memory_get_usage(); $start_memory = memory_get_usage();
echo "\n#### Load disco module $module ####\n"; echo "\n#### Load disco module $module ####\n";
include "includes/discovery/$module.inc.php"; include "includes/discovery/$module.inc.php";
$module_time = microtime(true) - $module_start; $module_time = microtime(true) - $module_start;
$module_time = substr($module_time, 0, 5); $module_time = substr($module_time, 0, 5);
$module_mem = (memory_get_usage() - $start_memory); $module_mem = (memory_get_usage() - $start_memory);
printf("\n>> Runtime for discovery module '%s': %.4f seconds with %s bytes\n", $module, $module_time, $module_mem); printf("\n>> Runtime for discovery module '%s': %.4f seconds with %s bytes\n", $module, $module_time, $module_mem);
echo "#### Unload disco module $module ####\n\n"; echo "#### Unload disco module $module ####\n\n";
} elseif (isset($attribs['discover_' . $module]) && $attribs['discover_' . $module] == '0') { } elseif (isset($attribs['discover_' . $module]) && $attribs['discover_' . $module] == '0') {

View File

@@ -1994,6 +1994,46 @@ function recordSnmpStatistic($stat, $start_time)
return $runtime; return $runtime;
} }
/**
* @param $device
* @param bool $record_perf
* @return array
*/
function device_is_up($device, $record_perf = false)
{
$address_family = snmpTransportToAddressFamily($device['transport']);
$ping_response = isPingable($device['hostname'], $address_family, $device['attribs']);
$device_perf = $ping_response['db'];
$device_perf['device_id'] = $device['device_id'];
$device_perf['timestamp'] = array('NOW()');
if ($record_perf === true && can_ping_device($device['attribs']) === true) {
dbInsert($device_perf, 'device_perf');
}
$response = array();
$response['ping_time'] = $ping_response['last_ping_timetaken'];
if ($ping_response['result']) {
if (isSNMPable($device)) {
$response['status'] = '1';
$response['status_reason'] = '';
} else {
echo 'SNMP Unreachable';
$response['status'] = '0';
$response['status_reason'] = 'snmp';
}
} else {
echo 'Unpingable';
$response['status'] = '0';
$response['status_reason'] = 'icmp';
}
if ($device['status'] != $response['status']) {
dbUpdate(array('status' => $response['status'], 'status_reason' => $response['status_reason']), 'devices', 'device_id=?', array($device['device_id']));
log_event('Device status changed to '.($response['status'] == '1' ? 'Up' : 'Down'). ' from ' . $response['status_reason'] . ' check.', $device, ($response['status'] == '1' ? 'up' : 'down'));
}
return $response;
}
function update_device_logo(&$device) function update_device_logo(&$device)
{ {
$icon = getImageName($device, false); $icon = getImageName($device, false);

View File

@@ -193,7 +193,6 @@ function poll_device($device, $options)
$device['snmp_max_repeaters'] = $attribs['snmp_max_repeaters']; $device['snmp_max_repeaters'] = $attribs['snmp_max_repeaters'];
$device['snmp_max_oid'] = $attribs['snmp_max_oid']; $device['snmp_max_oid'] = $attribs['snmp_max_oid'];
$status = 0;
unset($array); unset($array);
$device_start = microtime(true); $device_start = microtime(true);
// Start counting device poll time // Start counting device poll time
@@ -227,47 +226,9 @@ function poll_device($device, $options)
echo "Created directory : $host_rrd\n"; echo "Created directory : $host_rrd\n";
} }
$address_family = snmpTransportToAddressFamily($device['transport']); $response = device_is_up($device, true);
$ping_response = isPingable($device['hostname'], $address_family, $attribs); if ($response['status'] == '1') {
$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'), 4);
}
if ($status == '1') {
$graphs = array(); $graphs = array();
$oldgraphs = array(); $oldgraphs = array();
@@ -374,16 +335,16 @@ function poll_device($device, $options)
} }
// Ping response // Ping response
if (can_ping_device($attribs) === true && !empty($ping_time)) { if (can_ping_device($attribs) === true && !empty($response['ping_time'])) {
$tags = array( $tags = array(
'rrd_def' => 'DS:ping:GAUGE:600:0:65535', 'rrd_def' => 'DS:ping:GAUGE:600:0:65535',
); );
$fields = array( $fields = array(
'ping' => $ping_time, 'ping' => $response['ping_time'],
); );
$update_array['last_ping'] = array('NOW()'); $update_array['last_ping'] = array('NOW()');
$update_array['last_ping_timetaken'] = $ping_time; $update_array['last_ping_timetaken'] = $response['ping_time'];
data_update($device, 'ping-perf', $tags, $fields); data_update($device, 'ping-perf', $tags, $fields);
} }