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"
  • Copy
  • Fill window

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
  • Copy
  • Fill window

Example: TICKscript

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

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"
  • Copy
  • Fill window

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)
  • Copy
  • Fill window

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')
  • Copy
  • Fill window

Add and enable the TICKscript:

kapacitor define cpu_alert -tick cpu_alert.tick
kapacitor enable cpu_alert
  • Copy
  • Fill window

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
  • Copy
  • Fill window

Add the handler:

kapacitor define-topic-handler log_cpu_handler.yaml
  • Copy
  • Fill window

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 3 Core and Enterprise are now in Beta

InfluxDB 3 Core and Enterprise are now available for beta testing, available under MIT or Apache 2 license.

InfluxDB 3 Core is a high-speed, recent-data engine that collects and processes data in real-time, while persisting it to local disk or object storage. InfluxDB 3 Enterprise is a commercial product that builds on Core’s foundation, adding high availability, read replicas, enhanced security, and data compaction for faster queries. A free tier of InfluxDB 3 Enterprise will also be available for at-home, non-commercial use for hobbyists to get the full historical time series database set of capabilities.

For more information, check out: