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

相关推荐

  • 快速搭建SVN服务器

    一、背景介绍 svn服务器是一款上传代码的工具(貌似这么说不怎么严谨,但是在日常工作中基本上是这么用的),今天一个小伙伴折腾了一天也没有搭建好这个svn服务器。各种问题,其实搭建SVN服务器最重要的就是三个配置文件(svnserver.conf、 passwd 、authz)。出了问题的话十有八九是这三个配置文件的问题。最后,我自己搭建了一个,测试成功。于是…

    Linux干货 2016-12-11
  • 8.网络基础知识

    1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。 网桥:网桥就是把2个不同的网段桥接起来;可隔离冲突域。 集线器:集线器就是把多根以太网线或光纤集合连接在同一段物理介质下的装置;工作在物理层;不能隔离冲突域。 二层交换机:工作于OSI模型的第2层(数据链路层),故而称为二层交换机。二层交换技术的发展已经比较成熟,二层交换机属数据…

    Linux干货 2017-08-21
  • N22-妙手-第九周课程练习

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现;: #!/bin/bash # declare -i numberOfLoginUser=0 declare -i numberOfUnloginUs…

    Linux干货 2016-10-19
  • mysql备份之mysqldump

    mysql备份之mysqldump 注意:备份文件和二进制日志文件不能与mysql放在同一磁盘下 节点1 1、节点1上修改mysql配置文件,开起二进制日志保存 这里我将二进制日志放在/data/mysql/目录下,/data/是我创建的另外一个lvm磁盘,本来想直接放在/data/下,发现无法启动mysql,所以建议还是放在/data/mysql中 [ro…

    Linux干货 2016-07-19
  • MySQL复制: Galera

    MySQL复制: Galera mysql 主从复制 前言 Galera Replication简介 MariaDB-Galera-Server 环境部署 配置步骤 总结 前言 之前介绍了MySQL复制的各种解决方案, 但是我个人还是感觉Galera最好用也最实用, 什么是Galera, 它强大在哪里, 这篇文章就带你认识这个强大的工具 Galer…

    Linux干货 2016-04-28
  • bash基础特性(二)之命令别名,IO重定向,管道

    命令别名alias 别名通俗地说,叫花名,当我们常用的命令,要是输入太长,或经常要进入的目录,可以用一个别名来定义它们,定义别名时尽量不要和已有命令名相同。这样可以提高输入速度和工作效率。定义别名格式:alias 别名=’原命令 参数’ (要是没空格,可以不用引号)例如 : alias vi=vim [root@dxlcentOS ~…

    Linux干货 2017-09-24