zabbix报警媒介,微信报警,邮件报警

微信报警首先要申请微信企业公众号,创建相应应用,然后进行配置
微信企业公众号申请,目前可免费前往该地址进行申请https://qy.weixin.qq.com/
注册过程很简单,不信你试

然后进行企业公众号的基础设置









服务端报警微信脚本

[root@bogon alertscripts]# pwd
/usr/local/zabbix/share/zabbix/alertscripts
[root@bogon alertscripts]# cat wechat.py 
#!/usr/bin/python
#_*_coding:utf-8 _*_


 
import urllib,urllib2
import json
import sys
import simplejson


reload(sys)
sys.setdefaultencoding('utf-8')




def gettoken(corpid,corpsecret):
    gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
    print  gettoken_url
    try:
        token_file = urllib2.urlopen(gettoken_url)
    except urllib2.HTTPError as e:
        print e.code
        print e.read().decode("utf8")
        sys.exit()
    token_data = token_file.read().decode('utf-8')
    token_json = json.loads(token_data)
    token_json.keys()
    token = token_json['access_token']
    return token
 
 
 
def senddata(access_token,user,subject,content):
 
    send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
    send_values = {
        "touser":'@all',    #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
        "toparty":"2",    #企业号中的部门id。
        "msgtype":"text", #消息类型。
        "agentid":"1",    #企业号中的应用id。
        "text":{
            "content":subject + '\n' + content
           },
        "safe":"0"
        }
#    send_data = json.dumps(send_values, ensure_ascii=False)
    send_data = simplejson.dumps(send_values, ensure_ascii=False).encode('utf-8')
    send_request = urllib2.Request(send_url, send_data)
    response = json.loads(urllib2.urlopen(send_request).read())
    print str(response)
 
 
if __name__ == '__main__':
    user = str(sys.argv[1])     #zabbix传过来的第一个参数
    subject = str(sys.argv[2])  #zabbix传过来的第二个参数
    content = str(sys.argv[3])  #zabbix传过来的第三个参数
    
    corpid =  'wx4fb73930173'   #CorpID是企业号的标识
    corpsecret = 'o5dRgfJ-A1ZWx5FxJ14Wde3HAMCfk_QRTJhUyvnmP4btjR7jZJFOScNud8_a'  #corpsecretSecret是管理组凭证密钥
    accesstoken = gettoken(corpid,corpsecret)
    senddata(accesstoken,user,subject,content)
[root@bogon alertscripts]# 


创建媒介类型


配置媒介

创建用户,并配置用户


为用户添加报警媒介


报警设置

用户加入到超级管理员组

配置动作








告警主机:{HOSTNAME1}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}


邮件需要创建本地sendmail进行本地帐号配置,zabbix界面配置与微信一样

Zabbix邮件报警配置

一、安装sendmail或者postfix(安装一种即可)


yum install sendmail                 #安装
service sendmail start              #启动
chkconfig sendmail on                #设置开机启动
或者
yum install postfix
service postfix start
chkconfig postfix on


二、安装邮件发送工具mailx
yum install mailx         #安装 

三、设置发送邮件的email,用于邮件发送
cat /etc/mail.rc  //内容如下


set from=xxx@sina.cn
set smtp=smtp.sina.com
set smtp-auth-user=xxx@sina.cn
set smtp-auth-password=xxxxxxx
set smtp-auth=login

发送测试邮件

echo "zabbix test ..." |mail -s "zabbix" xxx@qq.com
mkdir /etc/zabbix/alertscripts 


vim /etc/zabbix/zabbix_server.conf                  //修改alert scripts为以下路径
AlertScriptsPath=/etc/zabbix/alertscripts


/etc/init.d/zabbix_server restart            //重新启动zabbix服务端


#vim /etc/zabbix/alertscripts/sendmail.sh            //编写邮件发送脚本

[root@bogon zabbix]# cd share/zabbix/alertscripts/
[root@bogon alertscripts]# cat sendmail.sh 
#!/bin/sh
#export.UTF-8
echo "$3" | sed s/'\r'//g | mail -s "$2" $1
[root@bogon alertscripts]# 

增加可执行权限

chown zabbix.zabbix /etc/zabbix/alertscripts/sendmail.sh
chmod +x /etc/zabbix/alertscripts/sendmail.sh

测试发送脚本

/etc/zabbix/alertscripts/sendmail.sh xxx@qq.com "测试邮件标题" "测试邮件内容"

上述操作均在zabbix service端操作.

五.zabbix后台配置,登录zabbix 控制台http://IP地址/zabbix.

1.> Administration|Media types | Create media type ,进入告警方式配置界面,创建sendmail.sh告警方式.

Name这里可以随便自己定义,Type选择scripts;Script name则输入linux下编写的脚本名称,即sendmail.sh,在Script parameters处点击Add分别添加
{ALERT.SENDTO},

{ALERT.SUBJECT},

{ALERT.MESSAGE} 3个参数,分别对应sendEmail.sh脚本需要的3个参数:收件人地址、主题、详细内容然后点击Add添加完成.


可参照http://www.cnblogs.com/saneri/p/6078069.html

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/25911813/viewspace-2129298/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/25911813/viewspace-2129298/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值