Skip to content

Instantly share code, notes, and snippets.

@nornagon
Created July 2, 2011 03:37
Show Gist options
  • Save nornagon/1059715 to your computer and use it in GitHub Desktop.
Save nornagon/1059715 to your computer and use it in GitHub Desktop.
Monitor network usage with SNMP
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