1.服務器:
192.168.9.111 zabbix 搭建zabbix
192.168.9.112 ng-to-re 搭建nginx 、tomcat、redis主從
192.168.9.113 za-my 搭建mysql主從
1 郵件報警
[root@ip-10-1-0-18 ~]# wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
[root@ip-10-1-0-18 ~]# yum -y install perl-Net-SSLeay perl-IO-Socket-SSL
[root@ip-10-1-0-18 ~]# tar xf sendEmail-v1.56.tar.gz -C /usr/local/
[root@ip-10-1-0-18 ~]# cd /usr/local/sendEmail-v1.56/
[root@ip-10-1-0-18 sendEmail-v1.56]# /bin/cp -ra sendEmail /usr/local/bin/
[root@ip-10-1-0-18 sendEmail-v1.56]# chmod +x /usr/local/bin/sendEmail
[root@ip-10-1-0-18 sendEmail-v1.56]# which sendmail
/sbin/sendmail
[root@ip-10-1-0-18 ~]# cd /usr/lib/zabbix/alertscripts
[root@ip-10-1-0-18 alertscripts]# vim sendmail.sh
#!/bin/bash
# author:Mr.chen
to=$1
subject=$2
body=$3
[email protected]
smtp=smtp.qq.com
passwd=iyvkubyfzfjjbgde
/usr/local/bin/sendEmail -f "$from" -t "$to" -s "$smtp" -u "$subject" -o message-content-type=html -o message-charset=utf8 -xu "$from" -xp "$passwd" -m "$body" -o tls=no
[root@ip-10-1-0-18 alertscripts]# chmod +x sendmail.sh
[root@ip-10-1-0-18 alertscripts]# chown zabbix.zabbix sendmail.sh
[root@ip-10-1-0-18 alertscripts]# sh sendmail.sh [email protected] "hello world" "新的一天"
Nov 26 12:37:03 ip-10-1-0-18 sendEmail[14996]: Email was sent successfully!
2.微信報警功能
2.1創建企業微信創建自應用
獲取以下參數:
企業ID或 CropID wwd777e2474ce584b9
AgentId 1000002
Secret ZlHBU34cImjCtIg3Dwl5iCuINUHONwkqRZPUTBjC_Yw
AppID=1000002 #這裏是創建的應用ID,即Agentld的值
PartyID=2 #接收消息的部門ID
獲取方式如圖:
點擊---我的企業---獲取企業id
點擊---應用於小程序--我們自建的zabbix服務(那個美女)---裏面有AgentId和Secret
點擊---通訊錄---點擊部門後的三個點--獲取部門id
2.2編寫報警腳本並更改權限和雙主
[root@zabbix alertscripts]# vim weixin.sh (此路徑在配置文件中,你自己設定的如果不改在/usr/lib/zabbix/alertscripts)
#!/bin/bash
# author:Mr.pan
CropID="wwd777e2474ce584b9" #這裏填寫我們的應用的CropID
Secret="ZlHBU34cImjCtIg3Dwl5iCuINUHONwkqRZPUTBjC_Yw" #這裏是應用的Secret
#下面的GURL和PURL地址無需改變,不用做任何變動
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
Gtoken=`/usr/bin/curl -s -G $GURL | awk -F\" '{print $10}'`
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"
function body() {
local int AppID=1000002 #這裏是創建的應用ID
local UserID=$1 #接收消息用戶,系統傳參
local PartyID=2 #接收消息的部門ID
local Msg=`echo "$@" | cut -d" " -f3-`
printf '{\n'
printf '\t"touser": "'"$UserID"\"",\n"
printf '\t"toparty": "'"$PartyID"\"",\n"
printf '\t"msgtype": "text",\n'
printf '\t"agentid": "'" $AppID "\"",\n"
printf '\t"text": {\n'
printf '\t\t"content": "'"$Msg"\""\n"
printf '\t},\n'
printf '\t"safe":"0"\n'
printf '}\n'
}
/usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL
[root@zabbix alertscripts]# chmod +x weixin.sh
[root@zabbix alertscripts]# chown zabbix.zabbix weixin.sh
[root@zabbix alertscripts]# ll
total 4
-rwxr-xr-x 1 zabbix zabbix 1151 Oct 21 23:45 weixin.sh
2.3測試腳本是否可用
[root@zabbix alertscripts]# sh weixin.sh yinsendemogui "題目" "報警內容"{"errcode":0,"errmsg":"ok","invaliduser":"#######"}
{"errcode":0,"errmsg":"ok","invaliduser":"yinsendemogui"}[root@zabbix alertscripts]#
#得到下面這行則表示成功,或者看企業微信是否有信息
2.4在zabbix中配置微信報警
點擊管理---添加報警媒介類型
點擊管理---用戶---選擇用戶(我們這裏選Administrator)
點擊添加---更新
點擊配置
動作---添加觸發條件(此處我們選擇示警度大於等於警告的報警)
操作---修改報警的內容格式(因爲原格式太亂)
#告警通知格式樣例
#題目
A故障:{TRIGGER.STATUS},服務器:{HOSTNAME1}發生:{TRIGGER.NAME}故障!
#內容
告警主機: ;{HOSTNAME1}<br/>
告警時間: ;{EVENT.DATE} {EVENT.TIME}<br/>
告警等級: ;{TRIGGER.SEVERITY}<br/>
告警信息: ;{TRIGGER.NAME}<br/>
告警項目: ;{TRIGGER.KEY1}<br/>
問題詳情: ;{ITEM.NAME} {ITEM.VALUE}<br/>
當前狀態: ;{TRIGGER.STATUS} {ITEM.VALUE1}<br/>
事件ID: ;{EVENT.ID}
添加操作,發送的次數、發送個哪個用戶、用什麼發送是所用媒介還是僅微信,此處默認1h我們可以不改
步驟1-3也就是從1開始到3結束。一旦發生故障,就是執行sendmail.sh腳本發生報警郵件給zabbix用戶。
假如故障持續了1個小時,它也只發送3次,第1-3次(即前3次)郵箱發送給zabbix用戶,時間間隔爲0秒。
如果改成1-0,0是表示不限制,無限發送。