码迷,mamicode.com
首页 > 其他好文 > 详细

Tomcat 监控的几种方法

时间:2015-06-17 11:09:10      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

Tomcat 监控方法

方法1:.使用tomcat自带的status页

具体方法:

步骤1:修改%tomcat安装路径%\conf \tomcat-users文件,配置admin设置权限。在<tomcat-users>中增加部分内容。具体如下:

 <role rolename="manager-gui"/>

 <user username=“manager" password=“1234" roles="manager-gui"/>

  注:用户名:manager,密码:1234

步骤2:完成后,启动tomcat,输入:http://localhost:8080  --(IP,端口号,可远程访问)
点击status,输入账号,密码(manager,1234),进入status,时时刷新页面,查看当前tomcat状态。 或者直接访问:http://localhost:8080/manager/status页面。

备注1:若希望整个服务器的性能数据以一个单行的xml文件形式表示,则进入如下界面:http://localhost:8080/manager/status?XML=true

备注2:若服务器中存在几个项目,单独对某个项目进行监控,则需要另行增加代码。

步骤3:上面得到的只是当前情况下的性能数据,要获得一个阶段的性能数据,必须设定采样频率,定时读取,将数据汇总并分析。

步骤4:对得到的数据得出图表。参考以下示例:

通过Linux自带的Bash来实现,绘制图表采用的是Gnuplot。下列代码生成html的报表只是监控整个服务器的,并未实现对每个测试建立单独目录的功能。

#!/bin/bash

# ========================Default Settings===============================

# address of status page

STATUS_ADDR="http://localhost:8080/manager/status?XML=true"

USER="manager"

PASS="1234"

# sample rate, default: 5seconds

SAMPLE_RATE=5

# if press "Ctrl+c", stop monitor

EXIT_SIGNAL=2

# connector to monitor

CONNECTOR="http-8080"

# result directory to store data

RESULT_DIR="/tmp"

# perf data file

PERF_DATA="perf_data"

# jvm data file

JVM_DATA="jvm_data"

# connector data file

CONNECTOR_DATA="connector_data"

# thread data file

THREAD_DATA="thread_data"

# request data file

REQUEST_DATA="request_data"

 

# ===========================Output Error Message========================

# Show Error Message and exit, get one parameter

errorMsg()

{

        if [[ $# -eq 1 ]]; then

                echo "Runtime Error: $1"

                exit 1

        else

                echo "Function Error: errorMsg"

                exit 127

        fi

}

 

# =========================Get Data Function=============================

# Get performance data, no parameter wanted

getPerfData()

{

        cd $RESULT_DIR

        wget --http-user="$USER" --http-password="$PASS" "$STATUS_ADDR" -O "$PERF_DATA" || errorMsg "Failed to get data, please check the connection"

        # JVM data

        sed ‘s/.*<jvm>//g;s/<\/jvm>.*//g‘ $PERF_DATA | awk -F \‘ ‘{ print $2, $4, $6 }‘ >> $JVM_DATA

        # ‘Connector data

        sed ‘s/.*‘$CONNECTOR‘.>//g;s/<\/connector>.*//g‘ $PERF_DATA >> $CONNECTOR_DATA

        # Thread data

        sed ‘s/.*<threadInfo//g;s/\/>.*//g‘ $CONNECTOR_DATA | awk -F \" ‘{ print $2, $4, $6 }‘ >> $THREAD_DATA

        # " Request data

        sed ‘s/.*<requestInfo//g;s/\/>.*//g‘ $CONNECTOR_DATA | awk -F \" ‘{ print $2, $4, $6, $8, $10, $12 }‘ >> $REQUEST_DATA

 

}

 

# ========================Build Chart Function==========================

# "according the data, build the chart (use gnuplot)

buildChart()

{   

        TITLE=""

        OUTPUT=""

        PLOT=""

        YRANGE="[0:]"

        case "$1" in

                "jvm" )

                TITLE="JVM"

                OUTPUT="jvm_graph.png"

                PLOT="plot ‘jvm_data‘ using 1 title ‘free‘ w linespoints, \

                ‘jvm_data‘ using 2 title ‘total‘ w linespoints,\

                ‘jvm_data‘ using 3 title ‘max‘ w linespoints"

                ;;

 

                "thread" )

                TITLE="Thread"

                OUTPUT="thread_graph.png"

                PLOT="plot ‘thread_data‘ using 1 title ‘max threads‘ w linespoints,\

                ‘thread_data‘ using 2 title ‘current thread count‘ w linespoints,\

                ‘thread_data‘ using 3 title ‘current thread busy‘ w linespoints"

                ;;

 

                "request" )

                TITLE="Request"

                YRANGE="[-1:]"

                OUTPUT="request_graph.png"

                PLOT="plot ‘request_data‘ using 1 title ‘max time‘ w linespoints,\

                ‘request_data‘ using 2 title ‘processing time‘ w linespoints,\

                ‘request_data‘ using 3 title ‘request count‘ w linespoints,\

                ‘request_data‘ using 4 title ‘error count‘ w linespoints,\

                ‘request_data‘ using 5 title ‘bytes received‘ w linespoints,\

                ‘request_data‘ using 6 title ‘bytes sent‘ w linespoints"

                ;;

        esac

 

        # build graph

        gnuplot <<EOF

        set terminal png small size 480,360

        set title "$TITLE"

        set yrange $YRANGE

        set grid

        set xlabel "timeline (s)"

        set output "$OUTPUT"

        $PLOT

EOF

}

 

# ========================Build Report Function=========================

# include data and chart, give a readable html report

buildReport()

{

        # build graph jvm, request,thread

        buildChart "jvm" || errorMsg "Function Error: build jvm graph"

        buildChart "thread" || errorMsg "Function Error: build thread graph"

        buildChart "request" || errorMsg "Function Error: build request graph"

        # build html report

}

 

# ========================Stop Monitor Function

# call buildReport function

stopMonitor()

{

    echo "Monitor stopped, and we are building the report ..."

    buildReport || errorMsg "Function Error: buildReport"

    exit

}

 

# =============================Main Function=============================

trap "stopMonitor" $EXIT_SIGNAL

while :

do

        getPerfData || errorMsg "Failed to get performance data"

        sleep $SAMPLE_RATE

done

方法2:使用JDK自带工具,Jconsole
具体方法:

步骤1:.编辑%tomcat安装路径%\bin\catalina.bat文件。添加下列内容:
  set JAVA_OPTS= -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=10004 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false

 步骤2启动tomcat,进入JDK安装路径\jdk1.5.0_22\bin 下双击打开Jconsole文件,显示Jconsole连接页面。选择进程点击连接即可。

 

方法3:使用SiteScope(可与lR结合)
 具体方法:

步骤1安装SiteScope,(LR安装文件夹下有安装文件:在Additional Components文件夹内)。安装后需重启,重启后还会有SiteScope后续的安装操作。
步骤2自动打开SiteScope页面,需输入注册码注册:
步骤3修改%tomcat安装路径%\conf \tomcat-users文件 (见方法1)。添加部分代码如下:
  <tomcat-users>
  <user password="1234" roles="manager" username="admin"/>
  </tomcat-users>
 通过http://localhost:8080/Status可访问。
步骤4编辑%tomcat安装路径%\bin下catalina.bat文件;
    加入:
    set JAVA_OPTS= -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=10004 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
保证进入JDK安装路径\jdk1.5.0_22\bin 下双击打开Jconsole文件,显示Jconsole连接页面。显示可连接的进程。
步骤5tomcat配置完成后,重启。
步骤6输入SiteScope地址,显示SiteScope配置页面:
    http://IP:端口号/SiteScope
     Add Group--Add Montior--选择JMX
     需注意:JMX URL 为 :service:jmx:rmi:///jndi/rmi://10.110.100.128:10004/jmxrmi
