26
MQTT for monitoring (and for the IoT) Jan-Piet Mens November 2014 @jpmens

OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

  • Upload
    netways

  • View
    388

  • Download
    4

Embed Size (px)

DESCRIPTION

MQTT is a PUB/SUB protocol for the Internet of Things, but it's also valuable for systems administration. We'll take a close look at MQTT and its infrastructure, and we'll show you how to use a microcontroller to monitor your server-room's temperature with it, publishing and monitoring it via MQTT and Icinga/Nagios

Citation preview

Page 1: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

MQTT for monitoring (and for the IoT)

Jan-Piet MensNovember 2014

@jpmens

Page 2: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

MQTT is a standard, a transport,

PUB/SUB messaging, designed for

unreliable networks

Page 3: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

transport protocol

binary payload, 256MB, (+2 bytes), fast,

lightweight, ideal for low-bandwith, high-latency networks

Page 4: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

security

TLSauthentication

ACLsTLS-PSK

(payload encryption)

Page 5: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

Quality of Service

0 At most once

1 Assured delivery

2 Once only

Page 6: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

more featureskeepalive

last will & testament,decoupled senders/recipients,

durable messages

Page 7: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

topic names

UTF-8, hierarchical, wildcards

temperature/room/livingdevices/#finance/+/eur/rate

Page 8: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

PUB/SUB cauldron

Page 9: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

MQTT brokersthe server bit of MQTT

Page 10: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

Mosquitto C, fast, lightweight, ACLs (plugin), TLS, TLS-PSK, bridge,

Websockets, logging via $SYS

http://mosquitto.org

Page 11: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

HiveMQ Java, plugins, Websockets, clustering, modules

http://hivemq.com

Page 12: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

MQTT brokers$SYS topic

$SYS/broker/clients/total 1771$SYS/broker/messages/received 36597465$SYS/broker/messages/sent 39714120$SYS/broker/messages/stored 2941$SYS/broker/bytes/received 2830787008$SYS/broker/bytes/sent 3810653433$SYS/broker/version mosquitto version 1.4$SYS/broker/publish/messages/received 19798673$SYS/broker/publish/messages/sent 30622855$SYS/broker/publish/bytes/received 1868229299$SYS/broker/publish/bytes/sent 3185942282

Page 13: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

bridging

Page 14: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

CLI utilitiesmosquitto_sub -v [-h localhost] [-p 1883] [--cafile file] [--cert file --key file] [-u username [-P password]] [ --tls-version tlsv1.2 ] -t 'topic/#'

subscribe

publish

mosquitto_pub ... [-r] -t topic -m payload

Page 15: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

languages

Lua, Python, C, JavaScript, Perl, Ruby, Java, ...

Page 16: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

Python API: PUB#!/usr/bin/env python

import paho.mqtt.publish as mqtt

mqtt.single('conf/hello', 'Hello MQTT')

$ mosquitto_sub -h localhost -v -t 'conf/#'conf/hello Hello MQTT

payloadtopic

Page 17: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

Python API: SUB

callbacks

#!/usr/bin/env python

import paho.mqtt.client as paho

def on_connect(mosq, userdata, rc): mqttc.subscribe("conf/+", 0)

def on_message(mosq, userdata, msg): print "%s %s %s" % (msg.topic, str(msg.payload), userdata)

data = { 'type' : 'conference' }mqttc = paho.Client(userdata=data)mqttc.on_connect = on_connectmqttc.on_message = on_message

mqttc.connect("localhost", 1883, 60)mqttc.loop_forever()

Page 18: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

Python API: SUB

$ mosquitto_pub -t 'conf/thirsty' -m 'Beertime?'$ mosquitto_pub -t 'conf/catering' -m 'Coffee is ready'

$ ./sub.pyconf/thirsty Beertime? {'type': 'conference'}conf/catering Coffee is ready {'type': 'conference'}

Page 19: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

practical solutions

alerting, metering, logging, location awareness, tracking, automation, and controlling, host monitoring

Page 20: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

alerting: mqttwarn

https://github.com/jpmens/mqttwarn

Page 21: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

temperature: Arduino

huh?

Page 22: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

all good!

Page 23: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

how does this work?

arduino/10.0.69.105/celsius 24

Page 24: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

critical: not good!

Page 25: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

via MQTT to mobile

Page 26: OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

mqtt.org @mqttorg