Title: 2015-12-30-通过微信发送zabbix告警
Date: 2015-12-30
Author: ColinLiu
Category: Zabbix
tags: zabbix,weixin
[TOC]
注册微信公共号
具体的注册流程可以参考 [注册个人微信公共号](https://kf.qq.com/faq/120911VrYVrA130620u2iA7n.html)
配置zabbix通过微信公共号发送告警信息
下载 weixin API
cd /etc/zabbix/alertscripts/
git clone https://github.com/lealife/WeiXin-Private-API
配置
# 修改权限
root@pts/0 $ chown -R zabbix:zabbix WeiXin-Private-API
# 查看
root@pts/0 $ ll
total 20
drwxr-xr-x 2 zabbix zabbix 4096 Dec 30 15:29 cache
-rwxr-xr-x 1 zabbix zabbix 298 Dec 30 15:31 config.php
drwxr-xr-x 2 zabbix zabbix 4096 Dec 30 15:29 include
-rw-r--r-- 1 zabbix zabbix 1478 Dec 30 15:29 README.md
-rwxr-xr-x 1 zabbix zabbix 2242 Dec 30 15:34 test.php
# 修改WX API 配置
cat /etc/zabbix/alertscripts/WeiXin-Private-API/config.php
<?php
// 全局配置
$G_ROOT = dirname(__FILE__);
$G_CONFIG["weiXin"] = array(
'account' => 'xxxxxx', # 微信公共号账号
'password' => 'xxxxxx', # 微信公共号密码
'cookiePath' => $G_ROOT. '/cache/cookie', // cookie缓存文件路径
'webTokenPath' => $G_ROOT. '/cache/webToken', // webToken缓存文件路径
);
# 修改test.php文件
$testFakeId = "$argv[1]"; ## 接受人微信Id
$msg = "$argv[3]"; ## 要发送的信息
print_r($weiXin->send($testFakeId, "$msg")); ## 微信不记录发送标题
# 添加weixin告警脚本
root@pts/0 $ cat /etc/zabbix/alertscripts/weixin
/usr/bin/php /etc/zabbix/alertscripts/WeiXin-Private-API/test.php "$1" "$2" "$3"
测试
测试之前必须知道对方的fakeid
。
这个可以从微信公共号里面 用户管理
,点击用户的头像会得到一个URL,
https://mp.weixin.qq.com/cgi-bin/singlesendpage?t=message/send&action=index&tofakeid=o55Kbs28rLBti0mRZ0IGgxyjpHHg&token=2102840639&lang=zh_CN
这个 tofakeid=o55Kbs28rLBti0mRZ0IGgxyjpHHg
等号后面的值就是fakeid
具体测试如下:
root@pts/0 $ /etc/zabbix/alertscripts/weixin o55Kbs28rLBti0mRZ0IGgxyjpHHg Test-subject Test-content-to-liuchao
PHP Notice: curl_setopt(): CURLOPT_SSL_VERIFYHOST no longer accepts the value 1, value 2 will be used instead in /etc/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php on line 32
PHP Stack trace:
PHP 1. {main}() /etc/zabbix/alertscripts/WeiXin-Private-API/test.php:0
PHP 2. WeiXin->__construct() /etc/zabbix/alertscripts/WeiXin-Private-API/test.php:5
PHP 3. WeiXin->getCookieAndWebToken() /etc/zabbix/alertscripts/WeiXin-Private-API/include/WeiXin.php:47
PHP 4. WeiXin->getUserInfo() /etc/zabbix/alertscripts/WeiXin-Private-API/include/WeiXin.php:100
PHP 5. LeaWeiXinClient->submit() /etc/zabbix/alertscripts/WeiXin-Private-API/include/WeiXin.php:173
PHP 6. LeaWeiXinClient->exec() /etc/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php:7
PHP 7. curl_setopt() /etc/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php:32
PHP Notice: Undefined index: HTTP_USER_AGENT in /etc/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php on line 33
PHP Stack trace:
PHP 1. {main}() /etc/zabbix/alertscripts/WeiXin-Private-API/test.php:0
PHP 2. WeiXin->__construct() /etc/zabbix/alertscripts/WeiXin-Private-API/test.php:5
PHP 3. WeiXin->getCookieAndWebToken() /etc/zabbix/alertscripts/WeiXin-Private-API/include/WeiXin.php:47
PHP 4. WeiXin->getUserInfo() /etc/zabbix/alertscripts/WeiXin-Private-API/include/WeiXin.php:100
PHP 5. LeaWeiXinClient->submit() /etc/zabbix/alertscripts/WeiXin-Private-API/include/WeiXin.php:173
PHP 6. LeaWeiXinClient->exec() /etc/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php:7
<pre>发送消息</br>PHP Notice: curl_setopt(): CURLOPT_SSL_VERIFYHOST no longer accepts the value 1, value 2 will be used instead in /etc/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php on line 32
PHP Stack trace:
PHP 1. {main}() /etc/zabbix/alertscripts/WeiXin-Private-API/test.php:0
PHP 2. WeiXin->send() /etc/zabbix/alertscripts/WeiXin-Private-API/test.php:17
PHP 3. LeaWeiXinClient->submit() /etc/zabbix/alertscripts/WeiXin-Private-API/include/WeiXin.php:125
PHP 4. LeaWeiXinClient->exec() /etc/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php:7
PHP 5. curl_setopt() /etc/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php:32
PHP Notice: Undefined index: HTTP_USER_AGENT in /etc/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php on line 33
PHP Stack trace:
PHP 1. {main}() /etc/zabbix/alertscripts/WeiXin-Private-API/test.php:0
PHP 2. WeiXin->send() /etc/zabbix/alertscripts/WeiXin-Private-API/test.php:17
PHP 3. LeaWeiXinClient->submit() /etc/zabbix/alertscripts/WeiXin-Private-API/include/WeiXin.php:125
PHP 4. LeaWeiXinClient->exec() /etc/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php:7
stdClass Object
(
[base_resp] => stdClass Object
(
[ret] => 0
[err_msg] => ok
)
)
得到用户信息</br>PHP Notice: curl_setopt(): CURLOPT_SSL_VERIFYHOST no longer accepts the value 1, value 2 will be used instead in /etc/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php on line 32
PHP Stack trace:
PHP 1. {main}() /etc/zabbix/alertscripts/WeiXin-Private-API/test.php:0
PHP 2. WeiXin->getUserInfo() /etc/zabbix/alertscripts/WeiXin-Private-API/test.php:26
PHP 3. LeaWeiXinClient->submit() /etc/zabbix/alertscripts/WeiXin-Private-API/include/WeiXin.php:173
PHP 4. LeaWeiXinClient->exec() /etc/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php:7
PHP 5. curl_setopt() /etc/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php:32
PHP Notice: Undefined index: HTTP_USER_AGENT in /etc/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php on line 33
PHP Stack trace:
PHP 1. {main}() /etc/zabbix/alertscripts/WeiXin-Private-API/test.php:0
PHP 2. WeiXin->getUserInfo() /etc/zabbix/alertscripts/WeiXin-Private-API/test.php:26
PHP 3. LeaWeiXinClient->submit() /etc/zabbix/alertscripts/WeiXin-Private-API/include/WeiXin.php:173
PHP 4. LeaWeiXinClient->exec() /etc/zabbix/alertscripts/WeiXin-Private-API/include/LeaWeiXinClient.php:7
Array
(
[base_resp] => Array
(
[ret] => 0
[err_msg] => ok
)
... ...
... ...
具体zabbix配置和脚本发送邮件配置一样,
具体参考后续的zabbix系列文档。
网友评论