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.178linux.com/78721

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

相关推荐

  • DNS 正反向解析 主从配置

    我的环境是     192.168.1.130    主DNS      192.168.1.112    从DNS DNS 的安装包有bind bind-libs bind-utils 安…

    Linux干货 2016-01-05
  • N26-第一周博客作业

    1.描述计算机的组成及其功能 完整的计算机系统由硬件和软件两部分组成。 现在大部分的计算机为冯诺依曼体系,主要有五个组成部分:运算器、控制器、存储器、输入设备、输出设备,以下为详细描述。(计算机的CPU由运算器、控制器和一二三层缓存等构成) 运算器:对数据进行算术运算和逻辑运算(对数据进行加工处理) 控制器:分析指令,控制协调输入、输出操作对内存的访问。 存…

    Linux干货 2017-01-02
  • systemd、awk命令 N28

    第八周-2018 1/22

    2018-01-22
  • httpd 高级应用

    回顾:httpd, lamp, mysql httpd:https, mod_deflate, … httpd+php:三种结合方式     Module:         prefork:libphp5.so  &n…

    Linux干货 2016-11-01
  • 马哥教育网络班第19期+第7周课程练习

    1、创建一个10G分区,并格式为ext4文件系统; (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl; (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳; 2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之; 3、写一个脚本  …

    Linux干货 2016-08-08
  • Linux之启动和内核管理

     Linux之启动和内核管理     本文主要包括以下内容一  CentOS 5和6的启动流程二  服务管理三  Grub管理四  自制Linux五  启动排错六  编译安装内核   Linux组成Linux: kernel+rootfskernel: 进程管…

    Linux干货 2016-09-15