bin/check-dynamodb-throttle.rb in sensu-plugins-aws-2.1.0 vs bin/check-dynamodb-throttle.rb in sensu-plugins-aws-2.1.1

- old
+ new

@@ -10,11 +10,11 @@ # # PLATFORMS: # Linux # # DEPENDENCIES: -# gem: aws-sdk-v1 +# gem: aws-sdk # gem: sensu-plugin # # USAGE: # Critical if session table's read throttle is over 50 for the last 5 minutes # check-dynamodb-throttle --table_names session --throttle-for read --critical-over 50 --statistics sum --period 300 @@ -26,11 +26,11 @@ # Released under the same terms as Sensu (the MIT license); see LICENSE # for details. # require 'sensu-plugin/check/cli' -require 'aws-sdk-v1' +require 'aws-sdk' require 'time' class CheckDynamoDB < Sensu::Plugin::Check::CLI option :aws_access_key, short: '-a AWS_ACCESS_KEY', @@ -97,60 +97,71 @@ region: config[:aws_region] } end def dynamo_db - @dynamo_db ||= AWS::DynamoDB.new aws_config + @dynamo_db ||= Aws::DynamoDB::Client.new aws_config end def cloud_watch - @cloud_watch ||= AWS::CloudWatch.new aws_config + @cloud_watch ||= Aws::CloudWatch::Client.new aws_config end def tables return @tables if @tables - @tables = dynamo_db.tables.to_a - @tables.select! { |table| config[:table_names].include? table.name } if config[:table_names] + table_names = dynamo_db.list_tables.table_names.to_a + table_names.select! { |table_name| config[:table_names].include? table_name } if config[:table_names] + @tables = [] + table_names.each do |table_name| + @tables.push(dynamo_db.describe_table( + table_name: table_name + ).table) + end @tables end def cloud_watch_metric(metric_name, table_name) - cloud_watch.metrics.with_namespace('AWS/DynamoDB').with_metric_name(metric_name).with_dimensions(name: 'TableName', value: table_name).first - end - - def statistics_options - { + cloud_watch.get_metric_statistics( + namespace: 'AWS/DynamoDB', + metric_name: metric_name, + dimensions: [ + { + name: 'TableName', + value: table_name + } + ], start_time: config[:end_time] - config[:period], - end_time: config[:end_time], + end_time: config[:end_time], statistics: [config[:statistics].to_s.capitalize], - period: config[:period] - } + period: config[:period], + unit: 'Count' + ) end def latest_value(metric) - metric.statistics(statistics_options.merge unit: 'Count').datapoints.sort_by { |datapoint| datapoint[:timestamp] }.last[config[:statistics]] + metric.datapoints.sort_by { |datapoint| datapoint[:timestamp] }.last[config[:statistics]] end def flag_alert(severity, message) @severities[severity] = true @message += message end def check_throttle(table) config[:throttle_for].each do |r_or_w| metric_name = "#{r_or_w.to_s.capitalize}ThrottleEvents" - metric = cloud_watch_metric metric_name, table.name + metric = cloud_watch_metric metric_name, table.table_name metric_value = begin latest_value(metric) rescue 0 end @severities.keys.each do |severity| threshold = config[:"#{severity}_over"] next unless threshold next if metric_value < threshold - flag_alert severity, "; On table #{table.name} #{r_or_w.to_s.capitalize}ThrottleEvents is #{metric_value} (higher_than #{threshold})" + flag_alert severity, "; On table #{table.table_name} #{r_or_w.to_s.capitalize}ThrottleEvents is #{metric_value} (higher_than #{threshold})" break end end end