zabbix 以html格式邮件报警

本文介绍了一个用于Zabbix监控系统的邮件报警脚本,该脚本通过sendEmail工具发送HTML格式的邮件通知,内容包括触发器状态、主机名、IP地址等关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

样板:

#邮件报警脚本
  1. #!/bin/sh
  2. export smtpemailfrom="Zabbix_monitoring@ecvision.com"
  3. export zabbixemailto="$1"
  4. export zabbixsubject="$2"
  5. export zabbixbody="$3"
  6. export smtpserver=mail.server.host
  7. #export smtplogin=SMTP_LOGIN
  8. #export smtppass=SMTP_PASSWORD
  9. /usr/bin/sendEmail \
  10. -f $smtpemailfrom \
  11. -t $zabbixemailto \
  12. -u $zabbixsubject \
  13. -m $zabbixbody \
  14. -s $smtpserver:25 \
  15. -o tls=no \
  16. -o message-content-type=html \
  17. -o message-charset=utf8 \
  18. -o tls=auto >>/tmp/sendEmail.log
 
 #html格式
  1. <tablealign="center"class="MsoNormalTable"border="1"cellpadding="0"style="mso-cellspacing:1.5pt; border:solid black 1.0pt; margin-top:50px;mso-yfti-tbllook:1184;mso-padding-alt:0cm0cm0cm0cm">
  2. <tbody><trstyle="mso-yfti-irow:0;mso-yfti-firstrow:yes">
  3. <tdcolspan="2"style="border:none;padding:.75pt.75pt.75pt.75pt">
  4. <pclass="MsoNormal"><spanlang="EN-US"style="font-family:&quot;Verdana&quot;,sans-serif"><ahref="http://internal.ecvision.cn/zabbix/index.php"style="color:#000000;text-decoration:none;">Zabbix
  5. Monitoring System Notification</a></span><spanlang="EN-US"style="font-size:10.0pt;
  6. font-family:&quot;Verdana&quot;,sans-serif">
  7. <o:p></o:p></span></p></td>
  8. </tr>
  9. <trstyle="mso-yfti-irow:1">
  10. <tdnowrap=""style="border:none;background:#D9D9D9;padding:.75pt.75pt.75pt.75pt">
  11. <pclass="MsoNormal"><b><spanstyle="font-size:10.0pt;font-family:
  12. &quot;Verdana&quot;,sans-serif">Trigger Status: </span></b></p></td>
  13. <tdstyle="border:none;background:#FF8080;padding:.75pt.75pt.75pt.75pt">
  14. <pclass="MsoNormal"><spanstyle="font-size:10.0pt;font-family:&quot;Verdana&quot;,sans-serif">{TRIGGER.STATUS}</span></p></td>
  15. </tr>
  16. <trstyle="mso-yfti-irow:2">
  17. <tdnowrap=""style="border:none;background:#F2F2F2;padding:.75pt.75pt.75pt.75pt">
  18. <pclass="MsoNormal"><b><spanlang="EN-US"style="font-size:10.0pt;font-family:
  19. &quot;Verdana&quot;,sans-serif">Trigger Name:
  20. </span></b></p></td>
  21. <tdstyle="border:none;padding:.75pt.75pt.75pt.75pt">
  22. <pclass="MsoNormal"><spanlang="EN-US"style="font-size:10.0pt;font-family:&quot;Verdana&quot;,sans-serif">{TRIGGER.NAME}</span></p></td>
  23. </tr>
  24. <trstyle='mso-yfti-irow:4'>
  25. <tdnowrap="nowrap"style='border:none;background:#D9D9D9;padding:.75pt.75pt.75pt.75pt'><pclass="MsoNormal"><b><spanstyle='font-size:10.0pt;font-family:
  26. &quot;Verdana&quot;,sans-serif'lang="EN-US"xml:lang="EN-US">Trigger Severity:
  27. </span></b></p></td>
  28. <tdstyle='border:none;background:#FFFF80;padding:.75pt.75pt.75pt.75pt'><pclass="MsoNormal"><spanstyle='font-size:10.0pt;font-family:&quot;Verdana&quot;,sans-serif'lang="EN-US"xml:lang="EN-US">{TRIGGER.SEVERITY}
  29. </span></p></td>
  30. </tr>
  31. <trstyle="mso-yfti-irow:5">
  32. <tdnowrap=""style="border:none;background:#F2F2F2;padding:.75pt.75pt.75pt.75pt">
  33. <pclass="MsoNormal"><b><spanlang="EN-US"style="font-size:10.0pt;font-family:
  34. &quot;Verdana&quot;,sans-serif">Trigger Time:</span></b></p></td>
  35. <tdstyle="border:none;background:#F2F2F2;padding:.75pt.75pt.75pt.75pt">
  36. <pclass="MsoNormal"><spanlang="EN-US"style="font-size:10.0pt;font-family:&quot;Verdana&quot;,sans-serif">{EVENT.DATE}&nbsp;&nbsp;{EVENT.TIME}</span></p></td>
  37. </tr>
  38. <trstyle='mso-yfti-irow:6'>
  39. <tdnowrap=""style="border:none;background:#D9D9D9;padding:.75pt.75pt.75pt.75pt">
  40. <pclass="MsoNormal"><b><spanlang="EN-US"style="font-size:10.0pt;font-family:
  41. &quot;Verdana&quot;,sans-serif">Trigger Severity:</span></b></p></td>
  42. <tdstyle='border:none;padding:.75pt.75pt.75pt.75pt'><pclass="MsoNormal"><spanstyle="font-size:10.0pt;font-family:&quot;Verdana&quot;,sans-serif">{TRIGGER.NSEVERITY}</span></p></td>
  43. </tr>
  44. <trstyle="mso-yfti-irow:7">
  45. <tdnowrap=""style="border:none;background:#F2F2F2;padding:.75pt.75pt.75pt.75pt">
  46. <pclass="MsoNormal"><b><spanlang="EN-US"style="font-size:10.0pt;font-family:
  47. &quot;Verdana&quot;,sans-serif">Host Name:</span></b></p></td>
  48. <tdstyle="border:none;background:#F2F2F2;padding:.75pt.75pt.75pt.75pt">
  49. <pclass="MsoNormal"><spanstyle="font-size:10.0pt;font-family:&quot;Verdana&quot;,sans-serif">{HOST.NAME}</span></p></td>
  50. </tr>
  51. <trstyle="mso-yfti-irow:8">
  52. <tdnowrap=""style="border:none;background:#D9D9D9;padding:.75pt.75pt.75pt.75pt">
  53. <pclass="MsoNormal"><b><spanstyle="font-size:10.0pt;font-family:
  54. &quot;Verdana&quot;,sans-serif">IP Address:
  55. </span></b></p></td>
  56. <tdstyle="border:none;padding:.75pt.75pt.75pt.75pt">
  57. <pclass="MsoNormal"><spanstyle="font-size:10.0pt;font-family:&quot;Verdana&quot;,sans-serif">{HOST.IP}</span></p></td>
  58. </tr>
  59. <trstyle="mso-yfti-irow:9">
  60. <tdnowrap=""style="border:none;background:#F2F2F2;padding:.75pt.75pt.75pt.75pt">
  61. <pclass="MsoNormal"><b><spanlang="EN-US"style="font-size:10.0pt;font-family:
  62. &quot;Verdana&quot;,sans-serif">Item Values:</span></b></p></td>
  63. <tdstyle="border:none;background:#F2F2F2;padding:.75pt.75pt.75pt.75pt">
  64. <pclass="MsoNormal"><spanlang="EN-US"style="font-size:10.0pt;font-family:&quot;Verdana&quot;,sans-serif">{ITEM.NAME1} ({HOST.NAME1}:{ITEM.KEY1}): {ITEM.VALUE1}</span></p></td>
  65. </tr>
  66. <trstyle="mso-yfti-irow:10;mso-yfti-lastrow:yes">
  67. <tdnowrap=""style="border:none;background:#D9D9D9;padding:.75pt.75pt.75pt.75pt">
  68. <pclass="MsoNormal"><b><spanlang="EN-US"style="font-size:10.0pt;font-family:
  69. &quot;Verdana&quot;,sans-serif">Original Event ID:<o:p></o:p></span></b></p></td>
  70. <tdstyle="border:none;padding:.75pt.75pt.75pt.75pt">
  71. <pclass="MsoNormal"><spanlang="EN-US"style="font-size:10.0pt;font-family:&quot;Verdana&quot;,sans-serif">{EVENT.ID}</span></p></td>
  72. </tr>
  73. </tbody>
  74. </table>
  75. <pclass="foot"align="center"style="text-align:center"><spanlang="EN-US"style="font-size:10.0pt;font-family:&quot;Verdana&quot;,sans-serif"><ahref="http://internal.ecvision.cn/zabbix/index.php"style="color:#000000;text-decoration:none;">Generated by Zabbix,
  76. the OpenSource monitoring solution by AmberRoad.com</a>
  77. </span></p>
 





