newrelic_samba_agent 0.1.0

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
+
@@ -0,0 +1,53 @@
1
+ ## New Relic Samba monitoring Plugin
2
+
3
+ The New Relic Samba Plugin enables monitoring Samba, and it reports the following data:
4
+
5
+ * Amount of active Users
6
+ * Amount of active Machines
7
+ * Amount of open Files
8
+ * Amount of open Files per User (Average)
9
+
10
+ ### Requirements
11
+
12
+ The Samba monitoring Plugin for New Relic requires the following:
13
+
14
+ * A New Relic account. Signup for a free account at http://newrelic.com
15
+ * You need a host to install the plugin on that is able to poll the desired Samba server. That host also needs Ruby (tested with 1.8.7, 1.9.3), and support for rubygems.
16
+
17
+ ### Instructions for running the Samba agent
18
+
19
+ 1. Install this gem from RubyGems:
20
+
21
+ `sudo gem install newrelic_samba_agent`
22
+
23
+ 2. Install config, execute
24
+
25
+ `sudo newrelic_samba_agent install` - it will create `/etc/newrelic/newrelic_samba_agent.yml` file for you.
26
+
27
+ 3. Edit the `/etc/newrelic/newrelic_samba_agent.yml` file generated in step 2.
28
+
29
+ 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.
30
+
31
+ 3.2. replace the agent name 'samba' to any unique instance name of choice
32
+
33
+ 3.3. replace the path of smbstatus if needed
34
+
35
+ 4. Execute
36
+
37
+ `newrelic_samba_agent run`
38
+
39
+ 5. Go back to the Plugins list and after a brief period you will see the Samba Plugin listed in your New Relic account
40
+
41
+
42
+ ## Keep this process running
43
+
44
+ You can use services like these to manage this process and run it as a daemon.
45
+
46
+ - [Upstart](http://upstart.ubuntu.com/)
47
+ - [Systemd](http://www.freedesktop.org/wiki/Software/systemd/)
48
+ - [Runit](http://smarden.org/runit/)
49
+ - [Monit](http://mmonit.com/monit/)
50
+
51
+ ## Support
52
+
53
+ Please use Github issues for support.
@@ -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_samba_agent"
6
+ require 'optparse'
7
+
8
+ NewRelic::Plugin::Config.config_file = "/etc/newrelic/newrelic_samba_agent.yml"
9
+
10
+ options = OptionParser.new do |opts|
11
+ opts.banner = <<-EOF
12
+ Usage:
13
+ newrelic_samba_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_samba_agent install' for setup config"
43
+ exit 1
44
+ end
45
+ NewRelicSambaAgent.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_samba_agent.yml", "w") do | io |
52
+ io.write(config_file)
53
+ end
54
+ puts "Saved agent config file #{File.expand_path("/etc/newrelic/newrelic_samba_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: 'YOUR_LICENSE_KEY_HERE'
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
+ samba:
20
+ smbstatus_path : "/usr/bin/smbstatus"
@@ -0,0 +1,65 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ #
4
+ # This is a NewRelic agent which pushes Samba information.
5
+ #
6
+
7
+ require "rubygems"
8
+ require "bundler/setup"
9
+ require "newrelic_plugin"
10
+
11
+ module NewRelicSambaAgent
12
+ class Agent < NewRelic::Plugin::Agent::Base
13
+ agent_guid "com.kangacoders.samba"
14
+ agent_version "0.1.0"
15
+ agent_config_options :smbstatus_path
16
+ agent_human_labels("Samba Agent") { ident }
17
+
18
+ attr_reader :ident
19
+
20
+ def poll_cycle
21
+ [:total_users, :total_machines, :total_files, :average_files_user].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(/^\d+\s+(.+)$/).map{|_x| _x[0].split(/\s+/)[_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(smb_cmd(:process), 0) }],
39
+ :total_machines => ["Total/Machines", "Machines", lambda{get_columns(smb_cmd(:process), 3) }],
40
+ :total_files => ["Total/Files", "Files", lambda{ get_columns(smb_cmd(:locks), 0, false) }],
41
+ :average_files_user => ["Average/Files_User", "Files/User", lambda{get_columns(smb_cmd(:locks), 0, false) / get_columns(smb_cmd(:process), 0) rescue 0}],
42
+ }
43
+ metrics[_type]
44
+ end
45
+
46
+ def smb_cmd(_type)
47
+ case _type
48
+ when :process
49
+ param = "-p"
50
+ when :locks
51
+ param = "-L"
52
+ end
53
+ `#{smbstatus_path} #{param}`
54
+ end
55
+
56
+ end
57
+
58
+ def self.run
59
+ NewRelic::Plugin::Config.config.agents.keys.each do |_agent|
60
+ NewRelic::Plugin::Setup.install_agent _agent, NewRelicSambaAgent
61
+ end
62
+
63
+ NewRelic::Plugin::Run.setup_and_run
64
+ end
65
+ end
@@ -0,0 +1,79 @@
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_samba_agent'
16
+ s.version = '0.1.0'
17
+ s.date = '2013-12-11'
18
+ # s.rubyforge_project = 'newrelic_samba_agent'
19
+
20
+ ## Make sure your summary is short. The description may be as long
21
+ ## as you like.
22
+ s.summary = "New Relic samba monitoring plugin"
23
+ s.description = <<-EOF
24
+ This is the New Relic plugin for monitoring Samba 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_samba_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
+ ## List your runtime dependencies here. Runtime dependencies are those
51
+ ## that are needed for an end user to actually USE your code.
52
+ s.add_dependency('bundler')
53
+ s.add_dependency('newrelic_plugin', "1.0.3")
54
+
55
+ s.post_install_message = <<-EOF
56
+ To get started with this plugin, do
57
+ newrelic_samba_agent -h
58
+ to find out how to install and run the plugin agent.
59
+ EOF
60
+
61
+ ## Leave this section as-is. It will be automatically generated from the
62
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
63
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
64
+ # = MANIFEST =
65
+ s.files = %w[
66
+ Gemfile
67
+ README.md
68
+ Rakefile
69
+ bin/newrelic_samba_agent
70
+ config/template_newrelic_plugin.yml
71
+ lib/newrelic_samba_agent.rb
72
+ newrelic_samba_agent.gemspec
73
+ ]
74
+ # = MANIFEST =
75
+
76
+ ## Test files will be grabbed from the file list. Make sure the path glob
77
+ ## matches what you actually use.
78
+ s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
79
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: newrelic_samba_agent
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Kai De Sutter
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-12-11 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: bundler
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: newrelic_plugin
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - "="
41
+ - !ruby/object:Gem::Version
42
+ hash: 17
43
+ segments:
44
+ - 1
45
+ - 0
46
+ - 3
47
+ version: 1.0.3
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ description: |
51
+ This is the New Relic plugin for monitoring Samba developed by KangaCoders Ltd.
52
+
53
+ email: kdesutter@kangacoders.com
54
+ executables:
55
+ - newrelic_samba_agent
56
+ extensions: []
57
+
58
+ extra_rdoc_files:
59
+ - README.md
60
+ files:
61
+ - Gemfile
62
+ - README.md
63
+ - Rakefile
64
+ - bin/newrelic_samba_agent
65
+ - config/template_newrelic_plugin.yml
66
+ - lib/newrelic_samba_agent.rb
67
+ - newrelic_samba_agent.gemspec
68
+ homepage: http://www.kangacoders.com/
69
+ licenses: []
70
+
71
+ post_install_message: " To get started with this plugin, do\n newrelic_samba_agent -h\n to find out how to install and run the plugin agent.\n"
72
+ rdoc_options:
73
+ - --charset=UTF-8
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ requirements: []
95
+
96
+ rubyforge_project:
97
+ rubygems_version: 1.8.25
98
+ signing_key:
99
+ specification_version: 2
100
+ summary: New Relic samba monitoring plugin
101
+ test_files: []
102
+