class Riemann::Tools::AWSBilling

Public Class Methods

new() click to toggle source
# File bin/riemann-aws-billing, line 22
def initialize
  if options[:fog_credentials_file]
    Fog.credentials_path = opts[:fog_credentials_file]
    Fog.credential = opts[:fog_credential].to_sym
    @cloudwatch = Fog::AWS::CloudWatch.new
  else
    if opts.has_key?('secret_key') and opts.has_key?('access_key')
      creds = {
        :aws_secret_access_key => opts[:secret_key],
        :aws_access_key_id => opts[:access_key]
      }
    else
      creds = { :use_iam_profile => true }
    end
    @cloudwatch = Fog::AWS::CloudWatch.new(creds)
  end
  @start_time = (Time.now.utc - opts[:time_start]).iso8601
  @end_time = (Time.now.utc - opts[:time_end]).iso8601
end

Public Instance Methods

tick() click to toggle source
# File bin/riemann-aws-billing, line 42
def tick
  opts[:services].each do |service|
    data = @cloudwatch.get_metric_statistics({
                                               'Statistics' => ["Maximum"],
                                               'StartTime' => @start_time,
                                               'EndTime' => @end_time,
                                               'Period' => 3600,
                                               'Unit' => "None",
                                               'MetricName' => "EstimatedCharges",
                                               'Namespace' => "AWS/Billing",
                                               'Dimensions' => [
                                                                {
                                                                  'Name' => "ServiceName",
                                                                  'Value' => service
                                                                },
                                                                {
                                                                  'Name' => "Currency",
                                                                  'Value' => "USD"
                                                                }
                                                               ]
                                             }).body['GetMetricStatisticsResult']['Datapoints']


    data.each do |metrics|
      name = "AWScloudwatch.Billing." + service
      value = metrics["Maximum"]
      timestamp = metrics["Timestamp"].to_i

      event = {
        host: nil,
        service: name,
        time: timestamp,
        description: "AWS Estimate Charges for #{service}",
        tags: ["aws_billing"],
        state: "ok",
        metric: value
      }

      report event
    end
  end
end