bin/check-beanstalkd.rb in sensu-plugins-beanstalk-1.1.0 vs bin/check-beanstalkd.rb in sensu-plugins-beanstalk-1.2.0

- old
+ new

@@ -52,10 +52,18 @@ description: 'beanstalkd server port', short: '-p PORT', long: '--port PORT', default: '11300' + option :alert_on_missing, + description: 'alert type when tube is missing', + short: '-a TYPE', + long: '--alert-on-missing TYPE', + proc: proc { |value| value.to_sym }, + in: [:critical, :warning, :ignore], + default: :ignore + option :ready, description: 'ready tasks WARNING/CRITICAL thresholds', short: '-r W,C', long: '--ready-tasks W,C', proc: proc { |a| a.split(',', 2).map(&:to_i) }, @@ -83,46 +91,67 @@ end conn end def run - stats = acquire_beanstalkd_connection.tubes[config[:tube].to_s].stats + warns, crits, msg = check_queues(tube_stats) message 'All queues are healthy' - warns, crits, msg = check_queues(stats) - msg.join("\n") - - if crits.size > 0 # rubocop:disable Style/ZeroLengthPredicate + unless crits.empty? message msg critical end - if warns.size > 0 # rubocop:disable Style/ZeroLengthPredicate + unless warns.empty? message msg warning end + ok end + JOB_STATES = [:ready, :urgent, :buried].freeze + def check_queues(stats) msg = [] crits = {} warns = {} - [:ready, :urgent, :buried].each do |task| - tasks = stats.send("current_jobs_#{task}".to_sym) + JOB_STATES.each do |job_state| + jobs = stats.send("current_jobs_#{job_state}".to_sym) - if tasks > config[task][1] - crits[task] = tasks - msg << task.to_s + " queue has #{tasks} items" + if jobs > config[job_state][1] + crits[job_state] = jobs + msg << job_state.to_s + " queue has #{jobs} items" next end - if tasks > config[task][0] - warns[task] = tasks - msg << task.to_s + " queue has #{tasks} items" + if jobs > config[job_state][0] + warns[job_state] = jobs + msg << job_state.to_s + " queue has #{jobs} items" end end [warns, crits, msg] + end + + def tube_stats + acquire_beanstalkd_connection.tubes[config[:tube].to_s].stats + rescue Beaneater::NotFoundError + case config[:alert_on_missing] + when :warning, :critical + send(config[:alert_on_missing], "Tube #{config[:tube]} is missing") + else + empty_queue_stats + end + end + + def empty_queue_stats + stats = OpenStruct.new + + JOB_STATES.each do |job_state| + stats.send("current_jobs_#{job_state}=", 0) + end + + stats end end