]> arthur.barton.de Git - netdata.git/blob - charts.d/tomcat.chart.sh
better log management for charts.d.plugin - fixes #1144
[netdata.git] / charts.d / tomcat.chart.sh
1 # no need for shebang - this file is loaded from charts.d.plugin
2
3 # Description: Tomcat netdata charts.d plugin
4 # Author: Jorge Romero
5
6 # the URL to download tomcat status info
7 # usually http://localhost:8080/manager/status?XML=true
8 tomcat_url=""
9 tomcat_curl_opts=""
10
11 # set tomcat username/password here
12 tomcat_user=""
13 tomcat_password=""
14
15 # _update_every is a special variable - it holds the number of seconds
16 # between the calls of the _update() function
17 tomcat_update_every=
18
19 tomcat_priority=60000
20
21 # convert tomcat floating point values
22 # to integer using this multiplier
23 # this only affects precision - the values
24 # will be in the proper units
25 tomcat_decimal_detail=1000000
26
27 # used by volume chart to convert bytes to KB
28 tomcat_decimal_KB_detail=1000
29
30 tomcat_check() {
31
32         require_cmd xmlstarlet || return 1
33
34
35         # check if url, username, passwords are set
36         if [ -z "${tomcat_url}" ]; then
37                 error "tomcat url is unset or set to the empty string"
38                 return 1
39         fi
40         if [ -z "${tomcat_user}" ]; then
41                 # check backwards compatibility
42                 if [ -z "${tomcatUser}" ]; then         
43                 error "tomcat user is unset or set to the empty string"
44                         return 1
45                 else
46                         tomcat_user="${tomcatUser}"
47                 fi
48         fi
49         if [ -z "${tomcat_password}" ]; then
50                 # check backwards compatibility
51                 if [ -z "${tomcatPassword}" ]; then
52                 error "tomcat password is unset or set to the empty string"
53                         return 1
54                 else
55                         tomcat_password="${tomcatPassword}"
56                 fi
57         fi
58
59         # check if we can get to tomcat's status page
60         tomcat_get
61         if [ $? -ne 0 ]
62                 then
63                 error "cannot get to status page on URL '${tomcat_url}'. Please make sure tomcat url, username and password are correct."
64                 return 1
65         fi
66
67         # this should return:
68         #  - 0 to enable the chart
69         #  - 1 to disable the chart
70
71         return 0
72 }
73
74 tomcat_get() {
75         # collect tomcat values
76         tomcat_port="$(IFS=/ read -ra a <<< "$tomcat_url"; hostport=${a[2]}; echo "${hostport#*:}")"
77         mapfile -t lines < <(run curl -u "$tomcat_user":"$tomcat_password" -Ss ${tomcat_curl_opts} "$tomcat_url" |\
78                 run xmlstarlet sel \
79                         -t -m "/status/jvm/memory" -v @free \
80                         -n -m "/status/connector[@name='\"http-bio-$tomcat_port\"']/threadInfo" -v @currentThreadCount \
81                         -n -v @currentThreadsBusy \
82                         -n -m "/status/connector[@name='\"http-bio-$tomcat_port\"']/requestInfo" -v @requestCount \
83                         -n -v @bytesSent -n -)
84
85         tomcat_jvm_freememory="${lines[0]}"
86         tomcat_threads="${lines[1]}"
87         tomcat_threads_busy="${lines[2]}"
88         tomcat_accesses="${lines[3]}"
89         tomcat_volume="${lines[4]}"
90
91         return 0
92 }
93
94 # _create is called once, to create the charts
95 tomcat_create() {
96         cat <<EOF
97 CHART tomcat.accesses '' "tomcat requests" "requests/s" statistics tomcat.accesses area $((tomcat_priority + 8)) $tomcat_update_every
98 DIMENSION accesses '' incremental
99 CHART tomcat.volume '' "tomcat volume" "KB/s" volume tomcat.volume area $((tomcat_priority + 5)) $tomcat_update_every
100 DIMENSION volume '' incremental divisor ${tomcat_decimal_KB_detail}
101 CHART tomcat.threads '' "tomcat threads" "current threads" statistics tomcat.threads line $((tomcat_priority + 6)) $tomcat_update_every
102 DIMENSION current '' absolute 1
103 DIMENSION busy '' absolute 1
104 CHART tomcat.jvm '' "JVM Free Memory" "MB" statistics tomcat.jvm area $((tomcat_priority + 8)) $tomcat_update_every
105 DIMENSION jvm '' absolute 1 ${tomcat_decimal_detail}
106 EOF
107         return 0
108 }
109
110 # _update is called continiously, to collect the values
111 tomcat_update() {
112         local reqs net
113         # the first argument to this function is the microseconds since last update
114         # pass this parameter to the BEGIN statement (see bellow).
115
116         # do all the work to collect / calculate the values
117         # for each dimension
118         # remember: KEEP IT SIMPLE AND SHORT
119
120         tomcat_get || return 1
121
122         # write the result of the work.
123         cat <<VALUESEOF
124 BEGIN tomcat.accesses $1
125 SET accesses = $((tomcat_accesses))
126 END
127 BEGIN tomcat.volume $1
128 SET volume = $((tomcat_volume))
129 END
130 BEGIN tomcat.threads $1
131 SET current = $((tomcat_threads))
132 SET busy = $((tomcat_threads_busy))
133 END
134 BEGIN tomcat.jvm $1
135 SET jvm = $((tomcat_jvm_freememory))
136 END
137 VALUESEOF
138
139         return 0
140 }