SNMP batch OIDs possible? [with examples]

Hi again!

I want to use HA as a dashboard to easily manage most of my network in one place.

Like another user suggested a couple of years ago, it would be nice to be able to configure snmp only once per host or be able to grab consecutive OIDs. Integrations would be easier and the snmp server would be less stressed.
I currently monitor the on/off status of each port, and check if a wire is connected to that port.

I’m not sure if such integration would be feasable as I don’t know enough about how HA backend works.

My current setup:

sensor:
    - platform: snmp
      name: TPLink Switch Port 1 Status
      host: !secret tplink_switch_ip
      auth_protocol: 'hmac-sha'
      username: !secret tplink_switch_snmp_username
      auth_key: !secret tplink_switch_snmp_authkey
      priv_protocol: 'des'
      priv_key: !secret tplink_switch_snmp_privkey
      version: '3'
      baseoid: 1.3.6.1.2.1.2.2.1.8.49153
      accept_errors: true

switch:
    - platform: snmp
      name: TPLink Switch Port 1 Toggle
      host: !secret tplink_switch_ip
      auth_protocol: 'hmac-sha'
      username: !secret tplink_switch_snmp_username
      auth_key: !secret tplink_switch_snmp_authkey
      priv_protocol: 'des'
      priv_key: !secret tplink_switch_snmp_privkey
      version: '3'
      baseoid: 1.3.6.1.2.1.2.2.1.7.49153
      payload_on: 1
      payload_off: 2

I still need to add a template for each port to check if it’s connected, on or off, based on the status of one or both sensors depending on the values returned.

I added those entries for OIDs 1.3.6.1.2.1.2.2.1.7.49153 to 1.3.6.1.2.1.2.2.1.7.49180 (and 1.3.6.1.2.1.2.2.1.8.49153 to 1.3.6.1.2.1.2.2.1.8.49180), that is 56 requests to get the status of the ports only. I’m not monitoring the traffic yet or checking for packets error.

I have 2 possible implementations in mind, either:

  • Major rewrite I think but the best, Ability to set the OIDs name and type directly in the UI (or YAML).
    Pros: Only 1 integration per hardware/SNMP device, less duplication data, easier to configure, Cons: still lots of duplicate settings depending on use case.
#This is an example only to illustrate, not sure what would be the best way to approach this
snmp:
  - name: tplink switch
    host: !secret tplink_switch_ip
    auth_protocol: 'hmac-sha'
    username: !secret tplink_switch_snmp_username
    auth_key: !secret tplink_switch_snmp_authkey
    priv_protocol: 'des'
    priv_key: !secret tplink_switch_snmp_privkey
    version: '3'
    oids:
      - oid: 1.3.6.1.2.1.2.2.1.7.49153 # On/Off
        type: 'switch'
        payload_on: 1
        payload_off: 2
        name: Port 1
      - oid: 1.3.6.1.2.1.2.2.1.8.49153 # Connected/Disconnected
        type: 'sensor'
        name: Port 1
        value_template: >...
      groups:
      - payload_on: 1
        type: 'switch'
        name: 'more switch'
        oids:
        - oid: 1.3.6.1.2.1.2.2.1.7.49154
          name: 'Port 2 Status' # name = tplink switch more switch port 2 status
        - 1.3.6.1.2.1.2.2.1.7.49155
        - 1.3.6.1.2.1.2.2.1.7.49156

This would take the name tplink switch and generate entities name: tplink_switch_port_1. I’m not sure if it would be possible to have the entity having both the connected status and the on/off status. It would know to merge them since they have the same name.

We could also add groups to elements. User would add their elements to a group, and could configure the whole group only once (payload, templates, etc). Useful if a group of OIDs return 1 for something while another group return 1 to say something else.

  • Ability to set from/to OIDs
    Pros: slightly less configuration duplication, Cons: can get confusing?, more code in the backend I think. I also hate that approach, but would still be better than right now.
switch:
  - platform: snmp
    name: TPLink Switch Toggle Port
    host: !secret tplink_switch_ip
    auth_protocol: 'hmac-sha'
    username: !secret tplink_switch_snmp_username
    auth_key: !secret tplink_switch_snmp_authkey
    priv_protocol: 'des'
    priv_key: !secret tplink_switch_snmp_privkey
    version: '3'
    baseoid: 1.3.6.1.2.1.2.2.1.7
    command_oid: 1.3.6.1.2.1.2.2.1.7
    from_oid: 49153
    to_oid: 49180
    limit: 28 #instead of to_oid?
    payload_on: 1
    payload_off: 2

This would get the OIDs from 1.3.6.1.2.1.2.2.1.7, then get the from and to values and create the switched for those. Switch entities would be names TPLink Switch Toggle Port X. This would only be for switches, it would need to be implemented for sensors too.

There is another thing that could get implemented but it would require more coding I think

oids: #not actual oids
  - switch: 1.3.6.1.2.1.2.2.1.7.49154
    status: 1.3.6.1.2.1.2.2.1.8.49154
    packet_in: 1.3.6.1.2.1.2.2.1.7.49154
    packet_in_discarded: 1.3.6.1.2.1.2.2.1.13.49153
    label: 1.3.6.1.2.1.31.1.1.1.18.49153