username 和password与tomcat-users文件中设置一致,此处分别为:manager,1234
点击Get Counters添加计数器。此时会显示tomcat可监控的计数器。
例如:
  java.lang/Memory/HeapMemoryUsage/used

  java.lang/Memory/HeapMemoryUsage/max

  Catalina/ThreadPool/jk-8010/currentThreadCount

  Catalina/ThreadPool/jk-8010/maxSpareThreads

  Catalina/ThreadPool/jk-8010/minSpareThreads

  其它项根据自己情况自己填写即可。完成后点击OK。

步骤8进入LR Controller中,选择:Available Graphs\System Resouce Graphs 中的SiteScope,添加计数器即可。

步骤9修改LR脚本:

在录制好的LR脚本中添加如下代码:
double atof(const char *string);

float FreeMemory,TotalMemory,MaxMemory;

 //定义tomcat内存使用情况的监视器事务;

lr_start_transaction("monitor tomcat");

//保存3个参数;

 web_reg_save_param("JVMFreeMemory", 

     "LB=Free memory: ",

     "RB= MB",

     "Ord=1",

   LAST);

   web_reg_save_param("JVMTotalMemory",

   "LB=Total memory: ",

   "RB= MB",

   "Ord=1",

   LAST);

     web_reg_save_param("JVMMaxMemory",

     "LB=Max memory: ",

     "RB= MB",

     "Ord=1",

    LAST);

//通过LR去访问tomcat监控页

   web_set_user("admin","1234","localhost:8080");

   web_url("status",

        "URL=http://localhost:8080/manager/status",

        "Resource=0",

        "RecContentType=text/html",

        "Referer=",

        "Snapshot=t1.inf",

        "Mode=HTTP",

    LAST);

 
 FreeMemory=atof(lr_eval_string("{JVMFreeMemory}"));
 TotalMemory=atof(lr_eval_string("{JVMTotalMemory}"));
 MaxMemory=atof(lr_eval_string("{JVMMaxMemory}"));
 lr_output_message("%.2f %.2f %.2f",FreeMemory,TotalMemory,MaxMemory);

// Tomcat JVM metrics  使用lr_user_data_point()添加数据到图表中去
    lr_user_data_point("Tomcat JVM Free memory", FreeMemory);
    lr_user_data_point("Tomcat JVM Total memory", TotalMemory);
    lr_user_data_point("Tomcat JVM Max memory", MaxMemory);
 lr_end_transaction("monitor tomcat", LR_AUTO);
return 0;

 

在设置过程中可能会存在问题:

  1. 每次启动tomcat后,SiteScope都不能访问;

怀疑端口冲突:将tomcat为默认的设置:8005 8080 8009

查看SiteScope的配置如下:SiteScope安装路径\Tomcat\conf下server文件:

 8005 58999 8009 后更改SiteScope中的8005 8009端口,重启后问题解决。

tomcat与SiteScope可同时启动。

    1. SiteScope中添加计数器时,username与password要和tomcat中的tomcat-users中配置一致。
    2. SiteScope中添加计数器时,JMX URL 中的端口要与tomcat中catalina.bat文件jmxremote.port 端口一致。
    3. SiteScope中不能得到计数器时,查看各项配置是否有误,http://localhost:8080/Status能否访问与Jconsole中有无进程可连接。保证配置无误。
    4. SiteScope重启服务可以在我的电脑--右击--管理--服务中去找。
    5. SiteScope安装在被监测的应用服务器或数据库服务器上。(自已没试远程。)
    6. 通过修改LR脚本监控tomcat时,如果使用atof函数,需先声明。否则会影响输出结果。

Tomcat 监控的几种方法

标签:

原文地址:http://www.cnblogs.com/zhengah/p/4582549.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!