Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

tlux/vnstat-ruby

Repository files navigation

vnstat-ruby

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.interfaces.ids # => ['eth01', 'eth02']

To only retrieve traffic stats for a single interface:

Vnstat.interfaces.first # => #

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

#">Vnstat['eth01'] # => #

Traffic Information

Total
interface = Vnstat['eth01']
interface.total # => #
interface.total.bytes_received # => 1024000
interface.total.bytes_sent # => 2048000
interface.total.bytes_transmitted # => 3072000
By Month
interface.months # => #
interface.months.first
interface.months[2015, 9]
# => #
interface.months[2015, 10].bytes_received # => 3072000
By Day
interface.days # => #
interface.days.first
interface.days[Date.new(2015, 11, 23)]
interface.days[2015, 11, 23]
# => #, ...>
interface.days[2015, 11, 23].bytes_received # => 2048000
By Hour
interface.hours # => #
interface.hours.first
interface.hours[Date.new(2015, 9, 22), 10]
interface.hours[2015, 9, 22, 10]
# => #, hour: 10>
interface.hours[2015, 9, 22, 10].bytes_received # => 2048000
Tops
interface.tops #=> #
interface.tops.count #=> 10
interface.tops.each do |top|
# ...
end
interface.tops.first
interface.tops[2]
# => #
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

Readme

License

MIT license

Stars

Watchers

Forks

Packages

Contributors

Languages