]> arthur.barton.de Git - netdata.git/blob - charts.d/apache.chart.sh
Merge pull request #2021 from ktsaou/master
[netdata.git] / charts.d / apache.chart.sh
1 # no need for shebang - this file is loaded from charts.d.plugin
2
3 # netdata
4 # real-time performance and health monitoring, done right!
5 # (C) 2016 Costa Tsaousis <costa@tsaousis.gr>
6 # GPL v3+
7 #
8
9 # the URL to download apache status info
10 apache_url="http://127.0.0.1:80/server-status?auto"
11 apache_curl_opts=
12
13 # _update_every is a special variable - it holds the number of seconds
14 # between the calls of the _update() function
15 apache_update_every=
16
17 apache_priority=60000
18
19 # convert apache floating point values
20 # to integer using this multiplier
21 # this only affects precision - the values
22 # will be in the proper units
23 apache_decimal_detail=1000000
24
25 declare -a apache_response=()
26 apache_accesses=0
27 apache_kbytes=0
28 apache_reqpersec=0
29 apache_bytespersec=0
30 apache_bytesperreq=0
31 apache_busyworkers=0
32 apache_idleworkers=0
33 apache_connstotal=0
34 apache_connsasyncwriting=0
35 apache_connsasynckeepalive=0
36 apache_connsasyncclosing=0
37
38 apache_keys_detected=0
39 apache_has_conns=0
40 apache_key_accesses=
41 apache_key_kbytes=
42 apache_key_reqpersec=
43 apache_key_bytespersec=
44 apache_key_bytesperreq=
45 apache_key_busyworkers=
46 apache_key_idleworkers=
47 apache_key_scoreboard=
48 apache_key_connstotal=
49 apache_key_connsasyncwriting=
50 apache_key_connsasynckeepalive=
51 apache_key_connsasyncclosing=
52 apache_detect() {
53         local i=0
54         for x in "${@}"
55         do
56                 case "${x}" in
57                         'Total Accesses')               apache_key_accesses=$((i + 1)) ;;
58                         'Total kBytes')                 apache_key_kbytes=$((i + 1)) ;;
59                         'ReqPerSec')                    apache_key_reqpersec=$((i + 1)) ;;
60                         'BytesPerSec')                  apache_key_bytespersec=$((i + 1)) ;;
61                         'BytesPerReq')                  apache_key_bytesperreq=$((i + 1)) ;;
62                         'BusyWorkers')                  apache_key_busyworkers=$((i + 1)) ;;
63                         'IdleWorkers')                  apache_key_idleworkers=$((i + 1));;
64                         'ConnsTotal')                   apache_key_connstotal=$((i + 1)) ;;
65                         'ConnsAsyncWriting')    apache_key_connsasyncwriting=$((i + 1)) ;;
66                         'ConnsAsyncKeepAlive')  apache_key_connsasynckeepalive=$((i + 1)) ;;
67                         'ConnsAsyncClosing')    apache_key_connsasyncclosing=$((i + 1)) ;;
68                         'Scoreboard')                   apache_key_scoreboard=$((i)) ;;
69                 esac
70
71                 i=$((i + 1))
72         done
73
74         # we will not check of the Conns*
75         # keys, since these are apache 2.4 specific
76         [ -z "${apache_key_accesses}"    ] && error "missing 'Total Accesses' from apache server: ${*}" && return 1
77         [ -z "${apache_key_kbytes}"      ] && error "missing 'Total kBytes' from apache server: ${*}" && return 1
78         [ -z "${apache_key_reqpersec}"   ] && error "missing 'ReqPerSec' from apache server: ${*}" && return 1
79         [ -z "${apache_key_bytespersec}" ] && error "missing 'BytesPerSec' from apache server: ${*}" && return 1
80         [ -z "${apache_key_bytesperreq}" ] && error "missing 'BytesPerReq' from apache server: ${*}" && return 1
81         [ -z "${apache_key_busyworkers}" ] && error "missing 'BusyWorkers' from apache server: ${*}" && return 1
82         [ -z "${apache_key_idleworkers}" ] && error "missing 'IdleWorkers' from apache server: ${*}" && return 1
83         [ -z "${apache_key_scoreboard}"  ] && error "missing 'Scoreboard' from apache server: ${*}" && return 1
84
85         if [ ! -z "${apache_key_connstotal}" \
86                 -a ! -z "${apache_key_connsasyncwriting}" \
87                 -a ! -z "${apache_key_connsasynckeepalive}" \
88                 -a ! -z "${apache_key_connsasyncclosing}" \
89                 ]
90                 then
91                 apache_has_conns=1
92         else
93                 apache_has_conns=0
94         fi
95
96         return 0
97 }
98
99 apache_get() {
100         local oIFS="${IFS}" ret
101         IFS=$':\n' apache_response=($(run curl -Ss ${apache_curl_opts} "${apache_url}"))
102         ret=$?
103         IFS="${oIFS}"
104
105         [ $ret -ne 0 -o "${#apache_response[@]}" -eq 0 ] && return 1
106
107         # the last line on the apache output is "Scoreboard"
108         # we use this label to detect that the output has a new word count
109         if [ ${apache_keys_detected} -eq 0 -o "${apache_response[${apache_key_scoreboard}]}" != "Scoreboard" ]
110                 then
111                 apache_detect "${apache_response[@]}" || return 1
112                 apache_keys_detected=1
113         fi
114
115         apache_accesses="${apache_response[${apache_key_accesses}]}"
116         apache_kbytes="${apache_response[${apache_key_kbytes}]}"
117         
118         float2int "${apache_response[${apache_key_reqpersec}]}" ${apache_decimal_detail}
119         apache_reqpersec=${FLOAT2INT_RESULT}
120
121         float2int "${apache_response[${apache_key_bytespersec}]}" ${apache_decimal_detail}
122         apache_bytespersec=${FLOAT2INT_RESULT}
123
124         float2int "${apache_response[${apache_key_bytesperreq}]}" ${apache_decimal_detail}
125         apache_bytesperreq=${FLOAT2INT_RESULT}
126
127         apache_busyworkers="${apache_response[${apache_key_busyworkers}]}"
128         apache_idleworkers="${apache_response[${apache_key_idleworkers}]}"
129
130         if [ -z "${apache_accesses}" \
131                 -o -z "${apache_kbytes}" \
132                 -o -z "${apache_reqpersec}" \
133                 -o -z "${apache_bytespersec}" \
134                 -o -z "${apache_bytesperreq}" \
135                 -o -z "${apache_busyworkers}" \
136                 -o -z "${apache_idleworkers}" \
137                 ]
138                 then
139                 error "empty values got from apache server: ${apache_response[*]}"
140                 return 1
141         fi
142
143         if [ ${apache_has_conns} -eq 1 ]
144                 then
145                 apache_connstotal="${apache_response[${apache_key_connstotal}]}"
146                 apache_connsasyncwriting="${apache_response[${apache_key_connsasyncwriting}]}"
147                 apache_connsasynckeepalive="${apache_response[${apache_key_connsasynckeepalive}]}"
148                 apache_connsasyncclosing="${apache_response[${apache_key_connsasyncclosing}]}"
149         fi
150
151         return 0
152 }
153
154 # _check is called once, to find out if this chart should be enabled or not
155 apache_check() {
156
157         apache_get
158         if [ $? -ne 0 ]
159                 then
160                 error "cannot find stub_status on URL '${apache_url}'. Please set apache_url='http://apache.server:80/server-status?auto' in $confd/apache.conf"
161                 return 1
162         fi
163
164         # this should return:
165         #  - 0 to enable the chart
166         #  - 1 to disable the chart
167
168         return 0
169 }
170
171 # _create is called once, to create the charts
172 apache_create() {
173         cat <<EOF
174 CHART apache_local.bytesperreq '' "apache Lifetime Avg. Response Size" "bytes/request" statistics apache.bytesperreq area $((apache_priority + 8)) $apache_update_every
175 DIMENSION size '' absolute 1 ${apache_decimal_detail}
176 CHART apache_local.workers '' "apache Workers" "workers" workers apache.workers stacked $((apache_priority + 5)) $apache_update_every
177 DIMENSION idle '' absolute 1 1
178 DIMENSION busy '' absolute 1 1
179 CHART apache_local.reqpersec '' "apache Lifetime Avg. Requests/s" "requests/s" statistics apache.reqpersec line $((apache_priority + 6)) $apache_update_every
180 DIMENSION requests '' absolute 1 ${apache_decimal_detail}
181 CHART apache_local.bytespersec '' "apache Lifetime Avg. Bandwidth/s" "kilobits/s" statistics apache.bytespersec area $((apache_priority + 7)) $apache_update_every
182 DIMENSION sent '' absolute 8 $((apache_decimal_detail * 1000))
183 CHART apache_local.requests '' "apache Requests" "requests/s" requests apache.requests line $((apache_priority + 1)) $apache_update_every
184 DIMENSION requests '' incremental 1 1
185 CHART apache_local.net '' "apache Bandwidth" "kilobits/s" bandwidth apache.net area $((apache_priority + 3)) $apache_update_every
186 DIMENSION sent '' incremental 8 1
187 EOF
188
189         if [ ${apache_has_conns} -eq 1 ]
190                 then
191                 cat <<EOF2
192 CHART apache_local.connections '' "apache Connections" "connections" connections apache.connections line $((apache_priority + 2)) $apache_update_every
193 DIMENSION connections '' absolute 1 1
194 CHART apache_local.conns_async '' "apache Async Connections" "connections" connections apache.conns_async stacked $((apache_priority + 4)) $apache_update_every
195 DIMENSION keepalive '' absolute 1 1
196 DIMENSION closing '' absolute 1 1
197 DIMENSION writing '' absolute 1 1
198 EOF2
199         fi
200
201         return 0
202 }
203
204 # _update is called continiously, to collect the values
205 apache_update() {
206         local reqs net
207         # the first argument to this function is the microseconds since last update
208         # pass this parameter to the BEGIN statement (see bellow).
209
210         # do all the work to collect / calculate the values
211         # for each dimension
212         # remember: KEEP IT SIMPLE AND SHORT
213
214         apache_get || return 1
215
216         # write the result of the work.
217         cat <<VALUESEOF
218 BEGIN apache_local.requests $1
219 SET requests = $((apache_accesses))
220 END
221 BEGIN apache_local.net $1
222 SET sent = $((apache_kbytes))
223 END
224 BEGIN apache_local.reqpersec $1
225 SET requests = $((apache_reqpersec))
226 END
227 BEGIN apache_local.bytespersec $1
228 SET sent = $((apache_bytespersec))
229 END
230 BEGIN apache_local.bytesperreq $1
231 SET size = $((apache_bytesperreq))
232 END
233 BEGIN apache_local.workers $1
234 SET idle = $((apache_idleworkers))
235 SET busy = $((apache_busyworkers))
236 END
237 VALUESEOF
238
239         if [ ${apache_has_conns} -eq 1 ]
240                 then
241         cat <<VALUESEOF2
242 BEGIN apache_local.connections $1
243 SET connections = $((apache_connstotal))
244 END
245 BEGIN apache_local.conns_async $1
246 SET keepalive = $((apache_connsasynckeepalive))
247 SET closing = $((apache_connsasyncclosing))
248 SET writing = $((apache_connsasyncwriting))
249 END
250 VALUESEOF2
251         fi
252
253         return 0
254 }