zabbix邮件告警
1. 使用外部邮箱账号发送报警邮件设置
//在服务端关闭sendmail或者postfix
[root@xaii ~]# systemctl stop postfix
[root@xaii ~]# systemctl disable postfix
Removed symlink /etc/systemd/system/multi-user.target.wants/postfix.service.
[root@xaii ~]# ss -antl (没有25号端口,则已经关闭)
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 *:10051 *:*
LISTEN 0 128 127.0.0.1:9000 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 80 :::3306 :::*
//安装邮件发送工具mailx
[root@xaii ~]# yum -y install mailx
......
已安装:
mailx.x86_64 0:12.5-19.el7
[root@xaii ~]# mail -V(查看版本信息)
12.5 7/5/10
//配置zabbix服务端外部邮箱
[root@xaii ~]# vim /etc/mail.rc
[root@xaii ~]# tail /etc/mail.rc
set from=940160129@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=940160129@qq.com
set smtp-auth-password=cslvnmsnytngbbjj(开启邮箱smtp服务后,生成的授权码)
set smtp-auth=login
//手动验证,利用QQ邮箱发送给126邮箱
[root@xaii ~]# echo 'hello,nice to meet you' | mail -s 'hello' bebejo@126.com
//配置zabbix监控
(这里需要添加三个参数:)
(为某个用户添加Media:有Admin和guest两个用户,选择Admin用户)
(添加动作:)
- 创建动作时,需要基于触发器来执行
- 动作名称为:send_mail
- 发现问题1分钟后,执行动作,大部分保持默认即可:
- 问题被解决(覆盖)后,执行的动作:
//在zabbix服务端写一个发送邮件的脚本,脚本存放位置为/usr/local/etc/alertscripts,需要自己创建目录
[root@xaii ~]# mkdir -pv /usr/local/etc/alertscripts
mkdir: 已创建目录 "/usr/local/etc/alertscripts"
[root@xaii ~]# cd /usr/local/etc/alertscripts
[root@xaii alertscripts]# vim mail.sh(脚本名称必须和zabbix添加的名称一致)
//脚本内容:
#! /bin/bash
message=$3
subject=$2
echo "$message" | mail -s "$subject" $1
//修改alertscripts目录的属主和属组,赋予脚本执行权限
[root@xaii alertscripts]# chmod +x mail.sh
[root@xaii ~]# chown -R zabbix.zabbix /usr/local/etc/alertscripts/
//编辑服务端配置文件,将存放脚本的路径添加到配置文件中,并重启服务
[root@xaii ~]# vim /usr/local/etc/zabbix_server.conf
[root@xaii ~]# cat /usr/local/etc/zabbix_server.conf
......
AlertScriptsPath=/usr/local/etc/alertscripts(添加此内容)
[root@xaii ~]# pkill zabbix
[root@xaii ~]# zabbix_server
[root@xaii ~]# zabbix_agentd
//重启客户端,测试脚本,就监控/etc/cc文件内容被修改
[root@agent ~]# pkill zabbix
[root@agent ~]# zabbix_agentd
[root@agent ~]# cat /etc/cc
hello
adssadasdas
asdsadasasdas
[root@agent ~]# echo 'hello world' >> /etc/cc
查看Problem: /etct/cc文件发生了改变邮件内容:
Problem started at 23:50:34 on 2019.02.26
Problem name: /etct/cc文件发生了改变
Host: web服务器-192.168.157.19
Severity: Warning
Original problem ID: 97
查看Resolved: /etct/cc文件发生了改变邮件内容:
Problem has been resolved at 23:51:04 on 2019.02.26
Problem name: /etct/cc文件发生了改变
Host: web服务器-192.168.157.19
Severity: Warning
Original problem ID: 97
2. 利用钉钉机器人告警
基础配置不再提了,下面展示一下zabbix配置
附上钉钉脚本:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import json
import sys
import os
headers = {'Content-Type': 'application/json;charset=utf-8'}
api_url = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#此处的api_url在生成钉钉webhook机器人时,会自动生成。
#需要更换为你机器人的地址
def msg(text):
json_text= {
"msgtype": "text",
"text": {
"content": text
},
"at": {
"atMobiles": [
"" #需要@群里谁
],
"isAtAll": True #是否全部@,True为是,False为否
}
}
print requests.post(api_url,json.dumps(json_text),headers=headers).content
if __name__ == '__main__':
text = sys.argv[1]
msg(text)