1
1
"""Nautobot Device LCM plugin application level metrics ."""
2
2
from datetime import datetime
3
3
4
+ from django .conf import settings
4
5
from django .db .models import Count , F , IntegerField , OuterRef , Q , Subquery , Value
5
6
from django .db .models .functions import Coalesce
6
7
from nautobot .dcim .models import Device , DeviceType , InventoryItem , Site
12
13
InventoryItemSoftwareValidationResult ,
13
14
)
14
15
16
+ PLUGIN_CFG = settings .PLUGINS_CONFIG ["nautobot_device_lifecycle_mgmt" ]
17
+
15
18
16
19
def metrics_lcm_validation_report_device_type ():
17
20
"""Calculate number of devices with valid/invalid software by device_type.
@@ -111,8 +114,8 @@ def metrics_lcm_validation_report_inventory_item():
111
114
yield inventory_item_software_compliance_gauge
112
115
113
116
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.
116
119
117
120
Yields:
118
121
GaugeMetricFamily: Prometheus Metrics
@@ -122,9 +125,6 @@ def metrics_lcm_hw_end_of_support(): # pylint: disable=too-many-locals
122
125
"Nautobot LCM Hardware End of Support per Part Number" ,
123
126
labels = ["part_number" ],
124
127
)
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
- )
128
128
129
129
today = datetime .today ().date ()
130
130
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
177
177
178
178
yield hw_end_of_support_part_number_gauge
179
179
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
+ )
180
199
# Initialize per site count to 0 for all sites
181
200
init_site_counts = Site .objects .values (site_slug = F ("slug" )).annotate (
182
201
site_count = Value (0 , output_field = IntegerField ())
@@ -214,8 +233,12 @@ def metrics_lcm_hw_end_of_support(): # pylint: disable=too-many-locals
214
233
yield hw_end_of_support_site_gauge
215
234
216
235
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 )
0 commit comments