前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Fluentd发送告警邮件

使用Fluentd发送告警邮件

作者头像
Fluentd中文网
发布2020-06-10 16:19:28
9520
发布2020-06-10 16:19:28
举报
文章被收录于专栏:Fluentd学习交流Fluentd学习交流

告警是预防系统故障的一个重要工具,目前已经有许多成熟的方案通过监控系统运行指标来进行阈值预警。

我们今天简单了解一下如何使用Fluentd实现邮件告警功能。

Fluentd的告警是基于日志分析实现的,通过监测日志中的业务状态信息,及时识别严重错误,实时发送告警信息。

我们以分析Apache的访问日志为例,了解一下Fluentd提供的邮件告警功能。

【安装软件】

  • 安装td-agent
  • 安装插件:fluent-plugin-grepcounter td-agent-gem install fluent-plugin-grepcounter
  • 安装插件:fluent-plugin-mail td-agent-gem install fluent-plugin-mail

【配置说明】

下边是一份完整的配置文件,可根据实际情况进行修改。

代码语言:javascript
复制
<source>
  @type tail
  path /var/log/apache2/access.log  # Set the location of your log file
  <parse>
    @type apache2
  </parse>
  tag apache.access
</source>

<match apache.access>
  @type grepcounter
  count_interval 3  # The time window for counting errors (in secs)
  input_key code    # The field to apply the regular expression
  regexp ^5\d\d$    # The regular expression to be applied
  threshold 1       # The minimum number of erros to trigger an alert
  add_tag_prefix error_5xx  # Generate tags like "error_5xx.apache.access"
</match>

<match error_5xx.apache.access>
  @type copy
  <store>
    @type stdout  # Print to stdout for debugging
  </store>
  <store>
    @type mail
    host smtp.gmail.com        # Change this to your SMTP server host
    port 587                   # Normally 25/587/465 are used for submission
    user USERNAME              # Use your username to log in
    password PASSWORD          # Use your login password
    enable_starttls_auto true  # Use this option to enable STARTTLS
    from example@gmail.com     # Set the sender address
    to alert@example.com       # Set the recipient address
    subject 'HTTP SERVER ERROR'
    message Total 5xx error count: %s\n\nPlease check your Apache webserver ASAP
    message_out_keys count     # Use the "count" field to replace "%s" above
  </store>
</match>

这个配置用到了邮件服务器,需要确保邮件服务器可用,且配置正确。

这个配置文件主要包含3部分:

  • <source> 使用in_tail追踪Apache的access日志,并使用apache2来解析日志。
  • <match apache.access> 使用grepcounter对日志中的5xx状态码进行过滤和计数。 如果在count_interval指定的时间内5xx错误数达到了threshold指定的阈值,Fluentd就会产生一条tag为error_5xx.apache.access的事件,该事件会重新进入Fluentd的事件路由中,被下一个<match>处理。
  • <match error_5xx.apache.access> 每收到一个error_5xx.apache.access事件,这个<match>会使用mail插件来发送一封邮件到alert@example.com。

如此配置,Fluentd就成了一个邮件告警服务,可用来监测Apache的异常访问。

【测试方法】

完成上边的配置之后,重启td-agent。

代码语言:javascript
复制
# for init.d users
$ sudo /etc/init.d/td-agent restart

# for systemd users
$ sudo systemctl restart td-agent

如果Apache有5xx错误发生(你也可以手动向access.log追加一条5xx错误),你将会收到一封标题为"HTTP SERVER ERROR"的告警邮件。

实际应用中你可能需要调整某些参数,比如统计间隔或阈值,还可以监测4xx错误码。

当然除了Apache,Fluentd还可以监测nginx等其他应用系统的日志错误,大家可根据需要选用合适的解析器插件进行处理。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-05-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Fluentd学习交流 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档