Commit 84898a27 authored by mik's avatar mik

neue mqtt-pfade und unter metrics/ gibts einen (hoffentlich) prometheus exporter

parent 961c6df6
......@@ -16,15 +16,15 @@ sensors['door_locked'] = [
{"topic":"devlol/h19/door/lockstatus","valuetype":"doorboolean","location":"Front door"}
]
sensors['temperature'] = [
{"topic":"devlol/h19/mainroom/temperature","unit":"°C","location":"main room"},
{"topic":"mainstripe/sensor/main_room_temperature/state","unit":"°C","location":"main room"},
{"topic":"devlol/h19/dungeon/DHT21/temp","unit":"°C","location":"dungeon"},
]
sensors['humidity'] = [
{"topic":"devlol/h19/mainroom/humidity","unit":"%","location":"main room"},
{"topic":"mainstripe/sensor/main_room_humidity/state","unit":"%","location":"main room"},
{"topic":"devlol/h19/dungeon/DHT21/hum","unit":"%","location":"dungeon"},
]
sensors['people_now_present'] = [
{"topic":"devlol/h19/mainroom/headcount"},
{"topic":"devlol/h19/mainroom/supermariocount/total"},
]
sensors['network_connections'] = [
{"topic":"devlol/h19/mainroom/dhcpleases","valuetype":"int"},
......@@ -39,6 +39,8 @@ class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
global r
logging.debug("self.path" + self.path)
if self.path == "/":
self.send_response(200)
self.send_header("Content-type", "application/json")
......@@ -47,7 +49,15 @@ class MyHandler(BaseHTTPRequestHandler):
self.wfile.write(str.encode(generate_hackerspaceapi_json()))
except BrokenPipeError:
pass
elif self.path == "/openstatus":
elif self.path == "/metrics/":
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
try:
self.wfile.write(str.encode(generate_hackerspaceapi_metrics()))
except BrokenPipeError:
pass
elif self.path == "/openstatus/":
self.send_response(302)
if str(r.get('devlol/h19/door/lockstatus'),'utf-8') == 'UNLOCKED':
self.send_header('Location','https://devlol.org/wiki/DevLoL?action=AttachFile&do=get&target=open.png')
......@@ -83,6 +93,40 @@ def on_connect(client, userdata, foo):
client.subscribe(sensor['topic'])
logging.debug ("subscribing to mqtt-topic: " + sensor['topic'])
def generate_hackerspaceapi_metrics():
global r
resp = ""
for sensortype in sensors:
for sensor in sensors[sensortype]:
try:
try:
if "valuetype" in sensor.keys():
if sensor['valuetype'] == 'int':
resp = resp + "\r\n" + sensor['topic'].replace('/','.') + " " + str(int(str(r.get(sensor['topic']),'utf-8')))
elif sensor['valuetype'] == 'doorboolean':
if str(r.get(sensor['topic']),'utf-8') == "UNLOCKED":
resp = resp + "\r\n" + sensor['topic'].replace('/','.') + " 1"
else:
resp = resp + "\r\n" + sensor['topic'].replace('/','.') + " 0"
else:
resp = resp + "\r\n" + sensor['topic'].replace('/','.') + " " + str((str(r.get(sensor['topic']),'utf-8')))
#if sensor['valuetype'] == 'string':
# pass
# #value = str(r.get(sensor['topic']),'utf-8')
#if sensor['valuetype'] == 'doorboolean':
# if str(r.get(sensor['topic']),'utf-8') == "UNLOCKED":
# value = False
# else: value = True
except Exception as e:
print(e)
validvalue = True
except:
validvalue = False
return resp
def generate_hackerspaceapi_json():
global r
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment