Class: Fluent::MailOutput
- Inherits:
-
Output
- Object
- Output
- Fluent::MailOutput
- Defined in:
- lib/fluent/plugin/out_mail.rb
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #create_dest_addr(dest_type, record) ⇒ Object
- #create_formatted_message(tag, time, record) ⇒ Object
- #create_formatted_subject(tag, time, record) ⇒ Object
-
#create_key_value_message(tag, time, record) ⇒ Object
The old ‘key=value` format for old version compatibility.
- #desc(description) ⇒ Object
- #emit(tag, es, chain) ⇒ Object
- #format_time(time, time_format) ⇒ Object
-
#initialize ⇒ MailOutput
constructor
A new instance of MailOutput.
- #sendmail(subject, msg, dest = nil) ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
- #with_scrub(string) ⇒ Object
- #with_timezone(tz) ⇒ Object
Constructor Details
#initialize ⇒ MailOutput
Returns a new instance of MailOutput.
72 73 74 75 76 |
# File 'lib/fluent/plugin/out_mail.rb', line 72 def initialize super require 'net/smtp' require 'string/scrub' if RUBY_VERSION.to_f < 2.1 end |
Instance Method Details
#configure(conf) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 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 |
# File 'lib/fluent/plugin/out_mail.rb', line 78 def configure(conf) super if @out_keys.empty? and @message.nil? raise Fluent::ConfigError, "Either 'message' or 'out_keys' must be specifed." end if @message begin @message % (['1'] * @message_out_keys.length) rescue ArgumentError raise Fluent::ConfigError, "string specifier '%s' of message and message_out_keys specification mismatch" end @create_message_proc = Proc.new {|tag, time, record| (tag, time, record) } else # The default uses the old `key=value` format for old version compatibility @create_message_proc = Proc.new {|tag, time, record| (tag, time, record) } end if @to_key or @cc_key or @bcc_key @process_event_stream_proc = Proc.new {|tag, es| = [] subjects = [] dests = [] es.each do |time, record| << @create_message_proc.call(tag, time, record) subjects << create_formatted_subject(tag, time, record) dests << %w(to cc bcc).each_with_object({}){|t, dest| dest[t] = create_dest_addr(t, record) } end [, subjects, dests] } else @process_event_stream_proc = Proc.new {|tag, es| = [] subjects = [] dests = [] es.each do |time, record| << @create_message_proc.call(tag, time, record) subjects << create_formatted_subject(tag, time, record) end [, subjects, dests] } end begin @subject % (['1'] * @subject_out_keys.length) rescue ArgumentError raise Fluent::ConfigError, "string specifier '%s' of subject and subject_out_keys specification mismatch" end end |
#create_dest_addr(dest_type, record) ⇒ Object
279 280 281 282 283 284 285 286 287 |
# File 'lib/fluent/plugin/out_mail.rb', line 279 def create_dest_addr(dest_type, record) addr = instance_variable_get(:"@#{dest_type}") dest_key = instance_variable_get(:"@#{dest_type}_key") if dest_key return record[dest_key] || addr else return addr end end |
#create_formatted_message(tag, time, record) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/fluent/plugin/out_mail.rb', line 174 def (tag, time, record) values = [] values = @message_out_keys.map do |key| case key when @time_key format_time(time, @time_format) when @tag_key tag else record[key].to_s end end = (@message % values) with_scrub() {|str| str.gsub(/\\n/, "\n") } end |
#create_formatted_subject(tag, time, record) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/fluent/plugin/out_mail.rb', line 192 def create_formatted_subject(tag, time, record) values = [] values = @subject_out_keys.map do |key| case key when @time_key format_time(time, @time_format) when @tag_key tag else record[key].to_s end end @subject % values end |
#create_key_value_message(tag, time, record) ⇒ Object
The old ‘key=value` format for old version compatibility
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/fluent/plugin/out_mail.rb', line 157 def (tag, time, record) values = [] values = @out_keys.map do |key| case key when @time_key format_time(time, @time_format) when @tag_key tag else "#{key}: #{record[key].to_s}" end end values.join("\n") end |
#desc(description) ⇒ Object
14 15 |
# File 'lib/fluent/plugin/out_mail.rb', line 14 def desc(description) end |
#emit(tag, es, chain) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/fluent/plugin/out_mail.rb', line 139 def emit(tag, es, chain) , subjects, dests = @process_event_stream_proc.call(tag, es) .each_with_index do |, i| subject = subjects[i] dest = dests[i] begin sendmail(subject, , dest) rescue => e log.warn "out_mail: failed to send notice to #{@host}:#{@port}, subject: #{subject}, message: #{}, " << "error_class: #{e.class}, error_message: #{e.}, error_backtrace: #{e.backtrace.first}" end end chain.next end |
#format_time(time, time_format) ⇒ Object
252 253 254 255 256 257 258 259 |
# File 'lib/fluent/plugin/out_mail.rb', line 252 def format_time(time, time_format) # Fluentd >= v0.12's TimeFormatter supports timezone, but v0.10 does not if @time_locale with_timezone(@time_locale) { Fluent::TimeFormatter.new(time_format, @localtime).format(time) } else Fluent::TimeFormatter.new(time_format, @localtime).format(time) end end |
#sendmail(subject, msg, dest = nil) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/fluent/plugin/out_mail.rb', line 209 def sendmail(subject, msg, dest = nil) smtp = Net::SMTP.new(@host, @port) if @user and @password smtp_auth_option = [@domain, @user, @password, @authtype.to_sym] smtp.enable_starttls if @enable_starttls_auto smtp.enable_tls if @enable_tls smtp.start(@domain, @user, @password, @authtype.to_sym) else smtp.start end subject = subject.force_encoding('binary') body = msg.force_encoding('binary') to = (dest && dest['to']) ? dest['to'] : @to cc = (dest && dest['cc']) ? dest['cc'] : @cc bcc = (dest && dest['bcc']) ? dest['bcc'] : @bcc # Date: header has timezone, so usually it is not necessary to set locale explicitly # But, for people who would see mail header text directly, the locale information may help something # (for example, they can tell the sender should live in Tokyo if +0900) date = format_time(Time.now, "%a, %d %b %Y %X %z") mid = sprintf("<%s@%s>", SecureRandom.uuid, SecureRandom.uuid) content = <<EOF Date: #{date} From: #{@from} To: #{to} Cc: #{cc} Bcc: #{bcc} Subject: #{subject} Message-Id: #{mid} Mime-Version: 1.0 Content-Type: #{@content_type} #{body} EOF response = smtp.send_mail(content, @from, to.split(/,/), cc.split(/,/), bcc.split(/,/)) log.debug "out_mail: content: #{content.gsub("\n", "\\n")}" log.debug "out_mail: email send response: #{response.string.chomp}" smtp.finish end |
#shutdown ⇒ Object
136 137 |
# File 'lib/fluent/plugin/out_mail.rb', line 136 def shutdown end |
#start ⇒ Object
133 134 |
# File 'lib/fluent/plugin/out_mail.rb', line 133 def start end |
#with_scrub(string) ⇒ Object
268 269 270 271 272 273 274 275 276 277 |
# File 'lib/fluent/plugin/out_mail.rb', line 268 def with_scrub(string) begin return yield(string) rescue ArgumentError => e raise e unless e..index("invalid byte sequence in") == 0 log.info "out_mail: invalid byte sequence is replaced in #{string}" string.scrub!('?') retry end end |
#with_timezone(tz) ⇒ Object
261 262 263 264 265 266 |
# File 'lib/fluent/plugin/out_mail.rb', line 261 def with_timezone(tz) oldtz, ENV['TZ'] = ENV['TZ'], tz yield ensure ENV['TZ'] = oldtz end |