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