Skip to content

A Ruby solution to retrieve network traffic information using the vnstat command line utility.

License

Notifications You must be signed in to change notification settings

tlux/vnstat-ruby

Repository files navigation

vnstat-ruby

Build Status Coverage Status Gem Version

Vnstat is a tool that tracks the traffic on your network interfaces. This tiny library is intended to provide network traffic information through an easy-to-use API.

It depends on the vnstat command line utility which is maintained by Teemu Toivola.

Prerequisites

  • Ruby 2.3.0 or greater
  • Make sure you have vnstat CLI installed by running which vnstat.

On Ubuntu you can get the vnstat package via apt-get install vnstat.

Getting Started

Setup

Once the dependent package is installed, you are ready to install the vnstat-ruby gem.

gem install vnstat-ruby

Or, add the following line to the Gemfile of your project and run the bundle install command:

gem 'vnstat-ruby'

Configuration

By default, vnstat-ruby tries to automatically determine the location of your vnstat executable. If you need to change this path for some reason, you can override the default:

Vnstat.config.executable_path = '/usr/bin/vnstat'

Usage

Network Interfaces

To retrieve a list of all known network interfaces:

Vnstat.interfaces # => #<Vnstat::InterfaceCollection ...>
Vnstat.interfaces.ids # => ['eth01', 'eth02']

To only retrieve traffic stats for a single interface:

Vnstat.interfaces.first # => #<Vnstat::Interface id: "eth01">

If you know the name of a network interface, you can also retrieve the stats of that particular one:

Vnstat['eth01'] # => #<Vnstat::Interface id: "eth01">

Traffic Information

Total
interface = Vnstat['eth01']
interface.total # => #<Vnstat::Result ...>
interface.total.bytes_received # => 1024000
interface.total.bytes_sent # => 2048000
interface.total.bytes_transmitted # => 3072000
By Month
interface.months # => #<Vnstat::Traffic::Monthly ...>
interface.months.first
interface.months[2015, 9]
# => #<Vnstat::Result::Month year: 2015, month: 9, ...>
interface.months[2015, 10].bytes_received # => 3072000
By Day
interface.days # => #<Vnstat::Traffic::Daily ...>
interface.days.first
interface.days[Date.new(2015, 11, 23)]
interface.days[2015, 11, 23]
# => #<Vnstat::Result::Day date: #<Date: 2015-11-23>, ...>
interface.days[2015, 11, 23].bytes_received # => 2048000
By Hour
interface.hours # => #<Vnstat::Traffic::Hourly ...>
interface.hours.first
interface.hours[Date.new(2015, 9, 22), 10]
interface.hours[2015, 9, 22, 10]
# => #<Vnstat::Result::Hour date: #<Date: 2015-11-23>, hour: 10>
interface.hours[2015, 9, 22, 10].bytes_received # => 2048000
Tops
interface.tops #=> #<Vnstat::Traffic::Tops ...>
interface.tops.count #=> 10
interface.tops.each do |top|
  # ...
end
interface.tops.first
interface.tops[2]
# => #<Vnstat::Result::Minute time: 2015-11-05 09:03:42 +0000>
interface.tops[1].bytes_received # => 1024000

Contributing to vnstat-ruby

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
  • Fork the project.
  • Start a feature/bugfix branch.
  • Commit and push until you are happy with your contribution.
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright (c) 2015-2020 Tobias Casper. See LICENSE.txt for further details.

About

A Ruby solution to retrieve network traffic information using the vnstat command line utility.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages