From 427878dcaac1ad33ec789e44839725638c2ec0f8 Mon Sep 17 00:00:00 2001 From: SourceDoctor Date: Fri, 28 Aug 2020 13:01:54 +0200 Subject: [PATCH] Availability Calculation for all Devices (#12004) * Availability Calculation for all Devices * remove not more need uptime column --- ...8_28_212054_drop_uptime_column_outages.php | 34 +++++++++ includes/functions.php | 38 +++++----- includes/polling/availability.inc.php | 74 +++++++++---------- includes/polling/functions.inc.php | 4 +- misc/config_definitions.json | 7 -- misc/db_schema.yaml | 1 - 6 files changed, 90 insertions(+), 68 deletions(-) create mode 100644 database/migrations/2020_08_28_212054_drop_uptime_column_outages.php diff --git a/database/migrations/2020_08_28_212054_drop_uptime_column_outages.php b/database/migrations/2020_08_28_212054_drop_uptime_column_outages.php new file mode 100644 index 0000000000..efa4f68283 --- /dev/null +++ b/database/migrations/2020_08_28_212054_drop_uptime_column_outages.php @@ -0,0 +1,34 @@ +dropColumn([ + 'uptime', + ]); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('device_outages', function (Blueprint $table) { + $table->bigInteger('uptime')->nullable(); + }); + } +} diff --git a/includes/functions.php b/includes/functions.php index 4250295bd0..9e8ed48ce6 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -2125,35 +2125,33 @@ function device_is_up($device, $record_perf = false) array($device['device_id']) ); + $uptime = $device['uptime'] ?: 0; + if ($response['status']) { $type = 'up'; $reason = $device['status_reason']; - if ($device['uptime']) { - $going_down = dbFetchCell('SELECT going_down FROM device_outages WHERE device_id=? AND up_again IS NULL', array($device['device_id'])); - if (!empty($going_down)) { - $up_again = time() - $device['uptime']; - if ($up_again <= $going_down) { - # network connection loss, not device down - $up_again = time(); - } - dbUpdate( - array('device_id' => $device['device_id'], 'up_again' => $up_again), - 'device_outages', - 'device_id=? and up_again is NULL', - array($device['device_id']) - ); + $going_down = dbFetchCell('SELECT going_down FROM device_outages WHERE device_id=? AND up_again IS NULL', array($device['device_id'])); + if (!empty($going_down)) { + $up_again = time() - $uptime; + if ($up_again <= $going_down) { + # network connection loss, not device down + $up_again = time(); } + dbUpdate( + array('device_id' => $device['device_id'], 'up_again' => $up_again), + 'device_outages', + 'device_id=? and up_again is NULL', + array($device['device_id']) + ); } } else { $type = 'down'; $reason = $response['status_reason']; - if ($device['uptime']) { - $data = ['device_id' => $device['device_id'], - 'uptime' => $device['uptime'], - 'going_down' => strtotime($device['last_polled'])]; - dbInsert($data, 'device_outages'); - } + + $data = ['device_id' => $device['device_id'], + 'going_down' => strtotime($device['last_polled'])]; + dbInsert($data, 'device_outages'); } log_event('Device status changed to ' . ucfirst($type) . " from $reason check.", $device, $type); diff --git a/includes/polling/availability.inc.php b/includes/polling/availability.inc.php index 556a4eb7ad..62967a0724 100644 --- a/includes/polling/availability.inc.php +++ b/includes/polling/availability.inc.php @@ -3,44 +3,42 @@ use LibreNMS\Config; use LibreNMS\RRD\RrdDefinition; -if (isset($device['uptime']) && ($device['uptime'] > 0 )) { - $os->enableGraph('availability'); +$os->enableGraph('availability'); - $col = dbFetchColumn('SELECT duration FROM availability WHERE device_id = ?', array($device['device_id'])); - foreach (Config::get('graphing.availability') as $duration) { - if (!in_array($duration, $col)) { - $data = ['device_id' => $device['device_id'], - 'duration' => $duration]; - dbInsert($data, 'availability'); - } +$col = dbFetchColumn('SELECT duration FROM availability WHERE device_id = ?', array($device['device_id'])); +foreach (Config::get('graphing.availability') as $duration) { + if (!in_array($duration, $col)) { + $data = ['device_id' => $device['device_id'], + 'duration' => $duration]; + dbInsert($data, 'availability'); } - - echo 'Availability: ' . PHP_EOL; - - foreach (dbFetchRows('SELECT * FROM availability WHERE device_id = ?', array($device['device_id'])) as $row) { - //delete not more interested availabilities - if (!in_array($row['duration'], Config::get('graphing.availability'))) { - dbDelete('availability', 'availability_id=?', array($row['availability_id'])); - continue; - } - - $avail = \LibreNMS\Device\Availability::availability($device, $row['duration']); - $human_time = \LibreNMS\Util\Time::humanTime($row['duration']); - - $rrd_name = array('availability', $row['duration']); - $rrd_def = RrdDefinition::make() - ->addDataset('availability', 'GAUGE', 0); - - $fields = array( - 'availability' => $avail, - ); - - $tags = array('name' => $row['duration'], 'rrd_def' => $rrd_def, 'rrd_name' => $rrd_name); - data_update($device, 'availability', $tags, $fields); - - dbUpdate(array('availability_perc' => $avail), 'availability', '`availability_id` = ?', array($row['availability_id'])); - - echo $human_time . ' : ' . $avail . '%'. PHP_EOL; - } - unset($duration); } + +echo 'Availability: ' . PHP_EOL; + +foreach (dbFetchRows('SELECT * FROM availability WHERE device_id = ?', array($device['device_id'])) as $row) { + //delete not more interested availabilities + if (!in_array($row['duration'], Config::get('graphing.availability'))) { + dbDelete('availability', 'availability_id=?', array($row['availability_id'])); + continue; + } + + $avail = \LibreNMS\Device\Availability::availability($device, $row['duration']); + $human_time = \LibreNMS\Util\Time::humanTime($row['duration']); + + $rrd_name = array('availability', $row['duration']); + $rrd_def = RrdDefinition::make() + ->addDataset('availability', 'GAUGE', 0); + + $fields = array( + 'availability' => $avail, + ); + + $tags = array('name' => $row['duration'], 'rrd_def' => $rrd_def, 'rrd_name' => $rrd_name); + data_update($device, 'availability', $tags, $fields); + + dbUpdate(array('availability_perc' => $avail), 'availability', '`availability_id` = ?', array($row['availability_id'])); + + echo $human_time . ' : ' . $avail . '%'. PHP_EOL; +} +unset($duration); diff --git a/includes/polling/functions.inc.php b/includes/polling/functions.inc.php index 8f0b7cfd0b..9a20c70075 100644 --- a/includes/polling/functions.inc.php +++ b/includes/polling/functions.inc.php @@ -291,10 +291,10 @@ function poll_device($device, $force_module = false) if ($response['status'] == '1') { if ($device['snmp_disable']) { - Config::set('poller_modules', []); + Config::set('poller_modules', ['availability' => true]); } else { // we always want the core module to be included, prepend it - Config::set('poller_modules', ['core' => true] + Config::get('poller_modules')); + Config::set('poller_modules', ['core' => true, 'availability' => true] + Config::get('poller_modules')); } printChangedStats(true); // don't count previous stats diff --git a/misc/config_definitions.json b/misc/config_definitions.json index d0bb4ecde3..2a8d7e1bf3 100644 --- a/misc/config_definitions.json +++ b/misc/config_definitions.json @@ -4091,13 +4091,6 @@ "default": false, "type": "boolean" }, - "poller_modules.availability": { - "order": 25, - "group": "poller", - "section": "poller_modules", - "default": true, - "type": "boolean" - }, "poller_modules.os": { "order": 320, "group": "poller", diff --git a/misc/db_schema.yaml b/misc/db_schema.yaml index d86bd126fd..fc22af61e2 100644 --- a/misc/db_schema.yaml +++ b/misc/db_schema.yaml @@ -575,7 +575,6 @@ device_outages: - { Field: device_id, Type: 'int unsigned', 'Null': false, Extra: '' } - { Field: going_down, Type: bigint, 'Null': false, Extra: '' } - { Field: up_again, Type: bigint, 'Null': true, Extra: '' } - - { Field: uptime, Type: bigint, 'Null': true, Extra: '' } Indexes: device_outages_device_id_index: { Name: device_outages_device_id_index, Columns: [device_id], Unique: false, Type: BTREE } device_outages_device_id_going_down_unique: { Name: device_outages_device_id_going_down_unique, Columns: [device_id, going_down], Unique: true, Type: BTREE }