SNMP bandwidth monitor using statistics

Im trying to create a bandwidth monitor for my omada setup. Ran snmpwalk to output the following:

IF-MIB::ifDescr.1025 = STRING: default/lo
IF-MIB::ifDescr.1026 = STRING: default/eth0
IF-MIB::ifDescr.1027 = STRING: default/ip6tnl0
IF-MIB::ifDescr.1028 = STRING: default/sit0
IF-MIB::ifDescr.1029 = STRING: default/gre0
IF-MIB::ifDescr.1030 = STRING: default/gretap0
IF-MIB::ifDescr.1031 = STRING: default/bond0
IF-MIB::ifDescr.1032 = STRING: default/veth0
IF-MIB::ifDescr.1034 = STRING: default/eth1
IF-MIB::ifDescr.1038 = STRING: default/br-lan
IF-MIB::ifDescr.1039 = STRING: default/eth1.4094
IF-MIB::ifDescr.2049 = STRING: vnet/lo
IF-MIB::ifDescr.2050 = STRING: vnet/ip6tnl0
IF-MIB::ifDescr.2051 = STRING: vnet/sit0
IF-MIB::ifDescr.2052 = STRING: vnet/gre0
IF-MIB::ifDescr.2053 = STRING: vnet/gretap0
IF-MIB::ifDescr.2057 = STRING: vnet/veth1
IF-MIB::ifDescr.2059 = STRING: vnet/eth0.1
IF-MIB::ifDescr.2060 = STRING: vnet/eth0.77
IF-MIB::ifDescr.2061 = STRING: vnet/eth0.69
IF-MIB::ifType.1025 = INTEGER: softwareLoopback(24)
IF-MIB::ifType.1026 = INTEGER: ethernetCsmacd(6)

I figure that 1026 is the WAN, 2059-2061 are my VLANs based on snmpwalk. Based on this post, I would use 1.3.6.1.2.1.2.2.1.10.xxxx for Rx and 1.3.6.1.2.1.2.2.1.16.xxxx for Tx.

The user in that post stated:

to get a measurement that you can use you need to get the delta of two measurements divided by the seconds that the measuremants where apart from each other.

Anyone know the template sensor to do this?

You need a derivative for that. Check my earlier post describing the sensor hierarchy that I used.

Thanks, tried out your sensor heirarchy, but the values don’t seem to correspond with my activity. I can confirm that .10 and .16 are “ifInOctets” and “ifOutOctets” but the values always stay under 1Mbps. I’ve tried these sensors for 1038 (br-lan) and 2059 (eth0.1) also.

Also looked at this guide from TP-Link: How to check the link status and data traffic statistics of TP-Link Business Routers with SNMP

The yaml I used:

- platform: snmp
  name: "Omada Router Eth0 Down"
  host: 192.168.29.1
  community: public
  baseoid: 1.3.6.1.2.1.2.2.1.10.1026
  version: '1'
  accept_errors: true  
  state_class: total_increasing
  icon: mdi:cloud-download
  unit_of_measurement: "B"

- platform: snmp
  name: "Omada Router Eth0 Up"
  host: 192.168.29.1
  community: public
  baseoid: 1.3.6.1.2.1.2.2.1.16.1026
  version: '1'
  accept_errors: true  
  state_class: total_increasing
  icon: mdi:cloud-download
  unit_of_measurement: "B"

- platform: derivative
  # Calculate the throughput by sampling the SNMP sensor
  name: "Network - WAN Down Throughput - Bps"
  source: sensor.omada_router_eth0_down
  unit_time: s
  round: 2
  
- platform: derivative
  # Calculate the throughput by sampling the SNMP sensor
  name: "Network - WAN Up Throughput - Bps"
  source: sensor.omada_router_eth0_up
  unit_time: s
  round: 2  

- platform: template
  sensors:
     wan_down_throughput:
       unit_of_measurement: Mbps
       # Convert throughput derivative from Bps to Mbps
       value_template: "{{ ((states('sensor.network_wan_down_throughput_bps') | int) * 8 / 10**6) | round(1) }}"
       
     wan_up_throughput:
       unit_of_measurement: Mbps
       # Convert throughput derivative from Bps to Mbps
       value_template: "{{ ((states('sensor.network_wan_up_throughput_bps') | int) * 8 / 10**6) | round(1) }}"

