bin/check-sqs-messages.rb in sensu-plugins-aws-2.1.1 vs bin/check-sqs-messages.rb in sensu-plugins-aws-2.2.0
- old
+ new
@@ -14,13 +14,13 @@
# DEPENDENCIES:
# gem: aws-sdk-v1
# gem: sensu-plugin
#
# USAGE:
-# #YELLOW
+# check-sqs-messages -q my_queue -a key -k secret -w 500 -c 1000
+# check-sqs-messages -p queue_prefix_ -a key -k secret -W 100 -C 50
#
-#
# NOTES:
#
# LICENSE:
# Copyright (c) 2013, Justin Lambert <jlambert@letsevenup.com>
# Released under the same terms as Sensu (the MIT license); see LICENSE
@@ -54,12 +54,18 @@
option :queue,
short: '-q SQS_QUEUE',
long: '--queue SQS_QUEUE',
description: 'The name of the SQS you want to check the number of messages for',
- required: true
+ default: ''
+ option :prefix,
+ short: '-p PREFIX',
+ long: '--prefix PREFIX',
+ description: 'The prefix of the queues you want to check the number of messages for',
+ default: ''
+
option :warn_over,
short: '-w WARN_OVER',
long: '--warnnum WARN_OVER',
description: 'Number of messages in the queue considered to be a warning',
default: -1,
@@ -94,16 +100,48 @@
end
def run
AWS.config aws_config
sqs = AWS::SQS.new
- messages = sqs.queues.named(config[:queue]).approximate_number_of_messages
- if (config[:crit_under] >= 0 && messages < config[:crit_under]) || (config[:crit_over] >= 0 && messages > config[:crit_over])
- critical "#{messages} message(s) in #{config[:queue]} queue"
- elsif (config[:warn_under] >= 0 && messages < config[:warn_under]) || (config[:warn_over] >= 0 && messages > config[:warn_over])
- warning "#{messages} message(s) in #{config[:queue]} queue"
+ if config[:prefix] == ''
+ if config[:queue] == ''
+ critical 'Error, either QUEUE or PREFIX must be specified'
+ end
+
+ messages = sqs.queues.named(config[:queue]).approximate_number_of_messages
+
+ if (config[:crit_under] >= 0 && messages < config[:crit_under]) || (config[:crit_over] >= 0 && messages > config[:crit_over])
+ critical "#{messages} message(s) in #{config[:queue]} queue"
+ elsif (config[:warn_under] >= 0 && messages < config[:warn_under]) || (config[:warn_over] >= 0 && messages > config[:warn_over])
+ warning "#{messages} message(s) in #{config[:queue]} queue"
+ else
+ ok "#{messages} messages in #{config[:queue]} queue"
+ end
else
- ok "#{messages} messages in #{config[:queue]} queue"
+ warn = false
+ crit = false
+ queues = []
+
+ sqs.queues.with_prefix(config[:prefix]).each do |q|
+ queue_name = q.arn.split(':').last
+ messages = q.approximate_number_of_messages
+
+ if (config[:crit_under] >= 0 && messages < config[:crit_under]) || (config[:crit_over] >= 0 && messages > config[:crit_over])
+ crit = true
+ queues << "#{messages} message(s) in #{queue_name} queue"
+ elsif (config[:warn_under] >= 0 && messages < config[:warn_under]) || (config[:warn_over] >= 0 && messages > config[:warn_over])
+ warn = true
+ queues << "#{messages} message(s) in #{queue_name} queue"
+ end
+ end
+
+ if crit
+ critical queues.to_s
+ elsif warn
+ warning queues.to_s
+ else
+ ok "All queues matching prefix '#{config[:prefix]}' OK"
+ end
end
end
end