class Riemann::Tools::S3Metrics

Public Instance Methods

base_metrics() click to toggle source
# File bin/riemann-s3-status, line 21
def base_metrics
  # get last 60 seconds
  start_time = (Time.now.utc - 3600 * 24 * 1).iso8601
  end_time = Time.now.utc.iso8601

  # The base query that all metrics would get
  metric_base = {
      "Namespace" => "AWS/S3",
      "StartTime" => start_time,
      "EndTime" => end_time,
      "Period" => 3600,
      "MetricName" => "NumberOfObjects",
  }

  metric_base
end
tick() click to toggle source
# File bin/riemann-s3-list, line 21
def tick
  if options[:fog_credentials_file]
    Fog.credentials_path = options[:fog_credentials_file]
    Fog.credential = options[:fog_credential].to_sym
    connection = Fog::Storage.new
  else
    if options[:aws_access] && options[:aws_secret]
      connection = Fog::Storage.new({
                                        :provider => "AWS",
                                        :aws_access_key_id => options[:aws_access],
                                        :aws_secret_access_key => options[:aws_secret],
                                        :region => options[:aws_region]
                                    })
    else
      connection = Fog::Storage.new({
                                        :provider => "AWS",
                                        :use_iam_profile => true,
                                        :region => options[:aws_region]
                                    })
    end
  end

  options[:buckets].each do |url|
    split = url.split('/')
    bucket = split[0]
    prefix = ""
    if (split[1])
      prefix = url[(split[0].length+1)..-1]
    end
    count = 0
    connection.directories.get(bucket, prefix: prefix).files.map do |file|
      count = count +1
      if (options[:max_objects]>0 && count>options[:max_objects])
        break
      end
    end
    if (options[:max_objects]>0 && count>options[:max_objects])
      event = event(url, "objectCount", count, "count was bigger than threshold #{options[:max_objects]}", "warning")
      report(event)
    else
      event = event(url, "objectCount", count, "All objects counted, threshold=#{options[:max_objects]}", "ok")
      report(event)
    end
  end
end

Private Instance Methods

event(bucket, label, metric, description, severity) click to toggle source
# File bin/riemann-s3-list, line 68
def event(bucket, label, metric, description, severity)
  event = {
      host: "bucket_#{bucket}",
      service: "s3.#{label}",
      ttl: 300,
      description: "#{bucket} #{description}",
      tags: ["s3_metrics"],
      metric: metric,
      state: severity
  }
end