collector: support custom poll intervals

Also simplify config context schema for SNMP since we only use a single
instance.
This commit is contained in:
Timotej Lazar 2025-11-04 14:56:40 +01:00 committed by Gašper Fele-Žorž
parent 05fe7eaf08
commit 68baf2d9fe
4 changed files with 21 additions and 16 deletions

View file

@ -1,18 +1,19 @@
Set up metric collection with prometheus and telegraf as the SNMP proxy. Set up metric collection with prometheus and telegraf as the SNMP proxy.
NetBox config context should contain the lists `prometheus_hosts` and `snmp_hosts` with job definitions. Each entry should define `name` and `nb_filter` user to query hosts from NetBox. For example: Each entry in `prometheus_config` should define `name`, `hosts` and optionally `interval`. As above, `hosts` is used as a query filter.
For SNMP the properties `snmp_hosts` and optional `snmp_interval` should define respectively the NetBox query filter and poll interval.
For example:
{ {
"prometheus_hosts": [ "prometheus_config": [
{ {
"name": "classroom", "name": "classroom",
"nb_filter": "role=desktop-computer status=active location=classroom" "hosts": "role=desktop-computer status=active location=classroom",
"interval": 300
} }
], ],
"snmp_hosts": [ "snmp_hosts": "role=switch name__isw=sw- status=active status=staged status=planned",
{ "snmp_interval": 300
"name": "switch",
"nb_filter": "role=switch name__isw=sw- status=active status=staged status=planned"
}
]
} }

View file

@ -28,7 +28,7 @@
template: template:
dest: "/etc/prometheus/conf.d/{{ item.name }}.yml" dest: "/etc/prometheus/conf.d/{{ item.name }}.yml"
src: "prometheus-job.yml.j2" src: "prometheus-job.yml.j2"
loop: "{{ prometheus_hosts }}" loop: "{{ prometheus_config }}"
loop_control: loop_control:
label: "{{ item.name }}" label: "{{ item.name }}"
notify: reload prometheus notify: reload prometheus

View file

@ -1,10 +1,14 @@
{% set devices = query("netbox.netbox.nb_lookup", "devices", api_filter="{{ item.nb_filter }}", raw_data=true) {% set devices = query("netbox.netbox.nb_lookup", "devices", api_filter="{{ item.hosts }}", raw_data=true)
| selectattr("primary_ip") | selectattr("primary_ip")
| map(attribute="name") | map(attribute="name")
| map("extract", hostvars) -%} | map("extract", hostvars) -%}
scrape_configs: scrape_configs:
- job_name: "{{ item.name }}" - job_name: "{{ item.name }}"
{% if item.interval is defined %}
scrape_interval: {{ item.interval }}s
scrape_timeout: {{ item.interval // 5 }}s
{% endif %}
relabel_configs: relabel_configs:
- source_labels: [__address__] - source_labels: [__address__]
regex: '([^.]+).*' regex: '([^.]+).*'

View file

@ -1,13 +1,13 @@
[[inputs.snmp]] [[inputs.snmp]]
interval = "300s" {% if snmp_interval is defined %}
interval = "{{ snmp_interval }}s"
{% endif %}
agent_host_tag = "source" agent_host_tag = "source"
agents = [ agents = [
{% for item in snmp_hosts %} {% for address in query("netbox.netbox.nb_lookup", "devices", api_filter=snmp_hosts, raw_data=true)
{% for address in query("netbox.netbox.nb_lookup", "devices", api_filter=item.nb_filter, raw_data=true)
| selectattr("primary_ip4") | map(attribute="primary_ip4.address") | selectattr("primary_ip4") | map(attribute="primary_ip4.address")
| ipaddr("address") %} | ipaddr("int") | sort | ipaddr("address") %}
"{{ address }}", "{{ address }}",
{% endfor %}
{% endfor %} {% endfor %}
] ]
version = 3 version = 3