Class: Fluent::ForwardAWSInput
- Inherits:
-
Input
- Object
- Input
- Fluent::ForwardAWSInput
- Includes:
- HandleTagNameMixin
- Defined in:
- lib/fluent/plugin/in_forward_aws.rb
Instance Method Summary collapse
- #configure(conf) ⇒ Object
-
#format_stream(tag, es) ⇒ Object
Workaround for HandleTagNameMixin bug github.com/fluent/fluentd/pull/109.
-
#initialize ⇒ ForwardAWSInput
constructor
A new instance of ForwardAWSInput.
- #process(notification) ⇒ Object
- #run ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ ForwardAWSInput
Returns a new instance of ForwardAWSInput.
18 19 20 21 22 23 24 25 |
# File 'lib/fluent/plugin/in_forward_aws.rb', line 18 def initialize super require 'aws-sdk' require 'zlib' require 'tempfile' require 'json' @locker = Mutex::new end |
Instance Method Details
#configure(conf) ⇒ Object
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 87 88 89 90 91 92 93 94 |
# File 'lib/fluent/plugin/in_forward_aws.rb', line 54 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.() 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 |
#format_stream(tag, es) ⇒ Object
Workaround for HandleTagNameMixin bug github.com/fluent/fluentd/pull/109
8 9 10 11 12 13 14 15 16 |
# File 'lib/fluent/plugin/in_forward_aws.rb', line 8 def format_stream(tag, es) out = '' es.each {|time,record| tag_temp = String.new(tag) filter_record(tag_temp, time, record) out << format(tag_temp, time, record) } out end |
#process(notification) ⇒ Object
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 204 205 206 207 208 209 210 211 212 |
# File 'lib/fluent/plugin/in_forward_aws.rb', line 156 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 # Apply HandleTagNameMixin manually filter_record(tag, time, record) router.emit(tag,time,record) } } return true rescue => e if(e. == "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 |
#run ⇒ Object
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 147 148 149 150 151 152 153 154 |
# File 'lib/fluent/plugin/in_forward_aws.rb', line 105 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.({ :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..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 |
#shutdown ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/fluent/plugin/in_forward_aws.rb', line 214 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 |
#start ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/fluent/plugin/in_forward_aws.rb', line 96 def start super init_aws_s3_bucket() init_aws_sqs_queue() if(@start_thread) @thread = Thread.new(&method(:run)) unless @thread end end |