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