pfSense stat monitor

OK. I managed to get it working. Here are what the instruction may have to be amended on the HASSIO installation side (step 4 in OP onwards):
0. There are 3 files that we need to put into our Homeassistant folder (for me, I put them into /usr/share/hassio/homeassistant/custom_components/pfSense):
PfsenseFauxapi.py, pffa_get_system_stats.py (can be any name), and "_version_.py"

  1. All the three .py files must be in a same directory

  2. Inside the PfsenseFauxapi.py, line no. 24, we have to delete the ‘.’ (dot) from the "from .__version__ import __version__", to read as "from __version__ import __version__"; otherwise, it will report error

  3. Inside the pffa_get_system_stats.py, I need to change every ‘Fauxapi’ to be ‘PfsenseFauxapi’; otherwise, it will report ImportError: cannot import name ‘Fauxapi’. I don’t know why, but changing it makes it work.

  4. The sample configuration.yaml above needs to insert “Platform: file” and “file_path” after the command-line (see my config below).

Here is the result from direct run by command line: python3 pffa_get_system_stats 192.xxx.x.x api-key secrekey
image

Last warning, the api-key length must be between 12 and 40 characters (alphanumeric) and the secret-key length must be between 41 and 128 characters. We can define them ourselves. I made it too short and it didn’t work until I make the secret-key long enough.

Next steps are to add its sensors in my HASSIO configuration.yaml and display as a group.
Here is the result:
image
As I put all fauxapi files in “/usr/share/hassio/homeassistant/custom_components/pfSense” folder, I have to define like these in my configuration.yaml

......
.....
  whitelist_external_dirs:
    - /config/custom_components/pfSense
    - /config
....
....
sensor:
....
....
#-- pfSense -----
  #this will call the python script and store the cpu temp information in a sensor. the script will export 
  #the rest of the data into a file. we cant dump all of the data into 1 sensor since there is a 255 character 
  #limit for the sensor.
  - platform: command_line
    command: "python3 custom_components/pfSense/pffa_get_system_stats.py 192.168.x.x ...your fauxapi api-key....  ...your fauxapi-secrit-key..."
  - platform: file    
    file_path: pfSense_stats.json
    name: pfSense_CPU_temp
    value_template: '{{ value_json["data"]["stats"]["temp"] }}'
    unit_of_measurement : '°C'
    
  - platform: file
    file_path: pfSense_stats.json
    name: pfSense_uptime
    value_template: '{{ value_json["data"]["stats"]["uptime"] }}'
  
  - platform: file
    file_path: pfSense_stats.json
    name: pfSense_mem
    value_template: '{{ value_json["data"]["stats"]["mem"] }}'
    unit_of_measurement : '%'
    
  - platform: file
    file_path: pfSense_stats.json
    name: pfSense_cpu
    value_template: '{{ ( ( ((value_json["data"]["stats"]["cpu"].split("|")[0] | float) / (value_json["data"]["stats"]["cpu"].split("|")[1] | float)) - 1.0 ) * 100.0 ) | round(1) }}'
    unit_of_measurement : '%'

  - platform: file
    file_path: pfSense_stats.json
    name: pfSense_mbufpercent
    value_template: '{{ value_json["data"]["stats"]["mbufpercent"] }}'
    unit_of_measurement : '%'
....
.....

In the groups.yaml

....
....
pfSense Monitor:
  control: hidden
  entities:
    - sensor.pfsense_cpu
    - sensor.pfsense_cpu_temp
    - sensor.pfsense_mbufpercent
    - sensor.pfsense_mem
    - sensor.pfsense_uptime
...
...

working pffa_get_system_stats.py, amended as described above:

import os, sys, json
sys.path.append(os.path.abspath(os.path.join(os.path.curdir, '../client-libs/python')))     # hack to make this work in-place
from PfsenseFauxapi import PfsenseFauxapi


# check args exist
if(len(sys.argv) < 4):
    print()
    print('usage: ' + sys.argv[0] + ' <host> <apikey> <apisecret>')
    print()
    print('pipe JSON output through jq for easy pretty print output:-')
    print(' $ ' + sys.argv[0] + ' <host> <apikey> <apisecret> | jq .')
    print()
    sys.exit(1)

# config
fauxapi_host=sys.argv[1]
fauxapi_apikey=sys.argv[2]
fauxapi_apisecret=sys.argv[3]

PfsenseFauxapi = PfsenseFauxapi(fauxapi_host, fauxapi_apikey, fauxapi_apisecret, debug=False)


# system_stats
# =============================================================================
print(json.dumps(
    PfsenseFauxapi.system_stats())
)
open('pfSense_stats.json', 'w').close()  # clear data
with open('pfSense_stats.json', 'a') as stat_file:
    print(json.dumps(
    PfsenseFauxapi.system_stats()), file=stat_file)
1 Like