If you poll those oid’s with snmpwalk, do you see increasing values that can be related to the current network usage?

yep, the actual values continue to go up. Based on the recordings from 9am today, it shows eth0 has received about 1.5 billion bytes, which is about 1.5gb of data?

Ok, the rest should work then. Do a speed test and see if the derivative jumps. During low network usage it will most likely stay below 1 Mbps.

it does now

https://community.ui.com/releases/UniFi-OS-Dream-Machines-4-0-3/90b24b90-8be8-4987-8d47-eb408eb07e45

Be careful with Unifi OS 4.0.3 and home assistant integrations. See this link

I was going to upgrade as well until I saw this.

1 Like

i do not use the integration - but this may help? -

https://community.ui.com/releases/UniFi-OS-Dream-Machines-4-0-3/90b24b90-8be8-4987-8d47-eb408eb07e45?page=17

Where can we find baseoids for the UniFi SNMP?

Interesting to note: v 4.x.x of the UDM-SE firmware has SNMP functionality enabled - no need to ssh in and install the packages manually any more, you can enable it via the console now

1 Like

Anyone get this working with UniFi OS 4.0.6? It should work in theory.

Yes

Thanks. How do I figure out the baseoid for the UDM Pro? For WAN Bandwidth in and out.

Check my post above yours. Others and I have posted some pointers there. In short, find the ethernet ports and use an SNMP tool to plot the usage while doing a large, known download or speedtest.

@odwide
I’m quite new to working with templates and derivatives, but I’ve been doing my best to figure things out. Thanks to your code, I’ve managed to get most of the sensors working, except for the “WAN Down Usage” sensor, which is showing as unavailable. Here’s my YAML code. I’d really appreciate any assistance could be provided here.

sensor:
  - platform: snmp
    name: "Optus WAN Interface Down Octets"
    unique_id: optus_wan_interface_down_octets
    host: 192.168.10.1
    version: "3"
    username: !secret isr_snmp_user
    auth_key: !secret isr_snmp_auth_key
    auth_protocol: "hmac-sha"
    priv_key: !secret isr_snmp_priv_key
    priv_protocol: "aes-cfb-128"
    # ifHCInOctets G0/0/0. 
    # Running total of octects (Bytes) going into the interface since router reboot.
    baseoid: 1.3.6.1.2.1.31.1.1.1.6.5
    state_class: total_increasing
    icon: mdi:cloud-download
    unit_of_measurement: "B"
  
  - platform: derivative
    # Calculate the throughput by sampling the SNMP sensor
    name: "Network - Optus WAN Down Throughput - Bps"
    source: sensor.optus_wan_interface_down_octets
    unit_time: s
    round: 2

template:
  - sensor:
    - name: "Optus WAN Down Throughput"
      unique_id: optus_wan_down_throughput
      unit_of_measurement: Mbps
      icon: mdi:cloud-download
      # Convert throughput derivative from Bps to Mbps
      state: "{{ ((states('sensor.network_optus_wan_down_throughput_bps') | int) * 8 / 10**6) | round(1) }}"
      state_class: measurement
    
    - name: "Optus WAN Down Usage"
      unique_id: wan_down_usage
      unit_of_measurement: GiB
      icon: mdi:cloud-download
      # Convert usage meter below from B to GiB
      state: "{{ ((states('sensor.network_optus_wan_down_data_usage_meter_bytes') | int) / 2**30) | round(2) }}"
      state_class: total_increasing

    
utility_meter:
  network_optus_wan_down_data_usage_meter_bytes:
    unique_id: network_optus_wan_down_data_usage_meter_bytes
    name: Network - WAN Down Data Usage Meter - Bytes
    source: sensor.optus_wan_interface_down_octets
    cycle: monthly
    offset:
      hours: 72
      minutes: 0
      seconds: 0

Based on your yaml the correct entity id for Optus Wan Down Usage is sensor.wan_down_usage, not sensor.optus_wan_down_usage.

Thanks, I am new in HA world and still learning. I’ve updated the unique ID for Optus Wan Down Usage to something more descriptive. However, the sensor is still showing as unavailable, and I haven’t found any other sensors with “wan” in their names either.

  - platform: snmp
    name: "Optus WAN Interface Down Octets"
    unique_id: optus_wan_interface_down_octets
    host: 192.168.10.1
    version: "3"
    username: !secret isr_snmp_user
    auth_key: !secret isr_snmp_auth_key
    auth_protocol: "hmac-sha"
    priv_key: !secret isr_snmp_priv_key
    priv_protocol: "aes-cfb-128"
    # ifHCInOctets G0/0/0. 
    # Running total of octects (Bytes) going into the interface since router reboot.
    baseoid: 1.3.6.1.2.1.31.1.1.1.6.5
    state_class: total_increasing
    icon: mdi:cloud-download
    unit_of_measurement: "B"
  
  - platform: derivative
    # Calculate the throughput by sampling the SNMP sensor
    name: "Network - Optus WAN Down Throughput - Bps"
    source: sensor.optus_wan_interface_down_octets
    unit_time: s
    round: 2

template:
  - sensor:
    - name: "Optus WAN Down Throughput"
      unique_id: optus_wan_down_throughput
      unit_of_measurement: Mbps
      icon: mdi:cloud-download
      # Convert throughput derivative from Bps to Mbps
      state: "{{ ((states('sensor.network_optus_wan_down_throughput_bps') | int) * 8 / 10**6) | round(1) }}"
      state_class: measurement
    
    - name: "Optus WAN Down Usage"
      unique_id: optus_wan_down_usage
      unit_of_measurement: GiB
      icon: mdi:cloud-download
      # Convert usage meter below from B to GiB
      state: "{{ ((states('sensor.network_optus_wan_down_data_usage_meter_bytes') | int) / 2**30) | round(2) }}"
      state_class: total_increasing

    
utility_meter:
  network_optus_wan_down_data_usage_meter_bytes:
    unique_id: network_optus_wan_down_data_usage_meter_bytes
    name: Network - WAN Down Data Usage Meter - Bytes
    source: sensor.optus_wan_interface_down_octets
    cycle: monthly
    offset:
      hours: 72
      minutes: 0
      seconds: 0

If network_optus_wan_down_data_usage_meter_bytes is giving the correct value then optus_wan_down_usage should too.

Apparently there are entities no longer being provided but that have id collision, since your sensor now has the id optus_wan_down_usage_3. To clean up, comment out the template for Optus WAN Down Usage, restart HA, under the Entities page search for any entity with id optus_wan_down_usage and delete it, uncomment the sensor in yaml and restart again. That should get rid of any all dangling sensors and leave only the one that will work.

1 Like

Thanks, you gave the idea of double-checking the sensor ids. Unfortunately the id for network_optus_wan_down_data_usage_meter_bytes wasn’t created properly even it was specified correctly in the code. I fixed that things started to work!

Here is the working YAML code for someone who has implemented Double WAN on a supported router.

sensor:

# Optus WAN Interface
  - platform: snmp
    name: "Optus WAN Interface Down Octets"
    unique_id: optus_wan_interface_down_octets
    host: 192.168.10.1
    version: "3"
    username: !secret isr_snmp_user
    auth_key: !secret isr_snmp_auth_key
    auth_protocol: "hmac-sha"
    priv_key: !secret isr_snmp_priv_key
    priv_protocol: "aes-cfb-128"
    # ifHCInOctets G0/0/0.
    # Run following command to get the baseoid:
    # snmpwalk -v3 -l authPriv -u <username> -a <auth_protocol> -A <auth_key> -x <priv_protocol> -X <priv_key> <host> <baseoid>
    # example: snmpwalk -v3 -l authPriv -u isr_snmp_user -a hmac-sha -A !secret isr_snmp_auth_key -x aes-cfb-128 -X !secret isr_snmp_priv_key 192.168.10.1 1.3.6.1.2.1.31.1.1.1.6[.5]
    # Running total of octects (Bytes) going into the interface since router reboot.
    baseoid: 1.3.6.1.2.1.31.1.1.1.6.5
    state_class: total_increasing
    icon: mdi:cloud-download
    unit_of_measurement: "B"

  - platform: derivative
    # Calculate the throughput by sampling the SNMP sensor
    name: "Network - Optus WAN Down Throughput - Bps"
    source: sensor.optus_wan_interface_down_octets
    unit_time: s
    round: 2

  - platform: snmp
    name: "Optus WAN Interface Up Octets"
    unique_id: optus_wan_interface_up_octets
    host: 192.168.10.1
    version: "3"
    username: !secret isr_snmp_user
    auth_key: !secret isr_snmp_auth_key
    auth_protocol: "hmac-sha"
    priv_key: !secret isr_snmp_priv_key
    priv_protocol: "aes-cfb-128"
    # ifHCInOctets G0/0/0.
    # Running total of octects (Bytes) going into the interface since router reboot.
    baseoid: 1.3.6.1.2.1.31.1.1.1.10.5
    state_class: total_increasing
    icon: mdi:cloud-upload
    unit_of_measurement: "B"

  - platform: derivative
    # Calculate the throughput by sampling the SNMP sensor
    name: "Network - Optus WAN Up Throughput - Bps"
    source: sensor.optus_wan_interface_up_octets
    unit_time: s
    round: 2

# Telstra WAN Interface 
  - platform: snmp
    name: "Telstra WAN Interface Down Octets"
    unique_id: telstra_wan_interface_down_octets
    host: 192.168.10.1
    version: "3"
    username: !secret isr_snmp_user
    auth_key: !secret isr_snmp_auth_key
    auth_protocol: "hmac-sha"
    priv_key: !secret isr_snmp_priv_key
    priv_protocol: "aes-cfb-128"
    # ifHCInOctets G0/0/0.
    # Running total of octects (Bytes) going into the interface since router reboot.
    baseoid: 1.3.6.1.2.1.31.1.1.1.6.6
    state_class: total_increasing
    icon: mdi:cloud-download
    unit_of_measurement: "B"

  - platform: derivative
    # Calculate the throughput by sampling the SNMP sensor
    name: "Network - Telstra WAN Down Throughput - Bps"
    source: sensor.telstra_wan_interface_down_octets
    unit_time: s
    round: 2

  - platform: snmp
    name: "Telstra WAN Interface Up Octets"
    unique_id: telstra_wan_interface_up_octets
    host: 192.168.10.1
    version: "3"
    username: !secret isr_snmp_user
    auth_key: !secret isr_snmp_auth_key
    auth_protocol: "hmac-sha"
    priv_key: !secret isr_snmp_priv_key
    priv_protocol: "aes-cfb-128"
    # ifHCInOctets G0/0/0.
    # Running total of octects (Bytes) going into the interface since router reboot.
    baseoid: 1.3.6.1.2.1.31.1.1.1.10.5
    state_class: total_increasing
    icon: mdi:cloud-upload
    unit_of_measurement: "B"

  - platform: derivative
    # Calculate the throughput by sampling the SNMP sensor
    name: "Network - Telstra WAN Up Throughput - Bps"
    source: sensor.telstra_wan_interface_up_octets
    unit_time: s
    round: 2

##########################################################################################
# Template
##########################################################################################

