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)
上一篇 2014-10-15 18:17
下一篇 2015-01-12 16:01

相关推荐

  • GOPS2017全球运维大会 • 深圳站将在深圳召开!

    第六届GOPS2017全球运维大会(本次)将于2017年4月21日-22日在深圳举行,历届金牌讲师精选亮相,各种精彩等您发掘。活动家为GOPS2017全球运维大会提供在线报名!在线报名地址:https://www.huodongjia.com/event-231365274.html 大会亮点 众多国外重量级嘉宾在路上 ► 目前正在和各位大咖商榷行程中,主会…

    2017-04-10
  • Bash基础命令操作

    Bash基础命令操作

    2018-03-18
  • 强大的文本处理工具:vim编辑器

    一:引言 系统管理员的重要工作就是修改与设置某些软件的重要配置文件,而vim作为vi编辑器的升级版本,不仅能够以不同颜色来显示文字内容,还能够进行诸如shell脚本、C等程序的编写,所以,它还是一个程序编辑器,接下来,就让我们一起来看看vim的有哪些好玩的用法和特征。 二:vim三种模式及其常见用法 虽然vim有非常庞大甚至说它复杂也不为过的功能体系,但用结…

    2017-08-05
  • rsync+inotify实现数据同步——单向传输

    实验环境:<从A主机推送数据到B主机[B主机可以有多个]> A主机:10.1.43.102 B主机:10.1.43.103 配置流程: —rsyncserver—- <B主机上配置> 1.vim /etc/rsyncd.conf(用户,目录,模块,虚拟用户及密码文件) vim /etc/rsyncd….

    Linux干货 2016-10-27
  • 关于 磁盘、文件系统管理

                   磁盘、文件系统管理               1  设备识别2  设备分区3 …

    系统运维 2016-08-30
  • http请求过程

    1、浏览器根据访问的域名找到其IP地址。DNS查找过程如下: 1.浏览器缓存:浏览器会缓存DNS记录一段时间。 2.系统缓存:如果在浏览器缓存里没有找到需要的域名,浏览器会查系统缓存中的记录。 3.路由器缓存:如果系统缓存也没找到需要的域名,则会向路由器发送查询请求。 4.ISP DNS缓存:如果依然没找到需要的域名,则最后要查的就是ISP缓存DNS的服务器…

    Linux干货 2017-10-23