注:本文基于:lnmp+zabbix监控服务器
一、监控主机内存使用情况
1.部署agent客户端
[root@agent /]# mkdir /zabbix # 个人习惯,可忽略
[root@agent /]# cd /zabbix/
[root@agent /]# rz
[root@agent zabbix]# tar zxf zabbix-3.2.1.tar.gz
[root@agent zabbix]# cd zabbix-3.2.1/
[root@agent zabbix-3.2.1]# useradd -M -s /sbin/nologin zabbix
[root@agent zabbix-3.2.1]# ./configure --prefix=/usr/local/zabbix --enable-agent && make && make install
[root@agent zabbix-3.2.1]# cp misc/init.d/fedora/core/zabbix_agentd /etc/init.d/
[root@agent /]# sed -i 's/BASEDIR=\/usr\/local/BASEDIR=\/usr\/local\/zabbix/g' /etc/init.d/zabbix_agentd
[root@agent /]# sed -i 's/PIDFILE=\/tmp\/$BINARY_NAME.pid/PIDFILE=$BASEDIR\/logs\/$BINARY_NAME.pid/g' /etc/init.d/zabbix_agentd
[root@agent /]# chmod +x /etc/init.d/zabbix_agentd
[root@agent /]# sed -i 's/# PidFile=\/tmp\/zabbix_agentd.pid/PidFile=\/usr\/local\/zabbix\/logs\/zabbix_agentd.pid/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/LogFile=\/tmp\/zabbix_agentd.log/LogFile=\/usr\/local\/zabbix\/logs\/zabbix_agentd.log/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/Server=127.0.0.1/Server=192.168.1.136/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/# ListenPort=10050/ListenPort=10050/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/ServerActive=127.0.0.1/ServerActive=192.168.1.136/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/Hostname=Zabbix server/Hostname=192.168.1.138/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/# Timeout=3/Timeout=15/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/# Include=\/usr\/local\/etc\/zabbix_agentd.conf.d\//Include=\/usr\/local\/zabbix\/etc\/zabbix_agentd.conf.d\//g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/# UnsafeUserParameters=0/UnsafeUserParameters=1/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# egrep -v '^$|^#' /usr/local/zabbix/etc/zabbix_agentd.conf
PidFile=/usr/local/zabbix/logs/zabbix_agentd.pid
LogFile=/usr/local/zabbix/logs/zabbix_agentd.log
Server=192.168.1.136
ListenPort=10050
ServerActive=192.168.1.136
Hostname=192.168.1.138
Timeout=15
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/
UnsafeUserParameters=1
[root@agent /]# mkdir -p /usr/local/zabbix/logs # 创建日志存放路径
[root@agent /]# chown -R zabbix:zabbix /usr/local/zabbix/ # 赋予权限
[root@agent /]# /etc/init.d/zabbix_agentd start # 启动agent
Reloading systemd: [ OK ]
Starting zabbix_agentd (via systemctl): [ OK ]
[root@agent /]# cat > /tmp/memavailable.sh << EOF
> #!/bin/bash
> free -m | grep Mem | awk '{print $NF}'
> EOF
[root@agent /]# chmod +x /tmp/memavailable.sh
[root@agent /]# cat > /usr/local/zabbix/etc/zabbix_agentd.conf.d/memtest.conf << EOF
> UserParameter=memtest,sh /tmp/memavailable.sh
> EOF
[root@agent zabbix]# /etc/init.d/zabbix_agentd restart
Restarting zabbix_agentd (via systemctl): [ OK ]
#服务端测试:
[root@zabbix /]# zabbix_get -s 192.168.1.138 -p 10050 -k "memtest"
1151
2、登录到Zabbix server端进行以下操作
二、监控Nginx服务
自行安装部署Nginx,可参考博文:搭建Nginx服务器及深度优化(1)
为了方便,我依旧是在本台主机上进行的,如果需要在另一台主机上监控的话也需要安装部署agent端
1、配置agent
#再原有的配置文件基础上再添加几行
[root@agent /]# echo 'UserParameter=nginx.info[*],/usr/local/zabbix/scripts/nginx.sh $1' >> /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# echo "UserParameter=nginx.ping[*],netstat -anput | grep -w 80 | grep -w LISTEN | wc -l" >> /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# egrep -v '^$|^#' /usr/local/zabbix/etc/zabbix_agentd.conf
PidFile=/usr/local/zabbix/logs/zabbix_agentd.pid
LogFile=/usr/local/zabbix/logs/zabbix_agentd.log
Server=192.168.1.136
ListenPort=10050
ServerActive=192.168.1.136
Hostname=192.168.1.138
Timeout=15
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/
UnsafeUserParameters=1
UserParameter=nginx.info[*],/usr/local/zabbix/scripts/nginx.sh $1
UserParameter=nginx.ping[*],netstat -anput | grep -w 80 | grep -w LISTEN | wc -l
2、在Nginx服务器上编写Nginx脚本
[root@agent /]# mkdir /usr/local/zabbix/scripts
[root@agent /]# cd /usr/local/zabbix/scripts/
[root@agent scripts]# vim nginx.sh
#!/bin/bash
case $1 in
index)
RS=`curl -s -I 192.168.1.138/index.html |head -n1 |awk '{print $2}'`
echo $RS
;;
WorkerProcesses)
RS=`ps aux |grep nginx | grep -w worker |wc -l`
echo $RS
;;
bps)
RS=`ab -q -n 1000 -c 1000 http://192.168.1.138/index.html |grep Request | awk '{print $4}'`
echo $RS
;;
Current)
LINES=`cat /usr/local/nginx/logs/access.log |wc -l`
sleep 1
NOW_LINES=`cat /usr/local/nginx/logs/access.log |wc -l`
echo $NOW_LINES $LINES | awk '{print $1 -$2}'
;;
IP)
RS=`cat /usr/local/nginx/logs/access.log | awk '{print $1}' |sort -u | wc -l`
echo $RS
;;
VirtualHost)
RS=`cat /usr/local/nginx/conf/nginx.conf |grep -v '#' | grep server_name | wc -l`
echo $RS
;;
MD5)
RS=`md5sum /usr/local/nginx/conf/nginx.conf |awk '{print $1}'`
echo $RS
;;
PV)
RS=`wc -l /usr/local/nginx/logs/access.log | awk '{print $1}'`
echo $RS
;;
esac
#调整相应权限,需要所有用户都可以执行netstat命令
[root@agent scripts]# chmod +s /usr/bin/netstat
[root@agent scripts]# chmod +x nginx.sh
[root@agent scripts]# /etc/init.d/zabbix_agentd restart
Restarting zabbix_agentd (via systemctl): [ OK ]
3、配置Zabbix-Server Web界面
在配置web界面前,可现在Zabbix-Server端测试一下相关信息是否可以获得,以便验证脚本的正确性(同时ab压力测试是必须要有的,Server端和Agent端都要装该命令)
[root@zabbix /]# yum -y install httpd-tools
[root@zabbix /]# ab -q -n 1000 -c 1000 http://192.168.1.138/index.html | grep Request | awk '{print $4}'
2707.67
#有信息返回则表示正常
配置Web界面:
[root@zabbix /]# cat a.sh
#!/bin/bash
while true
do
ab -n 1000 -c 100 192.168.1。138/index.html &> /dev/null
done
[root@zabbix /]# sh a.sh
执行脚本后,如下:
也可以自定义图表,如下:
三、监控Tomcat
监控Tomcat需要用到zabbix_java_gateway这个组件(在zabbix2.0引入的功能),顾名思义就是:Java网关,类似于agentd,但是只用于Java方面。需要注意的是,它只能主动去获取数据,而不能被动获取数据。它的数据最终会提交到server端或proxy。
如果需要开启监控Tomcat功能,在编译zabbix server端时,需要添加–enable_java 参数。
1、Zabbix Server安装zabbix_java_gateway程序
zabbix_java_gateway程序既可以安装在zabbix server端,也可以安装在被监控端,不过一般都是安装在Zabbix server端,这里我就安装在Zabbix Server端。
[root@zabbix /]# cd /zabbix/
[root@zabbix zabbix]# rpm -ivh zabbix-java-gateway-3.2.1-1.el7.x86_64.rpm
[root@zabbix zabbix]# cd /etc/zabbix/
[root@zabbix zabbix]# sed -i 's/# LISTEN_IP="0.0.0.0"/LISTEN_IP="0.0.0.0"/g' zabbix_java_gateway.conf
[root@zabbix zabbix]# sed -i 's/# LISTEN_PORT=10052/LISTEN_PORT=10052/g' zabbix_java_gateway.conf
[root@zabbix zabbix]# egrep -v '^$|^#' zabbix_java_gateway.conf
LISTEN_IP="0.0.0.0"
LISTEN_PORT=10052
PID_FILE="/var/run/zabbix/zabbix_java.pid"
TIMEOUT=3
#启动zabbix-java
[root@zabbix zabbix]# systemctl start zabbix-java-gateway.service
[root@zabbix zabbix]# netstat -anput | grep 10052 # 确定端口在接听
tcp6 0 0 :::10052 :::* LISTEN 15501/java
[root@zabbix zabbix]# cd /
[root@zabbix /]# sed -i 's/# JavaGateway=/JavaGateway=192.168.1.136/g' /usr/local/zabbix/etc/zabbix_server.conf
[root@zabbix /]# sed -i 's/# JavaGatewayPort=10052/JavaGatewayPort=10052/g' /usr/local/zabbix/etc/zabbix_server.conf
[root@zabbix /]# sed -i 's/# StartJavaPollers=0/StartJavaPollers=5/g' /usr/local/zabbix/etc/zabbix_server.conf
[root@zabbix /]# /etc/init.d/zabbix_server restart
Restarting zabbix_server (via systemctl): [ OK ]
2、测试端配置Tomcat
这里我安装在了nginx那台服务器上
[root@agent /]# rpm -qa | grep jdk
java-1.8.0-openjdk-headless-1.8.0.242.b08-0.el7_7.x86_64
java-1.8.0-openjdk-devel-1.8.0.242.b08-0.el7_7.x86_64
java-1.8.0-openjdk-1.8.0.242.b08-0.el7_7.x86_64
java-1.7.0-openjdk-1.7.0.111-2.6.7.8.el7.x86_64
java-1.7.0-openjdk-headless-1.7.0.111-2.6.7.8.el7.x86_64
copy-jdk-configs-3.3-10.el7_5.noarch
[root@agent /]# rpm -e --nodeps java-1.8.0-openjdk-headless-1.8.0.242.b08-0.el7_7.x86_64
[root@agent /]# rpm -e --nodeps java-1.7.0-openjdk-headless-1.7.0.111-2.6.7.8.el7.x86_64
[root@agent /]# mkdir tomcat
[root@agent /]# cd tomcat/
[root@agent tomcat]# rz
[root@agent tomcat]# ls
apache-tomcat-8.5.35.tar.gz jdk-8u211-linux-x64.tar.gz
[root@agent tomcat]# tar zxf jdk-8u211-linux-x64.tar.gz -C /usr/local/
[root@agent tomcat]# tar zxf apache-tomcat-8.5.35.tar.gz
[root@agent tomcat]# mv apache-tomcat-8.5.35 /usr/local/tomcat
[root@agent tomcat]# vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_211
export JRE_HOME=/usr/local/jdk1.8.0_211/jre
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
[root@agent tomcat]# . /etc/profile
[root@agent tomcat]# java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
#kaiqi开启tomcat的JMX远程管理功能
[root@agent /]# vim /usr/local/tomcat/bin/catalina.sh
export JDK_JAVA_OPTIONS # 跳转到306行,添加如下内容
CATALINA_OPTS=" $CATALINA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8888 -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=192.168.1.138 -Dcom.sun.management.jmxremote.authenticate=false"
#注:上面的IP为Tomcat本机的IP,端口“8888”可以自定义,不要冲突即可
[root@agent /]# /usr/local/tomcat/bin/startup.sh
[root@agent /]# netstat -anput | grep 8888
tcp6 0 0 :::8888 :::* LISTEN 70730/java
[root@agent /]# netstat -anput | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 70730/java
3、回到Zabbix-server进行JMX连接测试
注:该测试需要系统支持图形化桌面,若没有图形化桌面,此验证过程可以省略。
[root@zabbix ~]# jconsole #执行此命令,此命令是Java-gateway提供的。
执行上面的命令后,会弹出下面提示框(输入Tomcat的IP+JMX监听端口,进行连接):
4、登录到Zabbix Server的web界面进行配置