Skip to content

Instantly share code, notes, and snippets.

@gaborbarna
Created December 9, 2012 17:29
Show Gist options
  • Save gaborbarna/4246200 to your computer and use it in GitHub Desktop.
Save gaborbarna/4246200 to your computer and use it in GitHub Desktop.
cpu load
_ = require 'underscore'
http = require 'http'
fs = require 'fs'
every = (delay, cb) ->
setInterval cb, delay*1000
prev_total = prev_idle = 0
every 1, ->
fs.readFile '/proc/stat', (err, data) ->
parse data.toString()
calc_load = (total, idle) ->
d_total = total - prev_total
d_idle = idle - prev_idle
prev_total = total
prev_idle = idle
(d_total - d_idle) / d_total * 100
parse = (data) ->
stats = data.match(/^cpu[^0-9]*([ [0-9]*]*).*/)?[1].trim().split ' '
idle = stats[3]*1
total = stats[0..3].reduce (t, s) -> t*1 + s*1
load = calc_load total, idle
console.log load
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment