Collect Prometheus metrics with Sensu

The Prometheus ecosystem contains a number of actively maintained exporters, such as the node exporter for reporting hardware and operating system metrics or Google’s cAdvisor exporter for monitoring containers. These exporters expose metrics that Sensu can collect and route to one or more time-series databases. Sensu and Prometheus can run in parallel, complementing each other and making use of environments where Prometheus is already deployed.

You can use the HTTP checks collection to create checks that collect metrics from a Prometheus exporter or the Prometheus query API. This allows Sensu to route the collected metrics to one or more time-series databases, such as InfluxDB or Graphite.

At the end of this guide, Prometheus will be scraping metrics. The check that uses the sensu/http-checks asset will query the Prometheus API as a Sensu check and send the metrics to an InfluxDB Sensu handler, which will send metrics to an InfluxDB instance. Finally, Grafana will query InfluxDB to display the collected metrics.

Requirements

To follow this guide, install the Sensu backend, make sure at least one Sensu agent is running, and install and configure sensuctl.

The examples in this guide use CentOS 7 as the operating system, with all components running on the same compute resource. Commands and steps may change for different distributions or if components are running on different compute resources.

Configure a Sensu entity

Use sensuctl to add the app_tier subscription to one of your entities. Before you run the following code, replace <ENTITY_NAME> with the name of the entity on your system.

NOTE: To find your entity name, run sensuctl entity list. The ID is the name of your entity.

sensuctl entity update <ENTITY_NAME>
  • For Entity Class, press enter.
  • For Subscriptions, type app_tier and press enter.

Run this command to confirm both Sensu services are running:

systemctl status sensu-backend && systemctl status sensu-agent

The response should indicate active (running) for both the Sensu backend and agent.

Install and configure Prometheus

Download and extract Prometheus with these commands:

wget https://github.com/prometheus/prometheus/releases/download/v2.6.0/prometheus-2.6.0.linux-amd64.tar.gz
tar xvfz prometheus-*.tar.gz
cd prometheus-*

Replace the default prometheus.yml configuration file with the following configuration:

global:
  scrape_interval: 15s
  external_labels:
    monitor: 'codelab-monitor'

scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9090']

Start Prometheus in the background:

nohup ./prometheus --config.file=prometheus.yml > prometheus.log 2>&1 &

Ensure Prometheus is running:

ps -ef | grep "[p]rometheus"

The response should be similar to this example:

vagrant   7647  3937  2 22:23 pts/0    00:00:00 ./prometheus --config.file=prometheus.yml

Install and configure InfluxDB

Add an InfluxDB repo:

echo "[influxdb]
name = InfluxDB Repository - RHEL \$releasever
baseurl = https://repos.influxdata.com/rhel/\$releasever/\$basearch/stable
enabled = 1
gpgcheck = 1
gpgkey = https://repos.influxdata.com/influxdb.key" | sudo tee /etc/yum.repos.d/influxdb.repo

Install InfluxDB:

sudo yum -y install influxdb

Open /etc/influxdb/influxdb.conf and uncomment the http API line:

[http]
  # Determines whether HTTP endpoint is enabled.
  enabled = true

Start InfluxDB:

sudo systemctl start influxdb

Add the Sensu user and database with these commands:

influx -execute "CREATE DATABASE sensu"
influx -execute "CREATE USER sensu WITH PASSWORD 'sensu'"
influx -execute "GRANT ALL ON sensu TO sensu"

Install and configure Grafana

Install Grafana:

sudo yum install -y https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.1.4-1.x86_64.rpm

Change Grafana’s listen port so that it does not conflict with the Sensu web UI:

sudo sed -i 's/^;http_port = 3000/http_port = 4000/' /etc/grafana/grafana.ini

Create a /etc/grafana/provisioning/datasources/influxdb.yaml file, and add an InfluxDB data source:

apiVersion: 1

deleteDatasources:
  - name: InfluxDB
    orgId: 1

