zabbix入门

 

第一章 监控知识基本概述

1.1 为什么要使用监控

1.对系统不间断实时监控
2.实时反馈系统当前状态
3.保证服务可靠性安全性
4.保证业务持续稳定运行

1.2 如何进行监控,比如我们需要监控磁盘的使用率

1.如何查看磁盘使用率 df -h
2.监控磁盘的那些指标 block、 inode
3.如何获取具体的信息 df -h|awk ‘//(NF-1)’
4.获取的数值到达多少报警 80%

1.3 流行的监控工具

1.Zabbix
2.Lepus(天兔)数据库监控系统
3.Open-Falcon 小米开源监控系统
4.Prometheus(普罗米修斯, Docker、 K8s)

1.4 如果去到一家新公司,如何入手监控

1.硬件监控 路由器、交换机、防火墙
2.系统监控 CPU、内存、磁盘、网络、进程、 TCP
3.服务监控 nginx、 php、 tomcat、 redis、 memcache、 mysql
4.WEB监控 请求时间、响应时间、加载时间
5.日志监控 ELk(收集、存储、分析、展示) 日志易
6.安全监控 Firewalld、 WAF(Nginx+lua)、安全宝、牛盾云、安全狗
7.网络监控 smokeping 多机房
8.业务监控 活动引入多少流量、产生多少注册量、带来多大价值

第二章 单机时代如何监控

2.1 传统监控相关命令

CPU 监控命令: w、 top、 htop、 glances

1 %Cpu(s): 0.3 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
2 us 用户态: 跟用户的操作有关 35%
3 sy 系统态: 跟内核的处理有关 60%
4 id CPU 空闲:

内存监控命令: free

1 [root@m01 ~]# free -h
2               total        used        free      shared  buff/cache   available
3 Mem:           977M        105M        724M        6.6M        148M        729M
4 Swap:          1.0G          0B        1.0G

磁盘监控命令: df、 iotop

1 Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
2 sda 0.80 25.32 33.36 221034 291193
3 设备名 每秒传输次数 每秒读大小 每秒写大小 读的总大小 写的总大小

网络监控命令: ifconfig、 route、 glances、 iftop、 nethogs、 netstat

1 单位换算
2 Mbps 100Mbps/8
3 MB 12MB
4 iftop 中间的<= =>这两个左右箭头,表示的是流量的方向。
5 TX:发送流量、 RX:接收流量、 TOTAL:总流量
6 #查看 TCP11 中状态
7 netstat -an|grep ESTABLISHED
8 netstat -rn # 查看路由信息
9 netstat -lntup

2.2 传统监控痛点

随着时间的推移,用户不断的增多,服务随时可能扛不住会被 oom(out of memory),当系统内存不足的时候,就会触发 oom
1.当系统内存不足的时候就会大量使用 swap
2.当系统大量使用 swap 的时候,系统会特别卡
注意: 有时可能内存还有剩余 300Mb-500Mb,但会发现 swap 依然被使用

2.3 使用Shell脚本监控

那单机时代,如何使用 shell 脚本来实现服务器的监控
需求: 每隔 1 分钟监控一次内存,当你的可用内存低于 100m,发邮件报警,要求显示剩余内存
1.怎么获取内存可用的值 free -m|awk ‘/^Mem/{print $NF}’
2.获取到内存可用的值如何和设定的阈值进行比较
3.比较如果大于 100m 则不处理,如果小于 100 则报警
4.如何每隔 1 分钟执行一次

 1 [root@ZabbixServer ~]# cat free.sh
 2 #!/usr/bin/bash
 3 HostName=$(hostname)_$(hostname -i)
 4 Date=$(date +%F)
 5 while true;do
 6 Free=$(free -m|awk '/^Mem/{print $NF}')
 7 if [ $Free -le 100 ];then
 8 echo "$Date: $HostName Mem Is < ${Free}MB"
 9 fi
10 sleep 5
11 done
shell脚本监控

第三章 zabbix 监控快速安装

2.1 安装mariadb

2.1.1 安装官方最新版mariadb

由于官网的MariaDB版本要比阿里云的版本要高,所以我们应该优先使用官方的版本

 1 1.首先在RHEL/CentOS和Fedora操作系统中添加MariaDB的yum配置文件MariaDB.repo文件。
 2 # 编辑创建mariadb.repo仓库文件
 3 vim /etc/yum.repos.d/MariaDB.repo
 4 
 5 2.添加repo仓库配置
 6 [mariadb]
 7 name=MariaDB
 8 baseurl=http://yum.mariadb.org/10.1/centos7-amd64
 9 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
10 gpgcheck=1
11 
12 3.当 MariaDB 仓库地址添加好后,你可以通过下面的一行命令轻松安装 MariaDB。
13 yum install MariaDB-server MariaDB-client -y
安装最新mariadb
2.1.2 使用阿里云源安装Mariadb
 1 # 如果官方的下载网速太慢,我们就使用阿里云低版本的mariadb数据库
 2 1.删除或者重命名刚才创建的Mariadb.repo文件
 3 cd /etc/yum.repos.d
 4 mv Mariadb.repo Mariadb.repo.bak
 5 
 6 2.配置阿里云源
 7 参考链接: https://developer.aliyun.com/mirror
 8 
 9 3.一条命令安装Mariadb
10 yum install mariadb-server mariadb -y
2.1.3 安装好了之后,使用systemctl进行mariadb服务管理
1 systemctl start mariadb  #启动MariaDB
2 systemctl stop mariadb  #停止MariaDB
3 systemctl restart mariadb  #重启MariaDB
4 systemctl enable mariadb  #设置开机启动
2.1.4 初始化mariadb
1 mysql_secure_installation
 1 --为root用户设置密码
 2 --删除匿名账号
 3 --取消root用户远程登录
 4 --删除test库和对test库的访问权限
 5 --刷新授权表使修改生效
 6 [root@localhost ~]# mysql_secure_installation
 7 NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
 8 SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
 9 In order to log into MySQL to secure it, we'll need the current
10 password for the root user. If you've just installed MySQL, and
11 you haven't set the root password yet, the password will be blank,
12 so you should just press enter here.
13 Enter current password for root (enter for none):<–初次运行直接回车
14 OK, successfully used password, moving on…
15 Setting the root password ensures that nobody can log into the MySQL
16 root user without the proper authorisation.
17 Set root password? [Y/n]    #是否设置root用户密码,输入y并回车或直接回车
18 New password:               #设置root用户的密码
19 Re-enter new password:      #再输入一次你设置的密码
20 Password updated successfully!
21 Reloading privilege tables..
22 … Success!
23 By default, a MySQL installation has an anonymous user, allowing anyone
24 to log into MySQL without having to have a user account created for
25 them. This is intended only for testing, and to make the installation
26 go a bit smoother. You should remove them before moving into a
27 production environment.
28 Remove anonymous users? [Y/n]   #是否删除匿名用户,生产环境建议删除,所以直接回车
29 … Success!
30 Normally, root should only be allowed to connect from 'localhost'. This
31 ensures that someone cannot guess at the root password from the network.
32 Disallow root login remotely? [Y/n] #是否禁止root远程登录,根据自己的需求选择Y/n并回车,建议禁止
33 … Success!
34 By default, MySQL comes with a database named 'test' that anyone can
35 access. This is also intended only for testing, and should be removed
36 before moving into a production environment.
37 Remove test database and access to it? [Y/n] #是否删除test数据库,直接回车
38 - Dropping test database…
39 … Success!
40 - Removing privileges on test database…
41 … Success!
42 Reloading the privilege tables will ensure that all changes made so far
43 will take effect immediately.
44 Reload privilege tables now? [Y/n] #是否重新加载权限表,直接回车
45 … Success!
46 Cleaning up…
47 All done! If you've completed all of the above steps, your MySQL
48 installation should now be secure.
49 Thanks for using MySQL!
50 [root@localhost ~]#
运行mysql_secure_installation会执行几个设置
2.1.5 优先解决中文无法正常显示的问题
1 1.停掉mariadb服务
2 systemctl stop mariadb
3 2 修改配置文件vim /etc/my.cnf
1 [mysqld]
2 character-set-server=utf8
3 collation-server=utf8_general_ci
4 log-error=/var/log/mysqld.log
5 [client]
6 default-character-set=utf8
7 [mysql]
8 default-character-set=utf8
写入到/etc/my.cnf中

1568681912439

2.2 配置zabbix仓库

1 [root@m01 ~]# rpm -Uvh https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-release-4.4-1.el7.noarch.rpm
2 [root@m01 ~]# yum clean all
3 [root@m01 ~]# yum makecache fast

2.3 安装 Zabbix 程序包,以及 MySQL、前端、代理

1 [root@m01 ~]# yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-nginx-conf zabbix-agent

注意:nginx软件包在官方存储库中不可用。可以在epel资料库中找到。

