配置 ZABBIX 使用企業微信發送 Alert 消息

成功的從企業微信中收到的 Alert 消息的樣子:php

Oracle 日誌 Alert:
配置 ZABBIX 使用企業微信發送 Alert 消息前端

網絡專線 Alert:
配置 ZABBIX 使用企業微信發送 Alert 消息json

服務器 Alert:
配置 ZABBIX 使用企業微信發送 Alert 消息api

Linux 命令行使用方法:緩存

./workweixin_send.php
Usage: workweixin_send.php.php <username> <title> <content>
./workweixin_send.php <這裏填寫企業我的微信ID> hi  這是一條測試的企業微信消息

Windows 命令行使用方法:安全

php workweixin_send.php <這裏填寫企業我的微信ID> hi  這是一條測試的企業微信消息

測試消息發送成功的樣子:
配置 ZABBIX 使用企業微信發送 Alert 消息服務器

ZABBIX WEB前端中的配置:
配置 ZABBIX 使用企業微信發送 Alert 消息
配置 ZABBIX 使用企業微信發送 Alert 消息微信

ZABBIX 配置文件中的配置:網絡

AlertScriptsPath=/opt/zabbix/scripts/app

PHP腳本(須要安裝PHP解釋器和MEMCACHE,能夠參照這個文章進行安裝[《PHP版本5到7的源碼編譯安裝》](http://www.javashuo.com/article/p-ysbmjobu-d.html)):

#!/opt/php/bin/php -q
<?php
class Cache
{   
    /**
     * Cache instance
     * @var Cache
     */
    private static $instance;

    protected $conn;

    /**
     * 得到一個 cache 緩存實例
     * @param mixed $params
     * @return Cache
     */
    public static function getConnect($params = '') : Cache
    {
        if (! self::$instance instanceof self) {
            self::$instance = new self($params);
        }
        return self::$instance;
    }

    /**
     * 實例化一個 cache 實例
     * @param mixed $params
     * @throws \Exception
     */
    private function __construct($params)
    {
        if (! is_array($params)) {
            throw new \Exception('Invalid cache params, failed connect to cache server');
        }
        if (!isset($params['host'])) {
            throw new \Exception('Invalid cache host');
        }
        if (!isset($params['port']) or ($params['port'] < 1 and $params['port'] > 65535)) {
            throw new \Exception('Invalid cache port');
        }
        if (!isset($params['persistent'])) {
            $params['persistent'] = false;
        }
        $params['persistent'] = boolval($params['persistent']);

        $this->conn= new \Memcache();
        if ($params['persistent']) {
            $conn = $this->conn->pconnect($params['host'], $params['port']);
        } else {
            $conn = $this->conn->connect($params['host'], $params['port']);
        }
        if ($conn === false) {
            throw new \Exception('Failed connect to cache server');
        }
    }

    /**
     * 返回緩存值
     * @param string $key
     * @param mixed $default
     */
    public function get(string $key, $default = null)
    {
        if (!empty($key)) {
            $default = $this->conn->get($key);
        }
        return $default;
    }

    /**
     * 設置緩存值
     * @param string $key
     * @param mixed $value
     * @param int $ttl
     * @param bool $compress
     * @return bool
     */
    public function set(string $key, $value = null, int $ttl = null, $compress = false)
    {
        if ($ttl === null) {
            $ttl = $this->getTTL();
        }
        if ($compress) {
            return $this->conn->set($key, $value, MEMCACHE_COMPRESSED, $ttl);
        } else {
            return $this->conn->set($key, $value, 0 , $ttl);
        }
    }
}

class AccessToken
{   
    /**
     * 企業ID<p>
     * 每一個企業都擁有惟一的corpid,獲取此信息可在管理後臺「個人企業」-「企業信息」下查看「企業ID」
     * @var string
     */
    protected $corpid;

    /**
     * secret是企業應用裏面用於保障數據安全的「鑰匙」,每個應用都有一個獨立的訪問密鑰,爲了保證數據的安全,secret務必不能泄漏
     * @var string
     */
    protected $secret;

    /**
     * 獲取 access token 的 url
     * @var string
     */
    protected $url;

    /**
     * 過時時間
     * @var string
     */
    protected $expire;

    /**
     * 配置 corpid 和 secret
     * @param string $corpid
     * @param string $secret
     */
    public function __construct(string $corpid, string $secret)
    {
        $this->corpid = $corpid;
        $this->secret = $secret;
    }

    /**
     * 設置獲取 access token 的 url
     * @param string $url
     */
    public function setUrl(string $url) : AccessToken
    {
        $this->url = str_replace(['{corpid}', '{secret}'], [$this->corpid, $this->secret], $url);
        return $this;
    }

    /**
     * 返回 access token
     * @return string
     */
    public function getAccessToken() : string
    {
        $cache = Cache::getConnect();
        $accessToken = $cache->get('work.weixin.access.token');
        if (empty($accessToken)) {
            $accessToken = $this->getAccessTokenFromServer();
            $cache->set('work.weixin.access.token', $accessToken, $this->getExpire() - 60);
        }
        return $accessToken;
    }

    /**
     * 返回 access token 的有效時間
     * @return int
     */
    public function getExpire() : int
    {
        return $this->expire;
    }

    /**
     * 從企業微信服務器上獲取 access token
     * @return string
     */
    final protected function getAccessTokenFromServer() : string
    {
        $ch = curl_init();
        $options = [
            CURLOPT_URL             => $this->url,
            CURLOPT_TIMEOUT         => 10,
            CURLOPT_RETURNTRANSFER  => 1,
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_SSL_VERIFYHOST  => false
        ];
        curl_setopt_array($ch, $options);
        $response = curl_exec($ch);

        /*
         * 微信接口返回數據格式:
         * {'errcode','errmsg','access_token','expires_in'}
         */
        $data = json_decode($response, true);

        if (empty($data)) {
            throw new \Exception('Failed to access work weixin api');
        }

        // 判斷是否成功取得access_token
        if ($data['errcode'] !== 0) {
            throw new \Exception('Failed to get work weixin access token from server. ' . $data['errmsg']);
        }

        $this->expire = $data['expires_in'];

        return $data['access_token'];
    }
}

class SendMessage
{

    /**
     * access token instance
     * @var AccessToken
     */
    protected $accessToken;

    /**
     * 發送消息的url
     * @var string
     */
    protected $url;

    /**
     * 成員ID列表(指定爲@all時則向關注該企業應用的所有成員發送)
     * @var array
     */
    protected $toUsers = [];

    /**
     * 部門ID列表(當touser爲@all時忽略本參數)
     * @var array
     */
    protected $toParties = [];

    /**
     * 標籤ID列表(當touser爲@all時忽略本參數)
     * @var array
     */
    protected $toTags = [];

    /**
     * 企業應用的id,整型。可在應用的設置頁面查看
     * @var int
     */
    protected $agentId;

    /**
     * 消息內容
     * @var string
     */
    protected $content;

    /**
     * 保密信息(表示是不是保密消息,0表示否,1表示是,默認0)
     * @var int
     */
    protected $safe = 0;

    public function __construct(AccessToken $accessToken)
    {
        $this->accessToken = $accessToken;
    }

    /**
     * 設置成員ID
     * @param array $users
     * @return SendMessage
     */
    public function setToUsers(array $users) : SendMessage
    {
        foreach ($users as $k => $user) {
            if (is_string($user)) {
                $this->toUsers[] = $user;
            }
        }
        return $this;
    }

    /**
     * 設置部門ID
     * @param array $parties
     * @return SendMessage
     */
    public function setToParties(array $parties) : SendMessage
    {
        foreach ($parties as $k => $party) {
            if (is_string($party)) {
                $this->toParties[] = $party;
            }
        }
        return $this;
    }

    /**
     * 設置標籤ID
     * @param array $tags
     * @return SendMessage
     */
    public function setToTag(array $tags) : SendMessage
    {
        foreach ($tags as $k => $tag) {
            if (is_string($tag)) {
                $this->toTags[] = $tag;
            }
        }
        return $this;
    }

    /**
     * 設置應用ID
     * @param int $id
     * @return SendMessage
     */
    public function setAgentId(int $id) : SendMessage
    {
        $this->agentId = $id;
        return $this;
    }

    /**
     * 設置消息內容(最長不超過2048個字節)
     * @param string $content
     * @return SendMessage
     */
    public function setContent(string $content) : SendMessage
    {
        if (!empty($content)) {
            $this->content = $content;
        }
        return $this;
    }

    public function setUrl(string $url) : SendMessage
    {
        $this->url = str_replace('{accessToken}', $this->accessToken->getAccessToken(), $url);
        return $this;
    }

    /**
     * 調用企業微信 api 發送消息
     * @throws \Exception
     */
    private function send(string $data)
    {
        $ch = curl_init();
        $options = [
            CURLOPT_URL             => $this->url,
            CURLOPT_HEADER          => 0,
            CURLOPT_POST            => 1,
            CURLOPT_POSTFIELDS      => $data,
            CURLOPT_RETURNTRANSFER  => 1,
            CURLOPT_TIMEOUT         => 10,
            CURLOPT_SSL_VERIFYPEER  => 0,
            CURLOPT_SSL_VERIFYHOST  => 0
        ];
        curl_setopt_array($ch, $options);
        $response = curl_exec($ch);

        $data = json_decode($response, true);

        if (empty($data)) {
            throw new \Exception('Failed to access work weixin api');
        }
        if ($data['errcode'] !== 0) {
            throw new \Exception('Failed to send WeiXin message. ' . $data['errmsg']);
        }
    }

    /**
     * 發送文本消息
     * @throws \Exception
     */
    public function sendTextMessage() {
        if (empty($this->toUsers) && empty($this->toParties) && empty($this->toTags)) {
            throw new \Exception('Invalid message recevier');
        }
        if (empty($this->agentId)) {
            throw new \Exception('Invalid app agentid');
        }
        if (empty($this->content)) {
            throw new \Exception('Empty message content');
        }
        $data = json_encode([
            'touser'    => implode('|', $this->toUsers),
            'toparty'   => implode('|', $this->toParties),
            'totag'     => implode('|', $this->toTags),
            'msgtype'   => 'text',
            'agentid'   => $this->agentId,
            'safe'      => $this->safe,
            'text'      => [
                'content'   => $this->content
            ]
        ], JSON_UNESCAPED_UNICODE);

        // 若是第一次發送失敗,嘗試兩次發送
        try {
            $this->send($data);
        } catch (\Exception $e) {
            $this->send($data);
        }
    }

}

// -----------------------------------------------------------------------------------

$cacheParams = [
    'host' => '127.0.0.1',
    'port' => '11211'
];
// 每一個企業都擁有惟一的corpid,獲取此信息可在管理後臺「個人企業」-「企業信息」下查看「企業ID」
$corpid = '*************';
//secret是企業應用裏面用於保障數據安全的「鑰匙」,每個應用都有一個獨立的訪問密鑰,爲了保證數據的安全,secret務必不能泄漏
$secret = '*************';
// agentid 企業應用的id,整型。可在應用的設置頁面查看
$agentid = 1;
$fetchAccessTokenUrl= "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={secret}";
$sendMessageUrl = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={accessToken}';

// -----------------------------------------------------------------------------------

if (!isset($argv[1]) || !isset($argv[2]) || !isset($argv[3])) {
    echo 'Usage: ' . basename(__FILE__) . '.php <username> <title> <content>';
    return;
}

// -----------------------------------------------------------------------------------

try {

    Cache::getConnect($cacheParams);

    $accessToken = new AccessToken($corpid, $secret);
    $accessToken->setUrl($fetchAccessTokenUrl);

    $wwx = new SendMessage($accessToken);
    $wwx->setAgentId($agentid)
        ->setUrl($sendMessageUrl)
        ->setToUsers([$argv[1]])
        ->setContent(str_replace(' ', '', strip_tags($argv[3])))
        ->sendTextMessage();
} catch (\Exception $e) {
    echo $e->getMessage();
    file_put_contents(basename(__FILE__, '.php') . '.log', $e->getMessage());
}
相關文章
相關標籤/搜索