newrelic_resque_agent 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
@@ -0,0 +1,59 @@
1
+ ## New Relic Resque monitoring Plugin
2
+
3
+ The New Relic Resque Plugin enables monitoring of Resque, a library for processing background jobs, reporting the following data for a specified instance:
4
+
5
+ * Number of working workers
6
+ * Pending jobs number
7
+ * Total failed jobs number
8
+ * Queues number
9
+ * Number of workers
10
+ * Number of processed jobs
11
+
12
+ ### Requirements
13
+
14
+ The Resque monitoring Plugin for New Relic requires the following:
15
+
16
+ * A New Relic account. Signup for a free account at http://newrelic.com
17
+ * You need a host to install the plugin on that is able to poll the desired Redis server. That host also needs Ruby (tested with 1.8.7, 1.9.3), and support for rubygems.
18
+
19
+ ### Instructions for running the Resque agent
20
+
21
+ 1. Install this gem from RubyGems:
22
+
23
+ `sudo gem install newrelic_resque_agent`
24
+
25
+ 2. Install config, execute
26
+
27
+ `sudo newrelic_resque_agent install` - it will create `/etc/newrelic/newrelic_resque_agent.yml` file for you.
28
+
29
+ 3. Edit the `/etc/newrelic/newrelic_resque_agent.yml` file generated in step 2.
30
+
31
+ 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.
32
+
33
+ 3.2. add the Redis connection string: 'hostname:port' or 'hostname:port:db' or 'redis://user:password@hostname:port:db'
34
+
35
+ 4. Execute
36
+
37
+ `newrelic_resque_agent run`
38
+
39
+ 5. Go back to the Plugins list and after a brief period you will see the Resque 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
+ Also you can use [foreman](https://github.com/ddollar/foreman) for daemonization.
52
+
53
+ Foreman can be useful if you want to use [Heroku](https://www.heroku.com/) for run your agent. Just add Procfile and push to Heroku.
54
+
55
+ `monitor_daemon: newrelic_resque_agent run -c config/newrelic_plugin.yml`
56
+
57
+ ## Support
58
+
59
+ 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_resque_agent"
6
+ require 'optparse'
7
+
8
+ NewRelic::Plugin::Config.config_file = "/etc/newrelic/newrelic_resque_agent.yml"
9
+
10
+ options = OptionParser.new do |opts|
11
+ opts.banner = <<-EOF
12
+ Usage:
13
+ newrelic_resque_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_resque_agent install' for setup config"
43
+ exit 1
44
+ end
45
+ NewRelicResqueAgent.run
46
+ elsif args.first == "install"
47
+ config_file = File.read(File.expand_path("../../config/newrelic_plugin.yml.example", __FILE__))
48
+
49
+ require 'fileutils'
50
+ FileUtils.mkdir_p "/etc/newrelic"
51
+ File.open("/etc/newrelic/newrelic_resque_agent.yml", "w") do | io |
52
+ io.write(config_file)
53
+ end
54
+ puts "Saved agent config file #{File.expand_path("/etc/newrelic/newrelic_resque_agent.yml")}"
55
+ else
56
+ puts options
57
+ end
@@ -0,0 +1,38 @@
1
+ #
2
+ #
3
+ # This is a sample newrelic_plugin.yml file. Please move this file
4
+ # to the following location if it is not already there:
5
+ #
6
+ # ./config/newrelic_plugin.yml
7
+ #
8
+ # Where the current directory is the directory where your main program resides and is your current
9
+ # directory when you run the main program.
10
+ #
11
+ # Please make sure to update the license_key information with the license key for your New Relic
12
+ # account.
13
+ #
14
+ #
15
+ newrelic:
16
+ #
17
+ # Update with your New Relic account license key:
18
+ #
19
+ license_key: 'YOUR_LICENSE_KEY_HERE'
20
+ #
21
+ # Set to '1' for verbose output, remove for normal output.
22
+ # All output goes to stdout/stderr.
23
+ #
24
+ verbose: 1
25
+ #
26
+ # Agent Configuration:
27
+ #
28
+ agents:
29
+ my_resque_1:
30
+ # Redis connection string: 'hostname:port' or 'hostname:port:db' or 'redis://user:password@hostname:port:db'
31
+ redis: localhost:6379
32
+ # Resque namespace
33
+ namespace:
34
+ my_resque_2:
35
+ # Redis connection string: 'hostname:port' or 'hostname:port:db' or 'redis://user:password@hostname:port:db'
36
+ redis: localhost:6379
37
+ # Resque namespace
38
+ namespace:
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rubygems"
4
+ require "bundler/setup"
5
+ require "newrelic_plugin"
6
+
7
+ require 'resque'
8
+ require 'redis'
9
+
10
+ module NewRelicResqueAgent
11
+
12
+ VERSION = '1.0.0'
13
+
14
+ class Agent < NewRelic::Plugin::Agent::Base
15
+
16
+ agent_guid "com.railsware.resque"
17
+ agent_config_options :redis, :namespace
18
+ agent_version NewRelicResqueAgent::VERSION
19
+ agent_human_labels("Resque") { ident }
20
+
21
+ attr_reader :ident
22
+
23
+ def setup_metrics
24
+ @total_failed = NewRelic::Processor::EpochCounter.new
25
+ @processed = NewRelic::Processor::EpochCounter.new
26
+ end
27
+
28
+ def poll_cycle
29
+ if redis.nil?
30
+ raise "Redis connection URL "
31
+ end
32
+
33
+ begin
34
+ Resque.redis = redis
35
+ Resque.redis.namespace = namespace unless namespace.nil?
36
+ info = Resque.info
37
+
38
+ report_metric "Workers/Working", "Workers", info[:working]
39
+ report_metric "Workers/Total", "Workers", info[:workers]
40
+ report_metric "Jobs/Pending", "Jobs", info[:pending]
41
+ report_metric "Jobs/Rate/Processed", "Jobs/Second", @processed.process(info[:processed])
42
+ report_metric "Jobs/Rate/Failed", "Jobs/Second", @total_failed.process(info[:failed])
43
+ report_metric "Queues", "Queues", info[:queues]
44
+ report_metric "Jobs/Failed", "Jobs", info[:failed] || 0
45
+
46
+
47
+
48
+ rescue Redis::TimeoutError
49
+ raise 'Redis server timeout'
50
+ rescue Redis::CannotConnectError, Redis::ConnectionError
51
+ raise 'Could not connect to redis'
52
+ rescue Errno::ECONNRESET
53
+ raise 'Connection was reset by peer'
54
+ end
55
+ end
56
+
57
+ end
58
+
59
+ # Register and run the agent
60
+ def self.run
61
+ NewRelic::Plugin::Config.config.agents.keys.each do |agent|
62
+ NewRelic::Plugin::Setup.install_agent agent, NewRelicResqueAgent
63
+ end
64
+
65
+ #
66
+ # Launch the agent (never returns)
67
+ #
68
+ NewRelic::Plugin::Run.setup_and_run
69
+ end
70
+
71
+ 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_resque_agent'
16
+ s.version = '1.0.0'
17
+ s.date = '2013-07-01'
18
+ # s.rubyforge_project = 'newrelic_resque_agent'
19
+
20
+ ## Make sure your summary is short. The description may be as long
21
+ ## as you like.
22
+ s.summary = "New Relic Resque monitoring plugin"
23
+ s.description = <<-EOF
24
+ This is the New Relic plugin for monitoring Resque developed by Railsware Inc.
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 = ["Maxim Bondaruk"]
31
+ s.email = 'maxim.bondaruk@railsware.com'
32
+ s.homepage = 'https://github.com/railsware/newrelic_platform_plugins/tree/master/newrelic_resque_agent'
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_resque_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('newrelic_plugin', "1.0.3")
53
+ s.add_dependency('resque', ">= 1.24.1")
54
+ s.add_dependency('redis', ">= 3.0.4")
55
+
56
+
57
+ s.post_install_message = <<-EOF
58
+ To get started with this plugin, do
59
+ newrelic_resque_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_resque_agent
72
+ config/newrelic_plugin.yml.example
73
+ lib/newrelic_resque_agent.rb
74
+ newrelic_resque_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,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: newrelic_resque_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Maxim Bondaruk
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: newrelic_plugin
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.3
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.0.3
30
+ - !ruby/object:Gem::Dependency
31
+ name: resque
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.24.1
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.24.1
46
+ - !ruby/object:Gem::Dependency
47
+ name: redis
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 3.0.4
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.4
62
+ description: ! 'This is the New Relic plugin for monitoring Resque developed by Railsware
63
+ Inc.
64
+
65
+ '
66
+ email: maxim.bondaruk@railsware.com
67
+ executables:
68
+ - newrelic_resque_agent
69
+ extensions: []
70
+ extra_rdoc_files:
71
+ - README.md
72
+ files:
73
+ - Gemfile
74
+ - README.md
75
+ - Rakefile
76
+ - bin/newrelic_resque_agent
77
+ - config/newrelic_plugin.yml.example
78
+ - lib/newrelic_resque_agent.rb
79
+ - newrelic_resque_agent.gemspec
80
+ homepage: https://github.com/railsware/newrelic_platform_plugins/tree/master/newrelic_resque_agent
81
+ licenses: []
82
+ post_install_message: ! " To get started with this plugin, do\n newrelic_resque_agent
83
+ -h\n to find out how to install and run the plugin agent.\n"
84
+ rdoc_options:
85
+ - --charset=UTF-8
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.24
103
+ signing_key:
104
+ specification_version: 2
105
+ summary: New Relic Resque monitoring plugin
106
+ test_files: []