]> arthur.barton.de Git - netdata.git/blob - charts.d/tomcat.chart.sh
support custom curl options in charts.d plugins
[netdata.git] / charts.d / tomcat.chart.sh
1 #!/bin/bash
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 tomcatUser=""
13 tomcatPassword=""
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                 echo >&2 "tomcat url is unset or set to the empty string"
38                 return 1
39         fi
40         if [ -z "${tomcatUser}" ]; then
41                 echo >&2 "tomcat user is unset or set to the empty string"
42                 return 1
43         fi
44         if [ -z "${tomcatPassword}" ]; then
45                 echo >&2 "tomcat password is unset or set to the empty string"
46                 return 1
47         fi
48
49         # check if we can get to tomcat's status page
50         tomcat_get
51         if [ $? -ne 0 ]
52                 then
53                 echo >&2 "tomcat: couldn't get to status page on URL '${tomcat_url}'."\
54                 "Please make sure tomcat url, username and password are correct."
55                 return 1
56         fi
57
58         # this should return:
59         #  - 0 to enable the chart
60         #  - 1 to disable the chart
61
62         return 0
63 }
64
65 tomcat_get() {
66         # collect tomcat values
67         tomcat_port="$(IFS=/ read -ra a <<< "$tomcat_url"; hostport=${a[2]}; echo "${hostport#*:}")"
68         mapfile -t lines < <(curl -u "$tomcatUser":"$tomcatPassword" -Ss ${tomcat_curl_opts} "$tomcat_url" |\
69                 xmlstarlet sel \
70                         -t -m "/status/jvm/memory" -v @free \
71                         -n -m "/status/connector[@name='\"http-bio-$tomcat_port\"']/threadInfo" -v @currentThreadCount \
72                         -n -v @currentThreadsBusy \
73                         -n -m "/status/connector[@name='\"http-bio-$tomcat_port\"']/requestInfo" -v @requestCount \
74                         -n -v @bytesSent -n -)
75
76         tomcat_jvm_freememory="${lines[0]}"
77         tomcat_threads="${lines[1]}"
78         tomcat_threads_busy="${lines[2]}"
79         tomcat_accesses="${lines[3]}"
80         tomcat_volume="${lines[4]}"
81
82         return 0
83 }
84
85 # _create is called once, to create the charts
86 tomcat_create() {
87         cat <<EOF
88 CHART tomcat.accesses '' "tomcat requests" "requests/s" statistics tomcat.accesses area $((tomcat_priority + 8)) $tomcat_update_every
89 DIMENSION accesses '' incremental
90 CHART tomcat.volume '' "tomcat volume" "KB/s" volume tomcat.volume area $((tomcat_priority + 5)) $tomcat_update_every
91 DIMENSION volume '' incremental divisor ${tomcat_decimal_KB_detail}
92 CHART tomcat.threads '' "tomcat threads" "current threads" statistics tomcat.threads line $((tomcat_priority + 6)) $tomcat_update_every
93 DIMENSION current '' absolute 1
94 DIMENSION busy '' absolute 1
95 CHART tomcat.jvm '' "JVM Free Memory" "MB" statistics tomcat.jvm area $((tomcat_priority + 8)) $tomcat_update_every
96 DIMENSION jvm '' absolute 1 ${tomcat_decimal_detail}
97 EOF
98         return 0
99 }
100
101 # _update is called continiously, to collect the values
102 tomcat_update() {
103         local reqs net
104         # the first argument to this function is the microseconds since last update
105         # pass this parameter to the BEGIN statement (see bellow).
106
107         # do all the work to collect / calculate the values
108         # for each dimension
109         # remember: KEEP IT SIMPLE AND SHORT
110
111         tomcat_get || return 1
112
113         # write the result of the work.
114         cat <<VALUESEOF
115 BEGIN tomcat.accesses $1
116 SET accesses = $((tomcat_accesses))
117 END
118 BEGIN tomcat.volume $1
119 SET volume = $((tomcat_volume))
120 END
121 BEGIN tomcat.threads $1
122 SET current = $((tomcat_threads))
123 SET busy = $((tomcat_threads_busy))
124 END
125 BEGIN tomcat.jvm $1
126 SET jvm = $((tomcat_jvm_freememory))
127 END
128 VALUESEOF
129
130         return 0
131 }