fluent-plugin-mail 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -7,16 +7,88 @@ Add this line to your application's Gemfile:
7
7
 
8
8
  gem 'fluent-plugin-mail'
9
9
 
10
- And then execute:
11
-
12
- $ bundle
13
-
14
10
  Or install it yourself as:
15
11
 
16
12
  $ gem install fluent-plugin-mail
17
13
 
14
+ Or use td-agent : (on Ubuntu12.04)
15
+
16
+ $ sudo /usr/lib/fluent/ruby/bin/fluent-gem install fluent-plugin-mail
17
+
18
+
19
+ ## Mail Configuration with out_keys (no auth)
20
+
21
+ <match **>
22
+ type mail
23
+ host SMTPSERVER
24
+ port 25
25
+ from SOURCE
26
+ to DEST1,DEST2,DEST3
27
+ subject SUBJECT: %s
28
+ subject_out_keys target_tag
29
+ out_keys target_tag,pattern,value
30
+ </match>
31
+
32
+ Email is sent like
33
+
34
+ From: SOURCE
35
+ To: DEST1,DEST2,DEST3
36
+ Subject: SUBJECT: #{target_tag}
37
+ Mime-Version: 1.0
38
+ Content-Type: text/plain; charset=utf-8
39
+
40
+ target_tag: #{target_tag}
41
+ pattern: #{pattern}
42
+ value: #{value}
18
43
 
19
- ## SingleNode's Usage
44
+ ## Mail Configuration with Message Format (no auth)
45
+
46
+ You may use `message` parameter to define mail format as you like. Use `\n` to put a return code.
47
+
48
+ <match **>
49
+ type mail
50
+ host SMTPSERVER
51
+ port 25
52
+ from SOURCE
53
+ to DEST1,DEST2,DEST3
54
+ subject SUBJECT: %s
55
+ subject_out_keys target_tag
56
+ message %s %s\n%s
57
+ message_out_keys target_tag,pattern,value
58
+ </match>
59
+
60
+ Email is sent like
61
+
62
+ From: SOURCE
63
+ To: DEST1,DEST2,DEST3
64
+ Subject: SUBJECT: #{target_tag}
65
+ Mime-Version: 1.0
66
+ Content-Type: text/plain; charset=utf-8
67
+
68
+ #{target_tag} #{pattern}
69
+ #{value}
70
+
71
+ ## Mail Configuration for Gmail(use TLS)
72
+
73
+ <match **>
74
+ type mail
75
+ host smtp.gmail.com
76
+ port 587
77
+ domain gmail.com
78
+ from SOURCE
79
+ to DEST1,DEST2,DEST3
80
+ subject SUBJECT
81
+ user USERNAME( ex. hoge@gmail.com)
82
+ password PASSWORD
83
+ enable_starttls_auto true
84
+ out_keys target_tag,pattern,value
85
+ </match>
86
+
87
+
88
+
89
+ ## Usage Sample
90
+
91
+ ### SingleNode's syslog check
20
92
 
21
93
  use fluent_plugin_notifier(https://github.com/tagomoris/fluent-plugin-notifier)
22
94
 
@@ -53,12 +125,12 @@ configure td-agent.conf for single node
53
125
  domain DOMAIN
54
126
  from SOURCE_MAIL_ADDRESS
55
127
  to DESTINATION_MAIL_ADDRESS
56
- subject SUBJET
128
+ subject SUBJECT
57
129
  outkeys target_tag, pattern, value, message_time
58
130
  </match>
59
131
 
60
132
 
61
- ## MultiNode's Configuration for syslog
133
+ ### MulatiNode's syslog check
62
134
 
63
135
  use config_expander(https://github.com/tagomoris/fluent-plugin-config-expander)
64
136
 
@@ -115,27 +187,11 @@ log server("/etc/td-agent/td-agent.conf")
115
187
  domain DOMAIN
116
188
  from SOURCE_MAIL_ADDRESS
117
189
  to DESTINATION_MAIL_ADDRESS
118
- subject SUBJET
190
+ subject SUBJECT
119
191
  outkeys target_tag, pattern, value
120
192
  </match>
121
193
 
122
194
 
123
- ## Mail Configuration for Gmail(TLS)
124
-
125
- <match **>
126
- type mail
127
- host smtp.gmail.com
128
- port 587
129
- domain gmail.com
130
- from SOURCE
131
- to DEST1,DEST2,DEST3
132
- subject SUBJET
133
- user USERNAME( ex. hoge@gmail.com)
134
- password PASSWORD
135
- enable_starttls_auto true
136
- out_keys target_tag,pattern,value
137
- </match>
138
-
139
195
 
140
196
  ## TODO
141
197
 
@@ -12,9 +12,9 @@ Gem::Specification.new do |gem|
12
12
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
13
  gem.name = "fluent-plugin-mail"
14
14
  gem.require_paths = ["lib"]
15
- gem.version = '0.0.1'
15
+ gem.version = '0.0.2'
16
16
  gem.add_development_dependency "fluentd"
17
+ gem.add_development_dependency "rake"
17
18
  gem.add_runtime_dependency "fluentd"
18
- gem.add_runtime_dependency "rake"
19
19
  end
20
20
 
@@ -1,7 +1,9 @@
1
1
  class Fluent::MailOutput < Fluent::Output
2
2
  Fluent::Plugin.register_output('mail', self)
3
3
 
4
- config_param :out_keys, :string
4
+ config_param :out_keys, :string, :default => ""
5
+ config_param :message, :string, :default => nil
6
+ config_param :message_out_keys, :string, :default => ""
5
7
  config_param :time_key, :string, :default => nil
6
8
  config_param :time_format, :string, :default => nil
7
9
  config_param :tag_key, :string, :default => 'tag'
@@ -13,6 +15,7 @@ class Fluent::MailOutput < Fluent::Output
13
15
  config_param :from, :string, :default => 'localhost@localdomain'
14
16
  config_param :to, :string, :default => ''
15
17
  config_param :subject, :string, :default => 'Fluent::MailOutput plugin'
18
+ config_param :subject_out_keys, :string, :default => ""
16
19
  config_param :enable_starttls_auto, :bool, :default => false
17
20
 
18
21
  def initialize
@@ -24,7 +27,25 @@ class Fluent::MailOutput < Fluent::Output
24
27
  def configure(conf)
25
28
  super
26
29
 
27
- @out_keys = conf['out_keys'].split(',')
30
+ @out_keys = @out_keys.split(',')
31
+ @message_out_keys = @message_out_keys.split(',')
32
+ @subject_out_keys = @subject_out_keys.split(',')
33
+
34
+ if @out_keys.empty? and @message.nil?
35
+ raise Fluent::ConfigError, "Either 'message' or 'out_keys' must be specifed."
36
+ end
37
+
38
+ begin
39
+ @message % (['1'] * @message_out_keys.length) if @message
40
+ rescue ArgumentError
41
+ raise Fluent::ConfigError, "string specifier '%s' of message and message_out_keys specification mismatch"
42
+ end
43
+
44
+ begin
45
+ @subject % (['1'] * @subject_out_keys.length)
46
+ rescue ArgumentError
47
+ raise Fluent::ConfigError, "string specifier '%s' of subject and subject_out_keys specification mismatch"
48
+ end
28
49
 
29
50
  if @time_key
30
51
  if @time_format
@@ -48,30 +69,23 @@ class Fluent::MailOutput < Fluent::Output
48
69
 
49
70
  def emit(tag, es, chain)
50
71
  messages = []
72
+ subjects = []
51
73
 
52
74
  es.each {|time,record|
53
- values = []
54
- last = @out_keys.length - 1
55
-
56
- @out_keys.each do |key|
57
- case key
58
- when @time_key
59
- values << @time_format_proc.call(time)
60
- when @tag_key
61
- values << tag
62
- else
63
- values << "#{key}: #{record[key].to_s}"
64
- end
75
+ if @message
76
+ messages << create_formatted_message(tag, time, record)
77
+ else
78
+ messages << create_key_value_message(tag, time, record)
65
79
  end
66
-
67
- messages.push (values.join("\n"))
80
+ subjects << create_formatted_subject(tag, time, record)
68
81
  }
69
82
 
70
- messages.each do |msg|
83
+ messages.each_with_index do |msg, i|
84
+ subject = subjects[i]
71
85
  begin
72
- res = sendmail(msg)
86
+ res = sendmail(subject, msg)
73
87
  rescue
74
- $log.warn "out_mail: failed to send notice to #{@host}:#{@port}, message: #{msg}"
88
+ $log.warn "out_mail: failed to send notice to #{@host}:#{@port}, subject: #{subject}, message: #{msg}"
75
89
  end
76
90
  end
77
91
 
@@ -82,7 +96,58 @@ class Fluent::MailOutput < Fluent::Output
82
96
  "#{Time.at(time).strftime('%Y/%m/%d %H:%M:%S')}\t#{tag}\t#{record.to_json}\n"
83
97
  end
84
98
 
85
- def sendmail(msg)
99
+ def create_key_value_message(tag, time, record)
100
+ values = []
101
+
102
+ @out_keys.each do |key|
103
+ case key
104
+ when @time_key
105
+ values << @time_format_proc.call(time)
106
+ when @tag_key
107
+ values << tag
108
+ else
109
+ values << "#{key}: #{record[key].to_s}"
110
+ end
111
+ end
112
+
113
+ values.join("\n")
114
+ end
115
+
116
+ def create_formatted_message(tag, time, record)
117
+ values = []
118
+
119
+ values = @message_out_keys.map do |key|
120
+ case key
121
+ when @time_key
122
+ @time_format_proc.call(time)
123
+ when @tag_key
124
+ tag
125
+ else
126
+ record[key].to_s
127
+ end
128
+ end
129
+
130
+ (@message % values).gsub(/\\n/, "\n")
131
+ end
132
+
133
+ def create_formatted_subject(tag, time, record)
134
+ values = []
135
+
136
+ values = @subject_out_keys.map do |key|
137
+ case key
138
+ when @time_key
139
+ @time_format_proc.call(time)
140
+ when @tag_key
141
+ tag
142
+ else
143
+ record[key].to_s
144
+ end
145
+ end
146
+
147
+ @subject % values
148
+ end
149
+
150
+ def sendmail(subject, msg)
86
151
  smtp = Net::SMTP.new(@host, @port)
87
152
 
88
153
  if @user and @password
@@ -93,7 +158,7 @@ class Fluent::MailOutput < Fluent::Output
93
158
  smtp.start
94
159
  end
95
160
 
96
- subject = @subject.force_encoding('binary')
161
+ subject = subject.force_encoding('binary')
97
162
  body = msg.force_encoding('binary')
98
163
 
99
164
  smtp.send_mail(<<EOS, @from, @to.split(/,/))
@@ -2,32 +2,57 @@ require 'helper'
2
2
 
3
3
  class MailOutputTest < Test::Unit::TestCase
4
4
 
5
-
6
- CONFIG = %[
7
- message out_mail: %s [%s] %s
5
+ CONFIG_OUT_KEYS = %[
8
6
  out_keys tag,time,value
9
7
  time_key time
10
8
  time_format %Y/%m/%d %H:%M:%S
11
9
  tag_key tag
12
- subject Fluentd Notification Alerm
10
+ subject Fluentd Notification Alarm %s
11
+ subject_out_keys tag
13
12
  host localhost
14
13
  port 25
15
14
  from localhost@localdomain
16
15
  to localhost@localdomain
17
16
  ]
18
17
 
19
- def create_driver(conf=CONFIG,tag='test')
18
+ CONFIG_MESSAGE = %[
19
+ message out_mail: %s [%s]\\n%s
20
+ message_out_keys tag,time,value
21
+ time_key time
22
+ time_format %Y/%m/%d %H:%M:%S
23
+ tag_key tag
24
+ subject Fluentd Notification Alarm %s
25
+ subject_out_keys tag
26
+ host localhost
27
+ port 25
28
+ from localhost@localdomain
29
+ to localhost@localdomain
30
+ ]
31
+
32
+ def create_driver(conf=CONFIG_OUT_KEYS,tag='test')
20
33
  Fluent::Test::OutputTestDriver.new(Fluent::MailOutput, tag).configure(conf)
21
34
  end
22
35
 
23
36
  def test_configure
37
+ d = create_driver(CONFIG_OUT_KEYS)
38
+ assert_equal 'localhost', d.instance.host
39
+ d = create_driver(CONFIG_MESSAGE)
40
+ assert_equal 'localhost', d.instance.host
41
+ end
42
+
43
+ def test_out_keys
44
+ d = create_driver(CONFIG_OUT_KEYS)
45
+ time = Time.now.to_i
46
+ d.run do
47
+ d.emit({'value' => "out_keys mail from fluentd out_mail"}, time)
48
+ end
24
49
  end
25
50
 
26
- def test_notice
27
- d = create_driver
51
+ def test_message
52
+ d = create_driver(CONFIG_MESSAGE)
28
53
  time = Time.now.to_i
29
54
  d.run do
30
- d.emit({'value' => "message from fluentd out_mail: testing now"}, time)
55
+ d.emit({'value' => "message mail from fluentd out_mail"}, time)
31
56
  end
32
57
  end
33
58
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-mail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-05 00:00:00.000000000 Z
12
+ date: 2013-02-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fluentd
16
- requirement: &12125340 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,21 +21,31 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *12125340
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
- name: fluentd
27
- requirement: &12124060 !ruby/object:Gem::Requirement
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
31
36
  - !ruby/object:Gem::Version
32
37
  version: '0'
33
- type: :runtime
38
+ type: :development
34
39
  prerelease: false
35
- version_requirements: *12124060
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
- name: rake
38
- requirement: &12122980 !ruby/object:Gem::Requirement
47
+ name: fluentd
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,7 +53,12 @@ dependencies:
43
53
  version: '0'
44
54
  type: :runtime
45
55
  prerelease: false
46
- version_requirements: *12122980
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
47
62
  description: output plugin for Mail
48
63
  email:
49
64
  - yuichi.u@gmail.com
@@ -80,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
95
  version: '0'
81
96
  requirements: []
82
97
  rubyforge_project: fluent-plugin-mail
83
- rubygems_version: 1.8.11
98
+ rubygems_version: 1.8.23
84
99
  signing_key:
85
100
  specification_version: 3
86
101
  summary: output plugin for Mail