Skip to content

Commit

Permalink
Route Homie-style MQTT sensor value publications to check_mk spool files
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Oct 22, 2016
1 parent f76af5a commit c2bebab
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
Empty file added examples/homie/__init__.py
Empty file.
81 changes: 81 additions & 0 deletions examples/homie/homie.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# -*- coding: utf-8 -*-
# Demonstrate Homie function extensions for mqttwarn

; ========
; Synopsis
; ========
;
; Run mqttwarn::
;
; export MQTTWARNINI=examples/homie/homie.ini
; ./mqttwarn.py
;
; Send some homie-like data::
;
; mosquitto_pub -t homie/bee1/weight/value -m 42.42


; ==================
; Base configuration
; ==================

[defaults]
hostname = 'localhost'
clientid = 'mqttwarn'

; logging
logformat = '%(asctime)-15s %(levelname)-5s [%(module)s] %(message)s'
logfile = stream://sys.stderr

; one of: CRITICAL, DEBUG, ERROR, INFO, WARN
#loglevel = INFO
loglevel = DEBUG

; enable service providers
launch = log, file

; number of notification dispatcher threads
num_workers = 3

; path to file containing self-defined functions
functions = 'examples.homie.homie'


; ================
; check_mk routing
; ================

; See also https://github.com/jpmens/mqttwarn/wiki/Incorporating-topic-names#incorporate-topic-names-into-topic-targets

[check_mk_universal]
topic = homie/+/+/value
datamap = decode_homie_topic()
targets = file:cmk_spool
format = <<<<{device}>>>>\n<<<local>>>\n 0 {node} {node}={payload} {node}: {payload}

[config:file]
append_newline = True
overwrite = True
targets = {
'cmk_spool': ['/var/lib/check_mk_agent/spool/300{device}-{node}'],
}


; ===============
; Regular logging
; ===============

[homie-logging]
; Just log all incoming messages
topic = homie/#
targets = log:info

[config:log]
targets = {
'debug' : [ 'debug' ],
'info' : [ 'info' ],
'warn' : [ 'warn' ],
'crit' : [ 'crit' ],
'error' : [ 'error' ]
}

35 changes: 35 additions & 0 deletions examples/homie/homie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# Homie function extensions for mqttwarn
import re


# ------------------------------------------
# Synopsis
# ------------------------------------------
#
# Run mqttwarn::
#
# export MQTTWARNINI=examples/homie/homie.ini
# ./mqttwarn.py
#
# Send some homie-like data::
#
# mosquitto_pub -t homie/bee1/weight/value -m 42.42


def decode_homie_topic(topic):
"""
Split Homie-style MQTT topic path into segments for
enriching transformation data inside mqttwarn.
"""
if type(topic) == str:
try:
pattern = r'^(?P<realm>.+?)/(?P<device>.+?)/(?P<node>.+?)/(?P<property>.+?)$'
p = re.compile(pattern)
m = p.match(topic)
topology = m.groupdict()
except:
topology = {}
return topology
return None

0 comments on commit c2bebab

Please sign in to comment.