当前位置:网站首页>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端口)
个人水平有限,上面的内容可能存在没有描述清楚或者错误的地方,假如开发同学发现了,请及时告知,我会第一时间修改相关内容。假如我的这篇内容对你有任何帮助的话,麻烦给我点一个赞。你的点赞就是我前进的动力。
边栏推荐
- numpy.exp()
- numpy. random. choice
- Record how to modify the control across threads
- cross entropy loss = log softmax + nll loss
- Ribbon load balancing service call
- Prototype mode, Baa Baa
- Cython入门
- 原型模式,咩咩乱叫
- The model defined (modified) in pytoch loads some required pre training model parameters and freezes them
- How to use the tablet as the second extended screen of the PC
猜你喜欢

Consul service registration and discovery
![Selective search for object recognition paper notes [image object segmentation]](/img/cf/d3b08d41083f37c164b26a96b989c9.png)
Selective search for object recognition paper notes [image object segmentation]

类和对象的学习

Kolla ansible deploy openstack Yoga version

Pytorch (network model)

怎么把平板作为电脑的第二扩展屏幕

C generic speed

小程序如何关联微信小程序二维码,实现二码聚合

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

423-二叉树(110. 平衡二叉树、257. 二叉树的所有路径、100. 相同的树、404. 左叶子之和)
随机推荐
numpy. random. choice
MySQL-05
Tortoise and rabbit race example
How to use the tablet as the second extended screen of the PC
Household accounting procedures (First Edition)
Redis multithreading and ACL
怎么把平板作为电脑的第二扩展屏幕
Day3 - variables and operators
SQL Server view
Getting to know concurrency problems
Selective Search for Object Recognition 论文笔记【图片目标分割】
A new journey
E-commerce seeks growth breakthrough with the help of small program technology
MySQL-08
数据可视化实战:实验报告
[intra group questions semester summary] some reference questions for beginners
Implement the runnable interface
Easy to understand from the IDE, and then talk about the applet IDE
tf.nn.top_k()
【C语言】深度剖析数据在内存中的存储