Skip to content

Instantly share code, notes, and snippets.

@brainrake
Forked from gaborbarna/gist:4246200
Created December 9, 2012 23:54
Show Gist options
  • Save brainrake/4247563 to your computer and use it in GitHub Desktop.
Save brainrake/4247563 to your computer and use it in GitHub Desktop.
cpu load
_ = require 'underscore'
http = require 'http'
fs = require 'fs'
dgram = require 'dgram'
every = (delay, cb) -> setInterval cb, delay*1000
sock = dgram.createSocket('udp4')
prev_total = prev_idle = 0
every 1, ->
fs.readFile '/proc/stat', (err, 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
msg = 'load:'+load+'|g'
console.log msg
sock.send (buf=new Buffer msg), 0, buf.length, 8125, '10.0.0.100'
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment