zabbix 监控 tomcat
一, 脚本监控文件
1 #!/bin/bash 2 # @Function 3 # Find out the highest cpu consumed threads of java, and print the stack of these threads. 4 # 5 # @Usage 6 # $ ./show-busy-java-threads.sh 7 # 8 # @author Jerry Lee 9 10 readonly PROG=`basename $0` 11 readonly -a COMMAND_LINE=("$0" "$@") 12 13 usage() { 14 cat <<EOF 15 Usage: ${PROG} [OPTION]... 16 Find out the highest cpu consumed threads of java, and print the stack of these threads. 17 Example: ${PROG} -c 10 18 19 Options: 20 -p, --pid find out the highest cpu consumed threads from the specifed java process, 21 default from all java process. 22 -c, --count set the thread count to show, default is 5 23 -h, --help display this help and exit 24 EOF 25 exit $1 26 } 27 28 readonly ARGS=`getopt -n "$PROG" -a -o c:p:h -l count:,pid:,help -- "$@"` 29 [ $? -ne 0 ] && usage 1 30 eval set -- "${ARGS}" 31 32 while true; do 33 case "$1" in 34 -c|--count) 35 count="$2" 36 shift 2 37 ;; 38 -p|--pid) 39 pid="$2" 40 shift 2 41 ;; 42 -h|--help) 43 usage 44 ;; 45 --) 46 shift 47 break 48 ;; 49 esac 50 done 51 count=${count:-5} 52 53 redEcho() { 54 [ -c /dev/stdout ] && { 55 # if stdout is console, turn on color output. 56 echo -ne "\033[1;31m" 57 echo -n "$@" 58 echo -e "\033[0m" 59 } || echo "$@" 60 } 61 62 yellowEcho() { 63 [ -c /dev/stdout ] && { 64 # if stdout is console, turn on color output. 65 echo -ne "\033[1;33m" 66 echo -n "$@" 67 echo -e "\033[0m" 68 } || echo "$@" 69 } 70 71 blueEcho() { 72 [ -c /dev/stdout ] && { 73 # if stdout is console, turn on color output. 74 echo -ne "\033[1;36m" 75 echo -n "$@" 76 echo -e "\033[0m" 77 } || echo "$@" 78 } 79 80 # Check the existence of jstack command! 81 if ! which jstack &> /dev/null; then 82 [ -z "$JAVA_HOME" ] && { 83 redEcho "Error: jstack not found on PATH!" 84 exit 1 85 } 86 ! [ -f "$JAVA_HOME/bin/jstack" ] && { 87 redEcho "Error: jstack not found on PATH and $JAVA_HOME/bin/jstack file does NOT exists!" 88 exit 1 89 } 90 ! [ -x "$JAVA_HOME/bin/jstack" ] && { 91 redEcho "Error: jstack not found on PATH and $JAVA_HOME/bin/jstack is NOT executalbe!" 92 exit 1 93 } 94 export PATH="$JAVA_HOME/bin:$PATH" 95 fi 96 97 readonly uuid=`date +%s`_${RANDOM}_$$ 98 99 cleanupWhenExit() { 100 rm /tmp/${uuid}_* &> /dev/null 101 } 102 trap "cleanupWhenExit" EXIT 103 104 printStackOfThread() { 105 local line 106 local count=1 107 while IFS=" " read -a line ; do 108 local pid=${line[0]} 109 local threadId=${line[1]} 110 local threadId0x=`printf %x ${threadId}` 111 local user=${line[2]} 112 local pcpu=${line[4]} 113 114 local jstackFile=/tmp/${uuid}_${pid} 115 116 [ ! -f "${jstackFile}" ] && { 117 { 118 if [ "${user}" == "${USER}" ]; then 119 jstack ${pid} > ${jstackFile} 120 else 121 if [ $UID == 0 ]; then 122 sudo -u ${user} jstack ${pid} > ${jstackFile} 123 else 124 redEcho "[$((count++))] Fail to jstack Busy(${pcpu}%) thread(${threadId}/0x${threadId0x}) stack of java process(${pid}) under user(${user})." 125 redEcho "User of java process($user) is not current user($USER), need sudo to run again:" 126 yellowEcho " sudo ${COMMAND_LINE[@]}" 127 echo 128 continue 129 fi 130 fi 131 } || { 132 redEcho "[$((count++))] Fail to jstack Busy(${pcpu}%) thread(${threadId}/0x${threadId0x}) stack of java process(${pid}) under user(${user})." 133 echo 134 rm ${jstackFile} 135 continue 136 } 137 } 138 blueEcho "[$((count++))] Busy(${pcpu}%) thread(${threadId}/0x${threadId0x}) stack of java process(${pid}) under user(${user}):" 139 sed "/nid=0x${threadId0x} /,/^$/p" -n ${jstackFile} 140 done 141 } 142 143 144 ps -Leo pid,lwp,user,comm,pcpu --no-headers | { 145 [ -z "${pid}" ] && 146 awk '$4=="java"{print $0}' || 147 awk -v "pid=${pid}" '$1==pid,$4=="java"{print $0}' 148 } | sort -k5 -r -n | head --lines "${count}" | printStackOfThread
在tomcat中执行脚本
会这样显示
二, zabbix服务端配置
在清华源下载zabbix-java-getway
rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-java-gateway-4.0.0-2.el7.x86_64.rpm
再次安装依赖
yum install -y zabbix-java-gateway-4.0.0-2.el7.x86_64
启动服务
systemctl enable zabbix-java-gateway.service
systemctl start zabbix-java-gateway.service
systemctl status zabbix-java-gateway.service
重启服务器
systemctl restart zabbix-java-gateway.service
三, 客户端配置
安装zabbix -agent
rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
yum install zabbix-agent -y
配置被监控项
vim /application/tomcat/bin/catalina.sh +97
CATALINA_OPTS="$CATALINA_OPTS
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=12345
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=10.0.0.11"
重新启动
systemctl start zabbix-agent.service
四,web界面添加
添加主机
完成后添加模板等待就行