template:
# Optus WAN Interface Template
  - sensor:
      - name: "Optus WAN Down Throughput"
        unique_id: optus_wan_down_throughput
        unit_of_measurement: Mbps
        icon: mdi:cloud-download
        # Convert throughput derivative from Bps to Mbps
        state: "{{ ((states('sensor.network_optus_wan_down_throughput_bps') | int) * 8 / 10**6) | round(1) }}"
        state_class: measurement

      - name: "Optus WAN Down Usage"
        unique_id: optus_wan_down_usage
        unit_of_measurement: GiB
        icon: mdi:cloud-download
        # Convert usage meter below from B to GiB
        state: "{{ ((states('sensor.network_optus_wan_down_data_usage_meter_bytes') | int) / 2**30) | round(2) }}"
        state_class: total_increasing
      
      - name: "Optus WAN Up Throughput"
        unique_id: optus_wan_up_throughput
        unit_of_measurement: Mbps
        icon: mdi:cloud-upload
        # Convert throughput derivative from Bps to Mbps
        state: "{{ ((states('sensor.network_optus_wan_up_throughput_bps') | int) * 8 / 10**6) | round(1) }}"
        state_class: measurement

      - name: "Optus WAN Up Usage"
        unique_id: optus_wan_up_usage
        unit_of_measurement: GiB
        icon: mdi:cloud-upload
        # Convert usage meter below from B to GiB
        state: "{{ ((states('sensor.network_optus_wan_up_data_usage_meter_bytes') | int) / 2**30) | round(2) }}"
        state_class: total_increasing

# Telstra WAN Interface Template

  - sensor:
      - name: "Telstra WAN Down Throughput"
        unique_id: telstra_wan_down_throughput
        unit_of_measurement: Mbps
        icon: mdi:cloud-download
        # Convert throughput derivative from Bps to Mbps
        state: "{{ ((states('sensor.network_telstra_wan_down_throughput_bps') | int) * 8 / 10**6) | round(1) }}"
        state_class: measurement

      - name: "Telstra WAN Down Usage"
        unique_id: telstra_wan_down_usage
        unit_of_measurement: GiB
        icon: mdi:cloud-download
        # Convert usage meter below from B to GiB
        state: "{{ ((states('sensor.network_telstra_wan_down_data_usage_meter_bytes') | int) / 2**30) | round(2) }}"
        state_class: total_increasing
      
      - name: "Telstra WAN Up Throughput"
        unique_id: telstra_wan_up_throughput
        unit_of_measurement: Mbps
        icon: mdi:cloud-upload
        # Convert throughput derivative from Bps to Mbps
        state: "{{ ((states('sensor.network_telstra_wan_up_throughput_bps') | int) * 8 / 10**6) | round(1) }}"
        state_class: measurement

      - name: "Telstra WAN Up Usage"
        unique_id: telstra_wan_up_usage
        unit_of_measurement: GiB
        icon: mdi:cloud-upload
        # Convert usage meter below from B to GiB
        state: "{{ ((states('sensor.network_telstra_wan_up_data_usage_meter_bytes') | int) / 2**30) | round(2) }}"
        state_class: total_increasing

##########################################################################################
# Utility Meter
##########################################################################################  

utility_meter:
# Optus WAN Interface Utility Meter 
  network_optus_wan_down_data_usage_meter_bytes:
    unique_id: network_optus_wan_down_data_usage_meter_bytes
    name: Network - Optus WAN Down Data Usage Meter - Bytes
    source: sensor.optus_wan_interface_down_octets
    cycle: monthly
    offset:
      hours: 72
      minutes: 0
      seconds: 0
  
  network_optus_wan_up_data_usage_meter_bytes:
    unique_id: network_optus_wan_up_data_usage_meter_bytes
    name: Network - Optus WAN Up Data Usage Meter - Bytes
    source: sensor.optus_wan_interface_up_octets
    cycle: monthly
    offset:
      hours: 72
      minutes: 0
      seconds: 0

# Telstra WAN Interface Utility Meter 
  network_telstra_wan_down_data_usage_meter_bytes:
    unique_id: network_telstra_wan_down_data_usage_meter_bytes
    name: Network - Telstra WAN Down Data Usage Meter - Bytes
    source: sensor.telstra_wan_interface_down_octets
    cycle: monthly
    offset:
      hours: 72
      minutes: 0
      seconds: 0
  
  network_telstra_wan_up_data_usage_meter_bytes:
    unique_id: network_telstra_wan_up_data_usage_meter_bytes
    name: Network - Telstra WAN Up Data Usage Meter - Bytes
    source: sensor.telstra_wan_interface_up_octets
    cycle: monthly
    offset:
      hours: 72
      minutes: 0
      seconds: 0
1 Like