newrelic_pivpn_agent 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+ gemspec
3
+
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ ## New Relic piVPN (OpenVPN) monitoring Plugin
2
+
3
+ The New Relic piVPN Plugin enables monitoring piVPN, and it reports the following data:
4
+
5
+ * Amount of active Users
6
+ * Amount of bytes sent
7
+ * Amount of bytes received
8
+ * Average of bytes sent
9
+ * Average of bytes received
10
+
11
+ ### Requirements
12
+
13
+ The piVPN monitoring Plugin for New Relic requires the following:
14
+
15
+ * A New Relic account. Signup for a free account at http://newrelic.com
16
+ * You need to install this plugin on a piVPN server. That host also needs Ruby (tested with 1.8.7, 1.9.3), and support for rubygems.
17
+
18
+ ### Instructions for running the piVPN agent
19
+
20
+ 1. Install this gem from RubyGems:
21
+
22
+ `sudo gem install newrelic_pivpn_agent`
23
+
24
+ 2. Install config, execute
25
+
26
+ `sudo newrelic_pivpn_agent install` - it will create `/etc/newrelic/newrelic_pivpn_agent.yml` file for you.
27
+
28
+ 3. Edit the `/etc/newrelic/newrelic_pivpn_agent.yml` file generated in step 2.
29
+
30
+ 3.1. replace `YOUR_LICENSE_KEY_HERE` with your New Relic license key. Your license key can be found under Account Settings at https://rpm.newrelic.com, see https://newrelic.com/docs/subscriptions/license-key for more help.
31
+
32
+ 3.2. replace the agent name 'openvpn' to any unique instance name of choice
33
+
34
+ 3.3. replace the path of the piVPN status binary if needed
35
+
36
+ 4. Execute
37
+
38
+ `newrelic_pivpn_agent run`
39
+
40
+ 5. Go back to the Plugins list and after a brief period you will see the piVPN Plugin listed in your New Relic account
41
+
42
+
43
+ ## Keep this process running
44
+
45
+ You can use services like these to manage this process and run it as a daemon.
46
+
47
+ - [Upstart](http://upstart.ubuntu.com/)
48
+ - [Systemd](http://www.freedesktop.org/wiki/Software/systemd/)
49
+ - [Runit](http://smarden.org/runit/)
50
+ - [Monit](http://mmonit.com/monit/)
51
+
52
+ ## Support
53
+
54
+ Please use Github issues for support.
data/Rakefile ADDED
@@ -0,0 +1,153 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'date'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def version
16
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
17
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
18
+ end
19
+
20
+ def date
21
+ Date.today.to_s
22
+ end
23
+
24
+ def rubyforge_project
25
+ name
26
+ end
27
+
28
+ def gemspec_file
29
+ "#{name}.gemspec"
30
+ end
31
+
32
+ def gem_file
33
+ "#{name}-#{version}.gem"
34
+ end
35
+
36
+ def replace_header(head, header_name)
37
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
38
+ end
39
+
40
+ #############################################################################
41
+ #
42
+ # Standard tasks
43
+ #
44
+ #############################################################################
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/testtask'
49
+ Rake::TestTask.new(:test) do |test|
50
+ test.libs << 'lib' << 'test'
51
+ test.pattern = 'test/**/test_*.rb'
52
+ test.verbose = true
53
+ end
54
+
55
+ desc "Generate RCov test coverage and open in your browser"
56
+ task :coverage do
57
+ require 'rcov'
58
+ sh "rm -fr coverage"
59
+ sh "rcov test/test_*.rb"
60
+ sh "open coverage/index.html"
61
+ end
62
+
63
+ require 'rdoc/task'
64
+ Rake::RDocTask.new do |rdoc|
65
+ rdoc.rdoc_dir = 'rdoc'
66
+ rdoc.title = "#{name} #{version}"
67
+ rdoc.rdoc_files.include('lib/**/*.rb')
68
+ rdoc.rdoc_files.include('README.rdoc')
69
+ rdoc.rdoc_files.include('LICENSE')
70
+ rdoc.rdoc_files.include('CHANGES')
71
+ rdoc.main = "README.rdoc"
72
+ end
73
+
74
+ desc "Open an irb session preloaded with this library"
75
+ task :console do
76
+ sh "irb -rubygems -r ./lib/#{name}.rb"
77
+ end
78
+
79
+ #############################################################################
80
+ #
81
+ # Custom tasks (add your own tasks here)
82
+ #
83
+ #############################################################################
84
+
85
+
86
+
87
+ #############################################################################
88
+ #
89
+ # Packaging tasks
90
+ #
91
+ #############################################################################
92
+
93
+ desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
94
+ task :release => :build do
95
+ unless `git branch` =~ /^\* master$/
96
+ puts "You must be on the master branch to release!"
97
+ exit!
98
+ end
99
+ sh "git commit --allow-empty -a -m 'Release #{name} #{version}'"
100
+ #sh "git tag v#{version}"
101
+ sh "git push origin master"
102
+ #sh "git push origin v#{version}"
103
+ sh "gem push pkg/#{name}-#{version}.gem"
104
+ end
105
+
106
+ desc "Build #{gem_file} into the pkg directory"
107
+ task :build => :gemspec do
108
+ sh "mkdir -p pkg"
109
+ sh "gem build #{gemspec_file}"
110
+ sh "mv #{gem_file} pkg"
111
+ end
112
+
113
+ desc "Generate #{gemspec_file}"
114
+ task :gemspec => :validate do
115
+ # read spec file and split out manifest section
116
+ spec = File.read(gemspec_file)
117
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
118
+
119
+ # replace name version and date
120
+ replace_header(head, :name)
121
+ replace_header(head, :version)
122
+ replace_header(head, :date)
123
+ #comment this out if your rubyforge_project has a different name
124
+ #replace_header(head, :rubyforge_project)
125
+
126
+ # determine file list from git ls-files
127
+ files = `git ls-files`.
128
+ split("\n").
129
+ sort.
130
+ reject { |file| file =~ /^\./ }.
131
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
132
+ map { |file| " #{file}" }.
133
+ join("\n")
134
+
135
+ # piece file back together and write
136
+ manifest = " s.files = %w[\n#{files}\n ]\n"
137
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
138
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
139
+ puts "Updated #{gemspec_file}"
140
+ end
141
+
142
+ desc "Validate #{gemspec_file}"
143
+ task :validate do
144
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
145
+ unless libfiles.empty?
146
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
147
+ exit!
148
+ end
149
+ unless Dir['VERSION*'].empty?
150
+ puts "A `VERSION` file at root level violates Gem best practices."
151
+ exit!
152
+ end
153
+ end
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/env ruby
2
+ $stdout.sync = true
3
+
4
+ $LOAD_PATH.unshift File.expand_path "../../lib", __FILE__
5
+ require "newrelic_pivpn_agent"
6
+ require 'optparse'
7
+
8
+ NewRelic::Plugin::Config.config_file = "/etc/newrelic/newrelic_pivpn_agent.yml"
9
+
10
+ options = OptionParser.new do |opts|
11
+ opts.banner = <<-EOF
12
+ Usage:
13
+ newrelic_pivpn_agent ( run | install ) [options]
14
+ EOF
15
+
16
+ opts.on("-v", "--verbose", "Run verbosely") do
17
+ NewRelic::Plugin::Config.config.newrelic['verbose'] = 1
18
+ end
19
+
20
+ opts.on("-c", "--config FILE", "Override the location of the newrelic_plugin.yml") do | filename |
21
+ if !File.exists? filename
22
+ puts "File not found: #{filename.inspect}"
23
+ exit 1
24
+ end
25
+ NewRelic::Plugin::Config.config_file = filename
26
+ end
27
+
28
+ opts.on("-h", "--help") do
29
+ puts opts
30
+ if File.basename($0) == File.basename(__FILE__)
31
+ exit 0
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ args = options.parse!(ARGV)
38
+
39
+ if args.first == "run"
40
+ if !File.exists? NewRelic::Plugin::Config.config_file
41
+ puts "Agent config file not found: #{NewRelic::Plugin::Config.config_file.inspect}"
42
+ puts "Run 'sudo newrelic_pivpn_agent install' for setup config"
43
+ exit 1
44
+ end
45
+ NewRelicOpenvpnAgent.run
46
+ elsif args.first == "install"
47
+ config_file = File.read(File.expand_path("../../config/template_newrelic_plugin.yml", __FILE__))
48
+
49
+ require 'fileutils'
50
+ FileUtils.mkdir_p "/etc/newrelic"
51
+ File.open("/etc/newrelic/newrelic_pivpn_agent.yml", "w") do | io |
52
+ io.write(config_file)
53
+ end
54
+ puts "Saved agent config file #{File.expand_path("/etc/newrelic/newrelic_pivpn_agent.yml")}"
55
+ else
56
+ puts options
57
+ end
@@ -0,0 +1,20 @@
1
+ # Please make sure to update the license_key information with the license key for your New Relic
2
+ # account.
3
+ #
4
+ #
5
+ newrelic:
6
+ #
7
+ # Update with your New Relic account license key:
8
+ #
9
+ license_key: ''
10
+ #
11
+ # Set to '1' for verbose output, remove for normal output.
12
+ # All output goes to stdout/stderr.
13
+ #
14
+ verbose: 1
15
+ #
16
+ # Agent Configuration:
17
+ #
18
+ agents:
19
+ piVPN-host:
20
+ openvpn_status_path : "/var/log/openvpn-status.log"
@@ -0,0 +1,60 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ #
4
+ # This is a NewRelic agent which pushes OpenVPN information.
5
+ #
6
+
7
+ require "rubygems"
8
+ require "bundler/setup"
9
+ require "newrelic_plugin"
10
+
11
+ module NewRelicOpenvpnAgent
12
+ class Agent < NewRelic::Plugin::Agent::Base
13
+ agent_guid "home.secretnet.secretlab.pivpn"
14
+ agent_version "0.0.2"
15
+ agent_config_options :openvpn_status_path
16
+ agent_human_labels("OpenVPN Agent") { ident }
17
+
18
+ attr_reader :ident
19
+
20
+ def poll_cycle
21
+ [:total_users, :total_bytes_received, :total_bytes_sent, :average_bytes_received, :average_bytes_sent].each do |_type|
22
+ name, unit, _output = metric(_type)
23
+ report_metric name, unit, _output.call
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def get_columns(_output, _c_index, _uniq = true, _total = true)
30
+ result = _output.scan(/CLIENT_LIST[\s]+([0-9a-zA-Z]+)[\s]+((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):\d+)[\s]+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([A-Za-z0-9]+[\s]+[a-zA-Z0-9]+[\s]+[0-9]+[\s]+[0-9:]+[\s]+[0-9]+)(\s)+([0-9]+)(\s)+([a-zA-Z]+)/).map{|_x| _x[_c_index] }
31
+ result = result.uniq if _uniq
32
+ result = result.length if _total
33
+ result
34
+ end
35
+
36
+ def metric(_type)
37
+ metrics = {
38
+ :total_users => ["Total/Users", "Users", lambda{get_columns(ovpn_cmd, 1) }],
39
+ :total_bytes_received => ["Total/Bytes/Received", "bytes", lambda{ get_columns(ovpn_cmd, 4, false, false).inject(0){|_t, _b| (_t + _b.to_i) } }],
40
+ :total_bytes_sent => ["Total/Bytes/Sent", "bytes", lambda{ get_columns(ovpn_cmd, 5, false, false).inject(0){|_t, _b| (_t + _b.to_i) } }],
41
+ :average_bytes_received => ["Average/Bytes/Received", "bytes", lambda{ x = get_columns(ovpn_cmd, 4, false, false); x.inject(0){|_t, _b| (_t + _b.to_i) } / x.length }],
42
+ :average_bytes_sent => ["Average/Bytes/Sent", "bytes", lambda{ x = get_columns(ovpn_cmd, 5, false, false); x.inject(0){|_t, _b| (_t + _b.to_i) } / x.length }]
43
+ }
44
+ metrics[_type]
45
+ end
46
+
47
+ def ovpn_cmd
48
+ `cat #{openvpn_status_path}`
49
+ end
50
+
51
+ end
52
+
53
+ def self.run
54
+ NewRelic::Plugin::Config.config.agents.keys.each do |_agent|
55
+ NewRelic::Plugin::Setup.install_agent _agent, NewRelicOpenvpnAgent
56
+ end
57
+
58
+ NewRelic::Plugin::Run.setup_and_run
59
+ end
60
+ end
@@ -0,0 +1,81 @@
1
+ ## This is the rakegem gemspec template. Make sure you read and understand
2
+ ## all of the comments. Some sections require modification, and others can
3
+ ## be deleted if you don't need them. Once you understand the contents of
4
+ ## this file, feel free to delete any comments that begin with two hash marks.
5
+ ## You can find comprehensive Gem::Specification documentation, at
6
+ ## http://docs.rubygems.org/read/chapter/20
7
+ Gem::Specification.new do |s|
8
+ s.specification_version = 2 if s.respond_to? :specification_version=
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.rubygems_version = '1.3.5'
11
+
12
+ ## Leave these as is they will be modified for you by the rake gemspec task.
13
+ ## If your rubyforge_project name is different, then edit it and comment out
14
+ ## the sub! line in the Rakefile
15
+ s.name = 'newrelic_pivpn_agent'
16
+ s.version = '0.0.2'
17
+ s.date = '2017-11-13'
18
+ # s.rubyforge_project = 'newrelic_pivpn_agent'
19
+
20
+ ## Make sure your summary is short. The description may be as long
21
+ ## as you like.
22
+ s.summary = "New Relic openvpn monitoring plugin"
23
+ s.description = <<-EOF
24
+ This is the New Relic plugin for monitoring OpenVPN developed by KangaCoders Ltd.
25
+ EOF
26
+
27
+ ## List the primary authors. If there are a bunch of authors, it's probably
28
+ ## better to set the email to an email list or something. If you don't have
29
+ ## a custom homepage, consider using your GitHub URL or the like.
30
+ s.authors = ["Kai De Sutter"]
31
+ s.email = 'kdesutter@kangacoders.com'
32
+ s.homepage = 'http://www.kangacoders.com/'
33
+
34
+ ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
35
+ ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
36
+ s.require_paths = %w[lib]
37
+
38
+ ## This sections is only necessary if you have C extensions.
39
+ #s.require_paths << 'ext'
40
+ #s.extensions = %w[ext/extconf.rb]
41
+
42
+ ## If your gem includes any executables, list them here.
43
+ s.executables = ["newrelic_pivpn_agent"]
44
+
45
+ ## Specify any RDoc options here. You'll want to add your README and
46
+ ## LICENSE files to the extra_rdoc_files list.
47
+ s.rdoc_options = ["--charset=UTF-8"]
48
+ s.extra_rdoc_files = %w[README.md]
49
+
50
+ s.license = 'MIT'
51
+
52
+ ## List your runtime dependencies here. Runtime dependencies are those
53
+ ## that are needed for an end user to actually USE your code.
54
+ s.add_dependency('bundler')
55
+ s.add_dependency('newrelic_plugin', "1.0.3")
56
+
57
+ s.post_install_message = <<-EOF
58
+ To get started with this plugin, do
59
+ newrelic_pivpn_agent -h
60
+ to find out how to install and run the plugin agent.
61
+ EOF
62
+
63
+ ## Leave this section as-is. It will be automatically generated from the
64
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
65
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
66
+ # = MANIFEST =
67
+ s.files = %w[
68
+ Gemfile
69
+ README.md
70
+ Rakefile
71
+ bin/newrelic_pivpn_agent
72
+ config/template_newrelic_plugin.yml
73
+ lib/newrelic_pivpn_agent.rb
74
+ newrelic_pivpn_agent.gemspec
75
+ ]
76
+ # = MANIFEST =
77
+
78
+ ## Test files will be grabbed from the file list. Make sure the path glob
79
+ ## matches what you actually use.
80
+ s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
81
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: newrelic_pivpn_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kai De Sutter
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2017-11-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '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: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: newrelic_plugin
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.0.3
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.0.3
46
+ description: ! 'This is the New Relic plugin for monitoring OpenVPN developed by KangaCoders
47
+ Ltd.
48
+
49
+ '
50
+ email: kdesutter@kangacoders.com
51
+ executables:
52
+ - newrelic_pivpn_agent
53
+ extensions: []
54
+ extra_rdoc_files:
55
+ - README.md
56
+ files:
57
+ - Gemfile
58
+ - README.md
59
+ - Rakefile
60
+ - bin/newrelic_pivpn_agent
61
+ - config/template_newrelic_plugin.yml
62
+ - lib/newrelic_pivpn_agent.rb
63
+ - newrelic_pivpn_agent.gemspec
64
+ homepage: http://www.kangacoders.com/
65
+ licenses:
66
+ - MIT
67
+ post_install_message: ! " To get started with this plugin, do\n newrelic_pivpn_agent
68
+ -h\n to find out how to install and run the plugin agent.\n"
69
+ rdoc_options:
70
+ - --charset=UTF-8
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 1.8.23
88
+ signing_key:
89
+ specification_version: 2
90
+ summary: New Relic openvpn monitoring plugin
91
+ test_files: []