一、注冊微信公眾號
首先申請微信公眾平臺https://mp.weixin.qq.com/一個人最多申請5個公眾號,所以還是可以的
申請完之后就可以根據騰訊的提示使用微信公眾號了,然后用你自己的微信掃描關注微信號。
就可以看到用戶數了,接下來的就要使用的用戶的微信ID號了。點擊用戶查看用戶的微信ID號。在瀏覽器查看用戶的微信ID號。就是那個紅色的ID了。
https://mp.weixin.qq.com/cgi-bin/singlesendpage?t=message/send&action=index&tofakeid=770373640&token=748989398&lang=zh_CN
二、下載并配置微信公眾平臺私有接口
git clone https ://github.com/lealife/WeiXin-Private-API 修改配置文件 [root@zabbix ~]# git clone https://github.com/lealife/WeiXin-Private-API [root@zabbix ~]# cp -r WeiXin-Private-API/ /usr/local/zabbix/share/zabbix/alertscripts/ [root@zabbix ~]# cd /usr/local/zabbix/share/zabbix/alertscripts/WeiXin-Private-API [root@zabbix WeiXin-Private-API]# chown zabbix.zabbix /usr/local/zabbix/share/zabbix/alertscripts/WeiXin-Private-API 修改config文件
[root@zabbix WeiXin-Private-API]# vi config.php <?php // 全局配置 $G_ROOT = dirname(__FILE__); $G_CONFIG["weiXin"] = array( 'account' => '微信公眾登錄號碼', 'password' => '微信公眾登錄密碼', require "include/WeiXin.php"; 'cookiePath' => $G_ROOT. '/cache/cookie', // cookie緩存文件路徑 'webTokenPath' => $G_ROOT. '/cache/webToken', // webToken緩存文件路徑 ); 修改test.php文件,只需要保存這幾行即可 <?php require "config.php"; $weiXin = new WeiXin($G_CONFIG['weiXin']); $testFakeId = "$argv[1]"; print_r($weiXin->send($testFakeId, "$msg")); $msg="$argv[3]";
注意這里$msg=”$argv[3]”表示zabbix傳入的第三個參數,因為在zabbix報警時會傳入三個參數:一是微信好友ID,二是報警信息的主題,三是報警信息的具體內容,這里跳過了報警信息主題,直接發送報警信息內容創建微信報警腳本weixin
[root@zabbix alertscripts]# vi weixin /usr/bin/php /usr/local/zabbix/share/zabbix/alertscripts/WeiXin-Private-API/test.php "$1" "$2" "$3" [root@zabbix alertscripts]# chown -R zabbix.zabbix weixin [root@zabbix alertscripts]# chmod +x weixin
測試報警
[root@zabbix alertscripts]# /usr/local/zabbix/share/zabbix/alertscripts/weixin 770373640 "" "hello" PHP Notice: Undefined index: HTTP_USER_AGENT in /usr/local/zabbix/share/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php on line 33 PHP Notice: Undefined index: HTTP_USER_AGENT in /usr/local/zabbix/share/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php on line 33 ) stdClass Object ( [base_resp] => stdClass Object ( [ret] => 0 [err_msg] => ok )
查看結果
三、配置zabbix
添加報警媒介
用戶添加報警媒介,這里使用默認的administrator用戶
添加報警動作
信息如下,使用默認的信息也可以
修改操作條件,使用默認的也是可以的
保存設置
四、驗證結果
重啟一臺虛擬機進行測試。由于我也開啟了郵件報警,所以看一下郵件先
看一下微信
到此為至zabbix的報警功能就實現了
五、mutt+msmtp發送郵件,實現報警功能
配置yum源如下
[root@localhost ~]# cat /etc/yum.repos.d/test.repo [test] name=test baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/ gpgcheck=0 enabled=1 [test2] name=test2 enabled=1 baseurl=https://mirrors.aliyun.com/epel/7Server/x86_64/ gpgcheck=0 [root@localhost ~]# yum -y install mutt msmtp
配置/root/.muttrc如下
[root@localhost ~]# cat .muttrc set sendmail="/usr/bin/msmtp" set from=1582209xxxx@139.com set use_from=yes set envelope_from=yes
配置/root/.msmtprc如下
[root@localhost ~]# cat .msmtprc host smtp.139.com tls off from 1582209xxxx@139.com auth plain user 1582209xxxx@139.com password 1922xxxxxxxx [root@localhost ~]# !vim vim /root/bin/mutt.sh [root@localhost ~]# cat /root/bin/mutt.sh #!/bin/bash #Author Jin #Time 20160203 while sleep 1; do if ss -tnl | grep --color=auto ":80" ; then continue else echo "web Server Down" echo "Web Server Down" | mutt -s "Nginx Server" 1922006891@qq.com 15822097176@139.com; fi done
[root@localhost ~]# !chmod chmod +x /root/bin/mutt.sh [root@localhost ~]# systemctl start nginx [root@localhost ~]# mutt.sh LISTEN 0 128 *:80 *:* LISTEN 0 128 :::80 :::* LISTEN 0 128 *:80 *:* LISTEN 0 128 :::80 :::* LISTEN 0 128 *:80 *:* LISTEN 0 128 :::80 :::* LISTEN 0 128 *:80 *:* LISTEN 0 128 :::80 :::* LISTEN 0 128 *:80 *:* LISTEN 0 128 :::80 :::* LISTEN 0 128 *:80 *:* LISTEN 0 128 :::80 :::* LISTEN 0 128 *:80 *:* LISTEN 0 128 :::80 :::* LISTEN 0 128 *:80 *:* LISTEN 0 128 :::80 :::* LISTEN 0 128 *:80 *:* LISTEN 0 128 :::80 :::* LISTEN 0 128 *:80 *:*
此時將nginx服務停掉
[root@localhost ~]# systemctl stop nginx LISTEN 0 128 :::80 :::* web Server Down web Server Down web Server Down web Server Down web Server Down web Server Down web Server Down web Server Down
收到報警郵件及報警短信
同第三大步一樣,配置zabbix的時候,將此腳本調入進去就可以了
原創文章,作者:renjin,如若轉載,請注明出處:http://www.www58058.com/78721