class Riemann::Tools::AwsDynamoDB

Public Class Methods

new() click to toggle source

opt :metric, “Metric names with comma-separated warning/critical thresholds (eg., ‘CPUUtilization,50,90’). Multiple tuples can be set.”, :type => String, :multi => true

# File bin/riemann-aws-dynamodb, line 19
def initialize
  @only_tables = opts.fetch(:only_table)
  @ign_tables = opts.fetch(:ign_table)

  if opts.has_key?('access_key') and opts.has_key?('secret_key')
    creds = { :aws_access_key_id     => opts[:access_key],
              :aws_secret_access_key => opts[:secret_key] }
  else
    creds = { :use_iam_profile => true }
  end

  creds['region'] = opts[:region]
  @cloudwatch = Fog::AWS::CloudWatch.new(creds)
  @dynamodb = Fog::AWS::DynamoDB.new(creds)
end

Public Instance Methods

metrics_opts() click to toggle source
# File bin/riemann-aws-dynamodb, line 35
def metrics_opts
  {
    "ProvisionedReadCapacityUnits"  => { statistics: 'Sum', period: 300 },
    "ProvisionedWriteCapacityUnits" => { statistics: 'Sum', period: 300 },
    "ConsumedReadCapacityUnits"     => { statistics: 'Sum', period: 60 },
    "ConsumedWriteCapacityUnits"    => { statistics: 'Sum', period: 60 }
  }
end
tick() click to toggle source
# File bin/riemann-aws-dynamodb, line 44
def tick
  time = Time.new
  ddb = @dynamodb.list_tables
  @tables = ddb[:body]["TableNames"]

  if opts[:only_table_given]
    @tables &= @only_tables
  elsif opts[:ign_table_given]
    @tables -= @ign_tables
  end

  @tables.each do |table_name|
    metrics_opts.each_pair do |metric_name, opts|
      result = @cloudwatch.get_metric_statistics({
        "Namespace" => 'AWS/DynamoDB',
        "MetricName" => metric_name,
        "Statistics" => opts[:statistics],
        "Dimensions" => [{"Name" => "TableName", "Value" => table_name}],
        "StartTime" => (Time.now.utc - opts[:period]).iso8601,
        "EndTime" => Time.now.utc.iso8601,
        "Period" => opts[:period]
      })
      metric = (result[:body]["GetMetricStatisticsResult"]["Datapoints"][0] || {}).fetch(opts[:statistics], 0.0)

      msg = {
        metric: metric,
        service: "dynamodb #{table_name} #{metric_name} #{opts[:statistics]}",
        ttl: 120
      }

      report msg
    end
  end
end