操作步骤
1 编写脚本 推荐测试脚本后边加 三个个参数 这个关系后面 新增参数:{ALERT.SENDTO}{ALERT.SUBJECT}{ALERT.MESSAGE}
2 zabbix ui 界面 configration -》 Media types 添加 脚本媒介
3 去user里面 吧刚才的 media 添加到 用户的报警媒体
官方文档链接:https://www.zabbix.com/documentation/4.0/zh/manual/config/notifications/action/operation/macros
给个模板:
{HOST.NAME} Have a Problem: {EVENT.NAME}
group:{TRIGGER.HOSTGROUP.NAME}
Problem started at {EVENT.TIME} on {EVENT.DATE}
Event ID: {EVENT.ID}
Severity: {EVENT.SEVERITY}
URL:http://ip/zabbix/tr_events.php?triggerid={TRIGGER.ID}&eventid={EVENT.ID}
操作系统版本是ubuntu16,centos7类似差别不大,过程如下(相关配置文件和脚本在最下方):
--------------------------------- 一 邮件报警两种方式
1.1 mail.sh 方式存放目录/usr/lib/zabbix/alertscripts/ 需要安装 mail服务器
1.2 直接在zabbix配置 需要注意大部分邮箱输入密码是授权密码,qq邮箱和企业邮箱都是授权密码;163 126是邮箱密码即可。
举例:
SMTP server : mail.gongsi.com.cn
SMTP server :port 25
SMTP helo :gongsi.com.cn
SMTP email:zabbix@gongsi.com.cn
Connection security:None(STARTTLS SSL/TLS)
Authentication : NoneUsername and password
Username : zabbix@gongsi.com.cn
Password : password
---------------------------------- 二 微信报警
# 企业微信推送消息接口说明链接:https://work.weixin.qq.com/api/doc/90000/90135/90236
# 1
登陆微信企业后台 https://work.weixin.qq.com/ 制作zabbix 应用
记住企业id 应用id 接收信息用户id(配置在zabbix相应的media里面)
# 2 配置zabbix 服务器
注意参数和存放目录/usr/lib/zabbix/alertscripts/
# 3 操作zabbix web界面
A 添加 Media types
Ttpe 选择 script
weixin2.py 配置Parameter需要三个参数:
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
weixin.py 两个参数即可:
{ALERT.SENDTO}
{ALERT.MESSAGE}
发送消息更多模板变量参考官方文档:https://www.zabbix.com/documentation/4.0/zh/manual/config/notifications/action/operation/macros
B 添加 user media (zabbix web界面操作)
C 设置 tragger类型的 actions (zabbix web界面操作 推荐使用自定义key,参考另一篇文章 https://blog.csdn.net/yuezhilangniao/article/details/111653168)
D 去查看actions log (zabbix web界面操作)
附脚本权限 rpm包版本:
mailcap-2.1.41-2.el7.noarch
mailx-12.5-19.el7.x86_64
-rwxr-xr-x 1 zabbix zabbix 165 8月 16 10:27 mailx.sh
-rwxr-xr-x 1 root root 1351 9月 10 16:12 weixin2.py
-rwxr-xr-x 1 root root 946 9月 10 15:44 weixin.py
1,安装mailx
使用sudo apt-get install mailx会出现两个包选择
heirloom-mailx和bsd-mailx
区别在于前者配置文件为/etc/s-nail.rc后者为 后者不好用 /etc/mail.rc
我安装了heirloom-mailx这个包
然后配置文件
set from=发送邮箱的地址xx@163.com
set smtp=smtp.163.com
set smtp-auth-user=xx@163.com
set smtp-auth-password=password
set smtp-auth=login
发送命令
1
echo "practice"| mailx -s "Title" xx@163.com
---下面是参考文章复制过来的 可以用
关闭sendmail或者postfix
service sendmail stop
service postfix stop
1、安装heirloom-mailx
apt install heirloom-mailx
配置文件为/etc/s-nail.rc :
set bsdcompat
set from=zabbix@byzoro.com
set smtp=smtp.263.net
set smtp-auth-user=zabbix@byzoro.com
set smtp-auth-password=BB0B6225603aE932
#(授权密码,不是登录密码)#
set smtp-auth=login
如果配置端口的模板:
set from="xxx@163.com"
set smtp="smtps://smtp.163.com:465"
set smtp-auth-user="xxxm@163.com"
set smtp-auth-password="123456"
set smtp-auth=login
# mail.rc配置文件
# This is the configuration file for Heirloom mailx (formerly
# known under the name "nail".
# See mailx(1) for further options.
# This file is not overwritten when 'make install' is run in
# the mailx build process again.
# Sccsid @(#)nail.rc 2.11 (gritter) 8/2/08
# Do not forward to mbox by default since this is likely to be
# irritating for most users today.
set hold
# Append rather than prepend when writing to mbox automatically.
# This has no effect unless 'hold' is unset again.
set append
# Ask for a message subject.
set ask
# Assume a CRT-like terminal and invoke a pager.
set crt
# Messages may be terminated by a dot.
set dot
# Do not remove empty mail folders in the spool directory.
# This may be relevant for privacy since other users could
# otherwise create them with different permissions.
set keep
# Do not remove empty private mail folders.
set emptybox
# Quote the original message in replies by "> " as usual on the Internet.
set indentprefix="> "
# Automatically quote the text of the message that is responded to.
set quote
# Outgoing messages are sent in ISO-8859-1 if all their characters are
# representable in it, otherwise in UTF-8.
set sendcharsets=iso-8859-1,utf-8
# Display sender's real names in header summaries.
set showname
# Display the recipients of messages sent by the user himself in
# header summaries.
set showto
# Automatically check for new messages at each prompt, but avoid polling
# of IMAP servers or maildir folders.
set newmail=nopoll
# If threaded mode is activated, automatically collapse thread.
set autocollapse
# Mark messages that have been answered.
set markanswered
# Hide some header fields which are uninteresting for most human readers.
ignore received in-reply-to message-id references
ignore mime-version content-transfer-encoding
# Only include selected header fields when forwarding messages.
fwdretain subject date from to
# For Linux and BSD, this should be set.
set bsdcompat
set from=zabbix@byzoro.com
set smtp=smtp.263.net
set smtp-auth-user=zabbix@byzoro.com
set smtp-auth-password=BB0B6225603aE932
#(授权密码,不是登录密码)#
set smtp-auth=login
mailx.sh
#!/bin/bash
#send mail
messages=`echo $3 | tr '\r\n' '\n'`
subject=`echo $2 | tr '\r\n' '\n'`
echo "${messages}" | mail -s "${subject}" $1 >>/tmp/mailx.log 2>&1
# weixin.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
import sys
import urllib,urllib2
# 这三个地方都在自己创建的zabbix 里面可以看到 需要修改成你自己的
agentid = '1000002'
corpid = 'ww60be3bc7b4d40c58'
corpsecret = 'eRvNCpw3BLW7EeFkfox71xiboOkPZ8585g1cw2MBa1k'
#get tocken
gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
token_file = urllib2.urlopen(gettoken_url)
token_data = token_file.read().decode('utf-8')
token_json = json.loads(token_data)
my_token = token_json['access_token']
#send wechart
touser=sys.argv[1] #many user: 'zhangsan|wangwu'
content=sys.argv[2] #content
post_content = {
"touser":touser,
"agentid":agentid,
"msgtype": "text",
"text":{
"content":content,
}
}
json_content = json.dumps(post_content)
url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + my_token
response = urllib2.urlopen(url,json_content)
print(response.read().decode('utf-8'))
# weixin2.py 三个参数版本的:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#author: bgx
#date: 2018
#comment: zabbix接入微信报警脚本
import requests
import sys
import os
import json
import logging
logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s, %(filename)s, %(levelname)s, %(message)s',
datefmt = '%a, %d %b %Y %H:%M:%S',
filename = os.path.join('/tmp','weixin.log'),
filemode = 'a')
# 三个值记得设置成自己创建的zabbix应用的三个属性
corpid='ww60be3bc7b4d40c58'
appsecret='eRvNCpw3BLW7EeFkfox71xiboOkPZ8585g1cw2MBa1k'
agentid='1000002'
#获取accesstoken
token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret
req=requests.get(token_url)
accesstoken=req.json()['access_token']
#发送消息
msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken
touser=sys.argv[1]
subject=sys.argv[2]
#toparty='3|4|5|6'
message=sys.argv[2] + "\n\n" +sys.argv[3]
params={
"touser": touser,
# "toparty": toparty,
"msgtype": "text",
"agentid": agentid,
"text": {
"content": message
},
"safe":0
}
req=requests.post(msgsend_url, data=json.dumps(params))
logging.info('sendto:' + touser + ';;subject:' + subject + ';;message:' + message)
# webhook 告警 只有一个消息参数版本
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import requests
import sys
def func_webhook(s, webhook):
data = {
"msgtype": "markdown",
"markdown": {
"content": s,
#"content": "广州今日天气:29度,大部分多云,降雨概率:60%",
#"mentioned_list":["zhangjinshang","@all"],
"mentioned_mobile_list":["17701210386"]
}
}
r = requests.post(url=webhook, json=data)
print(r)
# 每次调用方法传你需要发送的报警信息和对应的webhook地址即可
if __name__ == "__main__":
webhook_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5840d3b5-9fbb-49f1-99f0-3e411aa34e90"
zabbix_webhook_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d6a352bc-d00f-48ae-b8ca-05e2f9b83dfa"
project_name = "车险快速出单"
env_type = "测试环境"
# touser=sys.argv[1] #many user: 'zhangsan|wangwu'
content=sys.argv[1] #content
# s = "大家好!温馨提示:\n 项目名称<font color=\"warning\">车险快速出单</font>hiplus资源申请已通过审批 \n环境类型:<font color=\"comment\">测试环境</font>\n云主机:<font color=\"comment\">2例</font>\n数据库:<font color=\"comment\">1表</font>\n您可以登录:[腾讯云控制台](https://cloud.tencent.com/login)进行相关操作"
func_webhook(content, zabbix_webhook_url)
’