discordstatusmodules/modules/network.js
2021-11-01 12:30:21 +01:00

33 lines
1.3 KiB
JavaScript

// This module was made to work on linux. You might need to change "interface".
const interface = "eno1";
const fields = ["bytes_rx", "packets_rx", "errs_rx", "drop_rx", "fifo_rx", "frame_rx", "compressed_rx", "multicast_rx", "bytes_tx", "packets_tx", "errs_tx", "drop_tx", "fifo_tx", "frame_tx", "compressed_tx", "multicast_tx"];
const fs = require("fs");
function parseNet() {
var net = fs.readFileSync("/proc/net/dev").toString();
var data = {};
net.split("\n").forEach(function (line) {
var regex = line.match(/(\w+):(\s+\d+)+/);
if (regex !== null) {
regex = regex[0].replace(/\s{1,}/g, " ");
var interface = regex.split(": ")[0];
data[interface] = {};
var values = regex.split(": ")[1].split(" ");
for (var counter = 0; counter < values.length; counter++) {
data[interface][fields[counter]] = parseInt(values[counter])
}
}
});
return data;
}
module.exports = function (storage, callback) {
var first = parseNet();
setTimeout(function () {
var second = parseNet();
const bytes_in = Math.round(second[interface].bytes_rx / 1000 - first[interface].bytes_rx / 1000) + "kB/s";
const bytes_out = Math.round(second[interface].bytes_tx / 1000 - first[interface].bytes_tx / 1000) + "kB/s";
callback("↓" + bytes_in + " ↑" + bytes_out);
}, 1000);
}