Skip to content

Commit c656575

Browse files
authored
[LTM1.6] Allow metrics to be enabled selectively and disable all by default. (#362)
* Allow metrics to be enabled selectively and disable all by default. * Update docs.
1 parent 1039cf3 commit c656575

File tree

6 files changed

+64
-27
lines changed

6 files changed

+64
-27
lines changed

development/dev.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ POSTGRES_HOST=postgres
5050
MYSQL_DATABASE=nautobot
5151
MYSQL_USER=nautobot
5252
MYSQL_ROOT_HOST=%
53+
54+
NAUTOBOT_DLM_ENABLED_METRICS = "nautobot_lcm_software_compliance_per_device_type,nautobot_lcm_software_compliance_per_inventory_item,nautobot_lcm_hw_end_of_support_per_part_number,nautobot_metrics_lcm_hw_end_of_support_site"

development/nautobot_config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@
234234
# }
235235

236236
# Expose Prometheus monitoring metrics at the HTTP endpoint '/metrics'
237-
METRICS_ENABLED = False
237+
METRICS_ENABLED = True
238238

239239
# Credentials that Nautobot will uses to authenticate to devices when connecting via NAPALM.
240240
NAPALM_USERNAME = os.environ.get("NAPALM_USERNAME", "")
@@ -262,6 +262,7 @@
262262
"barchart_bar_width": float(os.environ.get("BARCHART_BAR_WIDTH", 0.1)),
263263
"barchart_width": int(os.environ.get("BARCHART_WIDTH", 12)),
264264
"barchart_height": int(os.environ.get("BARCHART_HEIGHT", 5)),
265+
"enabled_metrics": [x for x in os.environ.get("NAUTOBOT_DLM_ENABLED_METRICS", "").split(",") if x],
265266
},
266267
}
267268

docs/admin/install.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ PLUGINS_CONFIG = {
4444
"barchart_bar_width": float(os.environ.get("BARCHART_BAR_WIDTH", 0.1)),
4545
"barchart_width": int(os.environ.get("BARCHART_WIDTH", 12)),
4646
"barchart_height": int(os.environ.get("BARCHART_HEIGHT", 5)),
47+
"enabled_metrics": [x for x in os.environ.get("NAUTOBOT_DLM_ENABLED_METRICS", "").split(",") if x],
4748
},
4849
}
4950
```
@@ -72,8 +73,21 @@ sudo systemctl restart nautobot nautobot-worker nautobot-scheduler
7273

7374
The plugin behavior can be controlled with the following list of settings.
7475

75-
| Key | Example | Default | Description |
76-
| ------- | ------ | -------- | ------------------------------------- |
77-
| enable_backup | True | True | A boolean to represent whether or not to run backup configurations within the plugin. |
78-
| platform_slug_map | {"cisco_wlc": "cisco_aireos"} | None | A dictionary in which the key is the platform slug and the value is what netutils uses in any "network_os" parameter. |
79-
| per_feature_bar_width | 0.15 | 0.15 | The width of the table bar within the overview report |
76+
| Key | ENV VAR | Example | Default | Description |
77+
| ------- | ------ | ------ | -------- | ------------------------------------- |
78+
| barchart_bar_width | BARCHART_BAR_WIDTH | 0.15 | 0.1 | The width of the table bar within the overview report. |
79+
| barchart_width | BARCHART_WIDTH | 15 | 12 | The width of the barchart within the overview report. |
80+
| barchart_height | BARCHART_HEIGHT | 8 | 5 | The height of the barchart within the overview report. |
81+
| enabled_metrics | NAUTOBOT_DLM_ENABLED_METRICS | `["nautobot_metrics_lcm_hw_end_of_support_site"]` | `[]` | Enables metrics corresponding to the provided, comma separated, entries. |
82+
83+
### Available Metric Names
84+
85+
Following are the metric names that can be defined in `enabled_metrics`:
86+
87+
- `nautobot_lcm_software_compliance_per_device_type`: Number of devices with valid/invalid software by device_type.
88+
89+
- `nautobot_lcm_software_compliance_per_inventory_item`: Number of inventory items with valid/invalid software.
90+
91+
- `nautobot_lcm_hw_end_of_support_per_part_number`: Number of End of Support devices and inventory items per Part Number.
92+
93+
- `nautobot_metrics_lcm_hw_end_of_support_site`: Number of End of Support devices and inventory items per Site.

nautobot_device_lifecycle_mgmt/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class DeviceLifeCycleConfig(PluginConfig):
2929
"barchart_bar_width": 0.1,
3030
"barchart_width": 12,
3131
"barchart_height": 5,
32+
"enabled_metrics": [],
3233
}
3334
caching_config = {}
3435

nautobot_device_lifecycle_mgmt/metrics.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Nautobot Device LCM plugin application level metrics ."""
22
from datetime import datetime
33

4+
from django.conf import settings
45
from django.db.models import Count, F, IntegerField, OuterRef, Q, Subquery, Value
56
from django.db.models.functions import Coalesce
67
from nautobot.dcim.models import Device, DeviceType, InventoryItem, Site
@@ -12,6 +13,8 @@
1213
InventoryItemSoftwareValidationResult,
1314
)
1415

16+
PLUGIN_CFG = settings.PLUGINS_CONFIG["nautobot_device_lifecycle_mgmt"]
17+
1518

1619
def metrics_lcm_validation_report_device_type():
1720
"""Calculate number of devices with valid/invalid software by device_type.
@@ -111,8 +114,8 @@ def metrics_lcm_validation_report_inventory_item():
111114
yield inventory_item_software_compliance_gauge
112115

113116

114-
def metrics_lcm_hw_end_of_support(): # pylint: disable=too-many-locals
115-
"""Calculate number of End of Support devices and inventory items per Part Number and per Site.
117+
def metrics_lcm_hw_end_of_support_part_number(): # pylint: disable=too-many-locals
118+
"""Calculate number of End of Support devices and inventory items per Part Number.
116119
117120
Yields:
118121
GaugeMetricFamily: Prometheus Metrics
@@ -122,9 +125,6 @@ def metrics_lcm_hw_end_of_support(): # pylint: disable=too-many-locals
122125
"Nautobot LCM Hardware End of Support per Part Number",
123126
labels=["part_number"],
124127
)
125-
hw_end_of_support_site_gauge = GaugeMetricFamily(
126-
"nautobot_lcm_hw_end_of_support_per_site", "Nautobot LCM Hardware End of Support per Site", labels=["site"]
127-
)
128128

129129
today = datetime.today().date()
130130
hw_end_of_support = HardwareLCM.objects.filter(end_of_support__lt=today)
@@ -177,6 +177,25 @@ def metrics_lcm_hw_end_of_support(): # pylint: disable=too-many-locals
177177

178178
yield hw_end_of_support_part_number_gauge
179179

180+
181+
def metrics_lcm_hw_end_of_support_site(): # pylint: disable=too-many-locals
182+
"""Calculate number of End of Support devices and inventory items per Site.
183+
184+
Yields:
185+
GaugeMetricFamily: Prometheus Metrics
186+
"""
187+
hw_end_of_support_site_gauge = GaugeMetricFamily(
188+
"nautobot_lcm_hw_end_of_support_per_site", "Nautobot LCM Hardware End of Support per Site", labels=["site"]
189+
)
190+
191+
today = datetime.today().date()
192+
hw_end_of_support = HardwareLCM.objects.filter(end_of_support__lt=today)
193+
hw_end_of_support_device_types = hw_end_of_support.exclude(device_type__isnull=True).values_list(
194+
"device_type", flat=True
195+
)
196+
hw_end_of_support_invitems = hw_end_of_support.exclude(inventory_item__isnull=True).values_list(
197+
"inventory_item", flat=True
198+
)
180199
# Initialize per site count to 0 for all sites
181200
init_site_counts = Site.objects.values(site_slug=F("slug")).annotate(
182201
site_count=Value(0, output_field=IntegerField())
@@ -214,8 +233,12 @@ def metrics_lcm_hw_end_of_support(): # pylint: disable=too-many-locals
214233
yield hw_end_of_support_site_gauge
215234

216235

217-
metrics = [
218-
metrics_lcm_hw_end_of_support,
219-
metrics_lcm_validation_report_device_type,
220-
metrics_lcm_validation_report_inventory_item,
221-
]
236+
metrics = []
237+
if "nautobot_lcm_software_compliance_per_device_type" in PLUGIN_CFG["enabled_metrics"]:
238+
metrics.append(metrics_lcm_validation_report_device_type)
239+
if "nautobot_lcm_software_compliance_per_inventory_item" in PLUGIN_CFG["enabled_metrics"]:
240+
metrics.append(metrics_lcm_validation_report_inventory_item)
241+
if "nautobot_lcm_hw_end_of_support_per_part_number" in PLUGIN_CFG["enabled_metrics"]:
242+
metrics.append(metrics_lcm_hw_end_of_support_part_number)
243+
if "nautobot_metrics_lcm_hw_end_of_support_site" in PLUGIN_CFG["enabled_metrics"]:
244+
metrics.append(metrics_lcm_hw_end_of_support_site)

nautobot_device_lifecycle_mgmt/tests/test_metrics.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
from django.test import TestCase
44

55
from nautobot_device_lifecycle_mgmt.metrics import (
6-
metrics_lcm_hw_end_of_support,
6+
metrics_lcm_hw_end_of_support_part_number,
7+
metrics_lcm_hw_end_of_support_site,
78
metrics_lcm_validation_report_device_type,
89
metrics_lcm_validation_report_inventory_item,
910
)
@@ -53,22 +54,20 @@ def test_metrics_lcm_validation_report_inventory_item(self):
5354
sample_labels = tuple(sample.labels.items())
5455
self.assertEqual(expected_ts_samples[sample_labels], sample.value)
5556

56-
def test_metrics_lcm_hw_end_of_support_does_not_error(self):
57+
def test_metrics_lcm_hw_end_of_support_site_does_not_error(self):
5758
"""Query providing data to hw_end_of_support_site_gauge metric should not error out.
5859
Guards against https://github.com/nautobot/nautobot-app-device-lifecycle-mgmt/issues/309
5960
"""
60-
metric_gen = metrics_lcm_hw_end_of_support()
61-
# skip hw_end_of_support_part_number_gauge
62-
next(metric_gen)
61+
metric_gen = metrics_lcm_hw_end_of_support_site()
6362
try:
6463
# Get hw_end_of_support_site_gauge
6564
next(metric_gen)
6665
except ProgrammingError:
67-
self.fail("hw_end_of_support_site_gauge query bug")
66+
self.fail("hw_end_of_support_site query bug")
6867

6968
def test_metrics_lcm_hw_end_of_support_part_number(self):
7069
"""Test metric hw_end_of_support_part_number_gauge."""
71-
metric_gen = metrics_lcm_hw_end_of_support()
70+
metric_gen = metrics_lcm_hw_end_of_support_part_number()
7271

7372
# Get hw_end_of_support_part_number_gauge
7473
metric = next(metric_gen)
@@ -86,10 +85,7 @@ def test_metrics_lcm_hw_end_of_support_part_number(self):
8685

8786
def test_metrics_lcm_hw_end_of_support_site_gauge(self):
8887
"""Test metric hw_end_of_support_site_gauge."""
89-
metric_gen = metrics_lcm_hw_end_of_support()
90-
91-
# skip hw_end_of_support_part_number_gauge
92-
next(metric_gen)
88+
metric_gen = metrics_lcm_hw_end_of_support_site()
9389

9490
# Get hw_end_of_support_site_gauge
9591
metric = next(metric_gen)

0 commit comments

Comments
 (0)