zabbix------關於微信報警的嘗試

1 網上關於這個的文章很多,大家應該都可以照着做就行了,我只是把相關問題總結下,並記錄。

2 準備工作

2.1準備微信公衆號

2.1.1 註冊公衆號,我註冊的是個人的,當然企業的最好了,羣發比較方便,不過個人也湊合用。

https://mp.weixin.qq.com/

網站註冊一個公衆號,如果出現註冊失敗,請換郵箱,我試了我的所有qq都不行,我用的126成功了,所以一旦註冊失敗請自覺換郵箱。

2.1.2 登錄公衆號

這時候大概會讓你綁定自己的管理微信號,掃個碼,然後綁定銀行卡(坑...)

最後大概是這個界面

wKiom1aMv93Q8n8bAAIOTmu3uXM494.png

點擊網頁上面的訂閱號,然後讓小夥伴掃一下二維碼關注,就會有一個用戶,點擊總用戶數,單機用戶的名稱的超鏈接位置,然後會大概是這樣的一個網絡url,

https://mp.weixin.qq.com/cgi-bin/singlesendpage?t=message/send&action=index&tofakeid=oVa4RwSPu3O7HC_5iDN8HowWQ6k&token=2020697380&lang=zh_CN,這個fakeid就是我們需要的,注意是fakeid,而不是微信id號,而你瞎發一個的話,一定是system error

2.2 準備API

 

[root@Zabbix-Server ~]# cd /etc/zabbix/alertscripts(你的報警腳本的位置,其實無所謂,不過爲了好管理)
[root@Zabbix-Server alertscripts]# git clone 
[root@Zabbix-Server ]# cd WeiXin-Private-API
[root@Zabbix-Server WeiXin-Private-API]# sed 's#CURLOPT_SSL_VERIFYHOST, 1#CURLOPT_SSL_VERIFYHOST, 2#' include/LeaWeiXinClient.php
[root@Zabbix-Server WeiXin-Private-API]# cat config.php
<?php

// 全局配置

$G_ROOT = dirname(__FILE__);

$G_CONFIG["weiXin"] = array(
	'account' => '你申請的公衆號的登錄名',
	'password' => '你申請的公衆號的登錄密碼',
	'cookiePath' => $G_ROOT. '/cache/cookie', // cookie緩存文件路徑
	'webTokenPath' => $G_ROOT. '/cache/webToken', // webToken緩存文件路徑
); 

[root@Zabbix-Server WeiXin-Private-API]# cat test.php | egrep -v '//|^$'
<?php
require "config.php";
require "include/WeiXin.php";
$weiXin = new WeiXin($G_CONFIG['weiXin']);
$testFakeId = "$argv[1]";
$msg="$argv[3]";
print_r($weiXin->send($testFakeId, "$msg"));
測試發微信

網友是寫了個腳本weixin.sh
[root@Zabbix-Server WeiXin-Private-API]# cat ../weixin.sh 
/usr/bin/php /etc/zabbix/alertscripts/WeiXin-Private-API/test.php "$1" "$2" "$3"
測試過程
①隨便寫個fakeid
[root@Zabbix-Server WeiXin-Private-API]# ../weixin.sh liuliancao '' 'hello'
PHP Notice:  Undefined index: HTTP_USER_AGENT in /usr/local/zabbix/share/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php on line 33

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

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] => 200003
            [err_msg] => invalid session
        )

)
②用關注你的人的fakeid然後就發現可以發了,由於我是個人用戶,我懷疑可能有多方面的限制吧,後來也出現了上面的錯誤,這裏我還發現如果清空一下緩存就又好了,以下是我的操作情況

多次發後報錯
[root@Zabbix-Server alertscripts]#  ./weixin.sh oVa4RwSPu-3O7HC_5iDN8HowWQ6k  ""  "hel"
PHP Notice:  Undefined index: HTTP_USER_AGENT in /usr/local/zabbix/share/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php on line 33

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

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] => 200003
            [err_msg] => invalid session
        )

)

清空緩存
[root@Zabbix-Server alertscripts] echo ''> WeiXin-Private-API/cache/webToken 
[root@Zabbix-Server alertscripts] echo ''> WeiXin-Private-API/cache/cookie

再次發送
[root@Zabbix-Server alertscripts]#  ./weixin.sh oVa4RwSPu-3O7HC_5iDN8HowWQ6k  ""  "hel"
PHP Notice:  Undefined index: HTTP_USER_AGENT in /usr/local/zabbix/share/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php on line 33

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

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
        )

)

然後大致總結如下吧,至於web添加我就不想演示了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章