How to populate InfluxDB metrics using handlers
What are Sensu handlers?
Sensu event handlers are actions executed by the Sensu server on events. In this example, we’ll use a handler to populate a time series database. If you’re not totally comfortable with handlers yet, check out the in-depth guide on handlers first!
Using a handler to populate InfluxDB
Installing the handler command
The first thing you’ll want to do is create an executable script named
sensu-influxdb-handler, which will be responsible for populating a configurable
InfluxDB with Sensu metrics. The source code and installation instructions of
this script is available on GitHub. You can also download the binaries
directly from releases.
Creating the handler
Now that our handler command is installed, the second step is to create a
handler that we will call
influx-db, which is a pipe handler that pipes
event data into our previous script named
sensu-influxdb-handler. We will also
pass the database name, address, username, and password of the InfluxDB you wish
We’ll use environment variables (
env-vars) to pass the InfluxDB address, username, and password to the handler.
sensuctl handler create influx-db \ --type pipe \ --command "sensu-influxdb-handler -d sensu" \ --env-vars "INFLUXDB_ADDR=http://influxdb.default.svc.cluster.local:8086, INFLUX_USER=sensu, INFLUX_PASSWORD=password"
Assigning the handler to an event
influx-db handler now created, it can be assigned to a check for
check output metric extraction. In this example, the check name is
sensuctl check set-output-metric-handlers collect-metrics influx-db
The handler can also be assigned to the Sensu StatsD listener at agent startup to pass all StatsD metrics into InfluxDB:
sensu-agent start --statsd-event-handlers influx-db
Validating the handler
It might take a few moments once the handler is assigned to the check or StatsD
server, for Sensu to receive the metrics, but once an event is handled, you
should start to see your InfluxDB being populated! Otherwise, you can verify the
proper behavior of this handler by using
sensu-backend logs. The default
location of these logs varies based on the platform used, but the
installation and configuration documentation provides this information.
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 one with the message
"msg":"pipelined executed event pipe
You now know how to apply a handler to metrics and take action on events. From this point, here are some recommended resources: