系统环境:
操作系统:CentOS7.2
依赖软件:gnokii、zabbix2.8、mariadb、php
网络环境:
ZabbixServer:192.168.5.254
ZabbixClient: 192.168.5.251、192.168.5.252
下载、安装编译ZABBIX2.4.8
http://www.zabbix.com/download.php
cd zabbix-2.4.8/ ./configure \ --prefix=/usr/local/zabbix \ --enable-server \ --enable-proxy \ --enable-agent \ --enable-java \ --with-mysql \ --with-libxml2 \ --with-net-snmp \ --with-openipmi \ --with-libcurl
配置mariadb
MariaDB [(none)]> GRANT ALL ON zabbix.* TO IDENTIFIED BY 'xxxxxxx'; MariaDB [(none)]> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)
导入Zabbix默认库
[root@localhost ~]# cd zabbix-2.4.8/database/mysql/ [root@localhost mysql]# ls data.sql images.sql schema.sql [root@localhost mysql]# mysql zabbix < schema.sql [root@localhost mysql]# mysql zabbix < images.sql [root@localhost mysql]# mysql zabbix < data.sql
复制zabbix网站至WEB工作目录,conf目录可读写
[root@localhost frontends]# cp -a /root/zabbix-2.4.8/frontends/php/* /var/www/html/ [root@localhost html]# chmod 777 /var/www/html/conf/ -R
访问ZABBIX首页,并安要求修改PHP参数
添加zabbix_server、zabbix_agentd,并设置相关程序自启动
[root@localhost core5]# pwd /root/zabbix-2.4.8/misc/init.d/fedora/core5 [root@localhost core5]# cp -a ./* /etc/init.d/ 修改Zabbix_server其中的Zabbix_BIN段的字符串 [root@localhost init.d] vim /etc/init.d/zabbix_agentd prog="Zabbix Server" ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_server" Zabbix_server,添加开机自启动 [root@localhost init.d]# chkconfig --add zabbix_server [root@localhost init.d]# service zabbix_server on 修改Agentd其中ZABBIX_BIN段的字符串,并添加开机自启动 [root@localhost init.d] vim /etc/init.d/zabbix_agentd prog="Zabbix Agent" ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_agentd" Zabbix_agtend,添加开机自启动 [root@localhost init.d]# chkconfig --add zabbix_agentd [root@localhost init.d]# service zabbix_agentd on 设置其他服务开机启动 [root@localhost init.d]# systemctl enable httpd mariadb
配置ZABBIX使用微信企业号报警
必须是微信企业号,参考:http://www.mamicode.com/info-detail-1007838.html
添加python脚本,并添加执行权限。 [root@localhost init.d]# vim /usr/local/zabbix/share/zabbix/alertscripts/weixin.py #!/usr/bin/env python # coding:utf-8 import sys import urllib2 import time import json import requests reload(sys) sys.setdefaultencoding('utf-8') touser = sys.argv[1] # 发送的用户名 title = sys.argv[2] # 位置参数获取title 适用于zabbix content = sys.argv[3] # 位置参数获取content 适用于zabbix class Token(object): # 获取token def __init__(self, corpid, corpsecret): self.baseurl = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}'.format( corpid, corpsecret) self.expire_time = sys.maxint def get_token(self): if self.expire_time > time.time(): request = urllib2.Request(self.baseurl) response = urllib2.urlopen(request) ret = response.read().strip() ret = json.loads(ret) if 'errcode' in ret.keys(): print >> ret['errmsg'], sys.stderr sys.exit(1) self.expire_time = time.time() + ret['expires_in' self.access_token = ret['access_token'] return self.access_token def send_msg(touser,title, content): # 发送消息 corpid = "xxxxxx" #此处隐藏 corpsecret = "xxxxxxxx" #此处隐藏 qs_token = Token(corpid=corpid, corpsecret=corpsecret).get_token() url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}".format( qs_token) payload = { "touser": touser, "msgtype": "text", "agentid": "0", "text": { "content": "{0}\n{1}".format(title, content) }, "safe": "0" } ret = requests.post(url, data=json.dumps(payload, ensure_ascii=False)) print ret.json() if __name__ == '__main__': send_msg(touser, title, content) 测试脚本执行是否正常。其中,0631是企业号中对应的号码 [root@localhost alertscripts]# ./weixin.py 0631 标题 正文 {u'errcode': 0, u'errmsg': u'ok'}
ZABBIX中的设置
为Zabbix administrators添加一个weixin的发送类型
停止zabbix_agentd查看脚本的发送告警是否成功
微信,以及短信猫的脚本程序,添加方式类似本文文档
1、邮件的脚本 #!/bin/bash SMTP_server='smtp.126.com' username='xxxxx' password='xxxxx'to_email_address="$1" message_subject_utf8="$2" message_body_utf8="$3" message_subject_gb2312=`iconv -t GB2312 -f UTF-8 << EOF $message_subject_utf8 EOF` [ $? -eq 0 ] && message_subject="$message_subject_gb2312" || message_subject="$message_subject_utf8" message_body_gb2312=`iconv -t GB2312 -f UTF-8 << EOF $message_body_utf8 EOF` [ $? -eq 0 ] && message_body="$message_body_gb2312" || message_body="$message_body_utf8" sendEmail='/usr/local/bin/sendEmail' $sendEmail -s "$SMTP_server" -xu "$username" -xp "$password" -f "$from_email_address" -t "$to_email_address" -u "$message_subject" -m "$message_body" -o message-content-type=text -o message-charset=gb2312 2、短信猫的脚本、测试使用金笛短信猫硬件 #!/bin/bash #SMS with gnokii PATH=/bin:/sbin/:/usr/bin:/usr/sbin:/usr/local/bin/:/usr/local/gnokii/bin/ export.UTF-8 LOGFILE='/tmp/zabbix_sms.log' DT=$(date +%F' '%T) echo "***************************START:$DT************************************" >> $LOGFILE echo 'Recipient='$1'' >> $LOGFILE echo 'Subject='$2'' >> $LOGFILE echo 'Message='$3'' >> $LOGFILE echo `` >> $LOGFILE MOBILE_NUMBER=`echo "$1"` # Log it echo 'Send Command:' >> $LOGFILE echo 'echo $3 | gnokii --sendsms $MOBILE_NUMBER ' >> $LOGFILE echo `` >> $LOGFILE # Send it echo 'Sending Process:' >> $LOGFILE echo "$3" | gnokii --sendsms "$MOBILE_NUMBER" 1>>$LOGFILE 2>&1 #EOF DT=$(date +%F' '%T) echo "***************************STOP:$DT************************************" >> $LOGFILE echo -e '\n' >> $LOGFILE