email数据输出
logstash的输出插件中提供了email的数据输出来源。这样当logstash遇见需要预警的内容时候,可以通过邮件向指定用户发送警报信件。
可配置参数
关于email的参数有下面几个
字段 | 参数类型 | 说明 |
---|---|---|
address | string | 邮件的服务器地址 |
username | string | 用来验证服务器的用户名 |
password | string | 用于验证服务器的密码 |
port | number | 用于与邮件服务器通信的端口 |
authentication | string | 服务器识别身份的方法 |
attachments | array | 附件地址 |
body | string | 电子邮件正文 |
cc | string | 邮件的抄送地址 |
bcc | string | 邮件的密件抄送地址 |
contenttype | string | 设置HTML部分的内容类型和/或字符集。 |
debug | boolean | 在调试模式下运行邮件中继 |
domain | string | 连接到远程SMTP服务器时使用的HELO / EHLO域名 |
from | string | 发件人 |
htmlbody | string | 电子邮件的HTML正文 |
replyto | string | 邮件回复字段 |
subject | string | 邮件主题 |
to | string | 电子邮件发送目标 |
use_tls | boolean | 与服务器通信时启用TLS |
via | string | Logstash应如何通过SMTP或通过调用sendmail发送电子邮件。 |
template_file | path | 用于电子邮件模板的[Mustache模板](https://mustache.github.io/)文件的路径。 |
以腾讯邮箱为例子
- 首先开启相关服务支持
需要打开设置-账号
- 开启对应服务
- 申请授权码
需要注意,此后所有配置中有关邮箱密码的部分都是指的授权码
根据其文档的介绍
- 接收邮件服务器:imap.qq.com,使用SSL,端口号993
- 发送邮件服务器:smtp.qq.com,使用SSL,端口号465或587
以邮件为目标的配置
email {
port => 587
address => "smtp.qq.com"
username => "邮箱@qq.com"
password => "授权码"
authentication => "plain"
contenttype => ""
from => "邮箱@qq.com"
subject => "错误告警"
to => "邮箱@qq.com"
use_tls => true
via => "smtp"
domain => "smtp.qq.com"
body => "错误告警:120秒内错误日志超过3条,请注意排查"
debug => true
}
关于logstash发送告警消息我之前有一篇文章使用throttle过滤器向钉钉发送预警消息
使用里面的配置,替换成邮件目标最终配置为下面内容
input {
redis {
key => "logstash-email"
host => "localhost"
password => "dailearn"
port => 6379
db => "0"
data_type => "list"
type => "email"
codec => plain{
charset=>"UTF-8"
}
}
}
filter {
grok {
match => { "message" => "%{TIMESTAMP_ISO8601:log_date} %{LOGLEVEL:log_info} %{DATA:thread} %{NOTSPACE} %{SPACE} %{NOTSPACE} %{JAVACLASS:log_class} %{SPACE}: %{GREEDYDATA:log_message}" }
}
if "_grokparsefailure" in [tags] {
drop {}
}
if [log_info] == "INFO" {
drop {}
}
if [log_info] == "ERROR" {
throttle {
before_count => -1
after_count => 3
period => 120
max_age => 240
key => "%{[log_info]}"
add_tag => "throttled"
}
}
if "throttled" in [tags] {
aggregate{
task_id => "%{log_info}"
code => "event.set('throttled_time',Time.parse(event.get('log_date')).to_f*1000)
map['throttled_time'] ||= 0
event.set('throttled_time_out', (event.get('throttled_time') - map['throttled_time']) > 10000)"
}
if [throttled_time_out] {
aggregate{
task_id => "%{log_info}"
code => "map['throttled_time'] = event.get('throttled_time')
event.set('throttled_time_test',map['throttled_time'])"
}
}
}
}
output {
if [throttled_time_out] {
email {
port => 587
address => "smtp.qq.com"
username => "邮箱@qq.com"
password => "授权码"
authentication => "plain"
contenttype => ""
from => "邮箱@qq.com"
subject => "错误告警"
to => "邮箱@qq.com"
use_tls => true
via => "smtp"
domain => "smtp.qq.com"
body => "错误告警:120秒内错误日志超过3条,请注意排查"
debug => true
}
}
stdout {
codec => rubydebug
}
}
使用上面配置就可以实现预警消息的邮件发送。
可能遇见的问题
一般出现问题的话首先需要检测是否开启了POP3和SMTP服务
- Something happen while delivering an email {:exception=>#<Net::OpenTimeout: execution expired>}
此时需要注意配置的address活动端口是否正确。
- Something happen while delivering an email {:exception=>#<EOFError: end of file reached>}
根据文字描述应该是邮件内容或者附件出现了问题,而且在GitHub上也有人提过这个问题 ,但是实际上出现上面的情况很大概率是超时问题导致的。这个时候需要尝试使用非SSL发送邮件。或者使用其他端口(假如有的话,比如腾讯提供的465和587端口)
个人水平有限,上面的内容可能存在没有描述清楚或者错误的地方,假如开发同学发现了,请及时告知,我会第一时间修改相关内容。假如我的这篇内容对你有任何帮助的话,麻烦给我点一个赞。你的点赞就是我前进的动力。