参考:http://www.iyunv.com/thread-227674-1-1.html
1、上传 catalina-jmx-remote.jar,
cmdline-jmxclient-0.10.3.jar,
jmx_discovery.sh 和 add_service_tomcat.sh 到 /tmp/目录下
2、给 add_service_tomcat.sh 赋执行权限,并执行
执行完这个脚本之后,需要重启你的tomcat服务,和zabbix_agentd的服务
3 cat add_service_tomcat.sh 脚本内容
#,根据自己的情况各自改下你们安装tomcat的路径即可。
t_datadir=`find /home/edu-*/*/bin/ -name catalina.sh|awk -F "/bin/catalina.sh" ‘{print $1}‘` tomcat_no=`find /home/edu-*/*/bin/ -name catalina.sh|awk -F "/bin/catalina.sh" ‘{print $1}‘|wc -l` n_port=12345 #配置监控初始端口 local_ip=`ifconfig eth0 |awk -F ‘[ :]+‘ ‘NR==2 {print $4}‘` #提取主机IP for tomcat in $t_datadir do #m_no=`cat -n $tomcat/bin/catalina.sh|grep ‘Execute The Requested Command‘|awk ‘{print $1}‘` #提取代码插入位置 cp $tomcat/bin/catalina.sh $tomcat/bin/catalina.sh_bak #备份catalina.sh cp /tmp/catalina-jmx-remote.jar $tomcat/lib/catalina-jmx-remote.jar #复制文件到实例lib目录 sed -i ‘‘84‘a export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote"‘ $tomcat/bin/catalina.sh #插入监控配置 #let "m_no=m_no+1" #设置行号 sed -i ‘‘85‘a export CATALINA_OPTS="$CATALINA_OPTS -Djava.rmi.server.hostname=‘$local_ip‘"‘ $tomcat/bin/catalina.sh #插入监控配置 #let "m_no=m_no+1" #设置行号 sed -i ‘‘86‘a export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.port=‘$n_port‘"‘ $tomcat/bin/catalina.sh #插入监控配置 #let "m_no=m_no+1" #设置行号 sed -i ‘‘87‘a export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.ssl=false"‘ $tomcat/bin/catalina.sh #插入监控配置 #let "m_no=m_no+1" #设置行号 sed -i ‘‘88‘a export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.authenticate=false"‘ $tomcat/bin/catalina.sh #插入监控配置 let "n_port=n_port+1" done local_ip=`ifconfig eth0 |awk -F ‘[ :]+‘ ‘NR==2 {print $4}‘` #提取主机IP cat >> /usr/local/zabbix/etc/zabbix_agentd.conf <<END #修改zabbix_agentd.conf,添加KEY UserParameter=java.jmx.discovery[*],/home/zabbix/bin/jmx_discovery.sh jmx_port_discovery UserParameter=java.Runtime.status[*],java -jar /lib/cmdline-jmxclient-0.10.3.jar - $local_ip:\$1 java.lang:type=Runtime \$2 2>&1 |grep \$2 |awk ‘{print \$NF}‘ UserParameter=java.Memory.status[*],java -jar /lib/cmdline-jmxclient-0.10.3.jar - $local_ip:\$1 java.lang:type=Memory \$2 2>&1 |grep \$2 |awk ‘{print \$NF}‘ UserParameter=java.System.status[*],java -jar /lib/cmdline-jmxclient-0.10.3.jar - $local_ip:\$1 java.lang:type=OperatingSystem \$2 2>&1 |grep \$2 |awk ‘{print \$NF}‘ UserParameter=java.HeapMemoryUsage.status[*],java -jar /lib/cmdline-jmxclient-0.10.3.jar - $local_ip:\$1 java.lang:type=Memory HeapMemoryUsage 2>&1 |grep \$2 |awk ‘{print \$NF}‘ UserParameter=java.NonHeapMemoryUsage.status[*],java -jar /lib/cmdline-jmxclient-0.10.3.jar - $local_ip:\$1 java.lang:type=Memory NonHeapMemoryUsage 2>&1 |grep \$2 |awk ‘{print \$NF}‘ UserParameter=java.LoadClass.status[*],java -jar /lib/cmdline-jmxclient-0.10.3.jar - $local_ip:\$1 java.lang:type=ClassLoading \$2 2>&1 |awk ‘{print \$NF}‘ UserParameter=java.Threading.status[*],java -jar /lib/cmdline-jmxclient-0.10.3.jar - $local_ip:\$1 java.lang:type=Threading \$2 2>&1 |awk ‘{print \$NF}‘ END
4 jmx_discovery.sh脚本
#!/bin/bash # function:monitor tcp connect status from zabbix source /etc/bashrc >/dev/null 2>&1 source /etc/profile >/dev/null 2>&1 jmx_port_discovery () { jmx_port=($(cat /home/zabbix/scripts/java.txt|cut -d "|" -f2)) Tomcat_Name=($(cat /home/zabbix/scripts/java.txt|cut -d "|" -f1)) printf ‘{\n‘ printf ‘\t"data":[\n‘ for((i=0;i<${#jmx_port[@]};++i)) { num=$(echo $((${#jmx_port[@]}-1))) if [ "$i" != ${num} ];then printf "\t\t{ \n" printf "\t\t\t\"{#JMX_PORT}\":\"${jmx_port[$i]}\",\n" printf "\t\t\t\"{#JAVA_NAME}\":\"${Tomcat_Name[$i]}\"},\n" else printf "\t\t{ \n" printf "\t\t \n" printf "\t\t\t\"{#JMX_PORT}\":\"${jmx_port[$i]}\",\n" printf "\t\t\t\"{#JAVA_NAME}\":\"${Tomcat_Name[$i]}\"}]}\n" fi } } case "$1" in jmx_port_discovery) jmx_port_discovery ;; *) echo "Usage:$0 {jmx_port_discovery}" ;; esac
5cat find_port.sh
##获取tomcat的目录和端口
#!/bin/bash tomcat_path=`find /home/*/*/bin/ -name catalina.sh|awk -F "/bin/catalina.sh" ‘{print $1}‘` #tomcat_path=`ps -ef | grep [t]omcat | awk ‘{print $9}‘| awk -F"=|conf" ‘{print $3}‘` for t_path in $tomcat_path do t_service=`echo "$t_path"|awk -F"/" ‘{print $(NF)}‘` cd $t_path/bin t_port=`cat "$t_path"/bin/catalina.sh | grep 1234* |awk -F ‘"‘ ‘{print $2}‘| awk -F ‘=‘ ‘{print $2}‘` #t_port=`grep "Dcom.sun.management.jmxremote.port" catalina.sh |awk -F "=|\"" ‘{print $4}‘` echo "$t_path | $t_port" if [ $? -eq 0 ] && [ ! -z "$t_port" ];then echo "$t_service|$t_port" # echo $t_port else echo "$t_port must be number or is null" fi done
6 执行find_port.sh脚本获取到的内容放到java.txt这个文本里
/home/edu-hdkt/tomcat-app-service | 12345 /home/edu-hdkt/tomcat-eclass-app | 12346 /home/edu-hdkt/tomcat-eclass-service | 12347 /home/edu-hdkt/tomcat-homework-services | 12348 /home/edu-hdkt/tomcat-redis-service | 12349 /home/edu-hdkt/tomcat-socket-1 | 12350 /home/edu-hdkt/tomcat-socket-2 | 12351 /home/edu-hdkt/tomcat-socket | 12352 /home/edu-hdkt/tomcat-statistics-service | 12353 /home/edu-hdkt/tomcat-student-service | 12354 /home/edu-hdkt/tomcat-sync | 12355 /home/edu-hdkt/tomcat-webapp | 12356 /home/edu-jcpt/edu-cas-server | 12357 /home/edu-jcpt/edu-platform-openApi | 12358 /home/edu-jcpt/edu-platform-recovery | 12359 /home/edu-jcpt/edu-platform-service | 12360 /home/edu-jcpt/edu-platform-webapp | 12361 /home/edu-jcpt/statistical-agent | 12362 /home/edu-jcpt/statistical-service | 12363 /home/edu-jcpt/tomcat-sync | 12364
7 测试在服务端执行下面的命令
/usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k java.jmx.discovery 测试 ip是客户端ip
返回下面这个即为正常
{ "data":[ { "{#JMX_PORT}":"12345", "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-app-service"}, { "{#JMX_PORT}":"12346", "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-eclass-app"}, { "{#JMX_PORT}":"12347", "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-eclass-service"}, { "{#JMX_PORT}":"12348", "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-homework-services"}, { "{#JMX_PORT}":"12349", "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-redis-service"}, { "{#JMX_PORT}":"12350", "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-socket-1"}, { "{#JMX_PORT}":"12351", "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-socket-2"}, { "{#JMX_PORT}":"12352", "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-socket"}, { "{#JMX_PORT}":"12353", "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-statistics-service"}, { "{#JMX_PORT}":"12354", "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-student-service"}, { "{#JMX_PORT}":"12355", "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-sync"}, { "{#JMX_PORT}":"12356", "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-webapp"}, { "{#JMX_PORT}":"12357", "{#JAVA_NAME}":"/home/edu-jcpt/edu-cas-server"}, { "{#JMX_PORT}":"12358", "{#JAVA_NAME}":"/home/edu-jcpt/edu-platform-openApi"}, { "{#JMX_PORT}":"12359", "{#JAVA_NAME}":"/home/edu-jcpt/edu-platform-recovery"}, { "{#JMX_PORT}":"12360", "{#JAVA_NAME}":"/home/edu-jcpt/edu-platform-service"}, { "{#JMX_PORT}":"12361", "{#JAVA_NAME}":"/home/edu-jcpt/edu-platform-webapp"}, { "{#JMX_PORT}":"12362", "{#JAVA_NAME}":"/home/edu-jcpt/statistical-agent"}, { "{#JMX_PORT}":"12363", "{#JAVA_NAME}":"/home/edu-jcpt/statistical-service"}, { "{#JMX_PORT}":"12364", "{#JAVA_NAME}":"/home/edu-jcpt/tomcat-sync"}]}
第二个测试:
java -jar /home/soft/cmdline-jmxclient-0.10.3.jar - 58.116.1.2:12345 java.lang:type=Memory NonHeapMemoryUsage 07/10/2017 13:58:56 +0800 org.archive.jmx.Client NonHeapMemoryUsage: committed: 145186816 init: 2555904 max: -1 used: 141596768
第三个测试:
/home/usr/local/zabbix/bin/zabbix_get -s 58.116.7.248 -k java.HeapMemoryUsage.status[12346,max] 返回下面的值即为正常 1072168960
8 最后倒入模板见附件
9 确认出图
时间: 2024-10-10 13:56:09