-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-mqtt-exporter.py
executable file
·260 lines (242 loc) · 7.95 KB
/
simple-mqtt-exporter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python3
import argparse
import importlib
import json
import os
import re
import sys
import time
import paho.mqtt.client as mqtt
import prometheus_client as prom
def smart_float(value):
if isinstance(value, bool):
if value:
return 1.0
else:
return 0.0
if isinstance(value, str):
if value in (
"on",
"ON",
"On",
"true",
"TRUE",
"True",
"yes",
"YES",
"Yes",
"Online",
"online",
):
return 1.0
if value in (
"off",
"OFF",
"Off",
"false",
"FALSE",
"False",
"no",
"NO",
"No",
"Offline",
"offline",
):
return 0.0
return float(value)
def get_field(content, field):
result = content
for f in field.split("."):
if isinstance(result, dict):
result = result.get(f, {})
return smart_float(result)
def regex_match(string):
for topic in list(config.mqtt_topics.keys()):
p = re.compile("^" + topic + "$")
if p.search(string):
# add regex based match to config.mqtt_topics as plain topic
config.mqtt_topics[string] = config.mqtt_topics[topic]
topic_init(string, config.mqtt_topics[topic])
return True
return False
def on_connect(client, userdata, flags, rc, properties=None):
codes = [
"Connection successful",
"Connection refused – incorrect protocol version",
"Connection refused – invalid client identifier",
"Connection refused – server unavailable",
"Connection refused – bad username or password",
"Connection refused – not authorised",
]
if rc != 0:
if hasattr(mqtt, "CallbackAPIVersion"):
print(rc)
elif rc > 0 and rc < 6:
print(codes[rc])
else:
print(f"Bad connection, unknown return code: {rc}")
os._exit(1)
def on_message(client, userdata, msg):
global data_received, succes, error
if config.debug:
print(f"{msg.topic}: {msg.payload}")
if msg.topic in config.mqtt_topics or regex_match(msg.topic):
try:
payload = str(msg.payload.decode("utf-8", "strict"))
succes[msg.topic] += 1
received_messages.labels(status="succes", topic=msg.topic).set(
succes[msg.topic]
)
except Exception as e:
print(f"{type(e).__name__}: {str(e)} while decoding topic {msg.topic}")
error[msg.topic] += 1
received_messages.labels(status="error", topic=msg.topic).set(
error[msg.topic]
)
return
data_received = True
# When the config is a list, we need to use the JSON fields in the message
if isinstance(config.mqtt_topics[msg.topic], list):
try:
content = json.loads(payload)
except Exception as e:
print(
f"{type(e).__name__}: {str(e)} while decoding json topic {msg.topic}"
)
error[msg.topic] += 1
received_messages.labels(status="error", topic=msg.topic).set(
error[msg.topic]
)
return
for item in config.mqtt_topics[msg.topic]:
field = item["field"]
try:
value = get_field(content, field)
except Exception as e:
print(
f"{type(e).__name__}: {str(e)} while decoding topic {msg.topic} field {field}"
)
error[msg.topic] += 1
received_messages.labels(status="error", topic=msg.topic).set(
error[msg.topic]
)
continue
gauges[msg.topic + ":" + field].set(value)
else:
try:
value = smart_float(payload)
except Exception as e:
print(
f"{type(e).__name__}: {str(e)} while decoding topic {msg.topic} field {field}"
)
error[msg.topic] += 1
received_messages.labels(status="error", topic=msg.topic).set(
error[msg.topic]
)
return
gauges[msg.topic].set(value)
if not msg.retain:
updated.set(time.time())
def topic_init(t, v):
global succes, error, gauges, parents
succes[t] = 0
error[t] = 0
parts = t.split("/")
if isinstance(v, list):
items = v
sep = ":"
else:
items = [v]
sep = ""
for i in items:
field = i.get("field", "")
topic = t + sep + field
name = i.get("name")
if not name:
name = parts[-1:][0]
description = i.get("help")
if not description:
description = t + sep + field
labels = i.get("labels", {})
labels["topic"] = t
if field:
labels["field"] = field
if len(parts) > 1 and not labels.get("sensor"):
labels["sensor"] = parts[1]
if len(parts) > 2 and not labels.get("device"):
labels["device"] = parts[2]
if not name in parents:
parents[name] = prom.Gauge(name, description, labels.keys())
try:
gauges[topic] = parents[name].labels(**labels)
except ValueError as e:
print(
f"{type(e).__name__} while adding gauge for topic {topic}: name = {name}, labels = {labels}, error = {str(e)}"
)
sys.exit(1)
def mqtt_init():
if hasattr(mqtt, "CallbackAPIVersion"):
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
else:
client = mqtt.Client()
if hasattr(config, "mqtt_username") and hasattr(config, "mqtt_password"):
client.username_pw_set(config.mqtt_username, config.mqtt_password)
client.on_connect = on_connect
client.on_message = on_message
try:
client.connect(config.mqtt_broker)
except TimeoutError as e:
print(f"{type(e).__name__} connecting to {config.mqtt_broker}: {e}")
sys.exit(1)
if hasattr(config, "mqtt_twc_topic") and not hasattr(config, "mqtt_topic"):
client.subscribe(config.mqtt_twc_topic)
else:
client.subscribe(config.mqtt_topic)
client.loop_start()
return client
if __name__ == "__main__":
sys.stdout.reconfigure(line_buffering=True)
sys.stderr.reconfigure(line_buffering=True)
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", help="config file to load", default="config")
args = parser.parse_args()
config = importlib.import_module(args.config)
succes = {}
error = {}
parents = {}
gauges = {}
for t, v in config.mqtt_topics.items():
# Do not initialize regex topics
if not "*" in t:
topic_init(t, v)
up = prom.Gauge("up", "client status")
updated = prom.Gauge("updated", "data last updated in epoch")
received_messages = prom.Gauge(
"received_messages",
"received messages per topic and status",
["status", "topic"],
)
client = mqtt_init()
prom.start_http_server(config.http_port)
fresh = time.time()
stale = 0
startup = True
while True:
data_received = False
if startup:
time.sleep(10)
else:
time.sleep(getattr(config, "sleep", 10))
if data_received:
up.set(1)
fresh = time.time()
stale = 0
startup = False
elif stale < 3:
stale += 1
else:
stale += 1
up.set(0)
if time.time() - fresh > 600:
print(f"Exiting, mqtt state = {client._state} for too long")
sys.exit(1)