fluent-plugin-mail 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +7 -0
- data/LICENSE +13 -0
- data/README.md +150 -0
- data/Rakefile +11 -0
- data/fluent-plugin-mail.gemspec +20 -0
- data/lib/fluent/plugin/out_mail.rb +113 -0
- data/test/helper.rb +28 -0
- data/test/plugin/test_out_mail.rb +35 -0
- metadata +89 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Copyright (c) 2012- Yuichi UEMURA
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
you may not use this file except in compliance with the License.
|
5
|
+
You may obtain a copy of the License at
|
6
|
+
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
See the License for the specific language governing permissions and
|
13
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
# Fluent::Plugin::Mail
|
2
|
+
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
gem 'fluent-plugin-mail'
|
9
|
+
|
10
|
+
And then execute:
|
11
|
+
|
12
|
+
$ bundle
|
13
|
+
|
14
|
+
Or install it yourself as:
|
15
|
+
|
16
|
+
$ gem install fluent-plugin-mail
|
17
|
+
|
18
|
+
|
19
|
+
## SingleNode's Usage
|
20
|
+
|
21
|
+
use fluent_plugin_notifier(https://github.com/tagomoris/fluent-plugin-notifier)
|
22
|
+
|
23
|
+
$ gem install fluent-plugin-notifier
|
24
|
+
|
25
|
+
configure td-agent.conf for single node
|
26
|
+
|
27
|
+
<source>
|
28
|
+
type tail
|
29
|
+
tag syslog
|
30
|
+
path /var/log/syslog
|
31
|
+
</source>
|
32
|
+
<match syslog**>
|
33
|
+
type notifier
|
34
|
+
<def>
|
35
|
+
pattern check_syslog
|
36
|
+
check string_find
|
37
|
+
warn_regexp .*warn.*
|
38
|
+
crit_regexp .*crit.*
|
39
|
+
target_key_pattern message
|
40
|
+
</def>
|
41
|
+
<def>
|
42
|
+
pattern check_syslog
|
43
|
+
check string_find
|
44
|
+
warn_regexp .*Error.*
|
45
|
+
crit_regexp .*Down.*
|
46
|
+
target_key_pattern message
|
47
|
+
</def>
|
48
|
+
</match>
|
49
|
+
<match notification**>
|
50
|
+
type mail
|
51
|
+
host MAILSERVER
|
52
|
+
port MAILSERVER_PORT
|
53
|
+
domain DOMAIN
|
54
|
+
from SOURCE_MAIL_ADDRESS
|
55
|
+
to DESTINATION_MAIL_ADDRESS
|
56
|
+
subject SUBJET
|
57
|
+
outkeys target_tag, pattern, value, message_time
|
58
|
+
</match>
|
59
|
+
|
60
|
+
|
61
|
+
## MultiNode's Configuration for syslog
|
62
|
+
|
63
|
+
use config_expander(https://github.com/tagomoris/fluent-plugin-config-expander)
|
64
|
+
|
65
|
+
$ gem install fluent-plugin-config-expander
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
source node("/etc/td-agent/td-agent.conf")
|
70
|
+
|
71
|
+
<source>
|
72
|
+
type config_expander
|
73
|
+
<config>
|
74
|
+
type tail
|
75
|
+
format syslog
|
76
|
+
path /var/log/syslog
|
77
|
+
tag syslog.${hostname}
|
78
|
+
pos_file /tmp/syslog.pos
|
79
|
+
</config>
|
80
|
+
</source>a
|
81
|
+
<match **>
|
82
|
+
type forward
|
83
|
+
<server>
|
84
|
+
host HOST_ADDRESS
|
85
|
+
</server>
|
86
|
+
</match>
|
87
|
+
|
88
|
+
|
89
|
+
log server("/etc/td-agent/td-agent.conf")
|
90
|
+
|
91
|
+
<source>
|
92
|
+
type forward
|
93
|
+
</source>
|
94
|
+
<match syslog**>
|
95
|
+
type copy
|
96
|
+
<store>
|
97
|
+
type file
|
98
|
+
path /var/log-server/syslog
|
99
|
+
</store>
|
100
|
+
<store>
|
101
|
+
type notifier
|
102
|
+
<def>
|
103
|
+
pattern check_syslog
|
104
|
+
check string_find
|
105
|
+
warn_regexp .*warn.*
|
106
|
+
crit_regexp .*crit.*
|
107
|
+
target_key_pattern message
|
108
|
+
</def>
|
109
|
+
</store>
|
110
|
+
</match>
|
111
|
+
<match notification**>
|
112
|
+
type mail
|
113
|
+
host MAILSERVER
|
114
|
+
port MAILSERVER_PORT
|
115
|
+
domain DOMAIN
|
116
|
+
from SOURCE_MAIL_ADDRESS
|
117
|
+
to DESTINATION_MAIL_ADDRESS
|
118
|
+
subject SUBJET
|
119
|
+
outkeys target_tag, pattern, value
|
120
|
+
</match>
|
121
|
+
|
122
|
+
|
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
|
+
|
140
|
+
## TODO
|
141
|
+
|
142
|
+
* add config "mail_text_format"
|
143
|
+
|
144
|
+
## Copyright
|
145
|
+
|
146
|
+
* Copyright
|
147
|
+
* Copyright (c) 2012- Yuichi UEMURA
|
148
|
+
* License
|
149
|
+
* Apache License, Version 2.0
|
150
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.authors = ["Yuichi UEMURA"]
|
5
|
+
gem.email = ["yuichi.u@gmail.com"]
|
6
|
+
gem.description = %q{output plugin for Mail}
|
7
|
+
gem.summary = %q{output plugin for Mail}
|
8
|
+
gem.homepage = "https://github.com/u-ichi/fluent-plugin-mail"
|
9
|
+
gem.rubyforge_project = "fluent-plugin-mail"
|
10
|
+
gem.files = `git ls-files`.split($\)
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.name = "fluent-plugin-mail"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = '0.0.1'
|
16
|
+
gem.add_development_dependency "fluentd"
|
17
|
+
gem.add_runtime_dependency "fluentd"
|
18
|
+
gem.add_runtime_dependency "rake"
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,113 @@
|
|
1
|
+
class Fluent::MailOutput < Fluent::Output
|
2
|
+
Fluent::Plugin.register_output('mail', self)
|
3
|
+
|
4
|
+
config_param :out_keys, :string
|
5
|
+
config_param :time_key, :string, :default => nil
|
6
|
+
config_param :time_format, :string, :default => nil
|
7
|
+
config_param :tag_key, :string, :default => 'tag'
|
8
|
+
config_param :host, :string
|
9
|
+
config_param :port, :integer, :default => 25
|
10
|
+
config_param :domain, :string, :default => 'localdomain'
|
11
|
+
config_param :user, :string, :default => nil
|
12
|
+
config_param :password, :string, :default => nil
|
13
|
+
config_param :from, :string, :default => 'localhost@localdomain'
|
14
|
+
config_param :to, :string, :default => ''
|
15
|
+
config_param :subject, :string, :default => 'Fluent::MailOutput plugin'
|
16
|
+
config_param :enable_starttls_auto, :bool, :default => false
|
17
|
+
|
18
|
+
def initialize
|
19
|
+
super
|
20
|
+
require 'net/smtp'
|
21
|
+
require 'kconv'
|
22
|
+
end
|
23
|
+
|
24
|
+
def configure(conf)
|
25
|
+
super
|
26
|
+
|
27
|
+
@out_keys = conf['out_keys'].split(',')
|
28
|
+
|
29
|
+
if @time_key
|
30
|
+
if @time_format
|
31
|
+
f = @time_format
|
32
|
+
tf = Fluent::TimeFormatter.new(f, @localtime)
|
33
|
+
@time_format_proc = tf.method(:format)
|
34
|
+
@time_parse_proc = Proc.new {|str| Time.strptime(str, f).to_i }
|
35
|
+
else
|
36
|
+
@time_format_proc = Proc.new {|time| time.to_s }
|
37
|
+
@time_parse_proc = Proc.new {|str| str.to_i }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def start
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
def shutdown
|
47
|
+
end
|
48
|
+
|
49
|
+
def emit(tag, es, chain)
|
50
|
+
messages = []
|
51
|
+
|
52
|
+
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
|
65
|
+
end
|
66
|
+
|
67
|
+
messages.push (values.join("\n"))
|
68
|
+
}
|
69
|
+
|
70
|
+
messages.each do |msg|
|
71
|
+
begin
|
72
|
+
res = sendmail(msg)
|
73
|
+
rescue
|
74
|
+
$log.warn "out_mail: failed to send notice to #{@host}:#{@port}, message: #{msg}"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
chain.next
|
79
|
+
end
|
80
|
+
|
81
|
+
def format(tag, time, record)
|
82
|
+
"#{Time.at(time).strftime('%Y/%m/%d %H:%M:%S')}\t#{tag}\t#{record.to_json}\n"
|
83
|
+
end
|
84
|
+
|
85
|
+
def sendmail(msg)
|
86
|
+
smtp = Net::SMTP.new(@host, @port)
|
87
|
+
|
88
|
+
if @user and @password
|
89
|
+
smtp_auth_option = [@domain, @user, @password, :plain]
|
90
|
+
smtp.enable_starttls if @enable_starttls_auto
|
91
|
+
smtp.start(@domain,@user,@password,:plain)
|
92
|
+
else
|
93
|
+
smtp.start
|
94
|
+
end
|
95
|
+
|
96
|
+
subject = @subject.force_encoding('binary')
|
97
|
+
body = msg.force_encoding('binary')
|
98
|
+
|
99
|
+
smtp.send_mail(<<EOS, @from, @to.split(/,/))
|
100
|
+
Date: #{Time::now.strftime("%a, %d %b %Y %X")}
|
101
|
+
From: #{@from}
|
102
|
+
To: #{@to}
|
103
|
+
Subject: #{subject}
|
104
|
+
Mime-Version: 1.0
|
105
|
+
Content-Type: text/plain; charset=utf-8
|
106
|
+
|
107
|
+
#{body}
|
108
|
+
EOS
|
109
|
+
smtp.finish
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
|
12
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
13
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
14
|
+
require 'fluent/test'
|
15
|
+
unless ENV.has_key?('VERBOSE')
|
16
|
+
nulllogger = Object.new
|
17
|
+
nulllogger.instance_eval {|obj|
|
18
|
+
def method_missing(method, *args)
|
19
|
+
# pass
|
20
|
+
end
|
21
|
+
}
|
22
|
+
$log = nulllogger
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'fluent/plugin/out_mail'
|
26
|
+
|
27
|
+
class Test::Unit::TestCase
|
28
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class MailOutputTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
|
6
|
+
CONFIG = %[
|
7
|
+
message out_mail: %s [%s] %s
|
8
|
+
out_keys tag,time,value
|
9
|
+
time_key time
|
10
|
+
time_format %Y/%m/%d %H:%M:%S
|
11
|
+
tag_key tag
|
12
|
+
subject Fluentd Notification Alerm
|
13
|
+
host localhost
|
14
|
+
port 25
|
15
|
+
from localhost@localdomain
|
16
|
+
to localhost@localdomain
|
17
|
+
]
|
18
|
+
|
19
|
+
def create_driver(conf=CONFIG,tag='test')
|
20
|
+
Fluent::Test::OutputTestDriver.new(Fluent::MailOutput, tag).configure(conf)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_configure
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_notice
|
27
|
+
d = create_driver
|
28
|
+
time = Time.now.to_i
|
29
|
+
d.run do
|
30
|
+
d.emit({'value' => "message from fluentd out_mail: testing now"}, time)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fluent-plugin-mail
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Yuichi UEMURA
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: fluentd
|
16
|
+
requirement: &12125340 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *12125340
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: fluentd
|
27
|
+
requirement: &12124060 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *12124060
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &12122980 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *12122980
|
47
|
+
description: output plugin for Mail
|
48
|
+
email:
|
49
|
+
- yuichi.u@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- fluent-plugin-mail.gemspec
|
60
|
+
- lib/fluent/plugin/out_mail.rb
|
61
|
+
- test/helper.rb
|
62
|
+
- test/plugin/test_out_mail.rb
|
63
|
+
homepage: https://github.com/u-ichi/fluent-plugin-mail
|
64
|
+
licenses: []
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project: fluent-plugin-mail
|
83
|
+
rubygems_version: 1.8.11
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: output plugin for Mail
|
87
|
+
test_files:
|
88
|
+
- test/helper.rb
|
89
|
+
- test/plugin/test_out_mail.rb
|