You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I run prometheus + grafana at home to graph weather, electric usage, and network data. I wanted to include pool data too.
I wrote a hacky python script which reads the poolState.json file and makes the data available as a webserver that prometheus can regularly poll.
Update the poolStatus_file path and change the exporter_port if you need to. A lot of the json paths are hardcoded to what was in my poolState.json. I'm not a good enough python developer to dynamically publish everything from poolState.json.
Save this as a .py file on your nodeJS-poolController device, make it executable, and then run python3 njsPC-prometheus-exporter.py. Try to browse to your device using a browser on exporter_port 9877. You should see standard prometheus data including output from poolState.json. Hit CTRL+C to exit out of it and stop it.
"""
njsPC exporter
Based on https://trstringer.com/quick-and-easy-prometheus-exporter/
"""
import os
import time
from prometheus_client import start_http_server, Gauge, Enum, Info
import requests
import json
class AppMetrics:
"""
Representation of Prometheus metrics and loop to fetch and transform
application metrics into Prometheus metrics.
"""
def __init__(self, poolStatus_file='', polling_interval_seconds=60):
self.poolStatus_file = poolStatus_file
self.polling_interval_seconds = polling_interval_seconds
# Prometheus metrics to collect
self.waterSensor1 = Gauge("waterSensor1", "water temp")
self.air = Gauge("air", "air temp")
self.pump1_watts = Gauge("pump1_watts", "pump1 watts")
self.pump1_rpm = Gauge("pump1_rpm", "pump1 rpm")
self.pump1_gpm = Gauge("pump1_gpm", "pump1 gpm")
self.salt_level = Gauge("salt_level", "salt level ppm")
self.filter_psi = Gauge("filter_psi", "filter psi")
self.time = Info("last_updated", "last updated")
# self.pending_requests = Gauge("app_requests_pending", "Pending requests")
# self.total_uptime = Gauge("app_uptime", "Uptime")
# self.health = Enum("app_health", "Health", states=["healthy", "unhealthy"])
def run_metrics_loop(self):
"""Metrics fetching loop"""
while True:
self.fetch()
time.sleep(self.polling_interval_seconds)
def fetch(self):
"""
Get metrics from application and refresh Prometheus metrics with
new values.
"""
# Fetch raw status data from the application
#resp = requests.get(url=f"http://localhost:{self.poolStatus_file}/status")
#status_data = resp.json()
poolStatus = open(self.poolStatus_file)
status_data = json.load(poolStatus)
poolStatus.close()
# Update Prometheus metrics with application metrics
self.waterSensor1.set(status_data['temps']['waterSensor1'])
self.air.set(status_data['temps']['air'])
self.pump1_watts.set(status_data['pumps'][0]['watts'])
self.pump1_rpm.set(status_data['pumps'][0]['rpm'])
self.pump1_gpm.set(status_data['pumps'][0]['flow'])
self.salt_level.set(status_data['chlorinators'][0]['saltLevel'])
self.filter_psi.set(status_data['filters'][0]['pressure'])
self.time.info({'last_updated':status_data['time']})
# self.pending_requests.set(status_data["pending_requests"])
# self.total_uptime.set(status_data["total_uptime"])
# self.health.state(status_data["health"])
def main():
"""Main entry point"""
polling_interval_seconds = int(os.getenv("POLLING_INTERVAL_SECONDS", "60"))
poolStatus_file = str(os.getenv("POOL_STATUS_FILE", "/home/chouse/nodejs-poolController/data/poolState.json"))
exporter_port = int(os.getenv("EXPORTER_PORT", "9877"))
app_metrics = AppMetrics(
poolStatus_file=poolStatus_file,
polling_interval_seconds=polling_interval_seconds
)
start_http_server(exporter_port)
app_metrics.run_metrics_loop()
if __name__ == "__main__":
main()
To run it as a service, create /etc/systemd/system/njsPC-prometheus-exporter.service with the below, updating the ExecStart path to where your exporter script is.
Then run sudo systemctl daemon-reload followed by sudo systemctl start njsPC-prometheus-exporter.service.
Use a browser again to try to reach your device on exporter_port 9877 to verify it is running.
On your prometheus server, add the following to your prometheus.yml, changing the target IP to your own device where the prometheus exporter is running. Restart the prometheus service to start collecting metrics.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I run prometheus + grafana at home to graph weather, electric usage, and network data. I wanted to include pool data too.
I wrote a hacky python script which reads the poolState.json file and makes the data available as a webserver that prometheus can regularly poll.
Update the poolStatus_file path and change the exporter_port if you need to. A lot of the json paths are hardcoded to what was in my poolState.json. I'm not a good enough python developer to dynamically publish everything from poolState.json.
Save this as a .py file on your nodeJS-poolController device, make it executable, and then run
python3 njsPC-prometheus-exporter.py
. Try to browse to your device using a browser on exporter_port 9877. You should see standard prometheus data including output from poolState.json. Hit CTRL+C to exit out of it and stop it.To run it as a service, create /etc/systemd/system/njsPC-prometheus-exporter.service with the below, updating the ExecStart path to where your exporter script is.
Then run
sudo systemctl daemon-reload
followed bysudo systemctl start njsPC-prometheus-exporter.service
.Use a browser again to try to reach your device on exporter_port 9877 to verify it is running.
On your prometheus server, add the following to your prometheus.yml, changing the target IP to your own device where the prometheus exporter is running. Restart the prometheus service to start collecting metrics.
Beta Was this translation helpful? Give feedback.
All reactions