forked from kylegordon/mqtt-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqtt-zabbix.py
executable file
·359 lines (306 loc) · 10.6 KB
/
mqtt-zabbix.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env python3
__author__ = "Kyle Gordon"
__copyright__ = "Copyright (C) Kyle Gordon"
import os
import logging
import logging.handlers
import signal
import socket
import time
import sys
import csv
import paho.mqtt.client as mqtt
import configparser
from datetime import datetime, timedelta
from zbxsend import Metric, send_to_zabbix
# LLD data
lld_data = {}
last_stale_check = 0
last_full_lld_send = 0
LLD_SEND_INTERVAL = 3600
# Read the config file
config = configparser.RawConfigParser()
config.read("/etc/mqtt-zabbix/mqtt-zabbix.cfg")
# Use ConfigParser to pick out the settings
DEBUG = config.getboolean("global", "debug")
LOGFILE = config.get("global", "logfile")
MQTT_HOST = config.get("global", "mqtt_host")
MQTT_PORT = config.getint("global", "mqtt_port")
MQTT_USER = config.get("global", "mqtt_user")
MQTT_PASSWORD = config.get("global", "mqtt_password")
MQTT_TOPIC = config.get("global", "mqtt_topic")
KEYFILE = config.get("global", "keyfile")
KEYHOST = config.get("global", "keyhost")
ZBXSERVER = config.get("global", "zabbix_server")
ZBXPORT = config.getint("global", "zabbix_port")
APPNAME = "mqtt-zabbix"
PRESENCETOPIC = "clients/" + socket.getfqdn() + "/" + APPNAME + "/state"
client_id = APPNAME + "_%d" % os.getpid()
mqttc = mqtt.Client(client_id="client_id")
LOGFORMAT = "%(asctime)-15s %(message)s"
if __name__ == "__main__":
logger = logging.getLogger()
else:
logger = logging.getLogger(__name__)
if DEBUG:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
fileformatter = logging.Formatter(LOGFORMAT)
filehandler = logging.handlers.WatchedFileHandler(LOGFILE)
filehandler.setFormatter(fileformatter)
logger.addHandler(filehandler)
logger.info("Starting " + APPNAME)
logger.info("INFO MODE")
logger.debug("DEBUG MODE")
# All the MQTT callbacks start here
def on_publish(mosq, obj, mid):
"""
What to do when a message is published
"""
logger.debug("MID " + str(mid) + " published.")
def on_subscribe(mosq, obj, mid, qos_list):
"""
What to do in the event of subscribing to a topic"
"""
logger.debug("Subscribe with mid " + str(mid) + " received.")
def on_unsubscribe(mosq, obj, mid):
"""
What to do in the event of unsubscribing from a topic
"""
logger.debug("Unsubscribe with mid " + str(mid) + " received.")
def on_connect(mosq, obj, flags, result_code):
"""
Handle connections (or failures) to the broker.
This is called after the client has received a CONNACK message
from the broker in response to calling connect().
The parameter rc is an integer giving the return code:
0: Success
1: Refused – unacceptable protocol version
2: Refused – identifier rejected
3: Refused – server unavailable
4: Refused – bad user name or password (MQTT v3.1 broker only)
5: Refused – not authorised (MQTT v3.1 broker only)
"""
logger.debug("on_connect RC: " + str(result_code))
if result_code == 0:
logger.info("Connected to %s:%s", MQTT_HOST, MQTT_PORT)
# Publish retained LWT as per
# http://stackoverflow.com/q/97694
# See also the will_set function in connect() below
mqttc.publish(PRESENCETOPIC, "1", retain=True)
process_connection()
elif result_code == 1:
logger.info("Connection refused - unacceptable protocol version")
cleanup()
elif result_code == 2:
logger.info("Connection refused - identifier rejected")
cleanup()
elif result_code == 3:
logger.info("Connection refused - server unavailable")
logger.info("Retrying in 30 seconds")
time.sleep(30)
elif result_code == 4:
logger.info("Connection refused - bad user name or password")
cleanup()
elif result_code == 5:
logger.info("Connection refused - not authorised")
cleanup()
else:
logger.warning("Something went wrong. RC:" + str(result_code))
cleanup()
def on_disconnect(mosq, obj, result_code):
"""
Handle disconnections from the broker
"""
if result_code == 0:
logger.info("Clean disconnection")
else:
logger.info("Unexpected disconnection! Reconnecting in 5 seconds")
logger.debug("Result code: %s", result_code)
time.sleep(5)
def on_message(mosq, obj, msg):
"""
What to do when the client recieves a message from the broker
"""
logger.debug("Received: " + msg.payload.decode() +
" received on topic " + msg.topic +
" with QoS " + str(msg.qos))
process_message(msg)
def on_log(mosq, obj, level, string):
"""
What to do with debug log output from the MQTT library
"""
logger.debug(string)
# End of MQTT callbacks
def process_connection():
"""
What to do when a new connection is established
"""
logger.debug("Processing connection")
mqttc.subscribe(MQTT_TOPIC, 2)
def send_lld_data(discovery_key_name, datadict):
output = "[\n"
first = True
for topic in sorted(datadict.keys()):
_, sensorname, metric = topic.split("/")
item = "{}.{}".format(sensorname, metric)
if not first:
output += ",\n"
first = False
output += "\t{\n"
output += '\t\t"{}":"{}",\n'.format("{#ITEMNAME}",item)
output += '\t\t"{}":"{}"\n'.format("{#ITEMDESCR}",KeyMap.item_names[topic])
output += "\t}"
output += "\n]\n"
logger.debug("Generated LLD data:\n{}".format(output))
logger.info("Sending LLD data for {} (len={}) for host {}".format(
discovery_key_name,
len(datadict),
KEYHOST
))
send_to_zabbix(
[Metric(KEYHOST, discovery_key_name, output)],
ZBXSERVER,
ZBXPORT
)
return
def get_key_type(topic):
_, sensorname, metric = topic.split("/")
if metric == "battery":
return "battery"
elif metric.startswith("t") or metric == "external temperature":
return "temperature"
elif metric == "humidity":
return "humidity"
elif metric == "rssi":
return "rssi"
else:
return "unknown"
def lld_update(topic):
global lld_data
global last_full_lld_send
global last_stale_check
keytype = get_key_type(topic)
if keytype not in lld_data:
# Initialize the dict if needed
lld_data[keytype] = {}
time_now = time.monotonic()
send_needed = False
if topic not in lld_data[keytype]:
send_needed = True
# Set/update the timestamp
lld_data[keytype][topic] = time_now
# Do the stale topic check only once a minute
if last_stale_check+60 < time_now:
last_stale_check = time_now
# Check all keytypes for any outdated topics
for kt in lld_data:
for t in lld_data[kt]:
if lld_data[kt][t]+LLD_SEND_INTERVAL < time_now:
# No data for that topic for some time, remove it
del lld_data[kt][t]
if last_full_lld_send+LLD_SEND_INTERVAL < time_now:
logger.info("Sending full LLD updates (every {} seconds)".format(LLD_SEND_INTERVAL))
last_full_lld_send = time_now
# Send LLD data for all keytypes
for keytype in lld_data:
send_lld_data("mqtt-zabbix.discovery.{}".format(keytype), lld_data[keytype])
# No need to continue further, sent all already
return
if send_needed:
# New topic was added so send this LLD data anyway
send_lld_data("mqtt-zabbix.discovery.{}".format(keytype), lld_data[keytype])
return
def get_zabbix_item(topic):
_, sensorname, metric = topic.split("/")
return "mqtt-zabbix.{}[{}.{}]".format(
get_key_type(topic),
sensorname,
metric
)
def process_message(msg):
"""
What to do with the message that's arrived.
Looks up the topic in the KeyMap dictionary, and forwards
the message onto Zabbix using the associated Zabbix key
"""
topic = msg.topic
payload = msg.payload.decode()
logger.debug("Processing: " + topic)
if topic in KeyMap.item_names:
lld_update(topic)
if payload == "ON":
payload = "1"
elif payload == "OFF":
payload = "0"
keyname = get_zabbix_item(topic)
logger.info("Sending {} = {} to Zabbix to host {} key {}".format(
topic, payload, KEYHOST, keyname
))
# Zabbix can also accept text and character data...
# should we sanitize input or just accept it as is?
send_to_zabbix(
[Metric(KEYHOST, keyname, payload)],
ZBXSERVER,
ZBXPORT
)
else:
# Received something with a /raw/ topic,
# but it didn't match anything. Log it, and discard it
logger.debug("Unknown: {}".format(topic))
def cleanup(signum, frame):
"""
Signal handler to ensure we disconnect cleanly
in the event of a SIGTERM or SIGINT.
"""
logger.info("Disconnecting from broker")
# Publish a retained message to state that this client is offline
mqttc.publish(PRESENCETOPIC, "0", retain=True)
mqttc.disconnect()
logger.info("Exiting on signal %d", signum)
sys.exit(signum)
def connect():
"""
Connect to the broker, define the callbacks, and subscribe
This will also set the Last Will and Testament (LWT)
The LWT will be published in the event of an unclean or
unexpected disconnection.
"""
logger.debug("Connecting to %s:%s", MQTT_HOST, MQTT_PORT)
# Set the Last Will and Testament (LWT) *before* connecting
mqttc.will_set(PRESENCETOPIC, "0", qos=0, retain=True)
mqttc.username_pw_set(MQTT_USER, MQTT_PASSWORD)
result = mqttc.connect(MQTT_HOST, MQTT_PORT, 60)
if result != 0:
logger.info("Connection failed with error code %s. Retrying", result)
time.sleep(10)
connect()
# Define the callbacks
mqttc.on_connect = on_connect
mqttc.on_disconnect = on_disconnect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
mqttc.on_unsubscribe = on_unsubscribe
mqttc.on_message = on_message
if DEBUG:
mqttc.on_log = on_log
class KeyMap:
"""
Read the topics and keys into a dictionary for internal lookups
"""
logger.debug("Loading map")
with open(KEYFILE, mode="r") as inputfile:
reader = csv.reader(inputfile)
item_names = dict((rows[0], rows[1]) for rows in reader)
# Use the signal module to handle signals
signal.signal(signal.SIGTERM, cleanup)
signal.signal(signal.SIGINT, cleanup)
# Connect to the broker
connect()
# Try to loop_forever until interrupted
try:
mqttc.loop_forever()
except KeyboardInterrupt:
logger.info("Interrupted by keypress")
sys.exit(0)