pfSense stat monitor

Could you help me with the command_line sensor? I have done everything on PfSense side and the python script (pffa_get_system_stats.py) works when tested on Windows Python but Hass.io says only “Command failed: python3 custom_components/pfSense/pff_get_system_stats.py <api_secret>”.

Any ideas what could be wrong? The pffa_get_system_stats.py is copied from your (bthoven) message. I have hass.io installed on raspberry pi 3 and I am on the newest HA version. Also tried to change the path of the script to “config/custom_components/pfSense/pff_get_system_stats.py”

Edit: There is typo in bthoven’s code, pffa_get_system_stats.py is missing letter ‘a’ in its name but this does not change the fact that I still do not get it working.

Edit2: Got it working with @gremblin first messages command_line method. So I put the .py files to python_scripts folder and call it there. Thanks gremblin for this guide!

1 Like

Just one more question. I got all the data in hass now. But I try to parse gateway_status to sensor but I’m not getting it. Could anyone help with that?

Basically the json in like this
data: gateway_status: ipv4 address: delay

Compared to the system_stats json that is in this thread
data: stats: temp

so there is one extra step. The system_stats is parsed like this:
value_template: ‘{{ value_json[“data”][“stats”][“temp”] }}’

But I’m not getting any value from gateway_status with this
value_template: ‘{{ value_json[“data”][“gateway_status”][“ipv4 address”][“delay”] }}’

The ipv4 address is my ip. And there is also ipv6 address in the json, thats why there is one extra layer compared to the system_stats.

Edit: Got it working again, the problem was that the data was in the same file so I just added another json file where to save the data from pfsense and now the parsing works like this:
value_template: ‘{{ value_json[“data”][“gateway_status”][“ipv4 address”][“delay”] }}’
Any ideas how to get the value with value_template?

Sorry for the flood!

1 Like

I was just about to reply to ask for more info, and the page refreshed with your update. :+1: glad you got it working.

1 Like

Just notice that the cpu temperature is updated at much higher frequency (in seconds) than the cpu load and memory usage (in several hours, when checking from HA ui). It is strange because all these values are derived from the same json file. Do you know why?

Hi,

How are your values for cpu usage compared to what top or vmstat report?

When running top command (on v2.4.4) it takes about 2 seconds to populate the cpu row (all the other rows are populated instantly).

This is a bit off topic, but I figured a few of you might be interested in this. Here is how to setup a bandwidth SNMP polling for your pfSence WAN interface. This will give you the following entities for your viewing pleasure:

image

You will need to enable SNMP read access on your firewall, I used the community string of “public”. Be sure to change the host IP below to your pfSense internal interface.

sensor:
  - platform: snmp
    name: 'pfsense WAN in'
    host: 10.1.1.1
    baseoid: 1.3.6.1.2.1.31.1.1.1.6.2
    community: 'public'
    version: '2c'
    scan_interval: 60

  - platform: snmp
    name: 'pfsense WAN out'
    host: 10.1.1.1
    baseoid: 1.3.6.1.2.1.31.1.1.1.10.2
    community: 'public'
    version: '2c'
    scan_interval: 60

  - platform: statistics
    name: 'pfsense WAN in Stats'
    entity_id: sensor.pfsense_wan_in
    sampling_size: 4
    max_age:
      hours: 24

  - platform: statistics
    name: 'pfsense WAN out Stats'
    entity_id: sensor.pfsense_wan_out
    sampling_size: 4
    max_age:
      hours: 24

  - platform: template
    sensors:
      internet_in_mbps:
        icon_template: mdi:cloud-download-outline
        value_template: "{{ (state_attr('sensor.pfsense_wan_in_stats','change_rate')|float*8*(state_attr('sensor.pfsense_wan_in_stats', 'sampling_size')-1)/1000000)|round(2) }}"
        unit_of_measurement: 'Mbps'
        entity_id: sensor.pfsense_wan_in_stats
      internet_out_mbps:
        icon_template: mdi:cloud-upload-outline
        value_template: "{{ (state_attr('sensor.pfsense_wan_out_stats','change_rate')|float*8*(state_attr('sensor.pfsense_wan_out_stats', 'sampling_size')-1)/1000000)|round(2) }}"
        unit_of_measurement: 'Mbps'
        entity_id: sensor.pfsense_wan_out_stats
8 Likes

I think we should keep on topic or post links to complete and/or improved alternatives (Faux API includes significantly more sensors than the bandwidth).

Did you figure out how to parse the ip4 address? I was looking at this and could only get it to work by hard coding the IP, which I would rather not do.

No, I just hard coded it in since that IP is not changing since it is the monitor IP of PFSense.

Hi I see this topic is a few months old but I’m having the same problem mentioned above; I have Home Assistant (not Hass.io) running in a docker container; when I run the python instruction in the command line I get the following error:

Traceback (most recent call last):
  File "custom_components/pfSense/pffa_get_system_stats.py", line 3, in <module>
    from PfsenseFauxapi import PfsenseFauxapi
  File "/config/home-assistant/custom_components/pfSense/PfsenseFauxapi.py", line 24, in <module>
    from __version__ import __version__
  File "/config/home-assistant/custom_components/pfSense/__version__.py", line 1
    <!DOCTYPE html>
    ^
SyntaxError: invalid syntax

I tried the solution @bthoven sugested but no changes. Any clue or suggestion?

I dont think you really need the version. It doesn’t look like it’s being used for any logic. Try deleting that import and just hard code it. You could just hard code it to what’s in that file. Something like:

__version__ = '20190317.1'

You can just delete that line and replace it with the above.

I modify it a now is not generating any error when executing from command line, but when I press CTRL+C I get the following:

Traceback (most recent call last):
  File "custom_components/pfSense/pffa_get_system_stats.py", line 27, in <module>
    PfsenseFauxapi.system_stats())
  File "/config/home-assistant/custom_components/pfSense/PfsenseFauxapi.py", line 91, in system_stats
    return self._api_request('GET', 'system_stats')
  File "/config/home-assistant/custom_components/pfSense/PfsenseFauxapi.py", line 125, in _api_request
    verify=self.use_verified_https
  File "/home/chapter/.local/lib/python3.6/site-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/home/chapter/.local/lib/python3.6/site-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/chapter/.local/lib/python3.6/site-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/chapter/.local/lib/python3.6/site-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/home/chapter/.local/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/home/chapter/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 672, in urlopen
    chunked=chunked,
  File "/home/chapter/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 376, in _make_request
    self._validate_conn(conn)
  File "/home/chapter/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 994, in _validate_conn
    conn.connect()
  File "/home/chapter/.local/lib/python3.6/site-packages/urllib3/connection.py", line 334, in connect
    conn = self._new_conn()
  File "/home/chapter/.local/lib/python3.6/site-packages/urllib3/connection.py", line 157, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "/home/chapter/.local/lib/python3.6/site-packages/urllib3/util/connection.py", line 74, in create_connection
    sock.connect(sa)
KeyboardInterrupt

@cweakland How do you find the baseoid of each sensor?

There’s a website you can use to lookup most OIDs:

http://www.oid-info.com/basic-search.htm

Let say you want to poll for interface em1, start with a snmpwalk:

$ snmpwalk -v2c -cpublic 10.1.1.1 1.3.6.1.2.1.31.1.1.1
iso.3.6.1.2.1.31.1.1.1.1.1 = STRING: “em0”
iso.3.6.1.2.1.31.1.1.1.1.2 = STRING: "em1"
iso.3.6.1.2.1.31.1.1.1.1.3 = STRING: “enc0”
iso.3.6.1.2.1.31.1.1.1.1.4 = STRING: “lo0”
iso.3.6.1.2.1.31.1.1.1.1.5 = STRING: “pflog0”
iso.3.6.1.2.1.31.1.1.1.1.6 = STRING: “pfsync0”
iso.3.6.1.2.1.31.1.1.1.1.7 = STRING: “em0.10”
iso.3.6.1.2.1.31.1.1.1.1.8 = STRING: “em0.20”
iso.3.6.1.2.1.31.1.1.1.1.9 = STRING: “em0.30”
iso.3.6.1.2.1.31.1.1.1.1.10 = STRING: “em0.40”

Next walk this branch:

$ snmpwalk -v2c -cpublic 10.1.1.1 1.3.6.1.2.1.31.1.1.1.6
iso.3.6.1.2.1.31.1.1.1.6.1 = Counter64: 2268394748162
iso.3.6.1.2.1.31.1.1.1.6.2 = Counter64: 2680156151093
iso.3.6.1.2.1.31.1.1.1.6.7 = Counter64: 3454055199
iso.3.6.1.2.1.31.1.1.1.6.8 = Counter64: 696671193
iso.3.6.1.2.1.31.1.1.1.6.9 = Counter64: 1631522976516
iso.3.6.1.2.1.31.1.1.1.6.10 = Counter64: 3773209828

Notice how the counters in the second walk align with the network interfaces from the first walk. In this case for em1 I want to use 1.3.6.1.2.1.31.1.1.1.6.2 . Let me know if you need more help.

Are those command in pfsense shell? Command wasnt recognized.

I would do this from a linux device, I believe you need to have the “snmp” package installed. You will need to setup snmp polling on pfsense and allow the linux device to poll it.

There are SNMP walk applications for windows as well.

Hrm. It looks like you interrupted it before it received any data. Do you get your stats_file (“pfSense_stats.json” in my example) to print? I’m not sure what the default timeout is for GET call, but it looks like it attempted to make the connection.

Okay seems to be working. Thanks. Im pretty sure I have the right data.