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
下一篇 2017-03-27

相關推薦

  • LAMP基于rsyslog+loganalyzer實現日志集中管理及分析

    前言 作為一名運維工程師,查看分析系統日志是每天必做的功課,但每次查看日志都得一臺服務器一臺服務器的看,幾臺服務器還可以這么對付,但如果管理成百上千臺線上服務器,這種方法就捉襟見肘了。于是我們就需要用到日志服務器了,但是如何能讓它更直觀的顯示呢?loganalyzer是一個不錯的選擇,本文將講解如何使用rsyslog+loganalyze實現日志統一管理及分…

    Linux干貨 2015-04-27
  • 搜索引擎的技術架構

    1. 搜索引擎的分類 搜索引擎按其工作方式主要可分為三種: 分別是全文搜索引擎(Full Text Search Engine) 目錄索引類搜索引擎(Search Index/Directory) 元搜索引擎(Meta Search Engine)。 ■ 全文搜索引擎  全文搜索引擎是名副其實的搜索引擎,國外具代表性的有Google、Fast/Al…

    Linux干貨 2015-11-18
  • 小練習題?!镜谖逯堋?

    1、顯示當前系統上root、fedora或user1用戶的默認shell; /]# cat /etc/passwd | grep '^root\|fedora\|user1' |cut -d':' -f1,7 2、找出/etc/rc.d/i…

    Linux干貨 2016-11-26
  • shell腳本編程小節及作業

    使用read命令來接收輸入          使用read來把輸入值分配給一個或多個shell變量:         -p:指定要顯示的提示     &nbsp…

    Linux干貨 2016-08-22
  • Find命令簡介與使用

    用途:         find命令用來在指定目錄下查找文件。任何位于參數之前的字符串都將被視為欲查找的目錄名。如果使用該命令時,不設置任何參數,則find命令將在當前目錄下查找子目錄與文件。并且將查找到的子目錄和文件全部進行顯示。 語法: find [OPTION]… [查找…

    Linux干貨 2016-08-30
  • 磁盤管理

    回顧 {a..f} a b c d e f ]# arr=({a..z}) ]# echo ${#arr[*]} 26 ]# N1=N2 ]# N2=zhuchaoming ]# echo ${!N1} eval N1=\$$N2 echo $N1 創建臨時文件 mktemp NAME.XXX 交互式登錄 /etc/profile –> /…

    Linux干貨 2016-09-07
欧美性久久久久