Files
monitoring/promMon.py
2026-04-27 11:35:05 +02:00

59 lines
1.9 KiB
Python

# Author: Antonin Kaplan
# Date: 2025-12-23
#
# Prometheus client library wrapper for easier usage
#
# On path through the deepest forest even the dimmest light shines on your path to enlightenment
from prometheus_client import start_http_server, Gauge, Counter
# create prometheus monitoring object
# @param name of the monitores app for example: hw-monitor
# @param port of the exported endpoint which can be scraped by prometheus
# @retval None
class prometheus:
def __init__(self, name="promMon", port=8000):
self.name = name
self.port = port
self.monitors = {}
start_http_server(self.port)
# create variable for monitoring and add it to dict
# @param type of variable (Gauge, Counter)
# @param name of monitored value
# @param unit of measurement of specified value
# @retval None
def add_monitor(self, name, unit, tags=["instance"], type="Gauge"):
if type == "Gauge":
self.monitors[name] = Gauge(self.name+"_"+name, unit, tags)
elif type == "Counter":
self.monitors[name] = Counter(self.name+"_"+name, unit, tags)
else:
pass
# delete all tags from monitoring variable (variable stays) useful when storing data in tags like SMART or processes
# @param name of monitored value
# retval none
def delete_monitor(self, name):
self.monitors[name].clear()
# monitor function for updating monitored values
# @param name of monitored value
# @param instance identificator eg. IP address
# @param value of monitored variable
# @retval None
def monitor(self, name, tags, value):
self.monitors[name].labels(*tags).set(value)
if __name__ == '__main__':
from time import sleep
pm = prometheus(port=9339, name="test")
pm.add_monitor("time", "s", tags=["ip"])
time = 0
while True:
pm.monitor("time", ("localhost",), time)
time += 1
sleep(1)