-
Notifications
You must be signed in to change notification settings - Fork 183
zabbix
The zabbix
service serves two purposes:
- it can create a Zabbix host on-the-fly via Low-level Discovery (LLD)
- it can send an item/value pair to a Zabbix trapper
The target and topic configuration look like this:
[config:zabbix]
; Zabbix host with MQTT host discovery template attached
;host = <zabbix host>
targets = {
# Trapper address port
't1' : [ '172.16.153.110', 10051 ],
}
[zabbix/clients/+]
alldata = ZabbixData()
targets = zabbix:t1
[zabbix/item/#]
alldata = ZabbixData()
targets = zabbix:t1
A transformation function in alldata
is required to extract the client's name
from the topic, and for #1, to define a "host alive" item key in Zabbix.
# If the topic begins with zabbix/clients we have a host going up or down
# e.g. "zabbix/clients/jog03" -> "jog03"
# extract client name (3rd part of topic)
# set status key (e.g. 'host.up') to publish 1/0 on it (e.g during LWT)
#
# if the topic starts with zabbix/item we have an item/value for the host
# e.g. "zabbix/item/jog03/time.stamp" -> "jog03"
# extract client name (3rd part of topic)
#
def ZabbixData(topic, data, srv=None):
client = 'unknown'
key = None
status_key = None
parts = topic.split('/')
client = parts[2]
if topic.startswith('zabbix/clients/'):
status_key = 'host.up'
if topic.startswith('zabbix/item/'):
key = parts[3]
return dict(client=client, key=key, status_key=status_key)
The blog post Zabbix meets MQTT explains a bit of the background.
We want to receive temperature and humidity values from ESP8266 devices, for instance such kind of IOT-devices: http://homecircuits.eu/blog/battery-powered-esp8266-iot-logger/ or https://github.com/tzapu/DeepSleepDHT22
As Firmware we can use: https://github.com/arendst/Sonoff-Tasmota or https://github.com/letscontrolit/ESPEasy
then we wish to capture all data from mqtt topic like tele/device/metric, for instance: tele/bathroom/temperature
To archieve this we add following settings to /opt/mqttwarn/mqttwarn.ini:
functions = 'samplefuncs'
[config:zabbix]
targets = {
't1' : [ 'localhost', 10051 ],
}
[tele/#]
alldata = ZabbixData()
targets = zabbix:t1
in the file /opt/mqttwarn/samplefuncs.py, we add:
def ZabbixData(topic, data, srv=None):
status_key = None
# the first part (part[0]) is always tele
# the second part (part[1]) is the device, the value comes from
# the third part (part[2]) is the name of the metric (e.g. temperature/humidity/voltage...)
parts = topic.split('/')
client = parts[1]
key = parts[2]
return dict(client=client, key=key, status_key=status_key)
After this, we need to create a host (or more hosts) in zabbix with the corresponding devicename that delivers the value ((part[1]) in samplefuncs.py). At last we need to create an _item _inside the new created host:
Name: You are free in create name for it, I named it mqtt_temp.
Type: zabbix_trapper.
Key: must correspond with the third part (part[2]) in the delivered mqtt-topic, for instance temperature
Type of information: numeric (float)
That's all! After publishing a value to the topic e.g. tele/bathroom/temperature, you should be able see this value in zabbix at host named "bathroom" with the item "temperature".