Netdata Tomcat執行情況監控外掛

百聯達發表於2016-05-06
一: 背景
1.伺服器 阿里雲 CentOS
2.Tomcat 8.0.24
3.該外掛通過tomcat的http://localhost:8080/manager/status?XML=true 進行監控
二:Tomcat配置

1.tomcat-users.xml配置

點選(此處)摺疊或開啟

  1. <?xml version='1.0' encoding='utf-8'?>
  2. <!--
  3.   Licensed to the Apache Software Foundation (ASF) under one or more
  4.   contributor license agreements. See the NOTICE file distributed with
  5.   this work for additional information regarding copyright ownership.
  6.   The ASF licenses this file to You under the Apache License, Version 2.0
  7.   (the "License"); you may not use this file except in compliance with
  8.   the License. You may obtain a copy of the License at

  9.       http://www.apache.org/licenses/LICENSE-2.0

  10.   Unless required by applicable law or agreed to in writing, software
  11.   distributed under the License is distributed on an "AS IS" BASIS,
  12.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.   See the License for the specific language governing permissions and
  14.   limitations under the License.
  15. -->
  16. <tomcat-users xmlns="http://tomcat.apache.org/xml"
  17.               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  18.               xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
  19.               version="1.0">
  20. <!--
  21.   NOTE: By default, no user is included in the "manager-gui" role required
  22.   to operate the "/manager/html" web application. If you wish to use this app,
  23.   you must define such a user - the username and password are arbitrary.
  24. -->
  25. <!--
  26.   NOTE: The sample user and role entries below are wrapped in a comment
  27.   and thus are ignored when reading this file. Do not forget to remove
  28.   <!.. ..> that surrounds them.
  29. -->
  30. <!--
  31.   <role rolename="tomcat"/>
  32.   <role rolename="role1"/>
  33.   <user username="tomcat" password="tomcat" roles="tomcat"/>
  34.   <user username="both" password="tomcat" roles="tomcat,role1"/>
  35.   <user username="role1" password="tomcat" roles="role1"/>
  36. -->
  37. <role rolename="admin-gui"/>
  38. <role rolename="admin-script"/>
  39. <role rolename="manager-gui"/>
  40. <role rolename="manager-script"/>
  41. <role rolename="manager-jmx"/>
  42. <role rolename="manager-status"/>
  43. <user username="tomcat" password="tomcat1" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-script,admin-gui"/>
  44. </tomcat-users>

2.開啟tomcat manager應用



三: 安裝外掛xmlstarlet

yum install xmlstarlet

四:tomcat監控外掛指令碼 tomcat.chart.sh

1.指令碼存放路徑 /usr/local/netdata/usr/libexec/netdata/charts.d

2.指令碼下載路徑 https://github.com/firehol/netdata/blob/master/charts.d/tomcat.chart.sh

3.綠色背景標示的地方,是需要根據自己的tomcat進行調整

點選(此處)摺疊或開啟

  1. #!/bin/bash

  2. # Description: Tomcat netdata charts.d plugin
  3. # Author: Jorge Romero

  4. # the URL to download tomcat status info
  5. tomcat_url="http://localhost:8080/manager/status?XML=true"

  6. # _update_every is a special variable - it holds the number of seconds
  7. # between the calls of the _update() function
  8. tomcat_update_every=1

  9. tomcat_priority=60000

  10. # convert tomcat floating point values
  11. # to integer using this multiplier
  12. # this only affects precision - the values
  13. # will be in the proper units
  14. tomcat_decimal_detail=1000000

  15. # used by volume chart to convert bytes to KB
  16. tomcat_decimal_KB_detail=1000

  17. tomcat_check() {

  18.     require_cmd xmlstarlet || return 1

  19.     tomcat_get
  20.     if [ $? -ne 0 ]
  21.         then
  22.         echo >&2 "tomcat: cannot find stub_status on URL '${tomcat_url}'. Please set tomcat_url='http://tomcat:$Brilliantstar2016@localhost:8080/manager/status?XML=true'"
  23.         return 1
  24.     fi

  25.     # this should return:
  26.     # - 0 to enable the chart
  27.     # - 1 to disable the chart

  28.     return 0
  29. }

  30. tomcat_get() {
  31.     # Collect tomcat values
  32.     mapfile -t lines < <(curl -u tomcatUser:tomcatPassword  -Ss "$tomcat_url" |\
  33.         xmlstarlet sel \
  34.             -t -m "/status/jvm/memory" -v @free \
  35.             -n -m "/status/connector[@name='\"http-nio-8080\"']/threadInfo" -v @currentThreadCount \
  36.             -n -v @currentThreadsBusy \
  37.             -n -m "/status/connector[@name='\"http-nio-8080\"']/requestInfo" -v @requestCount \
  38.             -n -v @bytesSent -n -)

  39.     tomcat_jvm_freememory="${lines[0]}"
  40.     tomcat_threads="${lines[1]}"
  41.     tomcat_threads_busy="${lines[2]}"
  42.     tomcat_accesses="${lines[3]}"
  43.     tomcat_volume="${lines[4]}"

  44.     return 0
  45. }

  46. # _create is called once, to create the charts
  47. tomcat_create() {
  48.     cat <<EOF
  49. CHART tomcat.accesses '' "tomcat requests" "requests/s" statistics tomcat.accesses area $((tomcat_priority + 8)) $tomcat_update_every
  50. DIMENSION accesses '' incremental
  51. CHART tomcat.volume '' "tomcat volume" "KB/s" volume tomcat.volume area $((tomcat_priority + 5)) $tomcat_update_every
  52. DIMENSION volume '' incremental divisor ${tomcat_decimal_KB_detail}
  53. CHART tomcat.threads '' "tomcat threads" "current threads" statistics tomcat.threads line $((tomcat_priority + 6)) $tomcat_update_every
  54. DIMENSION current '' absolute 1
  55. DIMENSION busy '' absolute 1
  56. CHART tomcat.jvm '' "JVM Free Memory" "MB" statistics tomcat.jvm area $((tomcat_priority + 8)) $tomcat_update_every
  57. DIMENSION jvm '' absolute 1 ${tomcat_decimal_detail}
  58. EOF
  59.     return 0
  60. }

  61. # _update is called continiously, to collect the values
  62. tomcat_update() {
  63.     local reqs net
  64.     # the first argument to this function is the microseconds since last update
  65.     # pass this parameter to the BEGIN statement (see bellow).

  66.     # do all the work to collect / calculate the values
  67.     # for each dimension
  68.     # remember: KEEP IT SIMPLE AND SHORT

  69.     tomcat_get || return 1

  70.     # write the result of the work.
  71.     cat <<VALUESEOF
  72. BEGIN tomcat.accesses $1
  73. SET accesses = $((tomcat_accesses))
  74. END
  75. BEGIN tomcat.volume $1
  76. SET volume = $((tomcat_volume))
  77. END
  78. BEGIN tomcat.threads $1
  79. SET current = $((tomcat_threads))
  80. SET busy = $((tomcat_threads_busy))
  81. END
  82. BEGIN tomcat.jvm $1
  83. SET jvm = $((tomcat_jvm_freememory))
  84. END
  85. VALUESEOF

  86.     return 0
  87. }
五:tomcat監控截圖

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28624388/viewspace-2094935/,如需轉載,請註明出處,否則將追究法律責任。

相關文章