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_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) ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
- #with_scrub(string) ⇒ Object
- #with_timezone(tz) ⇒ Object
Constructor Details
#initialize ⇒ MailOutput
Returns a new instance of MailOutput.
64 65 66 67 68 |
# File 'lib/fluent/plugin/out_mail.rb', line 64 def initialize super require 'net/smtp' require 'string/scrub' if RUBY_VERSION.to_f < 2.1 end |
Instance Method Details
#configure(conf) ⇒ Object
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 95 96 97 98 |
# File 'lib/fluent/plugin/out_mail.rb', line 70 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| (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 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_formatted_message(tag, time, record) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/fluent/plugin/out_mail.rb', line 147 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
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/fluent/plugin/out_mail.rb', line 165 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
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/fluent/plugin/out_mail.rb', line 130 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
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/fluent/plugin/out_mail.rb', line 106 def emit(tag, es, chain) = [] subjects = [] es.each {|time,record| << @create_message_proc.call(tag, time, record) subjects << create_formatted_subject(tag, time, record) } (0....size).each do |i| = [i] subject = subjects[i] begin sendmail(subject, ) 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
222 223 224 225 226 227 228 229 |
# File 'lib/fluent/plugin/out_mail.rb', line 222 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) ⇒ Object
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 213 214 215 216 217 218 219 220 |
# File 'lib/fluent/plugin/out_mail.rb', line 182 def sendmail(subject, msg) 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') # 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
103 104 |
# File 'lib/fluent/plugin/out_mail.rb', line 103 def shutdown end |
#start ⇒ Object
100 101 |
# File 'lib/fluent/plugin/out_mail.rb', line 100 def start end |
#with_scrub(string) ⇒ Object
238 239 240 241 242 243 244 245 246 247 |
# File 'lib/fluent/plugin/out_mail.rb', line 238 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
231 232 233 234 235 236 |
# File 'lib/fluent/plugin/out_mail.rb', line 231 def with_timezone(tz) oldtz, ENV['TZ'] = ENV['TZ'], tz yield ensure ENV['TZ'] = oldtz end |