zabbix通过JMX监控Tomcat
一、zabbix java gateway编译参数如下
如果没有zabbix的jmx封装的话,自己来写自定义脚本监控tomcat也是可以的,只是很麻烦。通过zabbix的jmx,监控tomcat就变得很简单。
java环境安装:
yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel -y
要使用java gateway需要在源码安装zabbix的时候,在./configure的时候加入
--enable-java
以下是我的zabbix server编译参数:
/configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2 --enable-java
make
make install
看下有没有成功:
ll -h /usr/local/zabbix/sbin/zabbix_java/
二、zabbix server启动java gateway
/usr/local/zabbix/sbin/zabbix_java/startup.sh
三、看下java gateway的监控端口(10052)
ps auxfw|grep JavaGateway
四、zabbix server配置vim /usr/local/zabbix/etc/zabbix_server.conf
JavaGateway=192.168.1.106 JavaGatewayPort=10052 StartJavaPollers=5 zabbix server --> zabbix java gateway --> 访问tomcat服务器
五、tomcat服务器加入如下配置(vim /usr/local/tomcat/bin/catalina.sh)
CATALINA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=12345 -Djava.rmi.server.hostname=192.168.1.106" 重启tomcat: /usr/local/tomcat/bin/startup.sh 需要先下载一个cmdline-jmxclient-0.10.3.jar,用来测试jmx获取监控数据,获取没问题后再在zabbix网页上添加监控项目。
六、自定义添加
jmx一般监控,都适用,讲讲自定义监控tomcat: cd /usr/local/tomcat/ java -jar cmdline-jmxclient-0.10.3.jar - 192.168.1.106:12345 java.lang:type=Memory HeapMemoryUsage java -jar cmdline-jmxclient-0.10.3.jar - 192.168.1.106:12345 java.lang:type=OperatingSystem ProcessCpuLoad
zabbix上添加监控:
先添加主机,使用jmx的接口进行监控。
jmx["java.lang:type=Memory",HeapMemoryUsage.used]默认(单位B) jmx["java.lang:type=Memory",HeapMemoryUsage.max]默认(单位B) jmx["java.lang:type=Memory",NonHeapMemoryUsage.max] 默认(单位B) jmx["java.lang:type=Memory",NonHeapMemoryUsage.used] 默认(单位B) jmx["java.lang:type=ClassLoading",UnloadedClassCount] 默认 jmx["java.lang:type=ClassLoading",LoadedClassCount] 默认 jmx["java.lang:type=ClassLoading",TotalLoadedClassCount] 默认 jmx["java.lang:type=OperatingSystem",OpenFileDescriptorCount] 默认 jmx["java.lang:type=OperatingSystem",MaxFileDescriptorCount] 默认 jmx["java.lang:type=OperatingSystem",ProcessCpuLoad]浮点数 % 自定义倍数100 jmx["java.lang:type=Runtime",Uptime] 默认 探测是否存活(nodata3分钟)
tomcat监控:
java -jar cmdline-jmxclient-0.10.3.jar - 192.168.1.106:12345 |grep -i Catalina java -jar cmdline-jmxclient-0.10.3.jar - 192.168.1.106:12345 "Catalina:name=\"http-bio-8080\",type=GlobalRequestProcessor" requestCount
zabbix上添加如下:
jmx["Catalina:name=\"http-bio-8080\",type=GlobalRequestProcessor",requestCount] 每秒的请求数 jmx["Catalina:name=\"http-bio-8080\",type=ThreadPool",maxThreads]
七、使用模板
jmx generic
jmx tomcat 默认的很多不能用,得改下
tomcat的安装脚本:
#!/bin/sh LANG=C if [ -d "/usr/local/tomcat/" ];then echo "tomcat is install" exit 1 else echo "tomcat in not install" fi my_gcc=`rpm -qa gcc` if [[ -n "$my_gcc" ]];then echo "$my_gcc" else yum -y install gcc fi my_make=`rpm -qa make` if [[ -n "$my_make" ]];then echo "$my_make" else yum -y install make fi my_tar=`rpm -qa tar` if [[ -n "$my_tar" ]];then echo "$my_tar" else yum -y install tar fi my_openssl=`rpm -qa openssl` if [[ -n "$my_openssl" ]];then echo "$my_openssl" else yum -y install openssl fi my_openssl_devel=`rpm -qa openssl-devel` if [[ -n "$my_openssl_devel" ]];then echo "$my_openssl_devel" else yum -y install openssl-devel fi my_jdk=`rpm -qa java-1.7.0-openjdk` if [[ -n "$my_jdk" ]];then echo "$my_jdk" else yum -y install java-1.7.0-openjdk fi cd /usr/local/src tar -zxvf apache-tomcat-7.0.63.tar.gz mv apache-tomcat-7.0.63 /usr/local/tomcat echo "install tomcat success. tomcat is in /usr/local/tomcat" exit 0