vnstat-metrics 0.1.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.
@@ -0,0 +1,3 @@
1
+ # License
2
+
3
+ MIT
@@ -0,0 +1,11 @@
1
+ # vnstat-metrics
2
+
3
+ A Sensu plugin to output traffic metrics from vnstat in Graphite format.
4
+
5
+ ##Example
6
+
7
+ ```
8
+ vnstat-metrics.rb -i en0 -p /usr/local/bin/vnstat
9
+ ```
10
+
11
+ Relies on the vnstat command line tool being installed.
@@ -0,0 +1,91 @@
1
+ #! /usr/bin/env ruby
2
+ # encoding: UTF-8
3
+ #
4
+ # vnstat-metrics
5
+ #
6
+ # DESCRIPTION:
7
+ #
8
+ # Outputs the results of vnstat -tr in Graphite text format
9
+ #
10
+ # OUTPUT:
11
+ # metric data about traffic
12
+ #
13
+ # PLATFORMS:
14
+ # Linux
15
+ #
16
+ # DEPENDENCIES:
17
+ # gem: sensu-plugin
18
+ #
19
+ # USAGE:
20
+ #
21
+ # NOTES:
22
+ #
23
+ # LICENSE:
24
+ #
25
+ # MIT
26
+ #
27
+ require 'sensu-plugin/metric/cli'
28
+ require 'socket'
29
+
30
+ #
31
+ # CPU Graphite
32
+ #
33
+ class CpuGraphite < Sensu::Plugin::Metric::CLI::Graphite
34
+ option :scheme,
35
+ description: 'Metric naming scheme, text to prepend to metric',
36
+ short: '-s SCHEME',
37
+ long: '--scheme SCHEME',
38
+ default: "#{Socket.gethostname}.vnstat"
39
+
40
+ option :path,
41
+ description: 'Full path to the vnstat executable to use',
42
+ short: '-p PATH',
43
+ long: '--path PATH',
44
+ default: '/usr/bin/vnstat'
45
+
46
+ option :time,
47
+ description: 'The sample time to pass to the -tr flag',
48
+ short: '-t TIME',
49
+ long: '--time TIME',
50
+ default: 5
51
+
52
+ option :interface,
53
+ description: 'The interface to query',
54
+ short: '-i INTERFACE',
55
+ long: '--interface INTERFACE',
56
+ default: 'eth0'
57
+
58
+ def match_and_scale(cmd_output, regular_exp, key)
59
+ if cmd_output =~ regular_exp
60
+ scaled_throughput = Regexp.last_match(1).to_f
61
+ scale = Regexp.last_match(2)
62
+
63
+ case scale
64
+ when 'kbit/s'
65
+ throughput = scaled_throughput
66
+ when 'mbit/s'
67
+ throughput = scaled_throughput * 1000
68
+ when 'gbit/s'
69
+ throughput = scaled_throughput * 1000 * 1000
70
+ end
71
+
72
+ output "#{config[:scheme]}.#{config[:interface]}.#{key}", throughput
73
+ else
74
+ warning "#{key} not found in output"
75
+ end
76
+ end
77
+
78
+
79
+ def run # rubocop:disable all
80
+ vnstat_output = %x[#{config[:path]} -i #{config[:interface]} -tr #{config[:time]} 2>/dev/null]
81
+ exit_code = $?.clone
82
+
83
+ if exit_code.exitstatus > 0
84
+ warning "vnstat returned a non-0 output"
85
+ else
86
+ match_and_scale(vnstat_output, /\n\s+rx\s+([\d\.]+)[\t ]+([kKmMbit\/s]+)/, 'rx')
87
+ match_and_scale(vnstat_output, /\n\s+tx\s+([\d\.]+)[\t ]+([kKmMbit\/s]+)/, 'tx')
88
+ ok
89
+ end
90
+ end
91
+ end
@@ -0,0 +1 @@
1
+ require 'vnstat-metrics/version'
@@ -0,0 +1,9 @@
1
+ module VnstatMetrics
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 0
6
+
7
+ VER_STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vnstat-metrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dan Pisarski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-08-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sensu-plugin
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.2.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.2.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.7'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.7'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ description: Sensu plugins for traffic metrics using vnstat
63
+ email: me@danpisarski.com
64
+ executables:
65
+ - vnstat-metrics.rb
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - bin/vnstat-metrics.rb
70
+ - lib/vnstat-metrics/version.rb
71
+ - lib/vnstat-metrics.rb
72
+ - LICENSE.md
73
+ - README.md
74
+ homepage: http://github.com/danpisarski/vnstat-metrics
75
+ licenses:
76
+ - MIT
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: 1.9.3
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 1.8.23.2
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Sensu plugins for traffic metrics using vnstat
99
+ test_files: []