zabbix/zabbix-md/zabbix邮箱报警.md

145 lines
3.1 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1><center>Zabbix邮箱报警</center></h1>
------
**作者:行癫(盗版必究)**
## 一:环境准备
#### 1.监控环境
zabbix-server正常运行并且正常对zabbix-agent进行监控
#### 2.注册163邮箱
![image-20230228211319924](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230228211319924.png)
注册邮箱设置客户端授权密码
![image-20230228211456188](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230228211456188.png)
![image-20230228211613622](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230228211613622.png)
## 二:邮箱部署
#### 1.软件安装
```shell
[root@xingdian ~]# yum install mailx -y
```
#### 2.邮箱配置
```shell
[root@xingdian ~]# vim /etc/mail.rc
set from=lisi@163.com邮箱地址
set smtp=smtp.163.comsmtp服务器
set smtp-auth-user=lisi@163.com(用户名)
set smtp-auth-password=*(授权邮箱密码)
set smtp-auth=login
```
#### 3.发邮件的方式
```shell
方式1mailx -s "邮件标题" 收件箱Email < 包含正文的文件
方式2cat 包含正文的文件 | mailx -s "邮件标题" 收件箱Email
方式3echo "正文内容" | mailx -s "邮件标题" 收件箱Email
方式4mailx -s "邮件标题" 收件箱Email回车按CTRL+D发送
```
#### 4.手动测试
```shell
[root@xingdian ~]# mailx -v -s 'hello' 'zhangsan@163.com'
nihao 回车然后ctrl+D
EOT
或者将邮件内容放到一个文件中content.txt
# cat content.txt | mailx -v -s hello zhangsan@163.com
```
#### 5.创建告警脚本
```shell
[root@xingdian ~]# cd /usr/lib/zabbix/alertscripts
[root@xingdian ~]# vim sendmail.sh
#!/bin/sh
echo "$3" | sed s/'\r'//g | mailx -s "$2" $1
[root@xingdian ~]# chmod u+x sendmail.sh && chown zabbix.zabbix sendmail.sh
```
## 三部署Zabbix监控
#### 1.创建主机群组
#### 2.创建主机
#### 3.创建监控项
监控nginx服务是否正常运行
#### 4.创建触发器
#### 5.创建报警媒介
脚本参数:
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
#### 6.创建动作
关联触发器
添加操作
```shell
"主题"对应"钉钉告警"媒介中的:{ALERT.SUBJECT}
"消息"对应"钉钉告警"媒介中的:{ALERT.MESSAGE}
主题:
故障名称(触发器名称){EVENT.NAME}
消息:
告警主机:{HOSTNAME1}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}
```
添加恢复操作
```shell
主题:故障恢复:{EVENT.NAME}
消息:
恢复主机:{HOSTNAME1}
恢复时间:{EVENT.DATE} {EVENT.TIME}
主机IP{HOST.IP}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}
```
#### 7.用户关联报警媒介
#### 8.模拟报警产生
#### 9.查看报警信息