]> arthur.barton.de Git - netdata.git/blob - charts.d/apache.chart.sh
adaptation of supporting apache 2.2 and 2.4
[netdata.git] / charts.d / apache.chart.sh
1 #!/bin/sh
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 # convert apache floating point values
11 # to integer using this multiplier
12 # this only affects precision - the values
13 # will be in the proper units
14 apache_decimal_detail=1000000
15
16 declare -a apache_response=()
17 apache_accesses=0
18 apache_kbytes=0
19 apache_reqpersec=0
20 apache_bytespersec=0
21 apache_bytesperreq=0
22 apache_busyworkers=0
23 apache_idleworkers=0
24
25 apache_keys_detected=0
26 apache_key_accesses=
27 apache_key_kbytes=
28 apache_key_reqpersec=
29 apache_key_bytespersec=
30 apache_key_bytesperreq=
31 apache_key_busyworkers=
32 apache_key_idleworkers=
33 apache_key_connstotal=
34 apache_key_connsasyncwriting=
35 apache_key_connsasynckeepalive=
36 apache_key_connsasyncclosing=
37 apache_detect() {
38         local i=0
39         for x in "${@}"
40         do
41                 case "${x}" in
42                         'Accesses:')                    apache_key_accesses=$[i + 1] ;;
43                         'kBytes:')                              apache_key_kbytes=$[i + 1] ;;
44                         'ReqPerSec:')                   apache_key_reqpersec=$[i + 1] ;;
45                         'BytesPerSec:')                 apache_key_bytespersec=$[i + 1] ;;
46                         'BytesPerReq:')                 apache_key_bytesperreq=$[i + 1] ;;
47                         'BusyWorkers:')                 apache_key_busyworkers=$[i + 1] ;;
48                         'IdleWorkers:')                 apache_key_idleworkers=$[i + 1] ;;
49                         'ConnsTotal:')                  apache_key_connstotal=$[i + 1] ;;
50                         'ConnsAsyncWriting:')   apache_key_connsasyncwriting=$[i + 1] ;;
51                         'ConnsAsyncKeepAlive:') apache_key_connsasynckeepalive=$[i + 1] ;;
52                         'ConnsAsyncClosing:')   apache_key_connsasyncclosing=$[i + 1] ;;
53                 esac
54
55                 i=$[i + 1]
56         done
57
58         # we will not check of the Conns*
59         # keys, since these are apache 2.4 specific
60         if [ -z "${apache_key_accesses}" \
61                 -o -z "${apache_key_kbytes}" \
62                 -o -z "${apache_key_reqpersec}" \
63                 -o -z "${apache_key_bytespersec}" \
64                 -o -z "${apache_key_bytesperreq}" \
65                 -o -z "${apache_key_busyworkers}" \
66                 -o -z "${apache_key_idleworkers}" \
67                 ]
68                 then
69                 echo >&2 "apache: Invalid response or missing keys from apache server: ${*}"
70                 return 1
71         fi
72
73         return 0
74 }
75
76 apache_get() {
77         apache_response=($(curl -s "${apache_url}"))
78         [ $? -ne 0 -o "${#apache_response[@]}" -eq 0 ] && return 1
79
80         if [ ${apache_keys_detected} -eq 0 ]
81                 then
82                 apache_detect "${apache_response[@]}" || return 1
83                 apache_keys_detected=1
84         fi
85
86         apache_accesses="${apache_response[${apache_key_accesses}]}"
87         apache_kbytes="${apache_response[${apache_key_kbytes}]}"
88         
89         float2int "${apache_response[${apache_key_reqpersec}]}" ${apache_decimal_detail}
90         apache_reqpersec=${FLOAT2INT_RESULT}
91
92         float2int "${apache_response[${apache_key_bytespersec}]}" ${apache_decimal_detail}
93         apache_bytespersec=${FLOAT2INT_RESULT}
94
95         float2int "${apache_response[${apache_key_bytesperreq}]}" ${apache_decimal_detail}
96         apache_bytesperreq=${FLOAT2INT_RESULT}
97
98         apache_busyworkers="${apache_response[${apache_key_busyworkers}]}"
99         apache_idleworkers="${apache_response[${apache_key_idleworkers}]}"
100
101         if [ -z "${apache_accesses}" \
102                 -o -z "${apache_kbytes}" \
103                 -o -z "${apache_reqpersec}" \
104                 -o -z "${apache_bytespersec}" \
105                 -o -z "${apache_bytesperreq}" \
106                 -o -z "${apache_busyworkers}" \
107                 -o -z "${apache_idleworkers}" \
108                 ]
109                 then
110                 echo >&2 "apache: empty values got from apache server: ${apache_response[*]}"
111                 return 1
112         fi
113
114         return 0
115 }
116
117 # _check is called once, to find out if this chart should be enabled or not
118 apache_check() {
119
120         apache_get
121         if [ $? -ne 0 ]
122                 then
123                 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"
124                 return 1
125         fi
126
127         # this should return:
128         #  - 0 to enable the chart
129         #  - 1 to disable the chart
130
131         return 0
132 }
133
134 # _create is called once, to create the charts
135 apache_create() {
136         cat <<EOF
137 CHART apache.bytesperreq '' "apache Lifetime Avg. Response Size" "bytes/request" apache apache area 16008 $apache_update_every
138 DIMENSION size '' absolute 1 ${apache_decimal_detail}
139 CHART apache.workers '' "apache Workers" "workers" apache apache stacked 16005 $apache_update_every
140 DIMENSION idle '' absolute 1 1
141 DIMENSION busy '' absolute 1 1
142 CHART apache.reqpersec '' "apache Lifetime Avg. Requests/s" "requests/s" apache apache line 16006 $apache_update_every
143 DIMENSION requests '' absolute 1 ${apache_decimal_detail}
144 CHART apache.bytespersec '' "apache Lifetime Avg. Bandwidth/s" "kilobits/s" apache apache area 16007 $apache_update_every
145 DIMENSION sent '' absolute 8 $[apache_decimal_detail * 1000]
146 CHART apache.requests '' "apache Requests" "requests/s" apache apache line 16001 $apache_update_every
147 DIMENSION requests '' incremental 1 1
148 CHART apache.net '' "apache Bandwidth" "kilobits/s" apache apache area 16002 $apache_update_every
149 DIMENSION sent '' incremental 8 1
150 EOF
151
152         return 0
153 }
154
155 # _update is called continiously, to collect the values
156 apache_update() {
157         local reqs net
158         # the first argument to this function is the microseconds since last update
159         # pass this parameter to the BEGIN statement (see bellow).
160
161         # do all the work to collect / calculate the values
162         # for each dimension
163         # remember: KEEP IT SIMPLE AND SHORT
164
165         apache_get || return 1
166
167         # write the result of the work.
168         cat <<VALUESEOF
169 BEGIN apache.requests $1
170 SET requests = $[apache_accesses]
171 END
172 BEGIN apache.net $1
173 SET sent = $[apache_kbytes]
174 END
175 BEGIN apache.reqpersec $1
176 SET requests = $[apache_reqpersec]
177 END
178 BEGIN apache.bytespersec $1
179 SET sent = $[apache_bytespersec]
180 END
181 BEGIN apache.bytesperreq $1
182 SET size = $[apache_bytesperreq]
183 END
184 BEGIN apache.workers $1
185 SET idle = $[apache_idleworkers]
186 SET busy = $[apache_busyworkers]
187 END
188 VALUESEOF
189
190         return 0
191 }