Class: Fluent::MailOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_mail.rb

Instance Method Summary collapse

Constructor Details

#initializeMailOutput

Returns a new instance of MailOutput.



70
71
72
73
74
# File 'lib/fluent/plugin/out_mail.rb', line 70

def initialize
  super
  require 'net/smtp'
  require 'string/scrub' if RUBY_VERSION.to_f < 2.1
end

Instance Method Details

#configure(conf) ⇒ Object



76
77
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
132
133
# File 'lib/fluent/plugin/out_mail.rb', line 76

def configure(conf)
  super

  @out_keys = @out_keys.split(',')
  @message_out_keys = @message_out_keys.split(',')
  @subject_out_keys = @subject_out_keys.split(',')

  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| create_formatted_message(tag, time, record) }
  else
    # The default uses the old `key=value` format for old version compatibility
    @create_message_proc = Proc.new {|tag, time, record| create_key_value_message(tag, time, record) }
  end

  if @to_key or @cc_key or @bcc_key
    @process_event_stream_proc = Proc.new {|tag, es|
      messages = []
      subjects = []
      dests = []

      es.each do |time, record|
        messages << @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

      [messages, subjects, dests]
    }
  else
    @process_event_stream_proc = Proc.new {|tag, es|
      messages = []
      subjects = []
      dests = []

      es.each do |time, record|
        messages << @create_message_proc.call(tag, time, record)
        subjects << create_formatted_subject(tag, time, record)
      end

      [messages, 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



281
282
283
284
285
286
287
288
289
# File 'lib/fluent/plugin/out_mail.rb', line 281

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



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/fluent/plugin/out_mail.rb', line 176

def create_formatted_message(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 = (@message % values)
  with_scrub(message) {|str| str.gsub(/\\n/, "\n") }
end

#create_formatted_subject(tag, time, record) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/fluent/plugin/out_mail.rb', line 194

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



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/fluent/plugin/out_mail.rb', line 159

def create_key_value_message(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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/fluent/plugin/out_mail.rb', line 141

def emit(tag, es, chain)
  messages, subjects, dests = @process_event_stream_proc.call(tag, es)

  messages.each_with_index do |message, i|
    subject = subjects[i]
    dest = dests[i]
    begin
      sendmail(subject, message, dest)
    rescue => e
      log.warn "out_mail: failed to send notice to #{@host}:#{@port}, subject: #{subject}, message: #{message}, " <<
        "error_class: #{e.class}, error_message: #{e.message}, error_backtrace: #{e.backtrace.first}"
    end
  end

  chain.next
end

#format_time(time, time_format) ⇒ Object



254
255
256
257
258
259
260
261
# File 'lib/fluent/plugin/out_mail.rb', line 254

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



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
251
252
# File 'lib/fluent/plugin/out_mail.rb', line 211

def sendmail(subject, msg, dest = nil)
  smtp = Net::SMTP.new(@host, @port)

  if @user and @password
    smtp_auth_option = [@domain, @user, @password, :plain]
    smtp.enable_starttls if @enable_starttls_auto
    smtp.enable_tls if @enable_tls
    smtp.start(@domain,@user,@password,:plain)
  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 = "Date: \#{date}\nFrom: \#{@from}\nTo: \#{to}\nCc: \#{cc}\nBcc: \#{bcc}\nSubject: \#{subject}\nMessage-Id: \#{mid}\nMime-Version: 1.0\nContent-Type: \#{@content_type}\n\n\#{body}\n"
  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

#shutdownObject



138
139
# File 'lib/fluent/plugin/out_mail.rb', line 138

def shutdown
end

#startObject



135
136
# File 'lib/fluent/plugin/out_mail.rb', line 135

def start
end

#with_scrub(string) ⇒ Object



270
271
272
273
274
275
276
277
278
279
# File 'lib/fluent/plugin/out_mail.rb', line 270

def with_scrub(string)
  begin
    return yield(string)
  rescue ArgumentError => e
    raise e unless e.message.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



263
264
265
266
267
268
# File 'lib/fluent/plugin/out_mail.rb', line 263

def with_timezone(tz)
  oldtz, ENV['TZ'] = ENV['TZ'], tz
  yield
ensure
  ENV['TZ'] = oldtz
end