How can I test the Librato service?

A quick way to test the Librato service is by sending measurements using scripts. Here are some example scripts using Python, Ruby, and CURL.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import psutil
import librato
import time

#init:
user = "accountemail@domain.com"
token = "12345supersecrettoken456789"
api = librato.connect(user, token)

while True:
  cpu = psutil.cpu_percent(interval=None, percpu=False)
  memory = psutil.virtual_memory()
  q = api.new_queue(tags={'language': 'python', 'device': 'macbook'})
  q.add('cpu', cpu)
  q.add('memory', memory[2])
  q.submit()
  time.sleep(30)
#!/usr/bin/env ruby

require 'librato/metrics'
require 'usagewatch_ext'

#init:
user = "accountemail@domain.com"
token = "12345supersecrettoken456789"
Librato::Metrics.authenticate user, token

usw = Usagewatch

while true
  queue = Librato::Metrics::Queue.new(
      tags: {
        device: 'macbook'
      }
    )
    queue.add mac_cpu: usw.uw_cpuused
    queue.add mac_disk_perc: usw.uw_diskused_perc
    queue.submit

  sleep 30
end
#!/bin/bash

LIBRATO_USERNAME="my_librato_email@domain.com"
LIBRATO_TOKEN="12345longalphanumericstring1234567"
LIBRATO_CREDENTIALS="${LIBRATO_USERNAME}:${LIBRATO_TOKEN}"
LIBRATO_API="https://metrics-api.librato.com"

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

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.