Skip to content

Instantly share code, notes, and snippets.

@whiskers75
Created May 25, 2014 10:12
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 whiskers75/326d4556196e266232ef to your computer and use it in GitHub Desktop.
Save whiskers75/326d4556196e266232ef to your computer and use it in GitHub Desktop.
Load averages and PPS with a PiGlow
var pcap = require('pcap');
var piglow = require('piglow');
var eth0 = pcap.createSession('eth0', 'ip proto \\tcp');
var wlan0 = pcap.createSession('wlan0', 'ip proto \\tcp');
var packets_per_sec = {eth0: 0, wlan0: 0};
piglow(function(err, pi) {
if (err) throw err;
setInterval(function() {
pi.startTransaction();
pi.all = 0;
console.log(JSON.stringify(packets_per_sec));
Object.keys(packets_per_sec).forEach(function(iface, i) {
var packets = packets_per_sec[iface];
if (packets > 1) pi['l_' + i + '_0'] = 10;
if (packets > 30) pi['l_' + i + '_1'] = 30;
if (packets > 60) pi['l_' + i + '_2'] = 60;
if (packets > 90) pi['l_' + i + '_3'] = 90;
if (packets > 120) pi['l_' + i + '_4'] = 120;
if (packets > 150) pi['l_' + i + '_5'] = 150;
});
packets_per_sec = {eth0: 0, wlan0: 0};
var ld = require('os').loadavg()[0];
if (ld < 0.4) pi.l_2_0 = 10; // Aaaahhhhhh.
if (ld >= 0.4 && ld < 0.6) pi.l_2_1 = 30;
if (ld >= 0.6 && ld < 1) pi.l_2_2 = 60;
if (ld >= 1 && ld < 1.2) pi.l_2_3 = 90;
if (ld >= 1.2 && ld < 1.6) pi.l_2_4 = 120;
if (ld >= 1.6 && ld < 2) pi.l_2_4 = 150;
if (ld > 2) pi.l_2_5 = 150;
pi.commitTransaction();
}, 1000);
eth0.on('packet', function() {
packets_per_sec.eth0++;
});
wlan0.on('packet', function() {
packets_per_sec.wlan0++;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment