Tomcatのメモリとスレッド監視

Tomcat の managerをたたいて、スレッド状態とメモリ状態を監視するためのスクリプト
適当につくったけど、こんなんでいいのかな。

#!/bin/bash

XML_FILE=test.xml
JK_PORT=8009
MANAGER_HOST=localhost
MANAGER_PORT=8080
MANAGER_USER=tomcat
MANAGER_PASS=tomcat

manager_url=`printf http://%s:%d/manager/status?XML=true $MANAGER_HOST $MANAGER_PORT`
curl --silent --basic --user $MANAGER_USER:$MANAGER_PASS  $manager_url > $XML_FILE

# memory
memtotal=`xmllint --xpath "string(/status/jvm/memory/@total)" $XML_FILE`
memfree=`xmllint --xpath "string(/status/jvm/memory/@free)" $XML_FILE`
memmax=`xmllint --xpath "string(/status/jvm/memory/@max)" $XML_FILE`

printf "memtotal=%d memfree=%d memmax=%d\n" $memtotal $memfree $memmax

# Thread 
xpath_str_maxThreads=`printf /status/connector[@name=\'jk-%d\']/threadInfo/@%s $JK_PORT 'maxThreads'`
maxThread=`xmllint --xpath "string($xpath_str_maxThreads)" $XML_FILE`

xpath_str_threadBusy=`printf /status/connector[@name=\'jk-%d\']/threadInfo/@%s $JK_PORT 'currentThreadsBusy'`
busyThread=`xmllint --xpath "string($xpath_str_threadBusy)" $XML_FILE`

xpath_str_currentThread=`printf /status/connector[@name=\'jk-%d\']/threadInfo/@%s $JK_PORT 'currentThreadCount'`
currentThread=`xmllint --xpath "string($xpath_str_currentThread)" $XML_FILE`

printf "maxThread=%d busyThread=%d currentThread=%d\n" $maxThread $busyThread $currentThread

rm $XML_FILE

実行結果

$ ./tomcat_check.sh 
memtotal=85000192 memfree=69646696 memmax=129957888
maxThread=200 busyThread=1 currentThread=4

Threadbusyは今リクエストを処理してるスレッドの数
currentThreadは、現在既に存在していて、待機しているスレッド
とかだったかな。

Macだと上ので動いたけど、CentOS5では動かなかったので、少し書き換えた。
属性の値をとるところはgrep,awk,trを使って文字列解析して取得することにした。

#!/bin/bash

XML_FILE=test.xml
JK_PORT=8009
MANAGER_HOST=localhost
MANAGER_PORT=8080
MANAGER_USER=tomcat
MANAGER_PASS=tomcat

manager_url=`printf http://%s:%d/manager/status?XML=true $MANAGER_HOST $MANAGER_PORT`
curl --silent --basic --user $MANAGER_USER:$MANAGER_PASS  $manager_url > $XML_FILE

# memory
memtotal=`echo cat '/status/jvm/memory/@total' | xmllint --shell $XML_FILE  | grep -v ">" |  awk 'BEGIN {FS="="}{ print $2 }' |  tr -d \"`
memfree=`echo cat '/status/jvm/memory/@free' | xmllint --shell $XML_FILE  | grep -v ">" |  awk 'BEGIN {FS="="}{ print $2 }' |  tr -d \"`
memmax=`echo cat '/status/jvm/memory/@max' | xmllint --shell $XML_FILE  | grep -v ">" |  awk 'BEGIN {FS="="}{ print $2 }' |  tr -d \"`

printf "memtotal=%d memfree=%d memmax=%d\n" $memtotal $memfree $memmax

# Thread 
xpath_str_maxThreads=`printf /status/connector[@name=\'jk-%d\']/threadInfo/@%s $JK_PORT 'maxThreads'`
maxThread=`echo cat "$xpath_str_maxThreads" | xmllint --shell $XML_FILE  | grep -v ">" |  awk 'BEGIN {FS="="}{ print $2 }' |  tr -d \"`

xpath_str_threadBusy=`printf /status/connector[@name=\'jk-%d\']/threadInfo/@%s $JK_PORT 'currentThreadsBusy'`
busyThread=`echo cat "$xpath_str_threadBusy" | xmllint --shell $XML_FILE  | grep -v ">" |  awk 'BEGIN {FS="="}{ print $2 }' |  tr -d \"`

xpath_str_currentThread=`printf /status/connector[@name=\'jk-%d\']/threadInfo/@%s $JK_PORT 'currentThreadCount'`
currentThread=`echo cat "$xpath_str_currentThread" | xmllint --shell $XML_FILE  | grep -v ">" |  awk 'BEGIN {FS="="}{ print $2 }' |  tr -d \"`

printf "maxThread=%d busyThread=%d currentThread=%d\n" $maxThread $busyThread $currentThread

rm $XML_FILE

JMXとかSNMPでもいいけど、とりあえず遊びとしてはこれでもいいかな。