Tracking CPU and Memory using CURL — Librato Blog

Tracking CPU and Memory using CURL


Nik Wekwerth


I wanted to get my hands dirty with Metrics so I grabbed (figuratively) some manuals, learned about curl, ps, and awk and figured out to to send data to Metrics. This post is how I got it all up and running. 

DISCLAIMER: I’m the marketing guy so don’t laugh at my coding skills (or lack thereof).

According to the experts, the simplest way to send data streams to Metrics is via the curl command. You can find an example of how to use the curl command in the API docs on the POST page

curl
-u <user>:<token>
-d 'measure_time=1234567950&source=blah.com' \
'&counters[0][name]=conn_servers' \
'&counters[0][value]=5' \
'&counters[1][name]=write_fails' \
'&counters[1][value]=3' \
'&gauges[0][name]=cpu_temp' \
'&gauges[0][value]=88.4' \
'&gauges[0][source]=cpu0_blah.com' \
'&gauges[0][measure_time]=1234567949'
-X POST https://metrics-api.librato.com/v1/metrics

So.. to measure CPU and memory and send it to Metrics I would need:

  • curl: to send data to the Metrics API
  • ps (process status command): to get %CPU and %MEM usage data
  • awk (an excellent filter and report writer): to filter out and sum up CPU and MEM information
  • a Metrics account (If you don’t have one, sign up for a free trial) with the account id and token

How to get the data

The command “ps aux” gives me a full listing of all processes on my Mac OS X system, including their %CPU (column 3) and %MEM (column 4). To calculate the total %CPU for all processes I use awk:

ps aux | awk '{s=s+$3}; END{print s}'

Awk takes the ps aux output, sums up the values in the 3rd column for each line and, after all the lines have been processed, returns the sum. For %MEM I just use $4 instead of $3.

Posting to Metrics

To create the curl command I need my user ID and token. I can find my token on my Account Settings page. Armed with that data I can construct the command. I won’t specify the measure_time parameter so Metrics will default to the time the measurement is received. I will call my metrics “CPU” and “MEM” and the source of my metrics is “macbook”.

curl -u my@email.com:<mytoken> \
-d '&gauges[0][name]=CPU'\
'&gauges[0][value]='`echo $(ps aux | awk '{s=s+$3}; END{print s}')`''\
'&gauges[0][source]=macbook' \
-X POST https://metrics-api.librato.com/v1/metrics

...and the equivalent for MEM works too. 

To generate new measurements every 30 seconds I wrap the curl command into an endless loop. Here’s the complete command for both metrics:

while true; do \
curl -u my@email.com:<mytoken> \
-d '&gauges[0][name]=CPU'\
'&gauges[0][value]='`echo $(ps aux | awk '{s=s+$3}; END{print s}')`''\
'&gauges[0][source]=macbook'\
'&gauges[1][name]=MEM'\
'&gauges[1][value]='`echo $(ps aux | awk '{s=s+$4}; END{print s}')`''\
'&gauges[1][source]=macbook' \
-X POST https://metrics-api.librato.com/v1/metrics;
sleep 30;
done

That’s all I have to do! No configuration is required in the Metrics app - as soon as data is sent to the API, the metric shows up in my list at https://metrics.librato.com:

Metrics List

Metrics List

When I click on a metric (in this case CPU) I see the graph with my metric and source:

CPU metric with attributes.

CPU metric with attributes.

To get the graph to look like this I configured the metric attributes by editing the title (click on it), setting the min (0) and max (100), adding “Percentage” as the y-axis title, and “%” as the unit so that it shows up in the tool tip. I also added a display transform - as I have two cores, the total CPU can add up to 200%. By entering “x/2” in the display transform window the max is now 100%.

To get both CPU and MEM metrics into one graph I use the correlate function. As I configured both metrics to have the same units (%) they share the same Y-axis:

Correlating both metrics in one graph.

Correlating both metrics in one graph.

If you want to get more sophisticated than using a curl command, check out the collection agents you can use to send metrics to Metrics.

Start using Librato now. Full-featured and free for 30 days. Pricing starts at $5 per month.
I accept the Terms of Service, Privacy Policy and occasional emails from Librato. Got questions? Talk to us.