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的時候,將此腳本調入進去就可以了

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

(0)
renjinrenjin
上一篇 2014-10-15
下一篇 2015-01-12

相關推薦

  • 網絡管理之多網卡bond,mode1

    一.概述 Bonding     就是將多塊網卡綁定同一IP地址對外提供服務,可以實現高可用或者負載均衡。當然,直接給兩塊網卡設置同一IP地址是不可能的。通過bonding,虛擬一塊網卡對外提供連接,物理網卡的被修改為相同的MAC地址。 Bonding的工作模式     Mode…

    Linux干貨 2016-09-15
  • N27網絡班第6周作業

    1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#; :%s@^[[:space:]]\+@#&@g 2、復制/boot/grub/grub.conf 至/tmp目錄中,刪除/tmp/grub.conf文件中的行首的空白字符 :%s@^[[:space:]]…

    Linux干貨 2017-09-03
  • 處理文本的工具sed,vim 編輯器的使用 Linux版

    處理文本的工具sed Stream EDitor 行編輯器 sed一次處理一行的內容,處理時,將當前處理的行存儲在臨時緩沖區,稱為“模式空間”,接著用sed命令處理緩沖區中的內容,處理完成后,將緩沖區的內容送往屏幕,接著處理下一行不斷重復,直到末尾。            一次處理一行 文件&#82…

    Linux干貨 2016-08-11
  • Linux基礎知識之文本查找和正則表達式擴展正則表達式

    1.什么是正則表達式?      正則表達式就是處理字符串的方法,它是以行為單位來進行字符串的處理行為,正則表達式通過一些特殊符號的復制,讓用戶可以輕易達到查找、刪除、替換某些特定字符串的處理程序。      正則表達式基本上是一種“表示法”,只要工具程序支持這種表示法,那么該工作程序就可以用來作為…

    Linux干貨 2016-08-10
  • 第三周(3):課堂練習與作業

    課堂練習: 1、找出ifconfig命令結果中本機的所有IPv4地址 [root@centos6 ~]# ifconfig | tr -cs '[0-9].' '\n'|sort -ut. -k3n 127.0.0.1 10.1.…

    Linux干貨 2016-08-08
  • Shell腳本編程基礎

    1編程基礎 程序:指令+數據 程序編程分格: 過程式:以指令為中心,數據服務于指令 對象式:以數據為中心,指令服務于數據 Shell程序:提供了編程能力,解釋執行 程序的執行方式 計算機:運行二進制指令 編程語言: 低級:匯編 高級:編譯:高級語言->編譯器->目標代碼 編譯性語言編寫的程序通常是需要通過編譯器去轉換成計算機可以識別的二進制文件,…

    2017-11-27
欧美性久久久久