Populate metrics in InfluxDB with handlers

A Sensu event handler is an action the Sensu backend executes when a specific event occurs. In this guide, you’ll use a handler to populate the time series database InfluxDB. If you’re not familiar with handlers, consider reading the handlers reference before continuing through this guide.

Use a handler to populate InfluxDB

The example in this guide explains how to populate Sensu metrics into the time series database InfluxDB. Metrics can be collected from check output or the Sensu StatsD Server.

Register the asset

Assets are shareable, reusable packages that make it easier to deploy Sensu plugins. This example uses the Sensu InfluxDB Handler asset to power an influx-db handler.

Use this sensuctl example to register the Sensu InfluxDB Handler asset for Linux AMD64 or download the latest asset definition for your platform from Bonsai and register the asset with sensuctl create --file filename.yml.

sensuctl asset create sensu-influxdb-handler --url "https://assets.bonsai.sensu.io/b28f8719a48aa8ea80c603f97e402975a98cea47/sensu-influxdb-handler_3.1.2_linux_amd64.tar.gz" --sha512 "612c6ff9928841090c4d23bf20aaf7558e4eed8977a848cf9e2899bb13a13e7540bac2b63e324f39d9b1257bb479676bc155b24e21bf93c722b812b0f15cb3bd"

You should see a confirmation message from sensuctl:

Created

Create the handler

Now that you have registered the asset, you’ll use sensuctl to create a handler called influx-db that pipes event data to InfluxDB with the sensu-influxdb-handler asset. Edit the command below to include your database name, address, username, and password. For more information about the Sensu InfluxDB handler, see the asset page in Bonsai.

sensuctl handler create influx-db \
--type pipe \
--command "sensu-influxdb-handler -d sensu" \
--env-vars "INFLUXDB_ADDR=http://influxdb.default.svc.cluster.local:8086, INFLUXDB_USER=sensu, INFLUXDB_PASS=password" \
--runtime-assets sensu-influxdb-handler

You should see a confirmation message from sensuctl:

Created

Assign the handler to an event

With the influx-db handler created, you can assign it to a check for check output metric extraction. In this example, the check name is collect-metrics:

sensuctl check set-output-metric-handlers collect-metrics influx-db

You can also assign the handler to the Sensu StatsD listener at agent startup to pass all StatsD metrics into InfluxDB:

sensu-agent start --statsd-event-handlers influx-db

Validate the handler

It might take a few moments after you assign the handler to the check or StatsD server for Sensu to receive the metrics, but after an event is handled you should start to see metrics populating InfluxDB. You can verify proper handler behavior with sensu-backend logs. See Troubleshooting for log locations by platform.

Whenever an event is being handled, a log entry is added with the message "handler":"influx-db","level":"debug","msg":"sending event to handler", followed by a second log entry with the message "msg":"pipelined executed event pipe handler","output":"","status":0.

Next steps

Now that you know how to apply a handler to metrics and take action on events, here are a few other recommended resources: