Class: LogStash::Inputs::CloudWatch

Inherits:
Base
  • Object
show all
Includes:
PluginMixins::AwsConfig::V2
Defined in:
lib/logstash/inputs/cloudwatch.rb

Overview

Pull events from the Amazon Web Services CloudWatch API.

To use this plugin, you must have an AWS account, and the following policy.

Typically, you should setup an IAM policy, create a user and apply the IAM policy to the user.

A sample policy for EC2 metrics is as follows:

source,json

{

"Version": "2012-10-17",
"Statement": [
    {
        "Sid": "Stmt1444715676000",
        "Effect": "Allow",
        "Action": [
            "cloudwatch:GetMetricStatistics",
            "cloudwatch:ListMetrics"
        ],
        "Resource": "*"
    },
    {
        "Sid": "Stmt1444716576170",
        "Effect": "Allow",
        "Action": [
            "ec2:DescribeInstances"
        ],
        "Resource": "*"
    }
]

}

See aws.amazon.com/iam/ for more details on setting up AWS identities.

# Configuration Example

source, ruby

input {

cloudwatch {
  namespace => "AWS/EC2"
  metrics => [ "CPUUtilization" ]
  filters => { "tag:Group" => "API-Production" }
  region => "us-east-1"
}

}

input {

cloudwatch {
  namespace => "AWS/EBS"
  metrics => ["VolumeQueueLength"]
  filters => { "tag:Monitoring" => "Yes" }
  region => "us-east-1"
}

}

input {

cloudwatch {
  namespace => "AWS/RDS"
  metrics => ["CPUUtilization", "CPUCreditUsage"]
  filters => { "EngineName" => "mysql" } # Only supports EngineName, DatabaseClass and DBInstanceIdentifier
  region => "us-east-1"
}

}

Instance Method Summary collapse

Instance Method Details

#aws_service_endpoint(region) ⇒ Object



124
125
126
# File 'lib/logstash/inputs/cloudwatch.rb', line 124

def aws_service_endpoint(region)
  { region: region }
end

#filters_required?(namespace) ⇒ Boolean

def register

Returns:

  • (Boolean)


136
137
138
139
140
141
142
143
# File 'lib/logstash/inputs/cloudwatch.rb', line 136

def filters_required?(namespace)
  case namespace
  when 'AWS/EC2'
    false
  else
    true
  end
end

#registerObject



128
129
130
131
132
133
134
# File 'lib/logstash/inputs/cloudwatch.rb', line 128

def register
  raise 'Interval needs to be higher than period' unless @interval >= @period
  raise 'Interval must be divisible by period' unless @interval % @period == 0
  raise "Filters must be defined for when using #{@namespace} namespace" if @filters.nil? && filters_required?(@namespace)

  @last_check = Time.now
end

#run(queue) ⇒ Object

Runs the poller to get metrics for the provided namespace

Parameters:

  • queue (Array)

    Logstash queue



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/logstash/inputs/cloudwatch.rb', line 148

def run(queue)
  while !stop?
    start = Time.now

    @logger.info('Polling CloudWatch API')

    raise 'No metrics to query' unless metrics_for(@namespace).count > 0

    # For every metric
    metrics_for(@namespace).each do |metric|
      @logger.debug "Polling metric #{metric}"
      if @filters.nil?
        from_resources(queue, metric)
      else
        @logger.debug "Filters: #{aws_filters}"
        @combined ? from_filters(queue, metric) : from_resources(queue, metric)
      end
    end
    sleep_for = @interval - (Time.now - start)
    Stud.stoppable_sleep(sleep_for) { stop? } if sleep_for > 0
  end # loop
end