Zabbix实战--监控Nginx、MySQL与JAVA应用
Zabbix支持的主要监控方式:
(1).Agent监控方式
在Agent监控方式下,zabbix-agent会主动收集本机的监控信息并通过TCP协议与zabbix-server传递信息。
Agent监控方式分为主动和被动模式。
在被动模式下,zabbix-agent监听10050端口,等待zabbix-server的监控信息收集信息请求;
在主动模式下,zabbix-agent收集监控信息并通过10050端口主动将数据传给zabbix-server所在服务器的10051端口。
优点:
(1)是zabbix最常用的监控方式,监测指标深入细致有针对性。
(2)内置监控功能强大,内置监控项目丰富。
(3)TCP方式实现通讯,可靠性也有保证。
缺点:
(1)需要在被监控机器上安装zabbix-agent客户端,部署相对麻烦,最初需要逐个机器安装代理软件
(2)zabbix-agent客户端运行在被监控机上,会收集本机信息
(2).Trapper监控方式
Trapper监控方式使用zabbix-sender程序主动向zabbix-server发送数据。key的名称和发送的数据内容都可以灵活定义。
发送的信息采用JSON格式,遵循zabbix-sender协议。可以自定义脚本利用zabbix-sender协议来zabbix-server发送信息。
优点:
(1)不需要在被监控机器上安装zabbix-agent
(2)不收集被监控机器的信息
(3)可以自定义发送的信息内容
(4)可以使用自定义脚本发送信息
缺点:
(1)需要自定义发送的信息内容
(2)无内置监控项目
(3).SNMP监控方式
SNMP全称Simple Network Management Protocol,即网络管理协议,包括进程管理和被管理设备两部分。
作为一种国际通用的网络管理协议被广泛的应用于各种交换机,路由器等网络设备的管理上,而现在也越来越多被用于对服务器的监控上。
优点:
(1)服务器一旦部署SNMPAgent,任何能实现SNMP协议的软件都可以对其进行监测。
(2)通过这种手段进行监测不需知道被监测服务器的用户名和密码,比较安全。
缺点:
(1)很多服务器并非默认安装SNMPAgent,如果通过这种方式监测则需要对所有服务器安装部署。
(2)能监测的参数指标比较固定不够深入,无法满足用户的特殊需求。
(3)由于SNMP协议是通过UDP方式实现的。在网络状况不佳的情况下其可靠性能以保证。
(4)JMX监控方式
JMX,全称Java Management Extensions,即Java管理拓展,是Java平台为应用程序,设备,系统等植入管理功能的框架。
在zabbix中,JMX数据的获取由zabbix-java-gateway代理程序来负责数据的采集。
优点:
可以详细的监控各类Java程序的运行状态
缺点:
被监控机上需要安装zabbix-java-gateway
(5)IPMI监控方式
IPMI,全称Interlligent Platform Management Interface,即智能平台管理接口,原本是Intel架构中企业系统的周边设备所采用的一种工业标准,以后成为业界通用的标准。
用户可以利用IPMI监控服务器的物理特性,如温度,电压,电扇工作状态,电源供应以及机箱等指标。
实验前提:
已经完成了zabbix的安装部署,以及添加server2主机(zabbix-agent)
1.自定义监控 Nginx
zabbix自带apache监控,但没有nginx监控,所以需要自定义。
实验环境:
server1:172.25.66.1 zabbix-server
server2:172.25.66.2 zabbix-agent nginx
1.配置nginx
(1).下载并解压nginx
#1.在官网上下载nginx
[[email protected] ~]# ls
nginx-1.15.8.tar.gz zabbix-agent-4.0.5-1.el7.x86_64.rpm
#2.解压
[[email protected] ~]# tar zxf nginx-1.15.8.tar.gz
[[email protected] ~]# ls
nginx-1.15.8 nginx-1.15.8.tar.gz zabbix-agent-4.0.5-1.el7.x86_64.rpm
(2).关闭debug日志
[[email protected] ~]# cd nginx-1.15.8
#关闭debug日志
[[email protected] nginx-1.15.8]# vim auto/cc/gcc
(3).源码编译
#1.安装依赖包
[[email protected] nginx-1.15.8]# yum install -y gcc make pcre-devel zlib-devel
#2.configure配置
[[email protected] nginx-1.15.8]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module
#3.编译与安装
[[email protected] nginx-1.15.8]# make && make install
(4).更改配置文件
[[email protected] nginx-1.15.8]# cd /usr/local/nginx/conf/
#1.更改配置文件
[[email protected] conf]# vim nginx.conf
####################
location /status {
stub_status on; #加载模块
access_log off;
allow 127.0.0.1; #访问控制;仅允许本机可以访问
deny all;
}
#2.检测语法
[[email protected] conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#3.开启nginx
[[email protected] conf]# /usr/local/nginx/sbin/nginx
2.采集数据并添加nginx监控项
(1)采集Nginx发起的活跃连接数
1.利用shell命令获取活跃连接数
#多访问几次
[[email protected] conf]# curl http://127.0.0.1/status
Active connections: 1
server accepts handled requests
10 10 10
Reading: 0 Writing: 1 Waiting: 0
[[email protected] conf]# curl http://127.0.0.1/status | grep Active
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 100 100 100 0 0 229k 0 --:--:-- --:--:-- --:--:-- 97k
Active connections: 1
[[email protected] conf]# curl -s http://127.0.0.1/status | grep Active
Active connections: 1
[[email protected] conf]# curl -s http://127.0.0.1/status | grep Active | awk '{print $3}'
1
2.指定key-value
[[email protected] conf]# cd /etc/zabbix/zabbix_agentd.d/
[[email protected] zabbix_agentd.d]# ls
userparameter_mysql.conf
#1.拷贝模板文件
[[email protected] zabbix_agentd.d]# cp userparameter_mysql.conf userparameter_nginx.conf
#2.更改文件
[[email protected] zabbix_agentd.d]# vim userparameter_nginx.conf
###############################
UserParameter=nginx.active,curl -s http://127.0.0.1/status | grep Active | awk '{print $3}'
#重启zabbix-agent
[[email protected] zabbix_agentd.d]# systemctl restart zabbix-agent
3.在zabbix-server端测试
(1).安装zabbix_get工具
安装包:
zabbix-get-4.0.5-1.el7.x86_64.rpm
[[email protected] ~]# yum install -y zabbix-get-4.0.5-1.el7.x86_64.rpm
(2).测试agent
#若可以获取到数据即时为配置成功
[[email protected] ~]# zabbix_get -s 172.25.66.2 -p 10050 -k "nginx.active"
1
4.网页配置
(1).在server2主机中,创建nginx_active监控项
(2).数据绘图
(3).预览图形
图形中有数据,即说明监控nginx并采集数据成功 发现问题:无法显示中文(中文乱码)
解决乱码问题:
#查看graphfont.ttf软连接,发现它链接到了本机系统
[[email protected] fonts]# pwd
/usr/share/zabbix/fonts
[[email protected] fonts]# ls
graphfont.ttf
[[email protected] fonts]# ll
total 0
lrwxrwxrwx 1 root root 33 Mar 9 10:23 graphfont.ttf -> /etc/alternatives/zabbix-web-font
[[email protected] fonts]# ll /etc/alternatives/zabbix-web-font
lrwxrwxrwx 1 root root 38 Mar 9 10:23 /etc/alternatives/zabbix-web-font -> /usr/share/fonts/dejavu/DejaVuSans.ttf
[[email protected] fonts]# ll /usr/share/fonts/dejavu/DejaVuSans.ttf
-rw-r--r-- 1 root root 720012 Feb 27 2011 /usr/share/fonts/dejavu/DejaVuSans.ttf
#1.在windos系统中拷贝某种字体,并将其放到/usr/share/zabbix/fonts目录下
[[email protected] Desktop]# scp simkai.ttf [email protected]:/usr/share/zabbix/fonts
[[email protected] fonts]# ls
graphfont.ttf simkai.ttf
[[email protected] fonts]# cd ..
[[email protected] zabbix]# cd include/
[[email protected] include]# pwd
/usr/share/zabbix/include
#2.启用该字体
[[email protected] include]# vim defines.inc.php
#######################
:%s/graphfont/simkai/g
测试:刷新网页,发现中文正确显示
(2)采集Nginx总共处理的请求数
1.利用shell命令获取总共处理的请求数
[[email protected] ~]# curl http://127.0.0.1/status
Active connections: 1
server accepts handled requests
40 40 40 #Nginx总共处理了40个连接,成功创建40次句柄(证明中间没有失败的),总共处理了40个请求
Reading: 0 Writing: 1 Waiting: 0
[[email protected] ~]# curl -s http://127.0.0.1/status | awk NR==3 | awk '{print $1}'
46
2.指定key-valus
[[email protected] zabbix_agentd.d]# pwd
/etc/zabbix/zabbix_agentd.d
[[email protected] zabbix_agentd.d]# vim userparameter_nginx.conf
###############################
UserParameter=nginx.active,curl -s http://127.0.0.1/status | grep Active | awk '{print $3}'
UserParameter=nginx.accept,curl -s http://127.0.0.1/status | awk NR==3 | awk '{print $1}'
#重启zabbix-agent
[[email protected] zabbix_agentd.d]# systemctl restart zabbix-agent
3.在zabbix-server端测试
[[email protected] include]# zabbix_get -s 172.25.66.2 -p 10050 -k "nginx.accept"
103
4.配置网页
(1).在server2主机中,创建nginx_accept监控项
(2)数据绘图
注意:此时无需重新创建图形,直接将其添加到刚创建的nginx_status图形中即可
(3)预览图形
图形中有数据,即说明监控nginx并采集数据成功
2. 通过percona插件监控 MySQL
实验环境:
server1: 172.25.66.1 zabbix-server mariadb
server2: 172.25.66.2 zabbix-agent
由于zabbix自带的MySQL模块的监控项太少,所以一般不使用
(1).在Zabbix server主机中,添加MySQL数据库模板
(2).查看模板,发现zabbix自带的数据库模板的监控项只有14项(太少了)
一般通过添加percona插件来监控MySQL
配置数据库:
#1.创建目录
[[email protected] ~]# mkdir /var/lib/zabbix
[[email protected] ~]# cd /var/lib/zabbix/
[[email protected] zabbix]# ls
#2.编写文件
[[email protected] zabbix]# vim .my.conf
######################
[mysql]
host = localhost
user = root
password = westos #数据库超级用户密码
socket = /var/lib/mysql/mysql.sock
[mysqladmin]
host = localhost
user = root
password = westos #数据库超级用户密码
socket = /var/lib/mysql/mysql.sock
#3.重启zabbix-agent
[[email protected] zabbix]# systemctl restart zabbix-agent
配置插件:
#1.安装mysql插件
[[email protected] ~]# rpm -ivh percona-zabbix-templates-1.1.8-1.noarch.rpm
warning: percona-zabbix-templates-1.1.8-1.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:percona-zabbix-templates-1.1.8-1 ################################# [100%]
Scripts are installed to /var/lib/zabbix/percona/scripts
Templates are installed to /var/lib/zabbix/percona/templates
[[email protected] ~]# cd /var/lib/zabbix/percona/templates/
[[email protected] templates]# ls
userparameter_percona_mysql.conf
zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml
#2.拷贝文件
[[email protected] templates]# cp userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/
[[email protected] templates]# cd ../scripts/
[[email protected] scripts]# pwd
/var/lib/zabbix/percona/scripts
[[email protected] scripts]# ls
get_mysql_stats_wrapper.sh ss_get_mysql_stats.php
#3.更改文件
[[email protected] scripts]# vim ss_get_mysql_stats.php.cnf
#######################
<?php
$mysql_user = 'root'; #数据库超户
$mysql_pass = 'westos'; #超户密码
#4.重启zabbix-agent
[[email protected] scripts]# systemctl restart zabbix-agent
(1).手工调用插件
[[email protected] scripts]# /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gg
4
[[email protected] scripts]# cd /tmp/
[[email protected] tmp]# ls
localhost-mysql_cacti_stats.txt
systemd-private-931a06948b7c464dbe30bcaa2238e289-httpd.service-EugAVR
systemd-private-931a06948b7c464dbe30bcaa2238e289-mariadb.service-4AQY0p
systemd-private-e6b865e1f8ac4c10b3556f0ec05be7fb-httpd.service-TmCtxz
systemd-private-e6b865e1f8ac4c10b3556f0ec05be7fb-mariadb.service-bpwqIt
[[email protected] tmp]# cat localhost-mysql_cacti_stats.txt
#删除数据
[[email protected] tmp]# rm -rf localhost-mysql_cacti_stats.txt
[[email protected] tmp]# ls
systemd-private-931a06948b7c464dbe30bcaa2238e289-httpd.service-EugAVR
systemd-private-931a06948b7c464dbe30bcaa2238e289-mariadb.service-4AQY0p
systemd-private-e6b865e1f8ac4c10b3556f0ec05be7fb-httpd.service-TmCtxz
systemd-private-e6b865e1f8ac4c10b3556f0ec05be7fb-mariadb.service-bpwqIt
(2).zabbix自动调用插件
1.下载模板
zbx_percona_mysql_template.xml
2.导入模板
3.将刚导入的模板添加到zabbix server主机中
测试:
查看主机,发现此时zabbix server主机的监控项变多
3.通过JMX监控JAVA应用
实验环境:
server1: 172.25.66.1 zabbix-server zabbix-java-gateway
server2: 172.25.66.2 zabbix-agent tomcat
配置agent端:
1.下载并安装jdk
安装包:
jdk-8u121-linux-x64.rpm
[[email protected] ~]# rpm -ivh jdk-8u121-linux-x64.rpm
2.配置tomcat
安装包:
apache-tomcat-8.5.24.tar.gz
#1.解压apache-tomcat
[[email protected] ~]# tar zxf apache-tomcat-8.5.24.tar.gz -C /usr/local/
[[email protected] ~]# cd /usr/local/
[[email protected] local]# ls
apache-tomcat-8.5.24 etc include lib64 nginx share
bin games lib libexec sbin src
#2.制作软链接,便于升级
[[email protected] local]# ln -s apache-tomcat-8.5.24/ tomcat
[[email protected] local]# cd tomcat/bin
[[email protected] bin]# pwd
/usr/local/tomcat/bin
#3.编写启动脚本
[[email protected] bin]# vim catalina.sh
###################
CATALINA_OPTS='-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=8888
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false'
[[email protected] bin]# pwd
/usr/local/tomcat/bin
[[email protected] bin]# ls
#4.启动tamcat
[[email protected] bin]# ./startup.sh
#查看端口
[[email protected] bin]# netstat -antlp | grep 8888
tcp6 0 0 :::8888 :::* LISTEN 2880/java
配置java-gateway端:
为了节省结点资源,直接将在server1结点(zabbix-server端)上部署java-gateway即可
1.下载并安装zabbix-java-gateway
安装包:
zabbix-java-gateway-4.0.5-1.el7.x86_64.rpm
[[email protected] ~]# yum install -y zabbix-java-gateway-4.0.5-1.el7.x86_64.rpm
2.开启zabbix-java-gateway
#2.开启zabbix-java-gateway
[[email protected] ~]# systemctl start zabbix-java-gateway
#查看端口
[[email protected] ~]# netstat -antlp | grep :10052
tcp6 0 0 :::10052 :::* LISTEN 11567/java
3.更改配置文件
[[email protected] ~]# cd /etc/zabbix/
#3.编写配置文件
[[email protected] zabbix]# vim zabbix_server.conf
######################
JavaGateway=172.25.66.1 #指定java网关(java-gateway服务器地址)
JavaGatewayPort=10052 #指定java端口(java-gateway服务器端口)
StartJavaPollers=5 #Java轮询器
#重启zabbix-server
[[email protected] zabbix]# systemctl restart zabbix-server
配置网页:
1.在server2主机中,添加JMX接口
2.在server2主机中,添加Java JMX模板
测试:
1.查看主机,发现server2主机可用性栏目多了一个JMX,即视为设定成功
2.预览图形,发现有数据,进一步验证添加java应用成功