{"id":274,"date":"2009-04-30T09:30:55","date_gmt":"2009-04-30T08:30:55","guid":{"rendered":"http:\/\/www.askthegerman.com\/?p=274"},"modified":"2023-02-13T07:36:23","modified_gmt":"2023-02-13T06:36:23","slug":"monitor-your-tomcat-servlet-on-a-regular-basis","status":"publish","type":"post","link":"https:\/\/www.askthegerman.com\/?p=274","title":{"rendered":"Monitor your Tomcat Servlet on a regular basis"},"content":{"rendered":"<p>Lets assume you have enabled the monitoring capabilities of your tomcat.<br \/>\nIf you do not know how to achieve this see my earlier <a href=\"\/archives\/252\" title=\"Enabling tomcat monitoring capabilities\">posting<\/a> here about.<\/p>\n<p>Now it is cool to monitor your application on a regular basis.<br \/>\nBecause I&#8217;m used to shell scripting in LINUX I present you here a scripting solution within a bash shell. But I&#8217;m sure you can do the same in Windows.<br \/>\nThe idea is to set up a cron job ( for windows use the scheduler) and get the statuspage from your tomcat every &#8211; lets say &#8211; five minutes.<br \/>\nGet some of the interesting values out of this page and save them in a csv &#8211; File.<br \/>\nCreate a new file every day. Perhaps you want to use this files as input for a spreadsheet program to create some nice charts.<\/p>\n<p>But to be honest &#8211; this is a very special script &#8211; you have to adopt it to your needs.<br \/>\nThis are the basic prerequisites:<\/p>\n<ul>\n<li>you need linux to run it (this script is developed on a SUSE &#8211; Distribution)<\/li>\n<li>eventually you need to install some tools (I think the w3m tool is not bundled with every linux distribution)<\/li>\n<li>this script only! works correct with a tomcat 6.0 version<\/li>\n<\/ul>\n<p><font face=\"Courier\" size=\"1\"><span style=\"color: #006600\">#!\/bin\/bash<br \/>\n# -configure below-<\/span><br \/>\nHTTPPORT=&#8221;8080&#8243; &nbsp;&nbsp; <span style=\"color: #0066ff\"># The http Port of your tomcat<\/span><br \/>\nJKPORT=&#8221;8709&#8243;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: #0066ff\"># The Modjk Port of your tomcat (if your tomcat should be reachable from a apache webserver)<\/span><br \/>\nSERVER=&#8221;localhost:$HTTPPORT&#8221; <span style=\"color: #0066ff\"># you can even monitor a tomcat on another server!<\/span><br \/>\nUSER=&#8221;tomcat-oper&#8221; <span style=\"color: #0066ff\"># the account and<\/span><br \/>\nPW=&#8221;IwantItAll&#8221;&nbsp;&nbsp;&nbsp; <span style=\"color: #0066ff\"># password you have defined in tomcat-users.xml<\/span><br \/>\nAPPL=&#8221;CoolAppl&#8221; &nbsp;&nbsp; <span style=\"color: #0066ff\"># The name of your application<\/span><br \/>\n<span style=\"color: #006600\"># -configure above-<\/span><br \/>\nTODAY=`date &#8216;+%m-%d-%y&#8217;`<br \/>\nTIME=`date &#8216;+%H:%M:%S&#8217;`<br \/>\nOUTFILE=&#8221;tomcat-stats-${TODAY}.csv&#8221;<br \/>\n<span style=\"color: #006600\">#<\/span><br \/>\n<span style=\"color: #006600\">#set -x<\/span>&nbsp;&nbsp; <span style=\"color: #0066ff\"># uncomment if you want to improve the script or if you are searching for a bug<\/span><br \/>\n<span style=\"color: #006600\">#<\/span><br \/>\n<span style=\"color: #006600\">#<\/span> <span style=\"color: #0066ff\"># If there is no csv-file create one and put a headerline in<\/span><br \/>\nif !(test -e $OUTFILE) then<br \/>\n&nbsp; echo &#8220;Time,FreeMemory,UsedMemory,MaxMemory,HttpThreadsMax,<br \/>\n&nbsp; HttpThreadsBusy,ModJKThreads,ModJKThreadsBusy,Sessions&#8221; &gt; $OUTFILE<br \/>\nfi<br \/>\n<span style=\"color: #006600\">#<\/span> <span style=\"color: #0066ff\"># get the statuspage from tomcat &#8211; filter it for the wanted informations &#8211; and create a temporary file<\/span><br \/>\n&nbsp;  w3m -dump http:\/\/${USER}:${PW}@${SERVER}\/manager\/status\/all| \\<br \/>\n&nbsp;  egrep &#8220;(memory|thread|Active sessions|<br \/>\n&nbsp; ^JVM|$HTTPPORT|$JKPORT|${APPL})&#8221; &gt;\/tmp\/tomcat-status<br \/>\n<span style=\"color: #006600\">#<br \/>\n#<\/span> <span style=\"color: #0066ff\"># put the values in variables \/ uncomment to get a better understanding of the process<\/span><br \/>\n<span style=\"color: #006600\">#<\/span><br \/>\nwhile read line<br \/>\n&nbsp;&nbsp;    do<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      case $line in<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;           JVM)   read line<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                  JVM_VALUE=`echo $line | awk &#8216;{print($3 &#8220;,&#8221; $7 &#8220;,&#8221; $11)}&#8217;`<br \/>\n<span style=\"color: #006600\"><\/span><\/font><font face=\"Courier\" size=\"1\"><span style=\"color: #0066ff\">#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uncomment <\/span><\/font><font face=\"Courier\" size=\"1\"><span style=\"color: #0066ff\">below <\/span><\/font><font face=\"Courier\" size=\"1\"><span style=\"color: #0066ff\">if you want to debug<\/span><\/font><font face=\"Courier\" size=\"1\"><span style=\"color: #006600\"><br \/>\n#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo &#8220;JVM_VALUE: &lt;$JVM_VALUE&gt;&#8221;<\/span><span style=\"color: #0066ff\"><\/span><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;            *${HTTPPORT}) read line<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HTTP_MAX_THREADS=`echo $line |awk &#8216;{print(&#8220;,&#8221; $3)}&#8217;`<br \/>\n<span style=\"color: #006600\"><\/span><\/font><font face=\"Courier\" size=\"1\"><span style=\"color: #006600\"><\/span> <span style=\"color: #0066ff\">#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uncomment if you want to debug<br \/>\n<\/span><\/font><font face=\"Courier\" size=\"1\"><span style=\"color: #006600\">#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo &#8220;HTTP_MAX_THREADS: &lt;$HTTP_MAX_THREADS&gt;&#8221;<\/span><span style=\"color: #0066ff\"><\/span><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                  HTTP_BSY_THREADS=`echo $line |awk &#8216;{print(&#8220;,&#8221; $11)}&#8217;`<br \/>\n<span style=\"color: #0066ff\">#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uncomment <\/span><\/font><font face=\"Courier\" size=\"1\"><span style=\"color: #0066ff\">below <\/span><\/font><font face=\"Courier\" size=\"1\"><span style=\"color: #0066ff\">if you want to debug<\/span><br \/>\n<span style=\"color: #006600\">#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                echo &#8220;HTTP_BSY_THREADS: &lt;$HTTP_BSY_THREADS&gt;&#8221;<\/span><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                  ;;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;           *${JKPORT}) read line<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                  MODJK_MAX_THREADS=`echo $line |awk &#8216;{print(&#8220;,&#8221; $3)}&#8217;`<br \/>\n<span style=\"color: #006600\"><\/span><\/font><font face=\"Courier\" size=\"1\"><span style=\"color: #0066ff\">#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uncomment <\/span><\/font><font face=\"Courier\" size=\"1\"><span style=\"color: #0066ff\">below <\/span><\/font><font face=\"Courier\" size=\"1\"><span style=\"color: #0066ff\">if you want to debug<br \/>\n<\/span><\/font><font face=\"Courier\" size=\"1\"><span style=\"color: #006600\">#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo &#8220;MODJK_MAX_THREADS: &lt;$MODJK_MAX_THREADS&gt;&#8221;<\/span><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                  MODJK_BSY_THREADS=`echo $line |awk &#8216;{print(&#8220;,&#8221; $11)}&#8217;`<br \/>\n<span style=\"color: #006600\"><\/span><\/font><font face=\"Courier\" size=\"1\"><span style=\"color: #0066ff\">#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uncomment <\/span><\/font><font face=\"Courier\" size=\"1\"><span style=\"color: #0066ff\">below <\/span><\/font><font face=\"Courier\" size=\"1\"><span style=\"color: #0066ff\">if you want to debug<br \/>\n<\/span><\/font><font face=\"Courier\" size=\"1\"><span style=\"color: #006600\">#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                echo &#8220;MODJK_BSY_THREADS: &lt;$MODJK_BSY_THREADS&gt;&#8221;<\/span><span style=\"color: #0066ff\"><\/span><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;           *${APPL}) read line<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                  SESSION_CNT=`echo $line |awk &#8216;{print(&#8220;,&#8221; $3)}&#8217;`<br \/>\n<span style=\"color: #006600\"><\/span><\/font><font face=\"Courier\" size=\"1\"><span style=\"color: #0066ff\">#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uncomment below if you want to debug<br \/>\n<\/span><\/font><font face=\"Courier\" size=\"1\"><span style=\"color: #006600\"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;               echo &#8220;SESSION_CNT: &lt;$SESSION_CNT&gt;&#8221;<\/span><span style=\"color: #0066ff\"><\/span><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         ;;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *) ;;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      esac<br \/>\n&nbsp;&nbsp;&nbsp;    done &lt; \/tmp\/tomcat-status<br \/>\n<span style=\"color: #006600\">#<\/span> <span style=\"color: #0066ff\"># Now is the time to write the data to the csv-file<\/span><br \/>\n&nbsp; echo &#8220;${TIME},${JVM_VALUE}${HTTP_MAX_THREADS}${HTTP_BSY_THREADS}<br \/>\n&nbsp; ${MODJK_MAX_THREADS}${MODJK_BSY_THREADS}${SESSION_CNT}&#8221; &gt;&gt;$OUTFILE<br \/>\nexit<\/font><\/p>\n<p><em>*) be aware that based on word wrapping this script might not work instantly<\/em><\/p>\n<p>Create a scriptfile (e.g. tomcat-check.sh) and apply the script to your tomcat. Then edit your crontab.<\/p>\n<p>There should be a line like:<br \/>\n<strong>&#8220;*\/5 * * * * cd ~\/statistics; .\/tomcat-check.sh  1&gt;tomcat-check.log 2&gt;&amp;1&#8221;<\/strong><\/p>\n<p>And then the script should create the csv-files. I&#8217;m not used to excel &#8211; but even I managed to create a more or less interesting diagram from this data. Note how regular the memory behaves while there is no user session.<\/p>\n<p><img decoding=\"async\" alt=\"Tomcat Monitoring Graphic\" title=\"Tomcat Monitoring Graphic\" src=\"\/wp-content\/uploads\/tomcat\/tomcat-graphic-small.jpg\">&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lets assume you have enabled the monitoring capabilities of your tomcat. If you do not know how to achieve this see my earlier posting here about. Now it is cool to monitor your application on a regular basis. Because I&#8217;m used to shell scripting in LINUX I present you here a scripting solution within a [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,14,53,238,15,49,50],"tags":[23,255,227,253,254,269],"class_list":["post-274","post","type-post","status-publish","format-standard","hentry","category-general","category-operatingsystem","category-programming","category-tomcat","category-unix","category-webtech","category-webserver","tag-cron","tag-excel","tag-monitoring","tag-script","tag-shell","tag-tomcat"],"_links":{"self":[{"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=\/wp\/v2\/posts\/274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=274"}],"version-history":[{"count":21,"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=\/wp\/v2\/posts\/274\/revisions"}],"predecessor-version":[{"id":353,"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=\/wp\/v2\/posts\/274\/revisions\/353"}],"wp:attachment":[{"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askthegerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}