Skip to content

Instantly share code, notes, and snippets.

@driverdan
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save driverdan/648175868fbfaea11711 to your computer and use it in GitHub Desktop.
Save driverdan/648175868fbfaea11711 to your computer and use it in GitHub Desktop.
Menubar load monitor using AnyBar and node
console.log('Starting...');
var dgram = require('dgram');
var os = require('os');
// Create UDP client
var client = dgram.createSocket('udp4');
// Colors to use
var good = new Buffer('green');
var warn = new Buffer('yellow');
var bad = new Buffer('red');
// Interval to check load (in ms)
var checkInterval = 5000;
// Get the number of CPUs
// Assume hyperthreading so divide by 2 for actual number of CPUs
var cpuCount = os.cpus().length / 2;
console.log('CPU Count: %d', cpuCount);
// Calculate load limits
var loadGood = cpuCount * 0.8;
// Warns when load is greater than loadGood but less than this number
var loadWarn = cpuCount;
var checkLoad = function () {
var load = os.loadavg();
var color;
console.log('Load: %s', load[0]);
if (load[0] < loadGood) {
color = good;
} else if (load[0] < loadWarn) {
color = warn;
} else {
color = bad;
}
// Send the color to AnyBar
client.send(color, 0, color.length, 1738, 'localhost', function(err) {
setTimeout(checkLoad, checkInterval);
});
};
checkLoad();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment