Class: Fluent::ForwardAWSInput

Inherits:
Input
  • Object
show all
Includes:
ForwardAWSUtil
Defined in:
lib/fluent/plugin/in_forward_aws.rb

Instance Method Summary collapse

Methods included from ForwardAWSUtil

filtertag

Constructor Details

#initializeForwardAWSInput

Returns a new instance of ForwardAWSInput.



7
8
9
10
11
12
13
14
# File 'lib/fluent/plugin/in_forward_aws.rb', line 7

def initialize
  super
  require 'aws-sdk'
  require 'zlib'
  require 'tempfile'
  require 'json'
  @locker = Mutex::new
end

Instance Method Details

#configure(conf) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fluent/plugin/in_forward_aws.rb', line 46

def configure(conf)
  super
  if /^\s*$/ =~ @channel
    raise Fluent::ConfigError.new("channel is invalid. Exp=[\w]+")
  end
  unless @aws_access_key_id
    raise Fluent::ConfigError.new("aws_access_key_id is required")
  end
  unless @aws_secret_access_key
    raise Fluent::ConfigError.new("aws_secret_access_key is required")
  end
  unless @aws_s3_endpoint
    raise Fluent::ConfigError.new("aws_s3_endpoint is required")
  end
  unless @aws_s3_bucketname
    raise Fluent::ConfigError.new("aws_s3_bucketname is required")
  end
  unless @aws_sqs_endpoint
    raise Fluent::ConfigError.new("aws_sqs_endpoint is required")
  end
  unless @aws_sqs_queue_url
    raise Fluent::ConfigError.new("aws_sqs_queue_url is required")
  end
  unless(@aws_s3_skiptest)
    init_aws_s3_bucket()
    begin
      @bucket.objects[@aws_s3_testobjectname].write("TEST", :content_type => 'text/plain')
    rescue
      raise Fluent::ConfigError.new("Cannot put object to S3. Need s3:PutObject permission for resource arn:aws:s3:::" + @aws_s3_bucketname+"/*")
    end
  end
  unless(@aws_sqs_skiptest)
    init_aws_sqs_queue()
    begin
      @queue.receive_message() do |notification|
      end
    rescue => e
      raise Fluent::ConfigError.new("Cannot fetch queue from SQS. Need sqs:ReceiveMessage sqs:DeleteMessage permission for resource " + @aws_sqs_queue_url)
    end
  end
end

#process(notification) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/fluent/plugin/in_forward_aws.rb', line 148

def process(notification)
  if notification["type"] == "ping"
    # Ignore ping message
    return true
  end
  if notification["type"] == "out"
    # Silently ignore non matching logs
    if notification["bucketname"] != @aws_s3_bucketname
      $log.debug "Bucketname does not match. Ignoring"
      return true
    end
    if(@channelEnableRegEx)
      unless Regexp.new(@channel).match(notification["channel"])
        $log.debug "Channel RegEx does not match. Ignoring"
        return true
      end
    else
      unless @channel == notification["channel"]
        $log.debug "Channel does not match. Ignoring"
        return true
      end
    end
    tmpFile = Tempfile.new("forward-aws-")
    begin
      #Download log file to temporary file
      $log.debug "Download log object from S3 bucket #{@aws_s3_bucketname} path #{notification["path"]}"
      @bucket.objects[notification["path"]].read do |chunk|
        tmpFile.write(chunk)
      end
      tmpFile.close()
      #gunzip and decode log file
      streamUnpacker = MessagePack::Unpacker.new()
      Zlib::GzipReader.open(tmpFile) {|reader|
        streamUnpacker.feed(reader.read())
        streamUnpacker.each {|event|
          (tag, time, record) = event
          tag = ForwardAWSUtil.filtertag(tag,@add_tag_prefix,@remove_tag_prefix)
          Fluent::Engine.emit(tag,time,record)
        }
      }
      return true
    rescue => e
      if(e.message == "Access Denied")
        $log.warn "Access Denied for key #{notification["path"]}"
        # Object may have been deleted. Do not retry
        return true
      else
        $log.error e
      end
    ensure
      tmp.close(true) rescue nil
    end
  end
  # Unknown notification. Do not delete
  return false
end

#runObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/fluent/plugin/in_forward_aws.rb', line 97

def run
  @running = true
  while true
    $log.debug "Long Polling SQS for #{@aws_sqs_wait_time_seconds} secs with message limit #{@aws_sqs_limit}"
    notificationRaws = @queue.receive_message({
      :limit               => @aws_sqs_limit,
      :wait_time_seconds   => @aws_sqs_wait_time_seconds,
      :visibilitiy_timeout => @aws_sqs_visibilitiy_timeout
    })
    $log.debug "Polling finished"
    if(notificationRaws && !notificationRaws.instance_of?(Array))
      notificationRaws = [notificationRaws]
    end
    if notificationRaws && notificationRaws.size != 0
      $log.debug "Received #{notificationRaws.size} messages"
      notificationRaws.each{ |notificationRaw|
        notification = JSON.parse(notificationRaw.as_sns_message.body)
        $log.debug "Received Notification#{notification}"
        if(process(notification))
          if dry_run
            $log.info "Notification processed in dry-run mode #{notification}"
          else
            notificationRaw.delete()
            $log.debug "Deleted processed notification #{notification}"
          end
        else
          $log.error "Could not process notification, pending... #{notification}"
        end
      }
      @locker.synchronize do
        return unless @running
      end
      sleep @aws_sqs_process_interval if(@aws_sqs_process_interval > 0)
      @locker.synchronize do
        return unless @running
      end
      next
    end
    $log.debug "No messages in queue, sleep for #{@aws_sqs_monitor_interval} secs"
    @locker.synchronize do
      return unless @running
    end
    sleep @aws_sqs_monitor_interval
    @locker.synchronize do
      return unless @running
    end
  end
rescue => e
  puts e 
end

#shutdownObject



205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/fluent/plugin/in_forward_aws.rb', line 205

def shutdown
  super
  # Stop Thread and join
  @locker.synchronize do
    $log.debug "Stopping thread"
    @running = false
  end
  if(@thread)
    @thread.run
    @thread.join
    @thread = nil
  end
end

#startObject



88
89
90
91
92
93
94
95
# File 'lib/fluent/plugin/in_forward_aws.rb', line 88

def start
  super
  init_aws_s3_bucket()
  init_aws_sqs_queue()
  if(@start_thread)
    @thread = Thread.new(&method(:run)) unless @thread
  end
end