Created
February 10, 2016 21:13
-
-
Save vugluskr86/fac2a24e00ec49faaef1 to your computer and use it in GitHub Desktop.
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 fs = require('fs'); | |
function readLines(input, func) { | |
var remaining = ''; | |
var lines = []; | |
input.on('data', function(data) { | |
remaining += data; | |
var index = remaining.indexOf('\n'); | |
while (index > -1) { | |
var line = remaining.substring(0, index); | |
remaining = remaining.substring(index + 1); | |
//func(line); | |
lines.push(line); | |
index = remaining.indexOf('\n'); | |
} | |
}); | |
input.on('end', function() | |
{ | |
if (remaining.length > 0) | |
{ | |
lines.push(remaining); | |
return func(lines); | |
} | |
func(lines); | |
}); | |
} | |
var data = []; | |
setInterval(function() | |
{ | |
var input = fs.createReadStream('/proc/net/dev'), | |
linesInterfaces = [], | |
interfaces = {}; | |
readLines(input, function(lines) | |
{ | |
linesInterfaces = lines.splice(2); | |
linesInterfaces.forEach(function(ifLine) | |
{ | |
var ifData = ifLine.split(':'), | |
ifName = ifData[0].trim(), | |
ifValues = ifData[1].split(' ').filter(function(v){ | |
var intV = parseInt(v); | |
return !isNaN(intV); | |
}).map(function(v) | |
{ | |
return parseInt(v) | |
}); | |
interfaces[ifName] = ifValues; | |
}); | |
data.push(interfaces); | |
if( data.length > 1 ) | |
{ | |
var l_data = data[data.length - 1], | |
r_data = data[data.length - 2], | |
l_size = l_data.length, | |
r_size = r_data.length, | |
if_data = l_size > r_size ? l_data : r_data; | |
Object.keys(if_data).forEach(function(if_name) | |
{ | |
if( l_data[if_name] == 'undefined' || r_data[if_name] == 'undefined' ) | |
{ | |
console.log( 'if: ' + if_name + ' is down' ); | |
} | |
else | |
{ | |
var l_v = l_data[if_name][0]; | |
var r_v = r_data[if_name][0]; | |
console.log( 'if: ' + if_name + ' : ' + (l_v - r_v) ); | |
} | |
}); | |
} | |
}); | |
}, 1000); | |
// bytes packets errs drop fifo frame compressed multicast | |
// bytes packets errs drop fifo colls carrier compressed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment