zabbix监控nginx和tomcat
zabbix部署请参考:https://blog.csdn.net/weixin_45636702/article/details/102952949
zabbix邮件监控以及邮件报警请参考:https://blog.csdn.net/weixin_45636702/article/details/102976313
一、监控tomcat
zabbix_java_gateway:在 zabbix2.0之后引入的一个功能。顾名思义: Java网关,类似 agentd,但是只用于Java方面。需要特别注意的是,它只能主动去获取数据,而不能被动获取数据。它的数据最终会提交到 server或者 proxy。
如果需要开启监控tomcat需要在zabbix编译的的过程中添加--enable_java参数
zabbix-server端配置:
安装zabbix_java_gatway程序:
[root@localhost ~]# rpm -ivh zabbix-java-gateway-3.2.1-1.el7.x86_64.rpm
[root@localhost ~]# vim /etc/zabbix/zabbix_java_gateway.conf
修改如下:
LISTEN_IP="0.0.0.0" //java-gateway监听地址
LISTEN_PORT=10052 //监听的端口
PID_FILE="/var/run/zabbix/zabbix_java.pid" //pid文件存放路径
启动java-gateway:
[root@localhost ~]# systemctl start zabbix-java-gateway.service
[root@localhost ~]# netstat -anput | grep 10052
tcp6 0 0 :::10052 :::* LISTEN 9376/java
修改zabbixserver的配置文件,默认zabbix没有启动java监控程序:
[root@localhost ~]# vim /usr/local/zabbix/etc/zabbix_server.conf
修改如下:
JavaGateway=192.168.1.10 //JavaGateway的监听地址
JavaGatewayPort=10052 //JavaGateway的端口号
StartJavaPollers=5 //运行时开启的线程数
重启zabbix-server:
[root@localhost ~]# /etc/init.d/zabbix_server restart
Restarting zabbix_server (via systemctl): [ 确定 ]
安装tomcat并配置:
[root@localhost ~]# rpm -qa | grep jdk //检查是否安装rpm的java环境
copy-jdk-configs-1.2-1.el7.noarch
java-1.7.0-openjdk-headless-1.7.0.111-2.6.7.8.el7.x86_64
java-1.8.0-openjdk-1.8.0.102-4.b14.el7.x86_64
java-1.7.0-openjdk-1.7.0.111-2.6.7.8.el7.x86_64
java-1.8.0-openjdk-headless-1.8.0.102-4.b14.el7.x86_64
[root@localhost ~]# rpm -e java-1.7.0-openjdk-headless --nodeps
[root@localhost ~]# rpm -e java-1.8.0-openjdk-headless --nodeps //卸载自带的java环境
[root@localhost ~]# tar zxf jdk-8u211-linux-x64.tar.gz -C /usr/local/
[root@localhost ~]# vim /etc/profile
在最后添加:
export JAVA_HOME=/usr/local/jdk1.8.0_211 //指定java的路径
export JRE_HOME=/usr/local/jdk1.8.0_211/jre //java运行环境
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar //声明java中的类
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
[root@localhost ~]# source /etc/profile
[root@localhost ~]# tar zxf apache-tomcat-8.5.35.tar.gz
[root@localhost ~]# mv apache-tomcat-8.5.35 /usr/local/tomcat
开启tomcatjmx远程调试:
[root@localhost ~]# vim /usr/local/tomcat/bin/catalina.sh
在308行上面添加:
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.20 -Dcom.sun.management.jmxremote.authenticate=flase"
启动tomcat:
[root@localhost ~]# /usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8.0_211/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@localhost ~]# netstat -anput | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 4460/java
回到zabbix-server的web界面进行配置:
配置---主机---创建主机:
二、监控nginx
安装nginx:
只是简单的安装,详细部署请参考网上教程:
[root@localhost ~]# yum -y install pcre-devel openssl-devel
[root@localhost ~]# tar zxf nginx-1.14.0.tar.gz
[root@localhost ~]# cd nginx-1.14.0/
[root@localhost nginx-1.14.0]# ./configure && make && make install
[root@localhost nginx-1.14.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.14.0]# nginx
[root@localhost nginx-1.14.0]# netstat -anput | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8106/nginx:master
安装zabbix客户端:
[root@localhost ~]# tar zxf zabbix-3.2.1.tar.gz
[root@localhost ~]# cd zabbix-3.2.1/
[root@localhost zabbix-3.2.1]# ./configure --prefix=/usr/local/zabbix --enable-agent
[root@localhost zabbix-3.2.1]# make && make install
[root@localhost zabbix-3.2.1]# cp misc/init.d/fedora/core/zabbix_agentd /etc/init.d/
[root@localhost zabbix-3.2.1]# chmod +x /etc/init.d/zabbix_agentd
[root@localhost zabbix-3.2.1]# vim /etc/init.d/zabbix_agentd
修改如下:
BASEDIR=/usr/local/zabbix
PIDFILE=/usr/local/zabbix/logs/$BINARY_NAME.pid
[root@localhost zabbix-3.2.1]# useradd zabbix -s /sbin/nologin -M
[root@localhost ~]# vim /usr/local/zabbix/etc/zabbix_agentd.conf
修改如下:
[root@localhost ~]# grep -v ^$ /usr/local/zabbix/etc/zabbix_agentd.conf | grep -v ^#
PidFile=/usr/local/zabbix/logs/zabbix_agentd.pid
LogFile=/usr/local/zabbix/logs/zabbix_agentd.log
Server=192.168.1.10
ListenPort=10050
ListenIP=0.0.0.0
ServerActive=192.168.1.10
Hostname=192.168.1.20
Timeout=3
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
编写监控nginx脚本:
[root@localhost ~]# mkdir /usr/local/zabbix/scripts
[root@localhost ~]# cd /usr/local/zabbix/script/
[root@localhost script]# vim nginx.sh
#!/bin/bash
case $1 in
index)
RS=`curl -s -I 192.168.1.20/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.20/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
[root@localhost script]# chmod +x nginx.sh
[root@localhost script]# mkdir /usr/local/zabbix/logs
[root@localhost script]# chown -R zabbix:zabbix /usr/local/zabbix/
[root@localhost script]# /etc/init.d/zabbix_agentd start
Starting zabbix_agentd (via systemctl): [ 确定 ]
[root@localhost script]# netstat -anput | grep 10050
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 15550/zabbix_agentd
[root@localhost ~]# chmod +s /usr/bin/netstat
回到zabbix-server的web界面,创建ngninx监控模板:
1、监控nginx运行状态:
2、监控nginx总访问量:
3、独立IP数:
4、nginx主页的状态:
5、nginx当前并发数:
6、配置文件MD5值用来监控nginx配置文件是否被修改:
7、当前吞吐量bps,并且更新时间为60秒:
8、nginx工作进程的个数:
9、nginx虚拟主机的个数:
创建触发器:
1、创建nginx服务意外停止触发器:
{Template App Nginx Service:nginx.ping.last(0)}=0
{Template App Nginx Service:nginx.ping.last(0)}>0
2、创建nginx主页不可访问触发器:
{Template App Nginx Service:nginx.info[index].last(0)}>400
{Template App Nginx Service:nginx.info[index].last(0)}=200
3、创建nginx文件被修改触发器:
{Template App Nginx Service:nginx.info[MD5].last(0)}<>{Template App Nginx Service:nginx.info[MD5].last(1)}
在客户端主机中添加创建的nginx模板:
配置---主机---创建主机:
查看zabbix监控的数据:
给监控nginx状态添加映射(更加友好):
关联到监控项:
模拟并发:
[root@localhost ~]# while true
> do
> ab -n 1000 -c 100 192.168.1.20/index.html &> /dev/null
> done
创建图形: