SNMP bandwidth monitor using statistics

This was my solution using SNMPv3 to query a Cisco ISR router.

sensor:
  - platform: snmp
    name: "WAN Interface Down Octets"
    host: 192.168.150.1
    version: "3"
    username: !secret snmp_user
    auth_key: !secret snmp_auth_key
    auth_protocol: "hmac-sha"
    priv_key: !secret snmp_priv_key
    priv_protocol: "aes-cfb-128"
    # ifHCInOctets G0/0
    baseoid: 1.3.6.1.2.1.31.1.1.1.6.2
    state_class: total_increasing
    icon: mdi:cloud-download
    unit_of_measurement: "B"

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

utility_meter:
  wan_down_usage_meter_bytes:
    unique_id: wan_down_data_usage_meter_bytes
    name: WAN Down Data Usage Meter - Bytes
    source: sensor.wan_interface_down_octets
    cycle: monthly

template:
  - sensor:   
    # Frontend sensors

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

   - name: "WAN Down Usage - GiB"
      unique_id: wan_down_usage_gib
      unit_of_measurement: GiB
      # Convert SNMP sensor from B to GiB
      state: "{{ ((states('sensor.wan_interface_down_octects') | int) / 2**30) | round(2) }}"
      icon: mdi:cloud-download
      state_class: total_increasing
1 Like

I recently switch from using a USG to a UDM-Pro, so wanted to share the config for others


sensor:
  - platform: snmp
    name: "UDM WAN in"
    host: 192.168.1.1
    baseoid: 1.3.6.1.2.1.2.2.1.10.4
    community: "public"
    version: "2c"
    scan_interval: 5

  - platform: snmp
    name: "UDM WAN out"
    host: 192.168.1.1
    baseoid: 1.3.6.1.2.1.2.2.1.16.4
    community: "public"
    version: "2c"
    scan_interval: 5

  - platform: statistics
    name: "UDM WAN in Stats"
    entity_id: sensor.udm_wan_in
    sampling_size: 4
    state_characteristic: change_second
    max_age:
      hours: 24

  - platform: statistics
    name: "UDM WAN out Stats"
    entity_id: sensor.udm_wan_out
    sampling_size: 4
    state_characteristic: change_second
    max_age:
      hours: 24


template:
  - sensor:
      - name: Downloads
        unique_id: 'sensor.udm_wan_in_mbps'
        icon: mdi:cloud-download
        state: "{{ states('sensor.udm_wan_in_stats')|float(0)*8/1024/1024|round(1) }}"
        unit_of_measurement: "MBps"

      - name: Uploads
        unique_id: 'sensor.udm_wan_out_mbps'
        icon: mdi:cloud-upload
        state: "{{ states('sensor.udm_wan_out_stats')|float(0)*8/1024/1024|round(1) }}"
        unit_of_measurement: "MBps"
3 Likes

I like your use of the stats platform. Did you compare totals with that of your ISP to know how accurate it is?

No sorry I don’t have access to that information. I use it to monitor the realtime traffic rather than total usage over time, so can’t speak to the long term accurancy

My counters match the ISP. Although I want to check them for a few more weeks for be more confident.

1 Like

I copied your config and sometimes I get huge negative spikes when a fast transfer is happening. Have you noticed anything like that?

I have perfected the config since and haven’t had to touch it anymore. Now my counters match the ISP ±1% and all measurements are reliable. This is a diagram of what I came up with eventually for each interface and traffic direction (up, down) to monitor:

image

sensor:
  - platform: snmp
    name: "WAN Interface Down Octets"
    unique_id: wan_interface_down_octets
    host: 192.168.150.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.1
    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.network_wan_interface_down_octects
    unit_time: s
    round: 2

template:
  - sensor:
    - name: "WAN Down Throughput"
      unique_id: wan_down_throughput
      unit_of_measurement: Mbps
      icon: mdi:cloud-download
      # Convert throughput derivative from Bps to Mbps
      state: "{{ ((states('sensor.network_wan_down_throughput_bps') | int) * 8 / 10**6) | round(1) }}"
      state_class: measurement
    
    - name: "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_wan_down_data_usage_meter_bytes') | int) / 2**30) | round(2) }}"
      state_class: total_increasing

    
utility_meter:
  network_wan_down_data_usage_meter_bytes:
    unique_id: network_wan_down_data_usage_meter_bytes
    name: Network - WAN Down Data Usage Meter - Bytes
    source: sensor.wan_interface_down_octets
    cycle: monthly
    offset:
      hours: 72
      minutes: 0
      seconds: 0
      
2 Likes

Awesome, and your graphs don’t have the negative spikes I assume?

Correct, no negatives. The accuracy of the readings is critical because I always get within a hair of my data limit and the ISP will ding me $10 per 100 GB above.

I still get negatives in throughoutput for a split second or so during spikes. I dont think you see it since you are using gauges for throughouts. Try using a graph and use a speedtest.


Anyway, i fixed it by adding a max to the formula in the template sensor:

      - name: "WAN Down Throughput"
        unique_id: sensor.udm_wan_in_mbps
        icon: mdi:cloud-download
        state: "{{ [((states('sensor.wan_in_stats') | int) * 8 / 10**6) | round(1)]|max }}"
        unit_of_measurement: "MBps"
        state_class: measurement

I tested with an automation that notifies if the throughput goes negative and it didn’t happen running a couple of speed tests. Have you checked that the problem is not the SNMP data?

Remember that integer (32-bit) values counting bytes will wrap fairly regularly. Calculating a difference (derivative) at that point will cause a large negative value. I have a solution for that somewhere on this thread. It’s not an issue for 64-bit values — at least not during a normal lifetime at current Internet speeds.

I beg to differ, seen it happen when adding interface boards to big routers. Tools like Cacti can re-index, but HA is not a network grapher by design as Cacti is

That was already addressed 3 years ago in the directly following post.

Thank you for this. I am using it and I think is very good.
Do you know also the oid of the upload octets also in order to do the same for the upload?

thnak you

Sure: 1.3.6.1.2.1.31.1.1.1.10.<interface index>.

which post was that Tom?

https://community.home-assistant.io/t/snmp-bandwidth-monitor-using-statistics/96684/44?u=tom_l

thanks very much

Im looking to monitor the WAN stats in and out on the Ubiquiti UDR, however it doesnt seem to have the ability to provide SNMP traffic from the UDR itself. Any ideas? The Ubuiti Integration doesnt provide them.