系統環境:php
操做系統:CentOS7.2html
依賴軟件:gnokii、zabbix2.八、mariadb、phpjava
網絡環境:python
ZabbixServer:192.168.5.254mysql
ZabbixClient: 192.168.5.25一、192.168.5.252sql
下載、安裝編譯ZABBIX2.4.8shell
http://www.zabbix.com/download.phpjson
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
配置mariadbvim
MariaDB [(none)]> GRANT ALL ON zabbix.* TO IDENTIFIED BY 'xxxxxxx'; MariaDB [(none)]> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)
導入Zabbix默認庫api
[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查看腳本的發送告警是否成功
微信,以及短信貓的腳本程序,添加方式相似本文文檔
一、郵件的腳本 #!/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 二、短信貓的腳本、測試使用金笛短信貓硬件 #!/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