How can I test the Librato service?

A quick way to test the Librato Metrics service is sending a measurement from the command-line with curl.

Set your your Librato user name, api token and the location of the Librato Metrics API in your environment: (You can create a token on the https://metrics.librato.com/tokens page).

LIBRATO_USERNAME="Librato Metrics userid (email address)"
LIBRATO_TOKEN="Librato Metrics API Token, (long alpha numeric string)"
LIBRATO_CREDENTIALS="${LIBRATO_USERNAME}:${LIBRATO_TOKEN}"
LIBRATO_API="https://metrics-api.librato.com"

Create a JSON payload:

request_body=$(< <(cat <<EOF
{
  "tags": {
    "region": "us-east-1",
    "az": "a"
  },
  "measurements": [
    {
    "name": "metric_a",
    "value": 42
    },
    {
    "name": "metric_b",
    "value": 17
    }
  ]
}
EOF
))

Submit the measurements

curl -i \
  -u $LIBRATO_CREDENTIALS \
  -H "Content-Type: application/json" \
  -d "$request_body" \
  -X POST \
  ${LIBRATO_API}/v1/measurements

The curl command should produce output similar to this:

HTTP/1.1 202 Accepted
Date: Wed, 29 Mar 2017 20:34:42 GMT
Content-Type: application/json
Content-Length: 76
Connection: keep-alive
Server: nginx

{"measurements":{"summary":{"total":2,"accepted":2,"failed":0}},"errors":[]}

Note

If you use measure_time to send a time stamp with your metric make sure you use the unix timestamp format in seconds not milliseconds.

Simple script to generate metric streams

This curl script will send CPU and memory metrics (tested on a Mac) to your Librato account every 30 seconds (this assumes you have set your LIBRATO_CREDENTIALS):

#!/bin/bash

while true; do
  cpu=$(ps aux | awk '{s=s+$3}; END{print s}')
  mem=$(ps aux | awk '{s=s+$4}; END{print s}')
  echo "%CPU: ${cpu}  %MEM: ${mem}"

  request_body=$(< <(cat <<EOF
  {
    "tags": {
      "host": "laptop"
    },
    "measurements": [
      {
      "name": "cpu",
      "value": "$cpu"
      },
      {
      "name": "memory",
      "value": "$mem"
      }
    ]
  }
  EOF
  ))

  curl \
    -u $LIBRATO_CREDENTIALS \
    -H "Content-Type: application/json" \
    -d "$request_body" \
    -X POST \
    ${LIBRATO_API}/v1/measurements

  sleep 30
done