Dustin's Blog

2/7/08 7:29 PM

RESTful api example : cpu monitor



Here is an example of using the RESTful api to track your own data. The simple script tracks the CPU usage of my developement box at ten minute intervals. It is somewhat frivolious (now you all can see how hard I –err my computer-- work everyday) but I think shows how easy it is to utilize the trendrr api.

From trendrr home page:
  1. Log in
  2. click 'Add Data' on left hand side
  3. click 'Add Custom Data Set'
The defaults work just fine for most of the fields. Just fill in 'Data Set Title' and 'Legend Text'.  In this case I used:
Data Set Title = 'CPU Monitor'
Legend Text = 'Dustins Linux Box'



Thats it! Press save and you will see your api-key on the next screen.


To get my current cpu usage I am using this simple shell script:


#do a sample every ten minutes
sampleSeconds=600
#your api key would go here
apiKey={your key here}

while [ 1 ]
do
   
    # This line gets the current CPU usage as decimal number
    cpuUsage=`top -b -n 2 -d $sampleSeconds | grep Cpu -n | nawk '{if ( $1 +0 > 15 ) print $2 + 0;}'`

# this is the Trendrr RESTful url    apiUrl="http://www.trendrr.com/api/simple?key=$apiKey&value=$cpuUsage"
   
    wget $apiUrl
    echo $apiUrl

done



Using the api is tre simple.  you just construct a url in the form:

http://www.trendrr.com/api/simple?key={your key}&value={your value}


which will inject the value at time=right now.  That is, you will see a point on the graph that corresponds to that value at the current time.  You could also pass a date parameter if you wish.  More documentation here


I am using the newest Ubuntu OS and the script works out of the box, it might work as is on Mac as well. What it does is take the average cpu usage for every ten minutes, then sends that number to trendrr via the RESTful url. Very simple. 


Posted By: doubleagame 4/4/08 1:02:57 AM

I love that Cheney is the default avatar. How about that for motivation to personalize a profile. Brilliant.

Add Comment:

You must be logged in to comment

TrendrrScratchpad

TrendrrSearch

What is: Trendrr?

Track, compare and share data, free. Identify trends across social graphs and networks, realize the potential of p2p, track engagement metrics, look at what is really happening, real time.

Trendrr: Featured Users

forgot password