Created
July 2, 2011 03:37
-
-
Save nornagon/1059715 to your computer and use it in GitHub Desktop.
Monitor network usage with SNMP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var exec = require('child_process').exec | |
function howManyOctets(cb) { | |
child = exec('snmpwalk -v 2c -c admin 192.168.0.1 IF-MIB::ifHCInOctets.1', // change to .32 to measure external usage | |
function (error, stdout, stderr) { | |
var numOctets = 0 | |
stdout.split('\n').forEach(function (line) { | |
numOctets += parseInt(/: (\d+)/.exec(stdout)[1]) | |
}) | |
cb(numOctets) | |
} | |
) | |
} | |
var lastOctets | |
var lastTime | |
function main() { | |
howManyOctets(function (octets) { | |
var now = Date.now() | |
if (!lastOctets) { | |
lastOctets = octets | |
lastTime = now | |
return main() | |
} else { | |
var diff = octets - lastOctets | |
var dt = now - lastTime | |
lastTime = now | |
console.log((diff/dt).toFixed(1),"kB/s") | |
lastOctets = octets | |
setTimeout(main, 1000) | |
} | |
}) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment