Netbox SNMP targets

726 views
Skip to first unread message

Waseem Karim

unread,
Feb 4, 2020, 6:48:46 PM2/4/20
to Prometheus Users
Hi,

I am working on a project where the intention is to use the snmp exporter to get the metrics of a bunch of switches that I have stored in Netbox.

The idea is that I do not want to be typing in targets for the snmp exporter manually, I would prefer to just have it update automatically.

I have had a look at the netbox-prometheus-sd but have no idea as to how to get the IP-Addresses as targets for my snmp_exporter job.

Any help would be greatly appreciated

Thanks!

Brian Candler

unread,
Feb 5, 2020, 4:28:53 AM2/5/20
to Prometheus Users
Here is another one: https://github.com/candlerb/netbox-prometheus (disclaimer: I wrote it :-)

It writes out target files that are picked up by file SD, in the form "<instance-name>/<primary-ip>" which are then written into "instance" and "__address__" labels respectively.

The front page of the repo gives some sample configs, which you might find useful even if you don't use this code.

HTH, Brian.

Ben Kochie

unread,
Feb 5, 2020, 4:55:08 AM2/5/20
to Brian Candler, Prometheus Users
If you wanted to write that in Go, it could be directly integrated into Prometheus. As we have ended the new service discovery method moratorium. :-)

--
You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/7e971666-eeb6-4480-8010-5ffdd38b735c%40googlegroups.com.

Brian Candler

unread,
Feb 5, 2020, 5:21:51 AM2/5/20
to Prometheus Users
Interesting to know, thank you.

Currently the python code is somewhat hackable to customise its behaviour.  It would need changing to take a YAML config with all useful permutations of filtering sites and devices.  If I ever get some spare time, it will be a project...

Cheers... Brian.

Brian Brazil

unread,
Feb 5, 2020, 5:23:55 AM2/5/20
to Brian Candler, Prometheus Users
On Wed, 5 Feb 2020 at 10:21, Brian Candler <b.ca...@pobox.com> wrote:
Interesting to know, thank you.

Currently the python code is somewhat hackable to customise its behaviour.  It would need changing to take a YAML config with all useful permutations of filtering sites and devices.  If I ever get some spare time, it will be a project...

That's what relabelling is for, so each SD doesn't need to reinvent that.

Brian
 

Cheers... Brian.

--
You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-use...@googlegroups.com.
Message has been deleted
Message has been deleted

Waseem Karim

unread,
Feb 6, 2020, 2:21:25 AM2/6/20
to Prometheus Users

Thank you so much for your help Brian. I believe you are now helping me on more than one discussion group!


I cloned your repo and the readme is a lot more helpful than most others I have read so thank you for that.

I have managed to get the snmp_targets.yml file. I was just wondering if there is any way for me to use this when starting prometheus?

I tried using it as the config file (a silly idea but I am very new to this) and it did not work. Basically, I want to know how to bring the snmp_exporter and the snmp_targets.yml file together to get the metrics from the various switches in my netbox server

Please can you advise

Thanks
Waseem

Brian Candler

unread,
Feb 6, 2020, 5:18:10 AM2/6/20
to Prometheus Users
On Thursday, 6 February 2020 07:21:25 UTC, Waseem Karim wrote:

I have managed to get the snmp_targets.yml file. I was just wondering if there is any way for me to use this when starting prometheus?


Later on in the README it gives an example of how to configure prometheus to do the scraping using snmp_targets.yml:

  - job_name: snmp
    scrape_interval: 15s
    file_sd_configs:
      - files:
        - /etc/prometheus/targets.d/snmp_targets.yml
    metrics_path: /snmp
    relabel_configs:
      - source_labels: [__address__]
        regex: '([^/]+)'  # name or address only
        target_label: instance
      - source_labels: [__address__]
        regex: '([^/]+)'  # name or address only
        target_label: __param_target
      - source_labels: [__address__]
        regex: '(.+)/(.+)'  # name/address
        target_label: instance
        replacement: '${1}'
      - source_labels: [__address__]
        regex: '(.+)/(.+)'  # name/address
        target_label: __param_target
        replacement: '${2}'
      - source_labels: [__param_target]
        regex: '\[(.+)\]'   # remove [...] from address
        target_label: __param_target
        replacement: '${1}'
      - source_labels: [module]
        target_label: __param_module
      - target_label: __address__
        replacement: 127.0.0.1:9116  # SNMP exporter

Brian Candler

unread,
Feb 6, 2020, 5:36:38 AM2/6/20
to Prometheus Users
I've expanded the comments now to make it a bit clearer.

 - job_name: snmp
    scrape_interval: 1m
    file_sd_configs:
      - files:
        - /etc/prometheus/targets.d/snmp_targets.yml
    metrics_path: /snmp
    relabel_configs:
      # When __address__ consists of just a name or IP address,
      # copy it to both the "instance" label (visible to user)
      # and "__param_target" (where snmp_exporter sends SNMP)
      - source_labels: [__address__]
        regex: '([^/]+)'
        target_label: instance
      - source_labels: [__address__]
        regex: '([^/]+)'
        target_label: __param_target
      # When __address__ is of the form "name/address", extract
      # name to "instance" label and address to "__param_target"
      - source_labels: [__address__]
        regex: '(.+)/(.+)'
        target_label: instance
        replacement: '${1}'
      - source_labels: [__address__]
        regex: '(.+)/(.+)'
        target_label: __param_target
        replacement: '${2}'
      # If __param_target is enclosed by square brackets, remove them
      - source_labels: [__param_target]
        regex: '\[(.+)\]'
        target_label: __param_target
        replacement: '${1}'
      # Copy "module" label to "__param_module" so that snmp_exporter
      # receives it as part of the scrape URL
      - source_labels: [module]
        target_label: __param_module
      # Send the actual scrape to SNMP exporter
      - target_label: __address__
        replacement: 127.0.0.1:9116

Brian Candler

unread,
Feb 6, 2020, 6:13:34 AM2/6/20
to Prometheus Users
On Wednesday, 5 February 2020 09:55:08 UTC, Ben Kochie wrote:
If you wanted to write that in Go, it could be directly integrated into Prometheus. As we have ended the new service discovery method moratorium. :-)


I have a question.  As well as service discovery, the python code also exports metadata of the form:

netbox_meta{instance="foo",role="xxx",site="yyy",tenant="zzz".... etc } 1

as additional labels to join on - as per how to have labels for machine roles.  It writes out a separate metrics file, one line per target, which can be scraped via HTTP in a separate job.

How would this normally be done when using SD?  I can imagine having two different jobs, both using netbox_sd:

- job: snmp
  netbox_sd_configs: ...
  ... relabel using __meta_netbox_primary_ip or whatever and do a real scrape

- job: netbox_meta
  netbox_sd_configs: ...
  relabel_configs:
    - source_labels: [__meta_netbox_role]
      target_label: role
    - source_labels: [__meta_netbox_site]
      target_label: site
    ... etc
    - target_label: __address__
      replacement: 127.0.0.1:8080     # ??? some dummy target which returns "netbox_meta{} 1"

I imagine people must be doing something like this for the various other SD mechanisms like EC2, but I haven't seen an example.

Also it would be good to avoid having to make a dummy HTTP server, and avoid doing actual HTTP dummy scrapes, if possible.  I don't suppose you can set "scheme: file" or "scheme: data" as part of the scrape job??

Waseem Karim

unread,
Feb 10, 2020, 12:44:24 AM2/10/20
to Prometheus Users
Thank you so much Brian, your script has worked like a charm. 

I have got my grafana and prometheus going with my required targets and they are working like a charm.

Just wondering while I'm here if you've got a link to installing a timescaledb to enable long-term storage of metric data from prometheus? I do not particularly want to use Docker and all the I have examples found online thus far have been using Docker.

Thanks
Waseem

Brian Candler

unread,
Feb 10, 2020, 2:58:19 AM2/10/20
to Prometheus Users
On Monday, 10 February 2020 05:44:24 UTC, Waseem Karim wrote:
Just wondering while I'm here if you've got a link to installing a timescaledb to enable long-term storage of metric data from prometheus? I do not particularly want to use Docker and all the I have examples found online thus far have been using Docker.

Reply all
Reply to author
Forward
0 new messages