zabbix之報警功能的實現

一、注冊微信公眾號

首先申請微信公眾平臺https://mp.weixin.qq.com/一個人最多申請5個公眾號,所以還是可以的

申請完之后就可以根據騰訊的提示使用微信公眾號了,然后用你自己的微信掃描關注微信號。

1.png

就可以看到用戶數了,接下來的就要使用的用戶的微信ID號了。點擊用戶查看用戶的微信ID號。在瀏覽器查看用戶的微信ID號。就是那個紅色的ID了。

https://mp.weixin.qq.com/cgi-bin/singlesendpage?t=message/send&action=index&tofakeid=770373640&token=748989398&lang=zh_CN

2.png

二、下載并配置微信公眾平臺私有接口

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 )

查看結果

3.png

三、配置zabbix

添加報警媒介

4.png

用戶添加報警媒介,這里使用默認的administrator用戶

5.png

添加報警動作

6.png

信息如下,使用默認的信息也可以

7.png

修改操作條件,使用默認的也是可以的

保存設置

四、驗證結果

重啟一臺虛擬機進行測試。由于我也開啟了郵件報警,所以看一下郵件先

8.png

看一下微信

9.jpg

到此為至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之報警功能的實現

zabbix之報警功能的實現

同第三大步一樣,配置zabbix的時候,將此腳本調入進去就可以了

原創文章,作者:kang,如若轉載,請注明出處:http://www.www58058.com/79159

(0)
kangkang
上一篇 2017-03-27 10:34
下一篇 2017-03-27 16:05

相關推薦

  • 推薦-Nginx專題: upstream模塊和緩存的簡單使用

    Nginx專題: upstream模塊和緩存的簡單使用 nginx 負載均衡 Nginx專題: upstream模塊和緩存的簡單使用 前言: 實驗拓撲 實驗環境 Nginx_Upstream模塊介紹 upstream使用方法 upstream使用示例 Nginx_Cache介紹 緩存靜態資源 總結 前言: 本文接著上篇Nginx專題: 從編譯安裝到…

    Linux干貨 2016-04-07
  • 硬鏈接和軟鏈接的說明

    1、硬鏈接和軟鏈接的區別    (1)、原理上:                   【1】 硬鏈接(hard link):                 &…

    2017-07-21
  • 系統基礎之權限管理作業題

    1.問題:  在/data/testdir里創建的新文件自動屬于g1組,組g2的成員如: alice能對這些新文件有讀寫權限,組g3的成員如:tom只能對新文件有讀權限,其它用戶(不屬于g1,g2,g3)不能訪問這個文件夾。 [root@wen-7 testdir]# mkdir -p /data/…

    Linux干貨 2016-08-04
  • Linux下編譯內核

                           Linux下編譯內核 內核編譯: 編譯內核就是把內核的相關文件重新生成。   內核編譯前期準備: Linux kernel下載網址:https://www.kernel.or…

    系統運維 2016-09-21
  • N25 – Week 4 blog

    第四周博客作業了,感覺前面的有好多都忘掉了,但是沒有時間補以前的啊……好郁悶,幸好今天周五,明天又是圣誕節,從馬云爸爸買了一顆圣誕樹,今晚回家裝上 1. 復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其他用戶均沒有任何訪問權限 [root@dhcp-10-129-6-166&nb…

    Linux干貨 2016-12-20
  • 第三周作業

    1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。 [root@localhost /]# who | cut -d ‘ ‘ -f 1  | sort -u root user1 或: [root@localhost /]# who | cut -d ‘ ‘ -…

    Linux干貨 2017-02-24
欧美性久久久久