datasources:
  - name: InfluxDB
    type: influxdb
    access: proxy
    orgId: 1
    database: sensu
    user: grafana
    password: grafana
    url: http://localhost:8086

Start Grafana:

sudo systemctl start grafana-server

Create a Sensu InfluxDB handler

Add the Sensu InfluxDB handler asset

To add the sensu/sensu-influxdb-handler dynamic runtime asset to Sensu, run the following command:

sensuctl asset add sensu/sensu-influxdb-handler:3.7.0 -r sensu-influxdb-handler

The response will confirm that the asset was added:

fetching bonsai asset: sensu/sensu-influxdb-handler:3.7.0
added asset: sensu/sensu-influxdb-handler:3.7.0

You have successfully added the Sensu asset resource, but the asset will not get downloaded until
it's invoked by another Sensu resource (ex. check). To add this runtime asset to the appropriate
resource, populate the "runtime_assets" field with ["sensu-influxdb-handler"].

This example uses the -r (rename) flag to specify a shorter name for the dynamic runtime asset: sensu-influxdb-handler.

To confirm that the sensu-influxdb-handler asset is ready to use, run:

sensuctl asset list

The response should list the sensu-influxdb-handler dynamic runtime asset:

             Name                                                     URL                                            Hash
───────────────────────────── ──────────────────────────────────────────────────────────────────────────────────── ──────────
  sensu-influxdb-handler       //assets.bonsai.sensu.io/.../sensu-influxdb-handler_3.7.0_linux_386.tar.gz           6719527
  sensu-influxdb-handler       //assets.bonsai.sensu.io/.../sensu-influxdb-handler_3.7.0_linux_amd64.tar.gz         d05650d
  sensu-influxdb-handler       //assets.bonsai.sensu.io/.../sensu-influxdb-handler_3.7.0_linux_armv7.tar.gz         38918c1
  sensu-influxdb-handler       //assets.bonsai.sensu.io/.../sensu-influxdb-handler_3.7.0_linux_arm64.tar.gz         944075f
  sensu-influxdb-handler       //assets.bonsai.sensu.io/.../sensu-influxdb-handler_3.7.0_windows_amd64.tar.gz       8228cbc
  sensu-influxdb-handler       //assets.bonsai.sensu.io/.../sensu-influxdb-handler_3.7.0_darwin_amd64.tar.gz        7c73e1d

Add an InfluxDB handler

To add the handler definition that uses the Sensu InfluxDB Handler dynamic runtime asset, run:

cat << EOF | sensuctl create
---
type: Handler
api_version: core/v2
metadata:
  name: influxdb
spec:
  command: sensu-influxdb-handler -a 'http://127.0.0.1:8086' -d sensu -u sensu -p sensu
  timeout: 10
  type: pipe
  runtime_assets:
  - sensu-influxdb-handler
EOF
cat << EOF | sensuctl create
{
  "type": "Handler",
  "api_version": "core/v2",
  "metadata": {
    "name": "influxdb"
  },
  "spec": {
    "command": "sensu-influxdb-handler -a 'http://127.0.0.1:8086' -d sensu -u sensu -p sensu",
    "timeout": 10,
    "type": "pipe",
    "runtime_assets": [
      "sensu-influxdb-handler"
    ]
  }
}
EOF

PRO TIP: sensuctl create --file also accepts files that contain multiple resources’ definitions. You could save both the asset and handler definitions in a single file and use sensuctl create --file FILE_NAME.EXT to add them.

Create a pipeline that includes the InfluxDB handler

Add your handler to a pipeline. A single pipeline workflow can include one or more filters, one mutator, and one handler.

In this case, the pipeline includes only the InfluxDB handler you’ve already configured. To create the pipeline, run:

cat << EOF | sensuctl create
---
type: Pipeline
api_version: core/v2
metadata:
  name: prometheus_metrics_workflows
spec:
  workflows:
  - name: influxdb_metrics
    handler:
      name: influxdb
      type: Handler
      api_version: core/v2
EOF
cat << EOF | sensuctl create
{
  "type": "Pipeline",
  "api_version": "core/v2",
  "metadata": {
    "name": "prometheus_metrics_workflows"
  },
  "spec": {
    "workflows": [
      {
        "name": "influxdb_metrics",
        "handler": {
          "name": "influxdb",
          "type": "Handler",
          "api_version": "core/v2"
        }
      }
    ]
  }
}
EOF

Now you can add the prometheus_metrics_workflows pipeline to a check for check output metric extraction.

Collect Prometheus metrics with Sensu

Add the sensu/http-checks asset

To add the sensu/http-checks dynamic runtime asset to Sensu, run the following command:

sensuctl asset add sensu/http-checks -r http-checks

The response will confirm that the asset was added:

no version specified, using latest: 0.8.0
fetching bonsai asset: sensu/http-checks:0.8.0
added asset: sensu/http-checks:0.8.0

You have successfully added the Sensu asset resource, but the asset will not get downloaded until
it's invoked by another Sensu resource (ex. check). To add this runtime asset to the appropriate
resource, populate the "runtime_assets" field with ["http-checks"].

This example uses the -r (rename) flag to specify a shorter name for the dynamic runtime asset: http-checks.

To confirm that the http-checks asset is ready to use, run:

sensuctl asset list

The response should list the http-checks dynamic runtime asset along with the previously added sensu-influxdb-handler asset:

             Name                                                     URL                                            Hash
───────────────────────────── ──────────────────────────────────────────────────────────────────────────────────── ──────────
  http-checks                  //assets.bonsai.sensu.io/.../http-checks_0.8.0_linux_amd64.tar.gz                    e1f7f69
  http-checks                  //assets.bonsai.sensu.io/.../http-checks_0.8.0_linux_386.tar.gz                      8629c1e
  http-checks                  //assets.bonsai.sensu.io/.../http-checks_0.8.0_linux_arm64.tar.gz                    aed9d9b
  http-checks                  //assets.bonsai.sensu.io/.../http-checks_0.8.0_linux_armv7.tar.gz                    2948ebc
  http-checks                  //assets.bonsai.sensu.io/.../http-checks_0.8.0_darwin_amd64.tar.gz                   29889d1
  http-checks                  //assets.bonsai.sensu.io/.../http-checks_0.8.0_windows_amd64.tar.gz                  167d08f
  sensu-influxdb-handler       //assets.bonsai.sensu.io/.../sensu-influxdb-handler_3.7.0_linux_386.tar.gz           6719527
  sensu-influxdb-handler       //assets.bonsai.sensu.io/.../sensu-influxdb-handler_3.7.0_linux_amd64.tar.gz         d05650d
  sensu-influxdb-handler       //assets.bonsai.sensu.io/.../sensu-influxdb-handler_3.7.0_linux_armv7.tar.gz         38918c1
  sensu-influxdb-handler       //assets.bonsai.sensu.io/.../sensu-influxdb-handler_3.7.0_linux_arm64.tar.gz         944075f
  sensu-influxdb-handler       //assets.bonsai.sensu.io/.../sensu-influxdb-handler_3.7.0_windows_amd64.tar.gz       8228cbc
  sensu-influxdb-handler       //assets.bonsai.sensu.io/.../sensu-influxdb-handler_3.7.0_darwin_amd64.tar.gz        7c73e1d

Add a Sensu check that references the pipeline

To add the check definition that uses the sensu/http-checks dynamic runtime asset and your prometheus_metrics_workflows pipeline, run:

cat << EOF | sensuctl create
---
type: CheckConfig
api_version: core/v2
metadata:
  name: prometheus_metrics
spec:
  command: http-get --url http://localhost:9090
  handlers: []
  interval: 10
  publish: true
  output_metric_format: influxdb_line
  pipelines:
  - name: prometheus_metrics_workflows
    type: Pipeline
    api_version: core/v2
  subscriptions:
  - app_tier
  timeout: 0
  runtime_assets:
  - http-checks
EOF
cat << EOF | sensuctl create
{
  "type": "CheckConfig",
  "api_version": "core/v2",
  "metadata": {
    "name": "prometheus_metrics"
  },
  "spec": {
    "command": "http-get --url http://localhost:9090",
    "handlers": [],
    "interval": 10,
    "publish": true,
    "output_metric_format": "influxdb_line",
    "pipelines": [
      {
        "name": "prometheus_metrics_workflows",
        "type": "Pipeline",
        "api_version": "core/v2"
      }
    ],
    "subscriptions": [
      "app_tier"
    ],
    "timeout": 0,
    "runtime_assets": [
      "http-checks"
    ]
  }
}
EOF

The check subscription matches the subscription you added to your entity at the beginning of this guide. The Sensu backend will coordinate check execution for you by comparing the subscriptions in your checks and entities. Sensu automatically executes a check when the check definition includes a subscription that matches a subscription for a Sensu entity.

Open the Sensu web UI to view the events generated by the prometheus_metrics check. Visit http://127.0.0.1:3000, and log in as the admin user (created during the initialization step when you installed the Sensu backend).

You can also view the metric event data using sensuctl. Run:

sensuctl event list

The response should be similar to this example:

     Entity            Check                                          Output                                   Status   Silenced             Timestamp                             UUID
─────────────── ──────────────────── ──────────────────────────────────────────────────────────────────────── ──────── ────────── ─────────────────────────────── ───────────────────────────────────────
  sensu-centos   keepalive            Keepalive last sent from sensu-centos at 2022-01-14 15:23:00 +0000 UTC        0   false      2022-01-14 15:23:00 +0000 UTC   a9kr7kf8-21h8-459k-v6f8-ad93mf82mkfd
  sensu-centos   prometheus_metrics   up,instance=localhost:9090,job=prometheus value=1 1642173795                  0   false      2022-01-14 15:23:15 +0000 UTC   sd8j4ls9-34gf-fr77-456g-92384738jd72

Visualize metrics with Grafana

Configure a dashboard in Grafana

Download the Grafana dashboard configuration file from the Sensu docs:

curl -O https://docs.sensu.io/sensu-go/latest/files/up_or_down_dashboard.json

Using the downloaded file, add the dashboard to Grafana with an API call:

curl  -XPOST -H 'Content-Type: application/json' -d@up_or_down_dashboard.json HTTP://admin:admin@127.0.0.1:4000/api/dashboards/db

View metrics in Grafana

Confirm metrics in Grafana: log in at http://127.0.0.1:4000. Use admin for both username and password.

Click Home in the upper left corner, then click the Up or Down Sample 2 dashboard. The page should include a graph with initial metrics, similar to this example:

Example Grafana dashboard for visualizing Prometheus metrics from Sensu

You should now have a working observability pipeline with Prometheus scraping metrics. The sensu/http-checks dynamic runtime asset runs via the prometheus_metrics Sensu check and collects metrics from the Prometheus API.

The check sends metrics to the prometheus_metrics_workflows pipeline, the influxdb handler sends the metrics to InfluxDB, and you can visualize the metrics in a Grafana dashboard.

What’s next

Learn more about the Sensu resources you created in this guide:

Now that you know how to extract metrics from check output, you can use a pipeline to send data to Sumo Logic.

Visit Bonsai, the Sensu asset index, for more information about the sensu/sensu-influxdb-handler and sensu/http-checks assets. With the sensu/http-checks in your Sensu ecosystem, you can use Prometheus to gather metrics and use Sensu to send them to the proper final destination. Prometheus has a comprehensive list of additional exporters to pull in metrics.

Read about sensuctl and the Sensu web UI to learn how to use these features as you develop your monitoring and observability pipelines.