]> arthur.barton.de Git - netdata.git/blob - charts.d/tomcat.chart.sh
Merge pull request #576 from paulfantom/master
[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 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                 echo >&2 "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                         echo >&2 "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                         echo >&2 "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                 echo >&2 "tomcat: couldn't get to status page on URL '${tomcat_url}'."\
64                 "Please make sure tomcat url, username and password are correct."
65                 return 1
66         fi
67
68         # this should return:
69         #  - 0 to enable the chart
70         #  - 1 to disable the chart
71
72         return 0
73 }
74
75 tomcat_get() {
76         # collect tomcat values
77         tomcat_port="$(IFS=/ read -ra a <<< "$tomcat_url"; hostport=${a[2]}; echo "${hostport#*:}")"
78         mapfile -t lines < <(curl -u "$tomcat_user":"$tomcat_password" -Ss ${tomcat_curl_opts} "$tomcat_url" |\
79                 xmlstarlet sel \
80                         -t -m "/status/jvm/memory" -v @free \
81                         -n -m "/status/connector[@name='\"http-bio-$tomcat_port\"']/threadInfo" -v @currentThreadCount \
82                         -n -v @currentThreadsBusy \
83                         -n -m "/status/connector[@name='\"http-bio-$tomcat_port\"']/requestInfo" -v @requestCount \
84                         -n -v @bytesSent -n -)
85
86         tomcat_jvm_freememory="${lines[0]}"
87         tomcat_threads="${lines[1]}"
88         tomcat_threads_busy="${lines[2]}"
89         tomcat_accesses="${lines[3]}"
90         tomcat_volume="${lines[4]}"
91
92         return 0
93 }
94
95 # _create is called once, to create the charts
96 tomcat_create() {
97         cat <<EOF
98 CHART tomcat.accesses '' "tomcat requests" "requests/s" statistics tomcat.accesses area $((tomcat_priority + 8)) $tomcat_update_every
99 DIMENSION accesses '' incremental
100 CHART tomcat.volume '' "tomcat volume" "KB/s" volume tomcat.volume area $((tomcat_priority + 5)) $tomcat_update_every
101 DIMENSION volume '' incremental divisor ${tomcat_decimal_KB_detail}
102 CHART tomcat.threads '' "tomcat threads" "current threads" statistics tomcat.threads line $((tomcat_priority + 6)) $tomcat_update_every
103 DIMENSION current '' absolute 1
104 DIMENSION busy '' absolute 1
105 CHART tomcat.jvm '' "JVM Free Memory" "MB" statistics tomcat.jvm area $((tomcat_priority + 8)) $tomcat_update_every
106 DIMENSION jvm '' absolute 1 ${tomcat_decimal_detail}
107 EOF
108         return 0
109 }
110
111 # _update is called continiously, to collect the values
112 tomcat_update() {
113         local reqs net
114         # the first argument to this function is the microseconds since last update
115         # pass this parameter to the BEGIN statement (see bellow).
116
117         # do all the work to collect / calculate the values
118         # for each dimension
119         # remember: KEEP IT SIMPLE AND SHORT
120
121         tomcat_get || return 1
122
123         # write the result of the work.
124         cat <<VALUESEOF
125 BEGIN tomcat.accesses $1
126 SET accesses = $((tomcat_accesses))
127 END
128 BEGIN tomcat.volume $1
129 SET volume = $((tomcat_volume))
130 END
131 BEGIN tomcat.threads $1
132 SET current = $((tomcat_threads))
133 SET busy = $((tomcat_threads_busy))
134 END
135 BEGIN tomcat.jvm $1
136 SET jvm = $((tomcat_jvm_freememory))
137 END
138 VALUESEOF
139
140         return 0
141 }