zabbix使用jstat自动发现监控tomcat多实例

使用jstat -gc TOMCAT ID将数据重新排序导入到$TOMCAT文件中,循环执行
但是jstat这条命令在被zabbix执行需要和tomcat启动用户一样的权限,你可以将zabbix-agent使用和tomcat启动用户一样的用户来启动zabbix-agent,也可以visudo

[root@iZ62bxn7uuzZ scripts]# cat jstat.sh 
#!/bin/bash
# -------------------------------------------------------------------------------
# Filename:    jstat.sh
# Revision:    1.1
# Date:        20160707
# Author:      mark
# Email:       usertzc@163.com
# Website:     www.linuxea.com
# -------------------------------------------------------------------------------
# Notice
# Apply zabbix version 2.4.x to 3.0.3 
# auto search jmx pid
# You need to understand:
# jstat.sh search pid;jvm_name.sh search tomcat name;jvm_status.sh Obtain $1 transfer
# Automatic discovery jmx monitoring.It applies only to jstat -gc pid it
#################################################################################
tomcat_name=`ps -ef | grep tomcat | grep -v grep | grep -v "jvm_status.sh" | awk -F "=" '{print $NF}' | cut -d "/" -f 3`

for t in ${tomcat_name[@]};do
        t_id=`ps -ef | grep  "$t/" | grep -v "grep" | awk '{print $2}'`
        /usr/java/jdk1.7.0_79/bin/jstat -gc $t_id | awk 'BEGIN{FS=" "}{for(i = 1;i <= NF;i++) {array[i,NR]=$i}}END {for(i = 1;i <= NF;i++) {for(j = 1;j <= NR;j++) {printf "%s ",array[i,j]}printf "\n"}}' > /tmp/"$t".gc
done

自动发现tomcat路径,以路径命名

[root@iZ62bxn7uuzZ scripts]# cat jvm_name.sh 
#!/bin/bash
# -------------------------------------------------------------------------------
# Filename:    jvm_name.sh
# Revision:    1.1
# Date:        20160707
# Author:      mark
# Email:       usertzc@163.com
# Website:     www.linuxea.com
# -------------------------------------------------------------------------------
# Notice
# Apply zabbix version 2.4.x to 3.0.3 
# auto search jmx name.
# You need to understand:
# jstat.sh search pid;jvm_name.sh search tomcat name;jvm_status.sh Obtain $1 transfer
# Automatic discovery jmx monitoring.It applies only to jstat -gc pid it
#################################################################################
tomcat_name=`ps -ef | grep tomcat | grep -v grep | awk -F "=" '{print $NF}' | cut -d "/" -f 3`
falg=0
count=`ps -ef | grep tomcat | grep -v grep | wc -l` 
if [ $count == 0 ];then
exit
fi
echo '{"data":['
echo "$tomcat_name" |while read LINE;do
echo -n '{"{#JVMNAME}":"'$LINE'"}'
flag=`expr $flag + 1`
if [ $flag -lt $count ];then
echo ','
fi
done
echo ']}'

这里其实可简写:

[root@iZ62bxn7uuzZ scripts]# cat jvm_status.sh 
#!/bin/bash
# -------------------------------------------------------------------------------
# Filename:    jvm_status.sh
# Revision:    1.1
# Date:        20160707
# Author:      mark
# Email:       usertzc@163.com
# Website:     www.linuxea.com
# -------------------------------------------------------------------------------
# Notice
# Apply zabbix version 2.4.x to 3.0.3 
# auto search jmx name
# You need to understand:
# jstat.sh search pid;jvm_name.sh search tomcat name;jvm_status.sh Obtain $1 transfer
# Automatic discovery jmx monitoring.It applies only to jstat -gc pid it
#################################################################################
t=$1
jvm_key=$2
cat /tmp/"$t".gc | grep -w "$jvm_key" | awk '{print $2}'

随便过滤下http的线程数即可:

jvm_thread_num.sh
#!/bin/sh
jvmname=$1
pid=`ps -ef | grep "$jvmname" | grep -v grep | grep -v "$0"| awk '{print $2}' `
jvm_status=`sudo -u ody /usr/local/java/jdk1.7.0_80/bin/jstack "$pid" > /data/zabbix/shell/jstack.txt`
function all {
    cat /data/zabbix/shell/jstack.txt | grep http|wc -l
         }  
function runnable { 
    cat /data/zabbix/shell/jstack.txt | grep http|grep runnable|wc -l
          }
$2

在配置文件中添加即可:

UserParameter=jvm.name,/usr/local/zabbix/scripts/jvm_name.sh
UserParameter=jvm.thread.num[*],/usr/local/zabbix/scripts/jvm_thread_num.sh $1 $2
UserParameter=jvm.status[*],/usr/local/zabbix/scripts/jvm_status.sh $1 $2

计划任务中需要每分钟执行循环脚本追加文件,保持每分钟的最新状态

*/1 * * * * /bin/bash /usr/local/zabbix/scripts/jstat.sh 2>/dev/null

jstat:

check_mode() {
    tomcat_name=`ps -ef | grep tomcat | grep -v grep | grep -v "jvm_status.sh" | awk -F "=" '{print $NF}' | cut -d "/" -f 3`

for t in ${tomcat_name[@]};do
   t_id=`ps -ef | grep  "$t/" | grep -v "grep" | awk '{print $2}'`
   /usr/java/jdk1.7.0_79/bin/jstat -gc $t_id | \
   awk 'BEGIN{FS=" "}{for(i=1;i<=NF;i++) {array[i,NR]=$i}}END {for(i = 1;i <= NF;i++) {for(j = 1;j <= NR;j++) {printf "%s ",array[i,j]}printf "\n"}}' > /tmp/"$t".gc
        done
}

while :; do
        check_mode
        sleep 60
done

模板下载 密码:k639

1 分享

您可以选择一种方式赞助本站

支付宝扫码赞助

支付宝扫码赞助

日期: 2016-08-19分类: zabbix

标签: zabbix

发表评论

已有 4 条评论

加载中,请稍候...
  1. fuck001
    fuck001
    June 21st, 2017 at 11:56 am

    你好,老司机,我用你的方法来做,zabbix web哪里总是提示Unsupported item key

    1. mark
      June 23rd, 2017 at 09:04 pm

      这也是个思路,不一定要用这种办法,问清楚监控那些项目即可
      不过,这个毕竟耗资源,我已经弃用了

      1. 小蜜蜂
        小蜜蜂
        January 3rd, 2018 at 05:05 pm

        老哥,那你现在用的什么方式监控tomcat

  2. kinglong
    kinglong
    November 3rd, 2017 at 05:15 pm

    导入 模板失败,提示
    Error in query [SELECT g.graphid,g.name FROM graphs g WHERE (g.name IN ('{#JVMNAME}','{#JVMNAME}采样'))] [Illegal mix of collations (latin1_swedish_ci,IMPLICIT), (utf8_general_ci,COERCIBLE), (utf8_general_ci,COERCIBLE) for operation ' IN ']
    Error in query [SELECT DISTINCT g.graphid FROM graphs g,graphs_items gi,items i WHERE i.hostid='10125' AND gi.graphid=g.graphid AND i.itemid=gi.itemid AND g.name='{#JVMNAME}采样'] [Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '=']

    怎么解决呢?