当前位置:网站首页>Logstash——Logstash向Email发送告警邮件
Logstash——Logstash向Email发送告警邮件
2022-06-26 05:59:00 【大·风】
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端口)
个人水平有限,上面的内容可能存在没有描述清楚或者错误的地方,假如开发同学发现了,请及时告知,我会第一时间修改相关内容。假如我的这篇内容对你有任何帮助的话,麻烦给我点一个赞。你的点赞就是我前进的动力。
边栏推荐
- Pytorch (network model)
- 423- binary tree (110. balanced binary tree, 257. all paths of binary tree, 100. same tree, 404. sum of left leaves)
- numpy. frombuffer()
- MySQL-06
- 组合模式、透明方式和安全方式
- 04. basic data type - list, tuple
- Kolla ansible deploy openstack Yoga version
- Explore small program audio and video calls and interactive live broadcast from New Oriental live broadcast
- The news of thunderbolt
- MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications
猜你喜欢

On site commissioning - final method of kb4474419 for win7 x64 installation and vs2017 flash back

Prototype mode, Baa Baa

Class and object learning
从新东方直播来探究下小程序音视频通话及互动直播

【C語言】深度剖析數據在內存中的存儲

Mysql-10 (key)

解决在win10下cmder无法使用find命令

The purpose of writing programs is to solve problems

pytorch(网络模型)

canal部署、原理和使用介绍
随机推荐
电商借助小程序技术发力寻找增长突破口
numpy.exp()
小程序如何关联微信小程序二维码,实现二码聚合
REUSE_ ALV_ GRID_ Display event implementation (data_changed)
Last flight
421- binary tree (226. reversed binary tree, 101. symmetric binary tree, 104. maximum depth of binary tree, 222. number of nodes of complete binary tree)
卷妹带你学jdbc---2天冲刺Day2
Status mode, body can change at will
Redis多线程与ACL
Bingc (inheritance)
MEF framework learning record
Household accounting procedures (the second edition includes a cycle)
numpy. exp()
The purpose of writing programs is to solve problems
Prototype mode, Baa Baa
423-二叉树(110. 平衡二叉树、257. 二叉树的所有路径、100. 相同的树、404. 左叶子之和)
Redis底层数据结构
【 langage c】 stockage des données d'analyse approfondie en mémoire
one billion two hundred and twelve million three hundred and twelve thousand three hundred and twenty-one
Implementation of third-party wechat authorized login for applet