OPNSense SNMP Bandwidth Monitor

Hello.

I configure some OPNSense sensors looking to other examples in this forum.

You need snmp plugin in opnsense

I intend to polish it over time, and add more things, this is something basic to start with

Here details config adapted from other examples (checked with 2022.11):

configuration.xml

sensor:

  - platform: snmp
    name: snmp_wan_in
    host: 192.168.1.1
    community: public
    baseoid: 1.3.6.1.2.1.2.2.1.10.1
    scan_interval: 2

  - platform: snmp
    name: snmp_wan_out
    host: 192.168.1.1
    community: public
    baseoid: 1.3.6.1.2.1.2.2.1.16.1
    scan_interval: 2

    
  - platform: snmp
    name: snmp_lan_in
    host: 192.168.1.1
    community: public
    baseoid: 1.3.6.1.2.1.2.2.1.10.2
    scan_interval: 6

  - platform: snmp
    name: snmp_lan_out
    host: 192.168.1.1
    community: public
    baseoid: 1.3.6.1.2.1.2.2.1.16.2
    scan_interval: 6

  - platform: snmp
    name: opnsense_gateway_uptime
    host: 192.168.1.1
    community: public
    baseoid: 1.3.6.1.2.1.1.3.0
    scan_interval: 2
    value_template: >
          {% set timetick = value  |int(0) %}
          {% set seconds = (timetick / 100) | round(0) | int(0) %}
          {% set days = (seconds / 60 / 60 / 24) | int(0)  %}
          {% set hours = ((seconds / 60 / 60) - (days * 24)) | int(0)  %}
          {% set minutes = ((seconds / 60 ) - ( hours * 60) - ( days * (24 * 60))) | int(0) %}
         
          {% if days  > 0 %}
            {{ days }}day
          {% endif %}   
          {% if hours  > 0 %}
            {{ hours }}h
          {% else %}
            {{ 00 }}h            
          {% endif %}   
          {% if minutes  > 0 %}
            {% if minutes < 10 %}
              0{{ minutes }}m
            {% else %}
              {{ minutes }}m
            {% endif %}       
          {% else %}
            {{ 00 }}m
          {% endif %}

  - platform: snmp
    name: opnsense_gateway_load
    host: 192.168.1.1
    community: public
    baseoid: 1.3.6.1.4.1.2021.10.1.3.1
    scan_interval: 2
    value_template: "{{ ((value | float(0) * 100) / 3) | round(2)  }}" 
    device_class: power_factor    
    unit_of_measurement: "%" 
    # value template ... 3  .. for 3 cores change it to your number of cores

  - platform: snmp
    name: opnsense_mem_size
    host: 192.168.1.1
    community: public
    baseoid: 1.3.6.1.2.1.25.2.2.0
    scan_interval: 100
    value_template: "{{ (value |int(0) / 1048576)  | round(2)  }}" 
    unit_of_measurement: "MB"
    
  - platform: snmp
    name: opnsense_mem_used_real
    host: 192.168.1.1
    community: public
    baseoid: 1.3.6.1.2.1.25.2.3.1.6.2
    scan_interval: 5
    #value_template: "{{ (value * 4096) | int(0)  }}" 


  - platform: snmp
    name: opnsense_mem_used_buffers
    host: 192.168.1.1
    community: public
    baseoid: 1.3.6.1.2.1.25.2.3.1.6.6
    scan_interval: 5
    
  - platform: snmp
    name: opnsense_mem_used_cached
    host: 192.168.1.1
    community: public
    baseoid: 1.3.6.1.2.1.25.2.3.1.6.7
    scan_interval: 5
    
  - platform: snmp
    name: opnsense_mem_used_shared_real
    host: 192.168.1.1
    community: public
    baseoid: 1.3.6.1.2.1.25.2.3.1.6.9
    scan_interval: 5

  - platform: snmp
    name: opnsense_storage_root_size
    host: 192.168.1.1
    community: public
    baseoid: 1.3.6.1.2.1.25.2.3.1.5.31
    scan_interval: 500    
    value_template: "{{  ( ((value | int(0)) * 4096) /  1073741824) | round(2) | float(2) }}" 
    unit_of_measurement: "GB"
    
  - platform: snmp
    name: opnsense_storage_root_size_used
    host: 192.168.1.1
    community: public
    baseoid: 1.3.6.1.2.1.25.2.3.1.6.31
    scan_interval: 50            
    value_template: "{{  ( ((value | int(0)) * 4096) /  1073741824) | round(2) | float(2) }}" 
    unit_of_measurement: "GB"

…