转载于:https://www.cnblogs.com/gyming/p/5781407.html

<think>好的,用户之前问过关于Rocky Linux发送邮件的问题,现在他想了解如何在Zabbix中创建邮件报警通知。我需要先回顾之前的回答,确保这次的内容不会重复,同时补充Zabbix特有的配置步骤。 首先,用户可能已经配置好了邮件发送环境,比如使用Postfix或SMTP中继,所以在Zabbix中需要集成这些设置。需要确认用户是否已经完成了邮件服务器的配置,或者是否需要从头开始指导。 接下来,Zabbix报警通知主要涉及两个部分:媒介类型(Media Type)的设置和动作(Action)的配置。我需要分步骤说明如何创建邮件媒介,包括SMTP服务器的配置、认证方式等。还要考虑到用户可能使用不同的邮件服务提供商,比如Gmail或阿里云,需要提示他们修改相应的SMTP地址和端口。 然后,用户可能需要关联报警媒介到具体的用户账户,这里需要指导他们如何在Zabbix的用户配置中添加媒介,并设置触发条件。同时,动作配置部分需要详细说明触发条件、操作细节,比如发送消息的内容格式、主题以及宏变量的使用,这样报警信息才能包含具体的主机和问题详情。 另外,用户可能对测试和故障排查不太熟悉,所以需要包括如何发送测试邮件,以及常见问题如邮件未收到、认证失败等的解决方法。还要提醒用户检查Zabbix服务器是否能连接到外部SMTP服务器,防火墙设置是否允许出站连接等。 最后,安全方面需要注意,比如使用应用专用密码而非真实密码,配置DKIM/SPF记录避免邮件被标记为垃圾邮件。可能还需要提到在Zabbix前端配置中的其他选项,比如TLS加密等,确保整个流程安全可靠。 总结下来,需要结构清晰,分步骤讲解,涵盖配置、测试、故障排查和安全建议,确保用户能够顺利创建并测试Zabbix邮件报警通知。</think>以下是 Rocky Linux 系统上配置 Zabbix 邮件报警通知的完整流程,分为 **邮件服务配置** 和 **Zabbix 报警规则设置** 两大部分: --- ### 一、配置邮件发送服务(SMTP) **推荐方案**:使用 `ssmtp` 轻量级工具(需提前完成) ```bash # 安装 ssmtp sudo dnf install ssmtp # 配置 SMTP 服务(以 Gmail 为例) sudo vim /etc/ssmtp/ssmtp.conf ``` ```properties root=your_email@gmail.com mailhub=smtp.gmail.com:587 AuthUser=your_email@gmail.com AuthPass=your_app_password # 使用Gmail需生成应用专用密码 UseTLS=YES UseSTARTTLS=YES ``` **验证邮件发送功能**: ```bash echo "测试邮件正文" | mail -s "Zabbix邮件测试" recipient@example.com ``` --- ### 二、Zabbix 前端配置 #### 步骤 1:创建报警媒介类型(Media Type) 1. 访问 `Administration → Media Types → Create media type` 2. 配置参数: - **Name**: `Email-Alert`(自定义名称) - **Type**: `Email` - **SMTP server**: `localhost`(若邮件服务在本地) - **SMTP server port**: `25`(或对应端口) - **SMTP helo**: `yourdomain.com` - **SMTP email**: `zabbix@yourdomain.com`(发件人邮箱) - **Authentication**:按需选择(如 PLAIN) - **用户密码**:填写 SMTP 认证信息 ![示例](https://zabbix.org/mw/images/7/75/3.0-email_media_type.png) #### 步骤 2:为用户关联报警媒介 1. 进入 `Administration → Users → 选择用户 → Media 标签页` 2. 添加新媒介: - **Type**: 选择刚创建的 `Email-Alert` - **Send to**: 填写接收报警的邮箱(如 admin@example.com) - 其他选项保持默认 #### 步骤 3:创建触发动作(Action) 1. 进入 `Configuration → Actions → Create action` 2. **Action 标签页**: - **Name**: `Server-Health-Alert` - **Conditions**:设置触发条件(如 `Trigger severity >= High`) 3. **Operations 标签页**: - 添加操作:`Send message` → 选择媒介类型 `Email-Alert` - **Message模板**(支持宏变量): ```text 报警主机:{HOST.NAME} 报警时间:{EVENT.DATE} {EVENT.TIME} 报警等级:{TRIGGER.SEVERITY} 故障详情:{TRIGGER.NAME} 当前值:{ITEM.VALUE} 故障持续时间:{EVENT.AGE} ``` ![操作配置示例](https://www.zabbix.com/documentation/current/_media/manual/config/notifications/action_operation_details.png) --- ### 三、测试与验证 1. **手动触发测试**: ```bash # 在监控项中制造异常(如强制停止某服务) systemctl stop nginx ``` 2. **查看报警状态**: - 在 `Monitoring → Problems` 中确认告警状态 - 检查目标邮箱是否收到报警邮件 3. **日志排查**: ```bash tail -f /var/log/zabbix/zabbix_server.log # 查看Zabbix服务日志 tail -f /var/log/maillog # 查看邮件发送日志 ``` --- ### 四、常见问题解决 | 问题现象 | 排查方法 | |---------|----------| | 收件箱未收到邮件 | 检查Zabbix服务器能否连接SMTP:`telnet smtp.gmail.com 587` | | 邮件内容乱码 | 在媒介类型中设置 `Content type` 为 `HTML` 或 `Plain text` | | 认证失败 | 确认是否启用双因素认证的邮箱需使用应用密码 | | 触发条件不匹配 | 检查Action中的 `Conditions` 是否与触发器严重级别匹配 | --- ### 高级配置建议 1. **邮件模板优化**:使用 HTML 格式美化报警内容(支持表格、颜色标记) 2. **报警升级机制**:配置 `Escalations` 实现重复报警或升级通知 3. **TLS加密**:在媒介类型中启用 `SSL/TLS` 增强安全性 4. **第三方集成**:通过 `Media Types` 支持 Slack/Webhook 等多渠道报警 **最终效果示例**: ![邮件报警示例](https://www.zabbix.com/documentation/current/_media/manual/config/notifications/action_message_template_example.png)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值