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 18:17
下一篇 2015-01-12 16:01

相關推薦

  • Heartbeat高可用http

    Heartbeat高可用http 實驗圖 heartbeat高可用http 一、實驗圖: 二、heartbeat高可用http(Heartbeat V1 haresource) heartbeat節點ssh互信 heartbeat節點名稱相互解析,uname -n heartbeat節點時間一致性 安裝前準備: ssh多節點互信 [root@node4&nb…

    Linux干貨 2016-07-07
  • 特殊權限

    特殊權限 文件特殊權限 一、SUID(4) SUID:當s這個標志出現在文件所有者的x權限上時,就被稱作SUID。 SUID的功能和限制:1、僅僅對二進制程序有效; 2、執行者對程序需要X的執行權限; 3、本權限僅僅在執行該程序的過程中有效; 4、執行者將具有該程序所有者的權限。 5、SUID僅僅可以用在二進制程序上,…

    Linux干貨 2016-08-05
  • rpm包管理

    一、概述 RPM 是RPM Package Manager(RPM軟件包管理器)的縮寫。由redhat公司的Redhat package manager改名而來,成了RedHat的工業標準 二、rpm的命名格式: rpm包的命名格式:name-version-relase.arch.rpm version: major.minor.release,同源代碼 …

    Linux干貨 2015-10-07
  • ansible實踐

    本偏文章以上圖拓撲部署 首先準備了6臺為centos7的主機(確保selinux和iptables不會成阻礙!關閉他們) 我們先來安裝ansible吧! 配置EPEL源過后使用 yum -y install ansible  ?。。。。。。。。。。。。。。。。。。。。。。。。。。。。?! 出師不利啊,安裝ansible就失敗了。?。。。。?! 看了一下…

    Linux干貨 2017-01-27
  • LVS NAT + Keepalived HOWTO

    LVS NAT + Keepalived HOWTO   這篇文章主要講解了,基于LVS/NAT,安裝,運行與檢測keepalived 目錄     1、什么是keepalived?     2、規劃你的網絡 &nbsp…

    Linux干貨 2015-09-21
  • 壓縮及解壓工具

    通過算法壓縮數據,時間換空間。文本文件的壓縮比更大,二進制文件的壓縮比較小。 壓縮工具占用cpu時鐘。 compress/uncompress .Z gzip/gunzip .gz bzip2/bunzip2 .bz2 壓縮比比gz大但是也沒取代gz xz/unxz .xz 壓縮比大,現在較流行。 zip/unzip .zip 歸檔工具,既能歸檔又能壓縮 t…

    Linux干貨 2016-08-21
欧美性久久久久