Giter Club home page Giter Club logo

node-net-stat's Introduction

net-stat

  ______  _______ _______ _______ _______ _______ _______
 |   _  \|   _   |       |   _   |       |   _   |       |
 |.  |   |.  1___|.|   | |   1___|.|   | |.  1   |.|   | |
 |.  |   |.  __)_`-|.  |-|____   `-|.  |-|.  _   `-|.  |-'
 |:  |   |:  1   | |:  | |:  1   | |:  | |:  |   | |:  |
 |::.|   |::.. . | |::.| |::.. . | |::.| |::.|:. | |::.|
 `--- ---`-------' `---' `-------' `---' `--- ---' `---'

Note: This repo can be found on npm here: net-stat

Note: This repo can be found on github here: node-net-stat

Caveat: Works by parsing /proc/net/dev, so will only work on nix OS.

Install

npm install net-stat

Examples

Require the module:

var netStat = require('net-stat');

By default totalRx() returns total received bytes for interface lo:

var totalrx = netStat.totalRx();
console.log(totalrx);

Return total received GiB on interface eth0, see docs below for accepted values of units and iface:

var totalrx = netStat.totalRx({ iface: 'eth0', units: 'GiB' });
console.log(totalrx);

Display the transmitted KiB per second of eth0 over the next 2000ms:

setInterval(function() {
  netStat.usageTx({
    iface: 'eth0',
    units: 'KiB',
    sampleMs: 2000,
  }, function(kbps) {
    console.log('Transmitted ' + kbps + ' kb/s');
  });
}, 2500);

Get all fields available from /proc/net/dev, useful for finding accepted values of iface:

var allStats = netStat.raw();
console.log(allStats);

totalRx([opts])

Returns a number representing the number of units received on iface.

Option Type Default Explanation
opts Object see below Options object, specify what you need, the defaults will be filled in
opts.iface String 'lo' The name of the interface to be used. See the raw() function for a list of interfaces.
opts.units String 'bytes' The units of the returned value, can be one of bytes, KiB, MiB or GiB.

totalTx([opts])

Returns a number representing the number of units transmitted on iface.

Option Type Default Explanation
opts Object see below Options object, specify what you need, the defaults will be filled in
opts.iface String 'lo' The name of the interface to be used. See the raw() function for a list of interfaces.
opts.units String 'bytes' The units of the returned value, can be one of bytes, KiB, MiB or GiB.

usageRx([opts,] cb)

Async function which returns data, the usage received per second in units on iface over sampleMs

Option Type Default Explanation
opts Object see below Options object, specify what you need, the defaults will be filled in
opts.iface String 'lo' The name of the interface to be used. See the raw() function for a list of interfaces.
opts.units String 'bytes' The units of the returned value, can be one of bytes, KiB, MiB or GiB.
opts.sampleMs Number 1000 The number of milliseconds to take a usage sample over.
cb Function none A callback function with signature cb(data) where data is the usage received per second in units on iface over sampleMs.

usageTx([opts,] cb)

Async function which returns data, the usage transmitted per second in units on iface over sampleMs

Option Type Default Explanation
opts Object see below Options object, specify what you need, the defaults will be filled in
opts.iface String 'lo' The name of the interface to be used. See the raw() function for a list of interfaces.
opts.units String 'bytes' The units of the returned value, can be one of bytes, KiB, MiB or GiB.
opts.sampleMs Number 1000 The number of milliseconds to take a usage sample over.
cb Function none A callback function with signature cb(data) where data is the usage transmitted per second in units on iface over sampleMs.

raw()

Returns an object representing the data in /proc/net/dev.

{
  eth0: {
    bytes: { receive: '2085477580', transmit: '187911353' },
    packets: { receive: '1782197', transmit: '1353772' },
    errs: { receive: '0', transmit: '0' },
    drop: { receive: '42', transmit: '0' },
    fifo: { receive: '0', transmit: '0' },
    frame: { receive: '0' },
    compressed: { receive: '0', transmit: '0' },
    multicast: { receive: '7923' },
    colls: { transmit: '0' },
    carrier: { transmit: '0' }
  },
  lo: {
    bytes: { receive: '52720310', transmit: '52720310' },
    packets: { receive: '443962', transmit: '443962' },
    errs: { receive: '0', transmit: '0' },
    drop: { receive: '0', transmit: '0' },
    fifo: { receive: '0', transmit: '0' },
    frame: { receive: '0' },
    compressed: { receive: '0', transmit: '0' },
    multicast: { receive: '0' },
    colls: { transmit: '0' },
    carrier: { transmit: '0' }
  },
}

Contributing

Just send a PR, or create an issue if you are not sure.

Areas ripe for contribution:

  • testing
  • cross compatability for windows and darwin/osx
  • performance
  • bugs

Other Stat Modules

Note: net-stat, disk-stat, mem-stat only work on nix platforms.

License

MIT

node-net-stat's People

Contributors

jub3i avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.