-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMqtt2Zabbix.py
34 lines (24 loc) · 908 Bytes
/
Mqtt2Zabbix.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
import paho.mqtt.client as mqtt
from pyzabbix import ZabbixMetric, ZabbixSender
ZabbixServer=
ZabbixPort= # Default: 10051
MqttServer=
MqttPort= # Default: 1883
MqttUser=
MqttPassword=
MqttClient = # Must be unique
def on_connect(client, userdata, flags, rc):
client.subscribe("+")
def on_message(client, userdata, msg):
print(msg.topic+" - "+str(msg.payload.decode('utf-8')))
packet = [
ZabbixMetric(str(msg.topic).split('.')[0], str(msg.topic).split('.')[1], str(msg.payload.decode('utf-8'))),
]
result = ZabbixSender(zabbix_server=ZabbixServer, zabbix_port=ZabbixPort, use_config=None, chunk_size=250).send(packet)
print(result)
mqttclient = mqtt.Client(client_id=MqttClient)
mqttclient.on_connect = on_connect
mqttclient.on_message = on_message
mqttclient.username_pw_set(MqttUser, password=MqttPassword)
mqttclient.connect(MqttServer)
mqttclient.loop_forever()