1 yum -y install epel-release

2.4 创建 Zabbix 数据库以及用户

1 [root@m01 ~]# mysql -uroot -predhat
2 MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
3 MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix';
4 MariaDB [(none)]> flush privileges;
创建Zabbix数据库以及用户

2.5 导入 Zabbix 数据至数据库中

1 [root@m01 ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix

2.6 配置Zabbix服务器数据库

编辑/etc/zabbix/zabbix_server.conf 文件,修改数据库配置

1 [root@m01 ~]# cat /etc/zabbix/zabbix_server.conf
2 DBHost=localhost
3 DBName=zabbix
4 DBUser=zabbix
5 DBPassword=zabbix
配置Zabbix服务器数据库

2.7 为zabbix前端配置PHP

编辑文件/etc/nginx/conf.d/zabbix.conf,取消注释并设置’listen’和’server_name’指令。

1 listen 80;
2 server_name 10.0.0.9;

编辑文件/etc/php-fpm.d/zabbix.conf,取消注释并设置正确的时区。

1 php_value[date.timezone] = Asia/Shanghai

2.8 启动 Zabbix 服务进程,并加入开机自启动

1 [root@m01 ~]# systemctl restart zabbix-server zabbix-agent nginx php-fpm
2 [root@m01 ~]# systemctl enable zabbix-server zabbix-agent nginx php-fpm

第四章 WEB安装步骤

4.1浏览器打开地址:http://10.0.0.9/setup.php

image-20191206133652309

4.2 检查依赖项是否存在异常

image-20191206133711500

4.3 配置zabbix连接数据库

image-20191206133933972

4.4 配置 ZabbixServer 服务器的信息

image-20191206134253061

4.5 最终确认检查

image-20191206134313364

4.6 安装成功

提示已成功地安装了 Zabbix 前端。配置文件/etc/zabbix/web/zabbix.conf.php 被创建。

image-20191206134339942

4.7 登陆zabbix

默认登陆 ZabbixWeb 的用户名 Admin,密码 zabbix

image-20191206134518046

4.8 调整字符集为中文

image-20191206134626731

查看仪表盘

image-20191206135003645

4.9 修复中文乱码

打开图形之后会发现语言为乱码,原因是缺少字体

image-20191206135155541

解决方法:安装字体并替换现有字体

1 [root@m01 ~]# yum install wqy-microhei-fonts -y
2 [root@m01 ~]# cp /usr/share/fonts/wqy-microhei/wqy-microhei.ttc /usr/share/zabbix/http://images.lxh1.com/zabbix/fonts/graphfont.ttf
 1 解决方法:用Windows的字体替换现有字体
 2 1.从Windows控制面板->字体->选择一种中文简体->复制粘贴到某个文件中
 3 2.找到服务器上面zabbix的字体文件夹
 4     find / -name fonts
 5     cd /usr/share/zabbix/assets/fonts/
 6     # 查看原有字体(graphfont.ttf),并改名
 7     ls
 8     mv graphfont.ttf graphfont.ttf.bk
 9     # 利用Xshell将windows中拷贝的字体粘贴进去
10 3.将defines.inc.php里面的原有字体替换成现在的
11     find / -name defines.inc.php
12     vim /usr/share/zabbix/include/defines.inc.php
13     (vim使用命令行编辑模式,全局替换字体)
14     :%s/graphfont/simhei/g
15 4.刷新网页,字体就正常显示了
16 如果没有中文显示就给simhei添加权限 chmod 777 simhei.ttf
方法2从windows电脑传过来

再次刷新发现已经变成中文了

image-20191206135505032

第五章 Zabbix 监控基础架构

zabbix-agent(数据采集)—>zabbix-server(数据分析|报警)—> 数据库(数据存储)—>zabbix web(数据展示)

image-20191206214933009

第六章 zabbix 快速监控主机

6.1安装zabbix-agent

1 [root@web01 ~]# rpm -ivh https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.4.1-1.el7.x86_64.rpm

6.2配置zabbix-agent

1 [root@web01 ~]# cat /etc/zabbix/zabbix_agentd.conf    
2 PidFile=/var/run/zabbix/zabbix_agentd.pid
3 LogFile=/var/log/zabbix/zabbix_agentd.log
4 LogFileSize=0
5 Server=10.0.0.9
6 ServerActive=127.0.0.1
7 Hostname=Zabbix server
8 Include=/etc/zabbix/zabbix_agentd.d/*.conf
配置zabbix-agent

6.3 启动zabbix-agent并检查

1 [root@web01 ~]# systemctl start zabbix-agent.service 
2 [root@web01 ~]# systemctl enable zabbix-agent.service
3 [root@web01 ~]# netstat - tunlp | grep 10050
启动zabbix-agent并检查

image-20191206155745014

6.4 zabbix-web界面,添加主机

image-20191206160111001

点击创建主机, 填写以下主机信息

image-20191206160547932

再点击并选择模板

image-20191206163907893

第七章 自定义监控主机小试身手

7.1 监控需求

监控TCP11种状态集

7.2 命令行实现

1 [root@web01 ~]# netstat -ant | grep -c TIME_WAIT
2 55
3 [root@web01 ~]# netstat -ant | grep -c LISTEN
4 16

7.3 编写zabbix监控文件(传参形式)

1 [root@web01 ~]# cat /etc/zabbix/zabbix_agentd.d/tcp_status.conf
23 [root@web01 ~]# cat /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
4 UserParameter=tcp_state[*],netstat -ant|grep -c $1
5 UserParameter=TIME_WAIT,netstat -ant|grep -c TIME_WAIT
6 root@web01 ~]# systemctl restart zabbix-agent
编写zabbix监控文件(传参形式)

7.4 server端进行测试

1 [root@m01 ~]# rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
2 [root@m01 ~]# yum install zabbix-get.x86_64 -y 
3 [root@m01 ~]# zabbix_get -s 10.0.0.8 -k tcp_state[TIME_WAIT]
4 51
5 [root@m01 ~]# zabbix_get -s 10.0.0.8 -k tcp_state[LISTEN]
6 12
server端进行测试

7.5 web端添加

image-20191206170217716

添加tcp_state监控项

image-20191206170507710

image-20191206173222404

点击图形查看状态

image-20191206180646688

用同样的方式再添加一个TIME_WAIT监控项

image-20191206180732104

查看TIME_WAIT状态

image-20191206180819766

7.6 克隆监控项

由于TCP有多种状态,需要添加多个监控项,我们可以使用克隆快速达到创建的效果

但前提是配置文件里面除了监控名称,其他都一样,例:

1 [root@web01 ~]# cat /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
2 UserParameter=TIME_WAIT,netstat -ant|grep -c TIME_WAIT
3 UserParameter=LISTEN,netstat -ant|grep -c LISTEN

添加好配置文件后,注意需要重启zabbix-agent端

image-20191206185749158

image-20191206181300792

image-20191206182801444

其他的状态依次添加即可

7.7 创建图形

image-20191206182932626

7.8 查看图形

image-20191206183243820

7.9 设置触发器

image-20191206183357352

点击创建触发器

image-20191206183526350

image-20191206183635635

image-20191206183731404

第八章 邮件报警

8.1 定义发件人

编辑Email报警配置

image-20191206184135698

image-20191206192637926

8.2 定义收件人

image-20191206184342302

image-20191206184425345

image-20191206184532865

image-20191206184626163

image-20191206184647085

8.3 自定义报警内容

定制报警内容:
https://www.zabbix.com/documentation/4.0/zh/manual/appendix/macros/supported_by_location
参考博客

1 https://www.cnblogs.com/bixiaoyu/p/7302541.html

发送警告

 1 报警邮件标题可以使用默认信息,亦可使用如下中文报警内容
 2 名称:Action-Email
 3 默认标题:故障{TRIGGER.STATUS},服务器:{HOSTNAME1}发生: {TRIGGER.NAME}故障!
 4 告警主机:{HOSTNAME1}
 5 告警时间:{EVENT.DATE} {EVENT.TIME}
 6 告警等级:{TRIGGER.SEVERITY}
 7 告警信息: {TRIGGER.NAME}
 8 告警项目:{TRIGGER.KEY1}
 9 问题详情:{ITEM.NAME}:{ITEM.VALUE}
10 当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
11 事件ID:{EVENT.ID}
发送告警

恢复警告

 1 恢复标题:恢复{TRIGGER.STATUS}, 服务器:{HOSTNAME1}: {TRIGGER.NAME}已恢复!
 2 恢复信息:
 3 告警主机:{HOSTNAME1}
 4 告警时间:{EVENT.DATE} {EVENT.TIME}
 5 告警等级:{TRIGGER.SEVERITY}
 6 告警信息: {TRIGGER.NAME}
 7 告警项目:{TRIGGER.KEY1}
 8 问题详情:{ITEM.NAME}:{ITEM.VALUE}
 9 当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
10 事件ID:{EVENT.ID}
恢复告警

image-20191206195328342

第九章 微信报警

9.1 查看配置文件里的脚本目录路径

1 [root@m01 ~]# grep "^AlertScriptsPath" /etc/zabbix/zabbix_server.conf
2 AlertScriptsPath=/usr/lib/zabbix/alertscripts

9.2 将weixin.py放在zabbix特定目录

1 [root@m01 /usr/lib/zabbix/alertscripts]# ll
2 总用量 4
3 -rwxr-xr-x 1 root root 1344 8月   7 21:58 weixin.py

9.3 配置发信人

image-20191206195623411

添加微信报警,配置脚本参数

1 {ALERT.SENDTO}    # 动作
2 {ALERT.SUBJECT}   # 标题
3 {ALERT.MESSAGE}   # 消息

image-20191206195816210

9.4 配置收信人

image-20191206200023568

9.5 登陆企业微信公众号添加账户

https://work.weixin.qq.com/wework_admin/loginpage_wx
1.登陆后在企业号上新建应用

image-20191206200153332

2.上传logo,填写应用名称 ,应用介绍等

image-20191206201427499

3.查看启动应用
同时会生成应用的AgentId以及Secret,这个在后面步骤会有用

image-20191206201529071

4.接口调用测试
http://work.weixin.qq.com/api/devtools/devtool.php

image-20191206200336312

这里的corpid为公司ID

image-20191206200400130

Corpsecret就是刚才创建应用生成的Secrt,确认没问题填写进去然后下一步
如果没问题会显示200状态码

image-20191206200424390

9.6 添加成员

image-20191206200449756

9.7 关注公众号

image-20191206200525523

9.8 查看自己的账号

image-20191206200543615

9.9 修改脚本里的信息

1 [root@m01 /usr/lib/zabbix/alertscripts]# cat weixin.py 
2 ..............
3 corpid='微信企业号corpid'
4 appsecret='应用的Secret'
5 agentid=应用的id
6 ..............

9.10 发信测试

1 [root@m01 /usr/lib/zabbix/alertscripts]# python  weixin.py  你的账号  '发信测试'  ‘微信测试消息’

例:

1 python weixin.py 13530930319 '发信测试' ‘微信测试消息’

9.11 微信号上查看

image-20191206200629118

9.12 配置文件权限(非常重要)

在脚本里面有写日志的功能,所以zabbix用户要有日志文件的所有权限

9.13 发送到整个微信组

虽然我们实现了发送到单个用户的功能,但是如果我们的用户比较多,这样还是麻烦的,不过我们可以发送到整个组,其实脚本里已经预留好了配置,只不过默认注释了。
将脚本修改为以下内容,注释掉用户,打开组设置

 1 #!/usr/bin/env python
 2 
 3 import requests
 4 import sys
 5 import os
 6 import json
 7 import logging
 8 
 9 logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s, %(filename)s, %(levelname)s, %(message)s',
10                 datefmt = '%a, %d %b %Y %H:%M:%S',
11                 filename = os.path.join('/tmp','weixin.log'),
12                 filemode = 'a')
13 corpid='wwd80fafb6320e1efa'
14 appsecret='Bhf89FnZfMu0e7l8i4yagnAR5Z9TCqKknYbx-SFQvmg'
15 agentid=1000001
16 
17 token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret
18 req=requests.get(token_url)
19 accesstoken=req.json()['access_token']
20 
21 msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken
22 
23 #touser=sys.argv[1]
24 toparty=sys.argv[1]
25 subject=sys.argv[2]
26 message=sys.argv[2] + "

" +sys.argv[3]
27 
28 params={
29         #"touser": touser,
30         "toparty": toparty,
31         "msgtype": "text",
32         "agentid": agentid,
33         "text": {
34                 "content": message
35         },
36         "safe":0
37 }
38 
39 req=requests.post(msgsend_url, data=json.dumps(params))
40 
41 logging.info('sendto:' + toparty + ';;subject:' + subject + ';;message:' + message)
发送到微信组脚本

9.14 发送到所有用户脚本

1 #!/bin/bash 
2 name=$(cat name.txt)
3 
4 for ok in ${name}
5 do
6   python  weixin.py ${ok}  "$1"  "$2"
7 done
发送到所有用户脚本 

9.15 随机发送到指定用户脚本

1 #!/bin/bash 
2 num=$(echo $(($RANDOM%28+1)))
3 name=$(sed -n "${num}p" name.txt)
4 ok_boy=$(grep -v "${name}" name.txt)
5 
6 for ok in ${ok_boy}
7 do
8   python  weixin.py ${ok}  "$1"  "$2"
9 done
随机发送到指定用户脚本

第十章 自定义模版(新版本不支持)

10.1 监控TCP11种状态

编写zabbix配置文件, 添加以下内容

 1 [root@web01 ~]# vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
 2 UserParameter=ESTABLISHED,netstat -ant|grep  -c 'ESTABLISHED'
 3 UserParameter=SYN_SENT,netstat -ant|grep  -c 'SYN_SENT'
 4 UserParameter=SYN_RECV,netstat -ant|grep  -c 'SYN_RECV'
 5 UserParameter=FIN_WAIT1,netstat -ant|grep  -c 'FIN_WAIT1'
 6 UserParameter=FIN_WAIT2,netstat -ant|grep  -c 'FIN_WAIT2'
 7 UserParameter=TIME_WAIT,netstat -ant|grep  -c 'TIME_WAIT'
 8 UserParameter=CLOSE,netstat -ant|grep  -c 'CLOSE'
 9 UserParameter=CLOSE_WAIT,netstat -ant|grep  -c 'CLOSE_WAIT'
10 UserParameter=LAST_ACK,netstat -ant|grep  -c 'LAST_ACK'
11 UserParameter=LISTEN,netstat -ant|grep  -c 'LISTEN'
12 UserParameter=CLOSING,netstat -ant|grep  -c 'CLOSING'
编写添加zabbix配置

10.2 重启zabbix-agent

1 [root@web01 ~]# systemctl restart zabbix-agent

10.3 测试监控项

使用zabbix-get命令测试

1 [root@m01 ~]# yum install zabbix-get.x86_64 -y
2 [root@tiger-vm-004 alertscripts]# zabbix_get -s 10.0.0.8 -k ESTABLISHED
3 2
4 [root@tiger-vm-004 alertscripts]# zabbix_get -s 10.0.0.8 -k LISTEN
5 10
测试监控项

10.4 导入模版文件

image-20191206202641092

image-20191206203018308

image-20191206203421002

10.5 主机关联模版文件

image-20191206203505624

image-20191206203732714

10.6 查看最新数据

image-20191206203839733

10.7 查看图形

jianshu

image-20191206215630433

第十一章 自定义模版监控nginx状态

11.1 开启监控页面并访问测试

 1 [root@web01 ~]# cat /etc/nginx/conf.d/status.conf 
 2 server {
 3    listen 80;
 4    server_name localhost;
 5    location /nginx_status {
 6        stub_status on;
 7        access_log off;
 8    }
 9 }
10 
11 [root@web01 ~]# curl 127.0.0.1/nginx_status/
12 Active connections: 1 
13 server accepts handled requests
14  6 6 6 
15 Reading: 0 Writing: 1 Waiting: 0
开启监控页面并访问测试

11.2 准备nginx监控状态脚本

 1 [root@web01 /etc/zabbix/zabbix_agentd.d]# cat nginx_monitor.sh 
 2 #!/bin/bash
 3 NGINX_COMMAND=$1
 4 CACHEFILE="/tmp/nginx_status.txt"
 5 CMD="/usr/bin/curl http://127.0.0.1/nginx_status/"
 6 if [ ! -f $CACHEFILE  ];then
 7    $CMD >$CACHEFILE 2>/dev/null
 8 fi
 9 # Check and run the script
10 TIMEFLM=`stat -c %Y $CACHEFILE`
11 TIMENOW=`date +%s`
12 
13 if [ `expr $TIMENOW - $TIMEFLM` -gt 60 ]; then
14     rm -f $CACHEFILE
15 fi
16 if [ ! -f $CACHEFILE  ];then
17    $CMD >$CACHEFILE 2>/dev/null
18 fi
19 
20 nginx_active(){
21          grep 'Active' $CACHEFILE| awk '{print $NF}'
22          exit 0;
23 }
24 nginx_reading(){
25          grep 'Reading' $CACHEFILE| awk '{print $2}'
26          exit 0;
27 }
28 nginx_writing(){
29          grep 'Writing' $CACHEFILE | awk '{print $4}'
30          exit 0;
31 }
32 nginx_waiting(){
33          grep 'Waiting' $CACHEFILE| awk '{print $6}'
34          exit 0;
35 }
36 nginx_accepts(){
37          awk NR==3 $CACHEFILE| awk '{print $1}' 
38          exit 0;
39 }
40 nginx_handled(){
41          awk NR==3 $CACHEFILE| awk '{print $2}' 
42          exit 0;
43 }
44 nginx_requests(){
45          awk NR==3 $CACHEFILE| awk '{print $3}'
46          exit 0;
47 }
48 
49 case $NGINX_COMMAND in
50     active)
51         nginx_active;
52         ;;
53     reading)
54         nginx_reading;
55         ;;
56     writing)
57         nginx_writing;
58         ;;
59     waiting)
60         nginx_waiting;
61         ;;
62     accepts)
63         nginx_accepts;
64         ;;
65     handled)
66         nginx_handled;
67         ;;
68     requests)
69         nginx_requests;
70         ;;
71     *)
72 echo 'Invalid credentials';
73 exit 2;
74 esac
nginx监控状态脚本

11.3 编写zabbix监控配置文件

1 [root@web01 ~]# cat /etc/zabbix/zabbix_agentd.d/nginx_status.conf
2 UserParameter=nginx_status[*],/bin/bash /etc/zabbix/zabbix_agentd.d/nginx_monitor.sh $1
3 
4 # 重启agent端
5 [root@web01 ~]# systemctl restart zabbix-agent.service

11.4 使用zabbix_get取值

1 [root@m01 ~]# zabbix_get -s 10.0.1.7 -k nginx_status[accepts]
2 7

11.5 导入模版

image-20191206211827202

image-20191206211900361

11.6 链接模版

image-20191206212107036

11.7 查看数据

image-20191206212535252

第十二章 自定义模版监控php状态

12.1 开启监控页面

 1 [root@web01 ~]# tail -1 /etc/php-fpm.d/www.conf    
 2 pm.status_path = /php_status
 3 
 4 [root@web01 ~]# cat /etc/nginx/conf.d/status.conf    
 5 server {
 6    listen 80;
 7    server_name localhost;
 8    location /nginx_status {
 9        stub_status on;
10        access_log off;
11    }
12 
13    location /php_status {
14        fastcgi_pass 127.0.0.1:9000;
15        fastcgi_index index.php;
16        fastcgi_param SCRIPT_FILENAME html$fastcgi_script_name;
17        include fastcgi_params;
18    }
19 }
20 
21 [root@web01 ~]# nginx -t
22 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
23 nginx: configuration file /etc/nginx/nginx.conf test is successful
24 [root@web01 ~]# systemctl restart nginx.service php-fpm.service
开启监控页面

12.2 访问测试

 1 [root@web01 ~]# curl 127.0.0.1/php_status
 2 pool:                 www
 3 process manager:      dynamic
 4 start time:           08/Aug/2019:22:31:27 +0800
 5 start since:          37
 6 accepted conn:        1
 7 listen queue:         0
 8 max listen queue:     0
 9 listen queue len:     128
10 idle processes:       4
11 active processes:     1
12 total processes:      5
13 max active processes: 1
14 max children reached: 0
15 slow requests:        0
访问测试

12.3 准备访问脚本

 1 [root@web01 ~]# cat /etc/zabbix/zabbix_agentd.d/fpm.sh 
 2 #!/bin/bash
 3 ##################################
 4 # Zabbix monitoring script
 5 #
 6 # php-fpm:
 7 #  - anything available via FPM status page
 8 #
 9 ##################################
10 # Contact:
11 #  vincent.viallet@gmail.com
12 ##################################
13 # ChangeLog:
14 #  20100922     VV      initial creation
15 ##################################
16 
17 # Zabbix requested parameter
18 ZBX_REQ_DATA="$1"
19 ZBX_REQ_DATA_URL="$2"
20 
21 # Nginx defaults
22 NGINX_STATUS_DEFAULT_URL="http://localhost/fpm/status"
23 WGET_BIN="/usr/bin/wget"
24 
25 #
26 # Error handling:
27 #  - need to be displayable in Zabbix (avoid NOT_SUPPORTED)
28 #  - items need to be of type "float" (allow negative + float)
29 #
30 ERROR_NO_ACCESS_FILE="-0.91"
31 ERROR_NO_ACCESS="-0.92"
32 ERROR_WRONG_PARAM="-0.93"
33 ERROR_DATA="-0.94" # either can not connect /   bad host / bad port
34 
35 # Handle host and port if non-default
36 if [ ! -z "$ZBX_REQ_DATA_URL" ]; then
37   URL="$ZBX_REQ_DATA_URL"
38 else
39   URL="$NGINX_STATUS_DEFAULT_URL"
40 fi
41 
42 # save the nginx stats in a variable for future parsing
43 NGINX_STATS=$($WGET_BIN -q $URL -O - 2>/dev/null)
44 
45 # error during retrieve
46 if [ $? -ne 0 -o -z "$NGINX_STATS" ]; then
47   echo $ERROR_DATA
48   exit 1
49 fi
50 
51 # 
52 # Extract data from nginx stats
53 #
54 #RESULT=$(echo "$NGINX_STATS" | awk 'print $0;match($0, "^'"$ZBX_REQ_DATA"':[[:space:]]+(.*)", a) { print a[1] }')
55 #RESULT=$(echo "$NGINX_STATS" | grep "$ZBX_REQ_DATA" | awk -F : '{print $2}')
56 RESULT=$(echo "$NGINX_STATS" | awk -F : "{if($1=="$ZBX_REQ_DATA") print $2}")
57 if [ $? -ne 0 -o -z "$RESULT" ]; then
58     echo $ERROR_WRONG_PARAM
59     exit 1
60 fi
61 
62 echo $RESULT
63 
64 exit 0
65 
66 [root@web01 ~]# bash /etc/zabbix/zabbix_agentd.d/fpm.sh "total processes" http://127.0.0.1/php_status
67 5
准备访问脚本

12.4 准备zabbix配置文件

1 [root@web01 ~]# cat /etc/zabbix/zabbix_agentd.d/fpm.conf    
2 UserParameter=php-fpm[*],/etc/zabbix/zabbix_agentd.d/fpm.sh "$1" "$2"
3 [root@web01 ~]# systemctl restart zabbix-agent.service

12.5 使用zabbix_get取值

1 [root@m01 ~]# zabbix_get -s 10.0.1.7 -k php-fpm["total processes",http://127.0.0.1/php_status]
2 5

12.6 导入模版

导入之后需要修改一下模版里的宏配置

image-20191206214507835

第十三章 WEB监控

需求,监控页面状态码

添加Web场景

image-20191206213213011

配置步骤:

image-20191206213356372

image-20191206213937433

第十四章 故障记录

故障1

故障现象:
提示zabbix-server is not running

image-20191206214118909

报错日志:

1 34983:20190807:202215.171 database is down: reconnecting in 10 seconds
2 34983:20190807:202225.172 [Z3001] connection to database 'zabbix' failed: [1045] Access denied for user 'zabbix'@'localhost' (using password: NO)

故障原因:
zabbix-server的配置文件里配有配置数据库密码
故障解决:
添加正确的数据库账号密码信息

1 [root@m01 ~]# grep "^DB" /etc/zabbix/zabbix_server.conf     
2 DBHost=localhost
3 DBName=zabbix
4 DBUser=zabbix
5 DBPassword=zabbix

故障2

故障现象:微信报警失败
报错日志:

 1 [root@m01 ~]# tail -f /var/log/zabbix/zabbix_server.log 
 2 Problem name: TIME_WAIT过多
 3 Host: web01
 4 Severity: Average
 5 
 6 Original problem ID: 51
 7 '": Traceback (most recent call last):
 8   File "/usr/lib/zabbix/alertscripts/weixin.py", line 7, in <module>
 9     import requests
10 ImportError: No module named requests
微信报警日志

问题原因:
缺少模块 requests

问题解决:
安装缺失的依赖包

1 [root@m01 ~]# yum install python-pip
2 [root@m01 ~]# pip install --upgrade pip
3 [root@m01 ~]# pip install requests

故障3

故障现象:
在server端使用zabbix_get命令测试键值命令时提示警告

1 [root@m01 ~]# zabbix_get -s 10.0.1.7 -k ESTABLISHED  
2 (Not all processes could be identified, non-owned process info
3  will not be shown, you would have to be root to see it all.)
4 2

问题原因:
zabbix_agent是以普通用户zabbix运行的,而普通用户执行netstat -antp时会有警告,网上查找发现只要不是用p参数就可以以普通用户运行
解决方案:
监控脚本里的命令修改为netstat -ant

转自虎哥:https://www.lxh1.com/2019/12/09/linux/zabbix%E7%9B%91%E6%8E%A7%E6%9C%8D%E5%8A%A1/

原文地址:https://www.cnblogs.com/wpnr/p/12055624.html