bin/metrics-sqs.rb in sensu-plugins-aws-2.1.1 vs bin/metrics-sqs.rb in sensu-plugins-aws-2.2.0
- old
+ new
@@ -1,8 +1,8 @@
#! /usr/bin/env ruby
#
-# sqs-metrics
+# metrics-sqs
#
# DESCRIPTION:
# Fetch SQS metrics
#
# OUTPUT:
@@ -14,11 +14,12 @@
# DEPENDENCIES:
# gem: aws-sdk-v1
# gem: sensu-plugin
#
# USAGE:
-# sqs-metrics -q my_queue -a key -k secret
+# metrics-sqs -q my_queue -a key -k secret
+# metrics-sqs -p queue_prefix_ -a key -k secret
#
# NOTES:
#
# LICENSE:
# Copyright 2015 Eric Heydrick <eheydrick@gmail.com>
@@ -32,12 +33,18 @@
class SQSMetrics < Sensu::Plugin::Metric::CLI::Graphite
option :queue,
description: 'Name of the queue',
short: '-q QUEUE',
long: '--queue QUEUE',
- required: true
+ default: ''
+ option :prefix,
+ description: 'Queue name prefix',
+ short: '-p PREFIX',
+ long: '--prefix PREFIX',
+ default: ''
+
option :scheme,
description: 'Metric naming scheme, text to prepend to metric',
short: '-s SCHEME',
long: '--scheme SCHEME',
default: ''
@@ -65,22 +72,36 @@
secret_access_key: config[:aws_secret_access_key],
region: config[:aws_region]
}
end
- def run
- scheme = if config[:scheme] == ''
- "aws.sqs.queue.#{config[:queue].tr('-', '_')}.message_count"
- else
- config[:scheme]
- end
+ def scheme(queue_name)
+ "aws.sqs.queue.#{queue_name.tr('-', '_')}.message_count"
+ end
+ def run
begin
sqs = AWS::SQS.new aws_config
- messages = sqs.queues.named(config[:queue]).approximate_number_of_messages
- output scheme, messages
+ if config[:prefix] == ''
+ if config[:queue] == ''
+ critical 'Error, either QUEUE or PREFIX must be specified'
+ end
+ scheme = if config[:scheme] == ''
+ scheme config[:queue]
+ else
+ config[:scheme]
+ end
+
+ messages = sqs.queues.named(config[:queue]).approximate_number_of_messages
+ output scheme, messages
+ else
+ sqs.queues.with_prefix(config[:prefix]).each do |q|
+ queue_name = q.arn.split(':').last
+ output scheme(queue_name), q.approximate_number_of_messages
+ end
+ end
rescue => e
critical "Error fetching SQS queue metrics: #{e.message}"
end
ok
end