From 38168832a667165506b0b53e5d7e28c4936ac183 Mon Sep 17 00:00:00 2001 From: Neil Lathwood Date: Wed, 15 Feb 2017 15:22:09 +0000 Subject: [PATCH] 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 --- includes/discovery/functions.inc.php | 11 +++++-- includes/functions.php | 40 +++++++++++++++++++++++ includes/polling/functions.inc.php | 49 +++------------------------- 3 files changed, 54 insertions(+), 46 deletions(-) diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index c724af96ad..22a445dc27 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -102,6 +102,12 @@ function discover_device($device, $options = null) // Start counting device poll time echo $device['hostname'] . ' ' . $device['device_id'] . ' ' . $device['os'] . ' '; + $response = device_is_up($device, true); + + if ($response['status'] !== '1') { + return; + } + if ($device['os'] == 'generic') { // verify if OS has changed from generic $device['os'] = getHostOS($device); @@ -138,14 +144,15 @@ function discover_device($device, $options = null) if ($force_module === true || $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); $start_memory = memory_get_usage(); echo "\n#### Load disco module $module ####\n"; include "includes/discovery/$module.inc.php"; $module_time = microtime(true) - $module_start; $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); echo "#### Unload disco module $module ####\n\n"; } elseif (isset($attribs['discover_' . $module]) && $attribs['discover_' . $module] == '0') { diff --git a/includes/functions.php b/includes/functions.php index aab0b2b8e1..d1f60a2e7d 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1994,6 +1994,46 @@ function recordSnmpStatistic($stat, $start_time) 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) { $icon = getImageName($device, false); diff --git a/includes/polling/functions.inc.php b/includes/polling/functions.inc.php index b85cd010f5..a11c5f521a 100644 --- a/includes/polling/functions.inc.php +++ b/includes/polling/functions.inc.php @@ -193,7 +193,6 @@ function poll_device($device, $options) $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 @@ -227,47 +226,9 @@ function poll_device($device, $options) 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); - - $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') { + if ($response['status'] == '1') { $graphs = array(); $oldgraphs = array(); @@ -374,16 +335,16 @@ function poll_device($device, $options) } // Ping response - if (can_ping_device($attribs) === true && !empty($ping_time)) { + if (can_ping_device($attribs) === true && !empty($response['ping_time'])) { $tags = array( 'rrd_def' => 'DS:ping:GAUGE:600:0:65535', ); $fields = array( - 'ping' => $ping_time, + 'ping' => $response['ping_time'], ); $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); }