#My current usecase is for port monitoring, but someone who want to monitor a PC could do
cpu_usage: 1.3.6.1.2.1.2.2.1....
ram_usage: ...
device_name: ...

As I said before, if it is possible to merge switch and sensors, it would be nice to do is_state('switch.tplink_port_1', 'is_plugged') is_state('switch.tplink_port_1', 'label') is_state('switch.tplink_port_1', 'speed') or whatever

This would take a month for me to code test and make this works, and I’m sure it wouldn’t get approved anyway for code quality or something. but if someone uses snmp and have the time, this would be awesome!

Other suggestions: Calculate bandwidth utilization, Better delta handling, counters

Thanks for reading that! <3

I wonder why this request didn’t garner much interest.
Currently, SNMP sensor and switch config is outdated.
It is easy if you want to set up a couple of sensors but it is absolutely nightmare if you want to set up a complex SNMP control environment.

I am managing my network switches through SNMP. Both of them has 24 ports with poe. I control both the switch status, port status and POE status.

And the current config is making my SNMP setup too much complicated.
For SNMP switches there is no value_template to check the status of the OID and you have to create separate sensors for the same host with all the configuration options.
A cleaner config can be arranged like this which will improve code quality and clean up the convoluted setups.

# Some values can be defined in the host part.
# Those values will be applied under all the defined sensors and switches unless a specific sensor has overridden it.
snmp:
  - sensor:
      - host: 192.168.10.1
        username: !secret snmpv3_username
        auth_key: !secret snmpv3_auth_key
        priv_key: !secret snmpv3_priv_key
        auth_protocol: hmac-sha
        priv_protocol: des
        version: "3"
        port: 161
        community: public
        accept_errors: false
        sensors:
          - name: "SNMP Sensor 1" # Required
            baseoid: 1.3.6.12.24.54.1 # Required
            unique_id: "Sensor 1" # Optional
            device_class: "power" # Optional, overrides the config defined under the host
            state_class: measurement # Optional, overrides the config defined under the host
            scan_interval: 60 # Optional, overrides the config defined under the host
            icon: mdi:power  # Optional (Template), overrides the config defined under the host
            picture: "" # Optional (Template), overrides the config defined under the host
            unit_of_measurement: "W"  # Optional, overrides the config defined under the host
            default_value: "0" # Optional (Template), overrides the config defined under the host
            value_template: "{{ value | float }}" # Optional (Template)
            accept_errors: true  # Optional, overrides the config defined under the host
            attributes: # Optional (Template)
              att1:
          - name: "SNMP Sensor 2" # Required
            baseoid: 1.3.6.12.24.54.2 # Required
            unique_id: "Sensor 2" # Optional
            device_class: "power" # Optional, overrides the config defined under the host
            state_class: measurement # Optional, overrides the config defined under the host
            scan_interval: 30 # Optional, overrides the config defined under the host
            icon: mdi:power  # Optional (Template), overrides the config defined under the host
            picture: "" # Optional (Template), overrides the config defined under the host
            unit_of_measurement: "W"  # Optional, overrides the config defined under the host
            default_value: "0" # Optional (Template), overrides the config defined under the host
            value_template: "{{ value | float }}" # Optional (Template)
            accept_errors: true  # Optional, overrides the config defined under the host
            attributes: # Optional (Template), not possible to define under the host
              att1:
  - switch:
      - host: 192.168.10.1
        username: !secret snmpv3_username
        auth_key: !secret snmpv3_auth_key
        priv_key: !secret snmpv3_priv_key
        auth_protocol: hmac-sha
        priv_protocol: des
        version: "3"
        port: 161
        community: public     
        vartype: Integer
        switches:
          - name: "SNMP Switch 1" # Required
            unique_id: "SNMP Switch 1"
            vartype: OctetString
            baseoid: 1.3.6.12.24.54.1 # Required
            command_oid: # Optional (Template)
            value_template: # Optional (Template), evaluates baseoid for state of the switch (true or false)
            payload_on: # Optional (Template), if command_oid not set, the template evaluation result is sent to baseoid when switch is turned on
            payload_off: # Optional (Template), if command_oid not set, the template evaluation result is sent to baseoid when switch is turned off
            device_class: "power" # Optional, overrides the config defined under the host
            state_class: measurement # Optional, overrides the config defined under the host
            scan_interval: 60 # Optional, overrides the config defined under the host
            icon: mdi:power  # Optional (Template), overrides the config defined under the host
            picture: "" # Optional (Template), overrides the config defined under the host

Though, this is a rough framework and open to improvements.

It costs me about 240 lines of YAML-code to monitor 3 servers, with each 2 disks…not to mention the issue where a disk OID changes (after a reboot)

I currently check SNMP values of IT-hardware using PRTG Network Monitor (in corp environment) and one thing I like is the possibility to “map” certain values.

For a disk for example, the mapping table looks like this:

Index Name Column
0 Disk Identifier “oid of diskIdentifier”
1 Disk Size “oid of diskSize”
2 Disk Usage “oid of diskUsage”
[…] […] […]

The resulting data would then be something like this:

Disk Identifier Disk Size Disk Usage
/dev/sda 512GB 120GB
/dev/sdb 512GB 200GB
[…] […] […]

PRTG SNMP Custom Table Sensor: