vnstat-ruby 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.codeclimate.yml +14 -0
- data/.document +5 -0
- data/.rspec +3 -0
- data/.rubocop.yml +1171 -0
- data/.travis.yml +14 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +84 -0
- data/LICENSE.txt +20 -0
- data/README.md +81 -0
- data/Rakefile +28 -0
- data/VERSION +1 -0
- data/lib/vnstat-ruby.rb +1 -0
- data/lib/vnstat.rb +65 -0
- data/lib/vnstat/configuration.rb +35 -0
- data/lib/vnstat/document.rb +56 -0
- data/lib/vnstat/error.rb +6 -0
- data/lib/vnstat/errors/executable_not_found.rb +7 -0
- data/lib/vnstat/errors/unknown_interface.rb +12 -0
- data/lib/vnstat/interface.rb +161 -0
- data/lib/vnstat/interface_collection.rb +106 -0
- data/lib/vnstat/parser.rb +54 -0
- data/lib/vnstat/result.rb +55 -0
- data/lib/vnstat/result/date_delegation.rb +31 -0
- data/lib/vnstat/result/day.rb +45 -0
- data/lib/vnstat/result/hour.rb +56 -0
- data/lib/vnstat/result/minute.rb +62 -0
- data/lib/vnstat/result/month.rb +47 -0
- data/lib/vnstat/result/time_comparable.rb +16 -0
- data/lib/vnstat/system_call.rb +90 -0
- data/lib/vnstat/traffic.rb +11 -0
- data/lib/vnstat/traffic/base.rb +46 -0
- data/lib/vnstat/traffic/daily.rb +40 -0
- data/lib/vnstat/traffic/hourly.rb +44 -0
- data/lib/vnstat/traffic/monthly.rb +27 -0
- data/lib/vnstat/traffic/tops.rb +39 -0
- data/lib/vnstat/utils.rb +68 -0
- data/spec/lib/vnstat/configuration_spec.rb +72 -0
- data/spec/lib/vnstat/document_spec.rb +54 -0
- data/spec/lib/vnstat/errors/executable_not_found_spec.rb +5 -0
- data/spec/lib/vnstat/errors/unknown_interface_spec.rb +5 -0
- data/spec/lib/vnstat/interface_collection_spec.rb +124 -0
- data/spec/lib/vnstat/interface_spec.rb +213 -0
- data/spec/lib/vnstat/result/day_spec.rb +39 -0
- data/spec/lib/vnstat/result/hour_spec.rb +43 -0
- data/spec/lib/vnstat/result/minute_spec.rb +52 -0
- data/spec/lib/vnstat/result/month_spec.rb +39 -0
- data/spec/lib/vnstat/result_spec.rb +86 -0
- data/spec/lib/vnstat/system_call_spec.rb +209 -0
- data/spec/lib/vnstat/traffic/daily_spec.rb +109 -0
- data/spec/lib/vnstat/traffic/hourly_spec.rb +153 -0
- data/spec/lib/vnstat/traffic/monthly_spec.rb +46 -0
- data/spec/lib/vnstat/traffic/tops_spec.rb +48 -0
- data/spec/lib/vnstat/utils_spec.rb +128 -0
- data/spec/lib/vnstat_spec.rb +61 -0
- data/spec/spec_helper.rb +98 -0
- data/spec/support/shared_examples/shared_examples_for_date_delegation.rb +19 -0
- data/spec/support/shared_examples/shared_examples_for_traffic_collection.rb +19 -0
- data/vnstat-ruby.gemspec +113 -0
- metadata +187 -0
data/.travis.yml
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- '1.9.3'
|
4
|
+
- '2.0.0'
|
5
|
+
- '2.1.7'
|
6
|
+
- '2.2.3'
|
7
|
+
- ruby-head
|
8
|
+
- jruby
|
9
|
+
- jruby-head
|
10
|
+
- rbx-2
|
11
|
+
script: bundle exec rspec
|
12
|
+
addons:
|
13
|
+
code_climate:
|
14
|
+
repo_token: 06a44082b0f5a5114eec226952c45443a4ac8c86e59e098869b5c07e420505ab
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
addressable (2.3.8)
|
5
|
+
builder (3.2.2)
|
6
|
+
codeclimate-test-reporter (0.4.8)
|
7
|
+
simplecov (>= 0.7.1, < 1.0.0)
|
8
|
+
descendants_tracker (0.0.4)
|
9
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
10
|
+
diff-lcs (1.2.5)
|
11
|
+
docile (1.1.5)
|
12
|
+
faraday (0.9.2)
|
13
|
+
multipart-post (>= 1.2, < 3)
|
14
|
+
git (1.2.9.1)
|
15
|
+
github_api (0.12.4)
|
16
|
+
addressable (~> 2.3)
|
17
|
+
descendants_tracker (~> 0.0.4)
|
18
|
+
faraday (~> 0.8, < 0.10)
|
19
|
+
hashie (>= 3.4)
|
20
|
+
multi_json (>= 1.7.5, < 2.0)
|
21
|
+
nokogiri (~> 1.6.6)
|
22
|
+
oauth2
|
23
|
+
hashie (3.4.2)
|
24
|
+
highline (1.7.8)
|
25
|
+
jeweler (2.0.1)
|
26
|
+
builder
|
27
|
+
bundler (>= 1.0)
|
28
|
+
git (>= 1.2.5)
|
29
|
+
github_api
|
30
|
+
highline (>= 1.6.15)
|
31
|
+
nokogiri (>= 1.5.10)
|
32
|
+
rake
|
33
|
+
rdoc
|
34
|
+
json (1.8.3)
|
35
|
+
jwt (1.5.1)
|
36
|
+
mini_portile (0.6.2)
|
37
|
+
multi_json (1.11.2)
|
38
|
+
multi_xml (0.5.5)
|
39
|
+
multipart-post (2.0.0)
|
40
|
+
nokogiri (1.6.6.2)
|
41
|
+
mini_portile (~> 0.6.0)
|
42
|
+
oauth2 (1.0.0)
|
43
|
+
faraday (>= 0.8, < 0.10)
|
44
|
+
jwt (~> 1.0)
|
45
|
+
multi_json (~> 1.3)
|
46
|
+
multi_xml (~> 0.5)
|
47
|
+
rack (~> 1.2)
|
48
|
+
rack (1.6.4)
|
49
|
+
rake (10.4.2)
|
50
|
+
rdoc (4.2.0)
|
51
|
+
rspec (3.3.0)
|
52
|
+
rspec-core (~> 3.3.0)
|
53
|
+
rspec-expectations (~> 3.3.0)
|
54
|
+
rspec-mocks (~> 3.3.0)
|
55
|
+
rspec-core (3.3.2)
|
56
|
+
rspec-support (~> 3.3.0)
|
57
|
+
rspec-expectations (3.3.1)
|
58
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
59
|
+
rspec-support (~> 3.3.0)
|
60
|
+
rspec-mocks (3.3.2)
|
61
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
62
|
+
rspec-support (~> 3.3.0)
|
63
|
+
rspec-support (3.3.0)
|
64
|
+
simplecov (0.10.0)
|
65
|
+
docile (~> 1.1.0)
|
66
|
+
json (~> 1.8)
|
67
|
+
simplecov-html (~> 0.10.0)
|
68
|
+
simplecov-html (0.10.0)
|
69
|
+
thread_safe (0.3.5)
|
70
|
+
yard (0.8.7.6)
|
71
|
+
|
72
|
+
PLATFORMS
|
73
|
+
ruby
|
74
|
+
|
75
|
+
DEPENDENCIES
|
76
|
+
bundler (~> 1.0)
|
77
|
+
codeclimate-test-reporter
|
78
|
+
jeweler (~> 2.0.1)
|
79
|
+
nokogiri
|
80
|
+
rspec (~> 3.0)
|
81
|
+
yard
|
82
|
+
|
83
|
+
BUNDLED WITH
|
84
|
+
1.10.6
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2015 Tobias Casper
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# vnstat-ruby
|
2
|
+
|
3
|
+
[](https://codeclimate.com/github/tlux/vnstat-ruby) [](https://codeclimate.com/github/tlux/vnstat-ruby/coverage) [](https://travis-ci.org/tlux/vnstat-ruby)
|
4
|
+
|
5
|
+
Vnstat is a tool that tracks the traffic on your network interfaces.
|
6
|
+
This tiny library dependends on the `vnstat` command line utility to provide
|
7
|
+
network traffic information through an easy-to-use API.
|
8
|
+
|
9
|
+
|
10
|
+
### Prerequisites
|
11
|
+
|
12
|
+
You need one of the following Ruby versions installed to run the gem:
|
13
|
+
|
14
|
+
* Ruby >= 1.9.3
|
15
|
+
* JRuby
|
16
|
+
* Rubinius >= 2
|
17
|
+
|
18
|
+
Also make sure you have the vnstat CLI installed by running `which vnstat`.
|
19
|
+
|
20
|
+
If the executable was not found, you are able to obtain the vnstat package
|
21
|
+
via aptitude (or a similar package manager):
|
22
|
+
|
23
|
+
```bash
|
24
|
+
apt-get install vnstat
|
25
|
+
```
|
26
|
+
|
27
|
+
|
28
|
+
### Getting Started
|
29
|
+
|
30
|
+
#### Setup
|
31
|
+
|
32
|
+
Once the dependent package is installed, you are ready to install the
|
33
|
+
vnstat-ruby gem.
|
34
|
+
|
35
|
+
```bash
|
36
|
+
gem install vnstat-ruby
|
37
|
+
```
|
38
|
+
|
39
|
+
Or, add the following line to the Gemfile of your project and run the
|
40
|
+
`bundle install` command:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
gem 'vnstat-ruby'
|
44
|
+
```
|
45
|
+
|
46
|
+
#### Configuration
|
47
|
+
|
48
|
+
By default, vnstat-ruby tries to automatically determine the location of your
|
49
|
+
vnstat executable. If you need to change this path for some reason, you can
|
50
|
+
override the default:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
Vnstat.configure do |config|
|
54
|
+
config.executable_path = '/usr/bin/vnstat'
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
### Usage
|
59
|
+
|
60
|
+
TODO
|
61
|
+
|
62
|
+
|
63
|
+
### Contributing to vnstat-ruby
|
64
|
+
|
65
|
+
* Check out the latest master to make sure the feature hasn't been implemented
|
66
|
+
or the bug hasn't been fixed yet.
|
67
|
+
* Check out the issue tracker to make sure someone already hasn't requested it
|
68
|
+
and/or contributed it.
|
69
|
+
* Fork the project.
|
70
|
+
* Start a feature/bugfix branch.
|
71
|
+
* Commit and push until you are happy with your contribution.
|
72
|
+
* Make sure to add tests for it. This is important so I don't break it in a
|
73
|
+
future version unintentionally.
|
74
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to
|
75
|
+
have your own version, or is otherwise necessary, that is fine, but please
|
76
|
+
isolate to its own commit so I can cherry-pick around it.
|
77
|
+
|
78
|
+
### Copyright
|
79
|
+
|
80
|
+
Copyright (c) 2015 Tobias Casper. See LICENSE.txt for
|
81
|
+
further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
|
17
|
+
gem.name = 'vnstat-ruby'
|
18
|
+
gem.homepage = 'http://github.com/tlux/vnstat-ruby'
|
19
|
+
gem.license = 'MIT'
|
20
|
+
gem.summary = 'A Ruby wrapper for vnstat'
|
21
|
+
gem.description = gem.summary
|
22
|
+
gem.email = 'tobias.casper@gmail.com'
|
23
|
+
gem.authors = ['Tobias Casper']
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
task default: :test
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/lib/vnstat-ruby.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'vnstat'
|
data/lib/vnstat.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'nokogiri'
|
3
|
+
|
4
|
+
##
|
5
|
+
# The Vnstat namespace.
|
6
|
+
module Vnstat
|
7
|
+
autoload :Configuration, 'vnstat/configuration'
|
8
|
+
autoload :Document, 'vnstat/document'
|
9
|
+
autoload :Interface, 'vnstat/interface'
|
10
|
+
autoload :InterfaceCollection, 'vnstat/interface_collection'
|
11
|
+
autoload :Parser, 'vnstat/parser'
|
12
|
+
autoload :Result, 'vnstat/result'
|
13
|
+
autoload :SystemCall, 'vnstat/system_call'
|
14
|
+
autoload :Traffic, 'vnstat/traffic'
|
15
|
+
autoload :Utils, 'vnstat/utils'
|
16
|
+
|
17
|
+
autoload :Error, 'vnstat/error'
|
18
|
+
autoload :ExecutableNotFound, 'vnstat/errors/executable_not_found'
|
19
|
+
autoload :UnknownInterface, 'vnstat/errors/unknown_interface'
|
20
|
+
|
21
|
+
module_function
|
22
|
+
|
23
|
+
##
|
24
|
+
# The configuration of the Vnstat environment.
|
25
|
+
#
|
26
|
+
# @return [Configuration]
|
27
|
+
def config
|
28
|
+
@config ||= Configuration.new
|
29
|
+
end
|
30
|
+
|
31
|
+
##
|
32
|
+
# Configures the Vnstat environment.
|
33
|
+
#
|
34
|
+
# @yieldparam [Configuration] config The environment configuration.
|
35
|
+
# @return [Configuration]
|
36
|
+
def configure
|
37
|
+
yield(config)
|
38
|
+
config
|
39
|
+
end
|
40
|
+
|
41
|
+
##
|
42
|
+
# Returns traffic information for the given interface.
|
43
|
+
#
|
44
|
+
# @param [String] id The network interface identifier.
|
45
|
+
# @return [Interface]
|
46
|
+
def [](id)
|
47
|
+
interfaces[id]
|
48
|
+
end
|
49
|
+
|
50
|
+
##
|
51
|
+
# Returns traffic information for all known interfaces.
|
52
|
+
#
|
53
|
+
# @return [InterfaceCollection]
|
54
|
+
def interfaces
|
55
|
+
InterfaceCollection.open
|
56
|
+
end
|
57
|
+
|
58
|
+
##
|
59
|
+
# Returns information for the currently installed version of vnstat.
|
60
|
+
#
|
61
|
+
# @return [String]
|
62
|
+
def version
|
63
|
+
Utils.call_executable('-v')
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Vnstat
|
2
|
+
##
|
3
|
+
# A class encapsulating configurations of the Vnstat environment.
|
4
|
+
#
|
5
|
+
# @attr_writer [String] executable_path Sets the location of the vnstat
|
6
|
+
# executable.
|
7
|
+
class Configuration
|
8
|
+
##
|
9
|
+
# Initializes the {Configuration}.
|
10
|
+
def initialize
|
11
|
+
reset
|
12
|
+
end
|
13
|
+
|
14
|
+
##
|
15
|
+
# Restores the configuration defaults.
|
16
|
+
#
|
17
|
+
# @return [Configuration]
|
18
|
+
def reset
|
19
|
+
@executable_path = nil
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
##
|
24
|
+
# Returns the location of the vnstat executable.
|
25
|
+
#
|
26
|
+
# @return [String]
|
27
|
+
def executable_path
|
28
|
+
@executable_path ||= Utils.system_call('which', 'vnstat') do
|
29
|
+
fail ExecutableNotFound, 'Unable to locate vnstat executable'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
attr_writer :executable_path
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Vnstat
|
2
|
+
# A class encapsulating document data.
|
3
|
+
#
|
4
|
+
# @attr_reader [Nokogiri::XML::Document] data The underlying XML document.
|
5
|
+
class Document
|
6
|
+
##
|
7
|
+
# Initializes the document.
|
8
|
+
#
|
9
|
+
# @param [String] data The raw XML data.
|
10
|
+
def initialize(data)
|
11
|
+
self.data = data
|
12
|
+
end
|
13
|
+
|
14
|
+
##
|
15
|
+
# @return [Document]
|
16
|
+
def self.open(*args)
|
17
|
+
new(*args, load_data(*args))
|
18
|
+
end
|
19
|
+
|
20
|
+
##
|
21
|
+
# A hook used by {.open} that is intended to be overridden by subclasses.
|
22
|
+
#
|
23
|
+
# @raise [NotImplementedError]
|
24
|
+
def self.load_data(*args)
|
25
|
+
fail NotImplementedError, "Please override #{name}.#{__method__}"
|
26
|
+
end
|
27
|
+
|
28
|
+
attr_reader :data
|
29
|
+
|
30
|
+
##
|
31
|
+
# Sets the raw XML data for the {Document}.
|
32
|
+
#
|
33
|
+
# @param [String] data A string representing the document.
|
34
|
+
# @raise [ArgumentError] Raised if the specified data was nil.
|
35
|
+
def data=(data)
|
36
|
+
fail ArgumentError, 'No document data specified' if data.nil?
|
37
|
+
@data = Nokogiri::XML.parse(data.to_s)
|
38
|
+
end
|
39
|
+
|
40
|
+
##
|
41
|
+
# Returns the version as specified in the vnstat element.
|
42
|
+
#
|
43
|
+
# @return [String]
|
44
|
+
def version
|
45
|
+
data.xpath('vnstat').attr('version').text
|
46
|
+
end
|
47
|
+
|
48
|
+
##
|
49
|
+
# Returns the XML version as specified in the vnstat element.
|
50
|
+
#
|
51
|
+
# @return [String]
|
52
|
+
def xml_version
|
53
|
+
data.xpath('vnstat').attr('xmlversion').text
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/vnstat/error.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
module Vnstat
|
2
|
+
#
|
3
|
+
# An exception that is raised when the specified interface could not be found
|
4
|
+
# among the known ones.
|
5
|
+
class UnknownInterface < Error
|
6
|
+
attr_reader :interface_id
|
7
|
+
|
8
|
+
def initialize(interface_id)
|
9
|
+
@interface_id = interface_id
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|