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