diff --git a/newrelic_resque_agent/README.md b/newrelic_resque_agent/README.md index 40f99b9..b932347 100644 --- a/newrelic_resque_agent/README.md +++ b/newrelic_resque_agent/README.md @@ -2,6 +2,7 @@ The New Relic Resque Plugin enables monitoring of Resque, a library for processing background jobs, reporting the following data for a specified instance: +* Dynamicly monitors all namespaces on redis server OR select only a few. * Number of working workers * Pending jobs number * Total failed jobs number @@ -32,6 +33,8 @@ The Resque monitoring Plugin for New Relic requires the following: 3.2. add the Redis connection string: 'hostname:port' or 'hostname:port:db' or 'redis://user:password@hostname:port:db' + 3.3 If you would like the plugin to monitor all the namespaces on a particular redis server, set the agent name to "dynamic" + 4. Execute `newrelic_resque_agent run` diff --git a/newrelic_resque_agent/bin/newrelic_resque_agent b/newrelic_resque_agent/bin/newrelic_resque_agent old mode 100644 new mode 100755 index b834062..099c98b --- a/newrelic_resque_agent/bin/newrelic_resque_agent +++ b/newrelic_resque_agent/bin/newrelic_resque_agent @@ -4,9 +4,53 @@ $stdout.sync = true $LOAD_PATH.unshift File.expand_path "../../lib", __FILE__ require "newrelic_resque_agent" require 'optparse' +require 'resque' +require 'redis' +require 'yaml' + +# Register and run the agent NewRelic::Plugin::Config.config_file = "/etc/newrelic/newrelic_resque_agent.yml" +def make_dynamic_file + agents = NewRelic::Plugin::Config.config.agents.collect do |agent_key,agent| + + if agent_key == "dynamic" + + if agent['redis'].nil? + raise "Redis connection URL " + end + + Resque.redis = agent['redis'] + + # get a list of namespaces + if agent['namespace'].nil? + resque_namespaces = Resque.redis.keys("*:queues").map { |key| key.gsub(/:queues$/, '').prepend("resque:") } + else + resque_namespaces = [agent['namespace']].flatten + end + + + resque_namespaces.each_with_object({}) do |rn, hsh| + clone = agent.clone + clone['namespace'] = rn + rn = rn.match(/resque:(.*):.*/)[1].downcase + hsh[rn] = clone + end + + else + {agent_key => agent} + end + end + + new_agents = agents.reduce({},:merge) + new_agents.delete('dynamic') + new_config = NewRelic::Plugin::Config.config.options + new_config['agents'] = new_agents + YAML.dump(new_config) +end + + options = OptionParser.new do |opts| opts.banner = <<-EOF Usage: @@ -42,6 +86,11 @@ if args.first == "run" puts "Run 'sudo newrelic_resque_agent install' for setup config" exit 1 end + + res = make_dynamic_file.to_s + NewRelic::Plugin::Config.config_file = nil + NewRelic::Plugin::Config.config_yaml = res + NewRelicResqueAgent.run elsif args.first == "install" config_file = File.read(File.expand_path("../../config/newrelic_plugin.yml.example", __FILE__)) diff --git a/newrelic_resque_agent/config/newrelic_plugin.yml.example b/newrelic_resque_agent/config/newrelic_plugin.yml.example index f8f8f9c..c91379a 100644 --- a/newrelic_resque_agent/config/newrelic_plugin.yml.example +++ b/newrelic_resque_agent/config/newrelic_plugin.yml.example @@ -18,9 +18,10 @@ newrelic: verbose: 0 # # Agent Configuration: -# +# setting the agent name to "dynamic" will make one agent per namespace. + agents: - my_resque_1: + my_resque_1: # <= this agent name could be dynamic # Redis connection string: 'hostname:port' or 'hostname:port:db' or 'redis://user:password@hostname:port:db' redis: localhost:6379 # Resque namespace diff --git a/newrelic_resque_agent/lib/newrelic_resque_agent.rb b/newrelic_resque_agent/lib/newrelic_resque_agent.rb index d03ad37..1beeb38 100644 --- a/newrelic_resque_agent/lib/newrelic_resque_agent.rb +++ b/newrelic_resque_agent/lib/newrelic_resque_agent.rb @@ -9,7 +9,7 @@ module NewRelicResqueAgent - VERSION = '1.0.1' + VERSION = '1.0.2' class Agent < NewRelic::Plugin::Agent::Base @@ -42,8 +42,8 @@ def poll_cycle report_metric "Jobs/Rate/Failed", "Jobs/Second", @total_failed.process(info[:failed]) report_metric "Queues", "Queues", info[:queues] report_metric "Jobs/Failed", "Jobs", info[:failed] || 0 - - + + rescue Redis::TimeoutError raise 'Redis server timeout' diff --git a/newrelic_resque_agent/newrelic_resque_agent.gemspec b/newrelic_resque_agent/newrelic_resque_agent.gemspec index ba6d0e1..b3137ba 100644 --- a/newrelic_resque_agent/newrelic_resque_agent.gemspec +++ b/newrelic_resque_agent/newrelic_resque_agent.gemspec @@ -13,7 +13,7 @@ Gem::Specification.new do |s| ## If your rubyforge_project name is different, then edit it and comment out ## the sub! line in the Rakefile s.name = 'newrelic_resque_agent' - s.version = '1.0.1' + s.version = '1.0.2' s.date = '2013-07-01' # s.rubyforge_project = 'newrelic_resque_agent'