Skip to content

Commit 3dc8638

Browse files
murrantStyleCIBot
andauthored
Device down status reason show all (librenms#19892)
* Refactor device connectivity code Changes status_reason to contain all down polling methods, comma separated Allow for individual polling method availability checks ConnectivityHelper implementation will be changed in future PR. This is to centralize checks and adjust module code. * Apply fixes from StyleCI * Docs and alerts * Keep status and status_reason consistent when user disables icmp or snmp * widen reasons and try to migrate existing rules and templates * style fixes * Cover more edge cases, update missed alert rule collection * fixes --------- Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent ce7240f commit 3dc8638

56 files changed

Lines changed: 437 additions & 167 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

LibreNMS/Alert/RunAlerts.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
use LibreNMS\Enum\MaintenanceStatus;
4848
use LibreNMS\Enum\Severity;
4949
use LibreNMS\Exceptions\AlertTransportDeliveryException;
50+
use LibreNMS\Exceptions\RrdException;
5051
use LibreNMS\Polling\ConnectivityHelper;
5152
use LibreNMS\Util\Number;
5253
use LibreNMS\Util\Time;
@@ -127,20 +128,20 @@ public function describeAlert($alert)
127128
$obj['proc'] = $alert['proc'];
128129
$obj['status'] = $device->status;
129130
$obj['status_reason'] = $device->status_reason;
130-
if (ConnectivityHelper::pingIsAllowed($device)) {
131+
132+
if ((new ConnectivityHelper($device))->icmpIsEnabled()) {
131133
try {
132134
$last_ping = Rrd::lastUpdate(Rrd::name($device->hostname, 'icmp-perf'));
133-
} catch (\Exception $e) {
135+
if ($last_ping) {
136+
$obj['ping_timestamp'] = $last_ping->timestamp;
137+
$obj['ping_loss'] = Number::calculatePercent($last_ping->get('xmt') - $last_ping->get('rcv'), $last_ping->get('xmt'));
138+
$obj['ping_min'] = $last_ping->get('min');
139+
$obj['ping_max'] = $last_ping->get('max');
140+
$obj['ping_avg'] = $last_ping->get('avg');
141+
$obj['debug'] = 'unsupported';
142+
}
143+
} catch (RrdException $e) {
134144
Log::error("Error getting last ping for device {$device->hostname}: {$e->getMessage()}");
135-
$last_ping = null;
136-
}
137-
if ($last_ping) {
138-
$obj['ping_timestamp'] = $last_ping->timestamp;
139-
$obj['ping_loss'] = Number::calculatePercent($last_ping->get('xmt') - $last_ping->get('rcv'), $last_ping->get('xmt'));
140-
$obj['ping_min'] = $last_ping->get('min');
141-
$obj['ping_max'] = $last_ping->get('max');
142-
$obj['ping_avg'] = $last_ping->get('avg');
143-
$obj['debug'] = 'unsupported';
144145
}
145146
}
146147
$extra = $alert['details'];
@@ -761,7 +762,7 @@ public function isParentDown($device)
761762
return false;
762763
}
763764

764-
$down_parent_count = dbFetchCell("SELECT count(*) from devices as d LEFT JOIN devices_attribs as a ON d.device_id=a.device_id LEFT JOIN device_relationships as r ON d.device_id=r.parent_device_id WHERE d.status=0 AND d.ignore=0 AND d.disabled=0 AND r.child_device_id=? AND (d.status_reason='icmp' OR (a.attrib_type='override_icmp_disable' AND a.attrib_value=true))", [$device]);
765+
$down_parent_count = dbFetchCell('SELECT count(*) from devices as d LEFT JOIN device_relationships as r ON d.device_id=r.parent_device_id WHERE d.status=0 AND d.ignore=0 AND d.disabled=0 AND r.child_device_id=?', [$device]);
765766
if ($down_parent_count == $parent_count) {
766767
return true;
767768
}

LibreNMS/Data/Source/Ipmitool.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Illuminate\Support\Facades\Log;
3434
use Illuminate\Support\Facades\Process;
3535
use LibreNMS\Exceptions\IpmiConnectionFailed;
36+
use LibreNMS\Polling\ConnectivityHelper;
3637

3738
class Ipmitool
3839
{
@@ -64,7 +65,7 @@ public static function init(?Device $device = null): ?self
6465
{
6566
$device ??= DeviceCache::getPrimary();
6667

67-
if ($device->getAttrib('ipmi_hostname') === null) {
68+
if (! (new ConnectivityHelper($device))->ipmiIsEnabled()) {
6869
return null;
6970
}
7071

LibreNMS/Data/Store/Rrd.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ public function write(string $measurement, array $fields, array $tags = [], arra
159159
}
160160
}
161161

162+
/**
163+
* @throws RrdException
164+
*/
162165
public function lastUpdate(string $filename): ?TimeSeriesPoint
163166
{
164167
$output = $this->command('lastupdate', $filename);

LibreNMS/Enum/AvailabilitySource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ enum AvailabilitySource: string
77
case None = '';
88
case Snmp = 'snmp';
99
case Icmp = 'icmp';
10+
case Both = 'icmp,snmp';
1011
}

LibreNMS/Interfaces/Module.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use App\Models\Device;
3030
use LibreNMS\Interfaces\Data\DataStorageInterface;
3131
use LibreNMS\OS;
32+
use LibreNMS\Polling\ConnectivityHelper;
3233
use LibreNMS\Polling\ModuleStatus;
3334

3435
interface Module
@@ -41,12 +42,12 @@ public function dependencies(): array;
4142
/**
4243
* Should this module be run?
4344
*/
44-
public function shouldDiscover(OS $os, ModuleStatus $status): bool;
45+
public function shouldDiscover(OS $os, ModuleStatus $status, ConnectivityHelper $connectivity): bool;
4546

4647
/**
4748
* Should polling run for this device?
4849
*/
49-
public function shouldPoll(OS $os, ModuleStatus $status): bool;
50+
public function shouldPoll(OS $os, ModuleStatus $status, ConnectivityHelper $connectivity): bool;
5051

5152
/**
5253
* Discover this module. Heavier processes can be run here

LibreNMS/Modules/ArpTable.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use LibreNMS\Interfaces\Discovery\ArpTableDiscovery;
1616
use LibreNMS\Interfaces\Module;
1717
use LibreNMS\OS;
18+
use LibreNMS\Polling\ConnectivityHelper;
1819
use LibreNMS\Polling\ModuleStatus;
1920
use LibreNMS\Util\Mac;
2021
use SnmpQuery;
@@ -34,17 +35,17 @@ public function dependencies(): array
3435
/**
3536
* @inheritDoc
3637
*/
37-
public function shouldDiscover(OS $os, ModuleStatus $status): bool
38+
public function shouldDiscover(OS $os, ModuleStatus $status, ConnectivityHelper $connectivity): bool
3839
{
39-
return $status->isEnabledAndDeviceUp($os->getDevice());
40+
return $status->isEnabled() && $connectivity->snmpIsAvailable();
4041
}
4142

4243
/**
4344
* @inheritDoc
4445
*/
45-
public function shouldPoll(OS $os, ModuleStatus $status): bool
46+
public function shouldPoll(OS $os, ModuleStatus $status, ConnectivityHelper $connectivity): bool
4647
{
47-
return $status->isEnabledAndDeviceUp($os->getDevice());
48+
return $status->isEnabled() && $connectivity->snmpIsAvailable();
4849
}
4950

5051
/**

LibreNMS/Modules/Availability.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use LibreNMS\Interfaces\Data\DataStorageInterface;
3232
use LibreNMS\Interfaces\Module;
3333
use LibreNMS\OS;
34+
use LibreNMS\Polling\ConnectivityHelper;
3435
use LibreNMS\Polling\ModuleStatus;
3536
use LibreNMS\RRD\RrdDefinition;
3637
use LibreNMS\Util\Time;
@@ -45,7 +46,7 @@ public function dependencies(): array
4546
return [];
4647
}
4748

48-
public function shouldDiscover(OS $os, ModuleStatus $status): bool
49+
public function shouldDiscover(OS $os, ModuleStatus $status, ConnectivityHelper $connectivity): bool
4950
{
5051
return false;
5152
}
@@ -60,9 +61,9 @@ public function discover(OS $os): void
6061
/**
6162
* @inheritDoc
6263
*/
63-
public function shouldPoll(OS $os, ModuleStatus $status): bool
64+
public function shouldPoll(OS $os, ModuleStatus $status, ConnectivityHelper $connectivity): bool
6465
{
65-
return $status->isEnabled();
66+
return $status->isEnabled() && $connectivity->hasAvailability();
6667
}
6768

6869
/**

LibreNMS/Modules/Core.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use LibreNMS\Interfaces\Data\DataStorageInterface;
3838
use LibreNMS\Interfaces\Module;
3939
use LibreNMS\OS;
40+
use LibreNMS\Polling\ConnectivityHelper;
4041
use LibreNMS\Polling\ModuleStatus;
4142
use LibreNMS\RRD\RrdDefinition;
4243
use LibreNMS\Util\Compare;
@@ -55,9 +56,9 @@ public function dependencies(): array
5556
return [];
5657
}
5758

58-
public function shouldDiscover(OS $os, ModuleStatus $status): bool
59+
public function shouldDiscover(OS $os, ModuleStatus $status, ConnectivityHelper $connectivity): bool
5960
{
60-
return ! $os->getDevice()->snmp_disable && $os->getDevice()->status;
61+
return $connectivity->snmpIsAvailable();
6162
}
6263

6364
public function discover(OS $os): void
@@ -99,9 +100,9 @@ public function discover(OS $os): void
99100
}
100101
}
101102

102-
public function shouldPoll(OS $os, ModuleStatus $status): bool
103+
public function shouldPoll(OS $os, ModuleStatus $status, ConnectivityHelper $connectivity): bool
103104
{
104-
return ! $os->getDevice()->snmp_disable && $os->getDevice()->status;
105+
return $connectivity->snmpIsAvailable();
105106
}
106107

107108
public function poll(OS $os, DataStorageInterface $datastore): void

LibreNMS/Modules/DiscoveryArp.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use LibreNMS\Interfaces\Data\DataStorageInterface;
1414
use LibreNMS\Interfaces\Module;
1515
use LibreNMS\OS;
16+
use LibreNMS\Polling\ConnectivityHelper;
1617
use LibreNMS\Polling\ModuleStatus;
1718
use LibreNMS\Util\IPv4;
1819

@@ -29,15 +30,15 @@ public function dependencies(): array
2930
/**
3031
* @inheritDoc
3132
*/
32-
public function shouldDiscover(OS $os, ModuleStatus $status): bool
33+
public function shouldDiscover(OS $os, ModuleStatus $status, ConnectivityHelper $connectivity): bool
3334
{
34-
return $status->isEnabledAndDeviceUp($os->getDevice());
35+
return $status->isEnabled() && $connectivity->snmpIsAvailable();
3536
}
3637

3738
/**
3839
* @inheritDoc
3940
*/
40-
public function shouldPoll(OS $os, ModuleStatus $status): bool
41+
public function shouldPoll(OS $os, ModuleStatus $status, ConnectivityHelper $connectivity): bool
4142
{
4243
return false;
4344
}

LibreNMS/Modules/EntityPhysical.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use LibreNMS\Interfaces\Data\DataStorageInterface;
1010
use LibreNMS\Interfaces\Module;
1111
use LibreNMS\OS;
12+
use LibreNMS\Polling\ConnectivityHelper;
1213
use LibreNMS\Polling\ModuleStatus;
1314

1415
class EntityPhysical implements Module
@@ -26,17 +27,17 @@ public function dependencies(): array
2627
/**
2728
* @inheritDoc
2829
*/
29-
public function shouldDiscover(OS $os, ModuleStatus $status): bool
30+
public function shouldDiscover(OS $os, ModuleStatus $status, ConnectivityHelper $connectivity): bool
3031
{
31-
return $status->isEnabledAndDeviceUp($os->getDevice());
32+
return $status->isEnabled() && $connectivity->snmpIsAvailable();
3233
}
3334

3435
/**
3536
* @inheritDoc
3637
*/
37-
public function shouldPoll(OS $os, ModuleStatus $status): bool
38+
public function shouldPoll(OS $os, ModuleStatus $status, ConnectivityHelper $connectivity): bool
3839
{
39-
return $status->isEnabledAndDeviceUp($os->getDevice());
40+
return $status->isEnabled() && $connectivity->snmpIsAvailable();
4041
}
4142

4243
/**

0 commit comments

Comments
 (0)