template:
    sensor:
    
      - name : "internet_wan_in"
        state: '{{ ((states.input_number.internet_traffic_delta_wanin.state | float(0) ) / 1000000 ) | round(2) }}'
        unit_of_measurement: 'Mbps'

      - name : "internet_wan_out"
        state: '{{ ((states.input_number.internet_traffic_delta_wanout.state | float(0) ) / 1000000 ) | round(2) }}'
        unit_of_measurement: 'Mbps'

      - name : "internet_lan_in"
        state: '{{ ((states.input_number.internet_traffic_delta_lanin.state | float(0) ) / 1000000 ) | round(2) }}'
        unit_of_measurement: 'Mbps'

      - name : "internet_lan_out"
        state: '{{ ((states.input_number.internet_traffic_delta_lanout.state | float(0) ) / 1000000 ) | round(2) }}'
        unit_of_measurement: 'Mbps'         

      - name : "opnsense_mem_total_used"
        state: "{{ (( states('sensor.opnsense_mem_used_real') | int(0) + 
            states('sensor.opnsense_mem_used_buffers') | int(0) +
            states('sensor.opnsense_mem_used_cached') | int (0) +
            states('sensor.opnsense_mem_used_shared_real') | int(0))  / 1048576 ) | round(2) 
        }}"
        unit_of_measurement: 'MB'

…

input_number:
  internet_traffic_delta_wanin:
    name: “Traffic WanIN Delta”
    initial: 0
    min: 0
    max: 1000000000000

  internet_traffic_delta_wanout:
    name: “Traffic WanOUT Delta”
    initial: 0
    min: 0
    max: 1000000000000
    
  internet_traffic_delta_lanin:
    name: “Traffic LanIN Delta”
    initial: 0
    min: 0
    max: 1000000000000

  internet_traffic_delta_lanout:
    name: “Traffic LanOUT Delta”
    initial: 0
    min: 0
    max: 1000000000000

That going in automations.yaml
Make sure ID is unique.


- id: '169402500241'
  alias: Monitor Traffic WanIn
  trigger:
      - platform: state
        entity_id: sensor.snmp_wan_in
  action:
      - service: input_number.set_value
        data_template:
            entity_id: input_number.internet_traffic_delta_wanin
            value: '{{ (((trigger.to_state.state | int - trigger.from_state.state | int) * 8 ) / ( as_timestamp(trigger.to_state.last_updated) - as_timestamp(trigger.from_state.last_updated) )) | int  }}'

- id: '169402500243'  
  alias: Monitor Traffic WanOut
  trigger:
      - platform: state
        entity_id: sensor.snmp_wan_out
  action:
      - service: input_number.set_value
        data_template:
          entity_id: input_number.internet_traffic_delta_wanout
          value: '{{ (((trigger.to_state.state | int - trigger.from_state.state | int) * 8) / ( as_timestamp(trigger.to_state.last_updated) - as_timestamp(trigger.from_state.last_updated) )) | int  }}'        

- id: '169402500244'
  alias: Monitor Traffic LanIn
  trigger:
      - platform: state
        entity_id: sensor.snmp_lan_in
  action:
      - service: input_number.set_value
        data_template:
            entity_id: input_number.internet_traffic_delta_lanin
            value: '{{ (((trigger.to_state.state | int - trigger.from_state.state | int) * 8 ) / ( as_timestamp(trigger.to_state.last_updated) - as_timestamp(trigger.from_state.last_updated) )) | int  }}'

- id: '169402500245'
  alias: Monitor Traffic LanOut
  trigger:
      - platform: state
        entity_id: sensor.snmp_lan_out
  action:
      - service: input_number.set_value
        data_template:
          entity_id: input_number.internet_traffic_delta_lanout
          value: '{{ (((trigger.to_state.state | int - trigger.from_state.state | int) * 8) / ( as_timestamp(trigger.to_state.last_updated) - as_timestamp(trigger.from_state.last_updated) )) | int  }}'
2 Likes

Thanks for the share, very interesting solution although I think you API configuration at begin is useless as you use SNMP :wink:
How reliable is it ?
I have used here a dedicated add-on that works really well too but makes you dependant of a third-party :confused: GitHub - travisghansen/hass-opnsense: OPNsense integration with Home Assistant

YES API config is useless! … trying few things and messing the config. FIXED

Seems reliable, but i config four days ago…

I check the plugin but is not very “clean install” and discard prefer this way, even if it is more basic, easy setup without install things in HA

thanks knACk!

this works a treat, only thing was my WAN interfaces were switched around…

much appreciated!!

update the template/sensor part fix some problems, add things.

1 Like

Many thanks for that great job!
I’m very new at Home Assistant. Would you please be so kind to share how you visualize the stuff on the dashboard? Screenshot and code please :wink: