Class: LogStash::Inputs::CloudWatch

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

Overview

Pull events from the Amazon Web Services CloudWatch API.

CloudWatch provides various metrics on EC2, EBS and SNS.

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 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.

Instance Method Summary collapse

Instance Method Details

#aws_service_endpoint(region) ⇒ Object



101
102
103
# File 'lib/logstash/inputs/cloudwatch.rb', line 101

def aws_service_endpoint(region)
  { region: region }
end

#registerObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/logstash/inputs/cloudwatch.rb', line 106

def register
  require "aws-sdk"
  AWS.config(:logger => @logger)

  if @instances
    raise LogStash::ConfigurationError, 'Should not specify both `instance_refresh` and `instances`' if @instance_refresh > 0
    raise LogStash::ConfigurationError, 'Should not specify both `tag_name` and `instances`' unless @tag_name.nil?
    raise LogStash::ConfigurationError, 'Should not specify both `tag_values` and `instances`' unless @tag_values.nil?
  else
    raise LogStash::ConfigurationError, 'Both `tag_name` and `tag_values` need to be specified if no `instances` are specified' if @tag_name.nil? || @tag_values.nil?
  end

  @cloudwatch = AWS::CloudWatch::Client.new(aws_options_hash)
  @ec2 = AWS::EC2::Client.new(aws_options_hash)
  @last_check = Time.now
end

#run(queue) ⇒ Object

def register



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/logstash/inputs/cloudwatch.rb', line 123

def run(queue)
  Stud.interval(@interval) do
    @logger.debug('Polling CloudWatch API')
    # Set up the instance_refresh check
    if @instance_refresh > 0 && (Time.now - @last_check) > @instance_refresh
      @instances = nil
      @last_check = Time.now
    end

    # Poll the instances
    instance_ids.each do |instance|
      metrics(instance).each do |metric|
        opts = options(metric, instance)
        @cloudwatch.get_metric_statistics(opts)[:datapoints].each do |dp|
          event = LogStash::Event.new(LogStash::Util.stringify_symbols(dp))
          event['@timestamp'] = LogStash::Timestamp.new(dp[:timestamp])
          event['metric'] = metric
          event['instance'] = instance
          @instance_tags[instance].each do |tag|
            event[tag[:key]] = tag[:value]
          end
          decorate(event)
          queue << event
        end
      end
    end
  end # loop
end