Skip to content

Commit c1cdc24

Browse files
authored
Merge pull request #69 from tyler-8/metricscfg
Add support for Netbox metrics configurations
2 parents 88060dd + 62a8ee7 commit c1cdc24

File tree

7 files changed

+44
-1
lines changed

7 files changed

+44
-1
lines changed

README.adoc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,17 @@ netbox_webhooks_enabled: false
305305

306306
Toggle `netbox_webhooks_enabled` to `true` to enable webhooks for NetBox.
307307

308+
[source,yaml]
309+
netbox_metrics_enabled: true
310+
311+
Toggle `netbox_metrics_enabled` to `true` to enable application metrics (via https://github.com/korfuri/django-prometheus[django-prometheus]). This adds relevant pieces of configuration for proper metrics handling. (https://netbox.readthedocs.io/en/stable/additional-features/prometheus-metrics/[more info]).
312+
313+
[source,yaml]
314+
netbox_metrics_dir: netbox_metrics
315+
netbox_metrics_path: "/run/{{ netbox_metrics_dir }}"
316+
317+
The directory name where the metrics files are stored can be set with `netbox_metrics_dir`. However, `netbox_metrics_path` must remain the default (seen above) in order to work with `systemd` and the `RuntimeDirectory` parameter (which only points to `/run`).
318+
308319
[source,yaml]
309320
netbox_keep_uwsgi_updated: false
310321

defaults/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ netbox_redis_ssl_enabled: false
3232

3333
netbox_webhooks_enabled: false
3434

35+
netbox_metrics_enabled: false
36+
netbox_metrics_dir: netbox_metrics
37+
netbox_metrics_path: "/run/{{ netbox_metrics_dir }}"
38+
3539
netbox_config:
3640
#SECRET_KEY:
3741
ALLOWED_HOSTS:

templates/configuration.py.j2

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import json
2-
2+
import os
33
{{ ansible_managed | comment }}
4+
5+
{% if netbox_metrics_enabled %}
6+
if "prometheus_multiproc_dir" in os.environ:
7+
try:
8+
import prometheus_client
9+
import uwsgi
10+
prometheus_client.values.ValueClass = prometheus_client.values.MultiProcessValue(
11+
_pidFunc=uwsgi.worker_id)
12+
except ImportError:
13+
pass # not running in uwsgi
14+
{% endif %}
15+
416
DATABASE = {
517
'NAME': '{{ netbox_database }}',
618
'USER': '{{ netbox_database_user }}',
@@ -28,6 +40,10 @@ REDIS = {
2840
WEBHOOKS_ENABLED = True
2941
{% endif %}
3042

43+
{% if netbox_metrics_enabled %}
44+
METRICS_ENABLED = True
45+
{% endif %}
46+
3147
{% for setting, value in netbox_config.items() %}
3248
{% if value in [True, False] %}
3349
{{ setting }} = {{ 'True' if value else 'False' }}

templates/netbox.service.j2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ DeviceAllow=/dev/urandom r
3131
DeviceAllow=/dev/zero r
3232
ProtectHome=yes
3333
NoNewPrivileges=yes
34+
{% if netbox_metrics_enabled %}
35+
RuntimeDirectory={{ netbox_metrics_dir }}
36+
{% endif %}
3437

3538
[Install]
3639
WantedBy=multi-user.target

templates/uwsgi.ini.j2

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ chdir={{ netbox_current_path }}/netbox
1616
static-map=/{{ _base_path }}static={{ netbox_current_path }}/netbox/static
1717
logger={{ netbox_application_log }}
1818
req-logger={{ netbox_requests_log }}
19+
{% if netbox_metrics_enabled %}
20+
env=prometheus_multiproc_dir={{ netbox_metrics_path }}
21+
{% endif %}
1922
{% for key, value in netbox_uwsgi_options.items() %}
23+
{% if 'prometheus_multiproc_dir' not in value|string %}
2024
{{ key }}={{ value }}
25+
{% endif %}
2126
{% endfor %}
2227

2328
# vim: ft=dosini

tests/group_vars/netbox

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ netbox_database_host: 10.0.3.1
1818
netbox_database_password:
1919
netbox_database_user: postgres
2020
netbox_webhooks_enabled: true
21+
netbox_metrics_enabled: true
2122
netbox_redis_host: 10.0.3.1
2223
netbox_uwsgi_options:
2324
buffer-size: 65535

tests/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@
2525
- name: Ensure that NetBox returns a successful HTTP response
2626
uri:
2727
url: "http://{{ inventory_hostname }}:8080"
28+
- name: Check that Netbox metrics endpoint works
29+
uri:
30+
url: "http://{{ inventory_hostname }}:8080/metrics"

0 commit comments

Comments
 (0)