Documentation

MQTT event handler

MQTT is a lightweight messaging protocol for small sensors and mobile devices. Kapacitor can be configured to send alert messages to an MQTT broker.

Configuration

Configuration as well as default option values for the MQTT event handler are set in your kapacitor.conf. Below is an example configuration:

[[mqtt]]
  enabled = true
  name = "localhost"
  default = true
  url = "tcp://localhost:1883"
  ssl-ca = "/etc/kapacitor/ca.pem"
  ssl-cert = "/etc/kapacitor/cert.pem"
  ssl-key = "/etc/kapacitor/key.pem"
  client-id = "xxxx"
  username = "xxxx"
  password = "xxxx"

Multiple MQTT brokers may be configured by repeating [[mqtt]] sections. The name acts as a unique identifier for each configured MQTT client.

enabled

Set to true to enable the MQTT event handler.

name

Unique name for this broker configuration.

default

When using multiple MQTT configurations, sets the current configuration as the default.

url

URL of the MQTT broker. Possible protocols include:

tcp - Raw TCP network connection
ssl - TLS protected TCP network connection
ws - Websocket network connection

ssl-ca

Absolute path to certificate authority (CA) file. A CA can be provided without a key/certificate pair.

ssl-cert

Absolute path to pem encoded certificate file.

ssl-key

Absolute path to pem encoded key file.

client-id

Unique ID for this MQTT client. If empty, the value of name is used.

username

MQTT username.

password

MQTT password.

Options

The following MQTT event handler options can be set in a handler file or when using .mqtt() in a TICKscript.

NameTypeDescription
broker-namestringThe name of the configured MQTT broker to use when publishing the alert. If empty defaults to the configured default broker.
topicstringThe MQTT topic to which alerts will be dispatched
qosint64The QoS that will be used to deliver the alerts. Valid values include:

0 : At most once delivery
1 : At least once delivery
2 : Exactly once delivery
retainedboolIndicates whether this alert should be delivered to clients that were not connected to the broker at the time of the alert.

Example: handler file

id: handler-id
topic: topic-name
kind: mqtt
options:
  broker-name: 'name'
  topic: 'topic-name'
  qos: 1
  retained: true

Example: TICKscript

|alert()
  // ...
  .mqtt('topic-name')
    .brokerName('name')
    .qos(1)
    .retained(TRUE)

Using the MQTT event handler

The MQTT event handler can be used in both TICKscripts and handler files to send alerts to an MQTT broker.

The examples below use the following MQTT broker configurations defined in the kapacitor.conf:

MQTT settings in kapacitor.conf

[[mqtt]]
  enabled = true
  name = "localhost"
  default = true
  url = "tcp://localhost:1883"

[[mqtt]]
  enabled = true
  name = "alerts-broker"
  default = false
  url = "ssl://123.45.67.89:1883"
  ssl-ca = "/etc/kapacitor/ca.pem"
  ssl-cert = "/etc/kapacitor/cert.pem"
  ssl-key = "/etc/kapacitor/key.pem"
  client-id = "alerts-broker"
  username = "myuser"
  password = "mysupersecretpassw0rd"

Send alerts to an MQTT broker from a TICKscript

The following TICKscript uses the .mqtt() event handler to send alerts to the alerts MQTT topic of the default MQTT broker defined in the kapacitor.confi whenever idle CPU usage drops below 10%.

log-cpu-alert.tick

stream
  |from()
    .measurement('cpu')
  |alert()
    .crit(lambda: "usage_idle" < 10)
    .message('{{ .Time }}: CPU usage over 90%')
    .mqtt('alerts')
      .qos(2)

Send alerts to an MQTT broker from a defined handler

The following setup sends an alert to the cpu topic. An MQTT handler is added that subscribes to the cpu topic and sends messages to alerts MQTT topic of the alerts-broker whenever a new message is published.

Create a TICKscript that publishes alert messages to a topic. The TICKscript below sends an alert message to the cpu topic any time idle CPU usage drops below 10%.

cpu_alert.tick

stream
  |from()
    .measurement('cpu')
  |alert()
    .crit(lambda: "usage_idle" < 10)
    .message('{{ .Time }}: CPU usage over 90%')
    .topic('cpu')

Add and enable the TICKscript:

kapacitor define cpu_alert -tick cpu_alert.tick
kapacitor enable cpu_alert

Create a handler file that subscribes to the cpu topic and uses the MQTT event handler to send alerts to the alerts-broker.

log_cpu_handler.yaml

id: log-cpu-alert
topic: cpu
kind: mqtt
options:
  broker-name: 'alerts-broker'
  topic: 'alerts'
  qos: 2
  retained: true

Add the handler:

kapacitor define-topic-handler log_cpu_handler.yaml

Was this page helpful?

Thank you for your feedback!


The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Read more

InfluxDB v3 enhancements and InfluxDB Clustered is now generally available

New capabilities, including faster query performance and management tooling advance the InfluxDB v3 product line. InfluxDB Clustered is now generally available.

InfluxDB v3 performance and features

The InfluxDB v3 product line has seen significant enhancements in query performance and has made new management tooling available. These enhancements include an operational dashboard to monitor the health of your InfluxDB cluster, single sign-on (SSO) support in InfluxDB Cloud Dedicated, and new management APIs for tokens and databases.

Learn about the new v3 enhancements


InfluxDB Clustered general availability

InfluxDB Clustered is now generally available and gives you the power of InfluxDB v3 in your self-managed stack.

Talk to us about InfluxDB Clustered