-
Notifications
You must be signed in to change notification settings - Fork 12
/
zbxApacheStatusCheck
363 lines (323 loc) · 10.3 KB
/
zbxApacheStatusCheck
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#! /bin/bash
#
# Name: zbxApacheStatusCheck
#
# Get Apache metrics and send to Zabbix using zabbix_sender
#
# Author: Rodrigo Luis Silva
#
# Based on:
# zapache: https://github.com/lorf/zapache
# ApacheStatsForZabbix: https://github.com/gpmidi/zabbix-apache-stats
#
name="zbxApacheStatusCheck"
version="0.4.1"
# Test if your getopt(1) is this enhanced version or an old version
getopt --test > /dev/null
if [[ $? != 4 ]]; then
echo "`getopt --test` failed in this environment."
exit 1
fi
# Help function
function usage()
{
echo "
$0 $version
Usage: $0 [Options]
This script collect statistics from Apache status page, write to a temporary file
and send using zabbix_sender, the script will do only one request per execution.
License: GPL
Options:
--version show version and exit
-h, --help show this help page and exit
-l URL, --url=URL Define a custom URL to use
When you use URL, the options
--host, --port, --proto, --user and --passwd are not used
[--url=http://localhost/my-server-status?auto]
[default: None]
-o HOST, --host=HOST Apache host to collect info.
[default: localhost]
-p PORT, --port=PORT Apache port to collect info
[default: 80]
-r PROTO, --proto=PROTO
Protocol to use. [http or https]
[default: http]
--TIMEOUT=seconds Connection timeout
[default: 30]
-z ZABBIXSERVER, --zabbixserver=ZABBIXSERVER
Hostname or IP address of Zabbix server
[default: localhost]
-u USER, --user=USER HTTP authentication user
[default: None]
-a PASSWD, --passwd=PASSWD
HTTP authentication password
[default: None]
-s SENDERLOC, --sender=SENDERLOC
Path of zabbix_sender binary
[default: /usr/bin/zabbix_sender]
-q ZABBIXPORT, --zabbixport=ZABBIXPORT
Specify port number of Zabbix server
trapper running on the server
[default: 10051]
-c ZABBIXSOURCE, --zabbixsource=ZABBIXSOURCE
Specify host name the item belongs to
(as registered in Zabbix frontend)
[default: localhost]
-k CONFIGFILE, --config=CONFIGFILE
Specify the file with the list of statistics to collect
[default: run $0 --dumpconfig]
--dumpconfig Show the atual config
-d, --debug Run using Debug mode
-t, --test Do not send data, simply test, use with --debug
"
}
# Define some default values
HOST=localhost
PORT=80
PROTO=http
ZABBIXSERVER=localhost
SENDER=/usr/bin/zabbix_sender
DEBUG=0
TEST=0
ZABBIXPORT=10051
ZABBIXSOURCE=localhost
TIMEOUT=30
DEFAULTCONFIG="ServerVersion
Total Accesses
Total kBytes
CPULoad
CPUSystem
CPUUser
Load1
Load5
Load15
Uptime
ReqPerSec
BytesPerSec
BytesPerReq
BusyWorkers
IdleWorkers
Scoreboard"
# Use getopt(1) program to parse command line options
SHORT=vhl:o:p:r:z:u:a:s:q:c:k:dt
LONG=version,help,url:,host:,port:,proto:,zabbixserver:,user:,passwd:,sender:,zabbixport:,zabbixsource:,config:,timeout:,debug,dumpconfig,test
PARSED=`getopt --options $SHORT --longoptions $LONG --name "$0" -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$PARSED"
while true; do
case "$1" in
-h|--help)
usage
exit 0
;;
-v|--version)
echo "$name $version"
exit 0
;;
-l|--url)
URL="$2"
shift 2
;;
-o|--host)
HOST="$2"
shift 2
;;
-p|--port)
PORT="$2"
shift 2
;;
-r|--proto)
PROTO="$2"
if [[ $PROTO != "http" && $PROTO != "https" ]]; then
usage
exit 3
fi
shift 2
;;
-z|--zabbixserver)
ZABBIXSERVER="$2"
shift 2
;;
-u|--user)
APACHEUSER="$2"
shift 2
;;
-a|--passwd)
APACHEPASSWD="$2"
shift 2
;;
-s|--sender)
SENDER="$2"
shift 2
;;
-q|--zabbixport)
ZABBIXPORT="$2"
shift 2
;;
-c|--zabbixsource)
ZABBIXSOURCE="$2"
shift 2
;;
--timeout)
TIMEOUT="$2"
shift 2
;;
-k|--config)
CONFIGFILE="$2"
shift 2
;;
-d|--debug)
DEBUG=1
shift
;;
--dumpconfig)
echo "$DEFAULTCONFIG"
exit 0
;;
-t|--test)
TEST=1
shift
;;
--)
shift
break
;;
*)
usage
exit 3
;;
esac
done
# Temporary files
STATUSFILE=`mktemp`
SENDERFILE=`mktemp`
if [[ -z $CONFIGFILE ]]; then
CONFIGFILE=`mktemp`
tmpCONFIGFILE=1
echo "$DEFAULTCONFIG" > $CONFIGFILE
fi
# Build URL to be used
if [[ -n $URL ]]; then
STATUS_URL=$URL
else
STATUS_URL="$PROTO://$HOST:$PORT/server-status?auto"
fi
# get data using curl or wget
curl="`which curl`"
if [ "$curl" ]; then
GET_CMD="$curl --insecure --max-time $TIMEOUT --silent --location -H \"Cache-Control: no-cache\" \"$STATUS_URL\""
if [[ -n $APACHEUSER && -n $APACHEPASSWD ]]; then
GET_CMD="${GET_CMD} --user $APACHEUSER:$APACHEPASSWD"
fi
else
wget="`which wget`"
if [ "$wget" ]; then
GET_CMD="$wget --no-check-certificate --timeout=$TIMEOUT --quiet --header \"Cache-Control: no-cache\" -O - \"$STATUS_URL\""
if [[ -n $APACHEUSER && -n $APACHEPASSWD ]]; then
GET_CMD="${GET_CMD} --user=$APACHEUSER --password=$APACHEPASSWD"
fi
else
echo "I can not find curl or wget on your system"
exit 1
fi
fi
# get data from server
eval $GET_CMD > $STATUSFILE
RET_VAL=$?
# in case of error, just send [failed] = 1
# otherwise send all collected data
if [[ $RET_VAL = 0 ]]; then
echo "\"$ZABBIXSOURCE\" \"custom.apache[failed]\" 0" >> $SENDERFILE
# read the config file to get all relevant statistics
while read STATNAME
do
if [[ $STATNAME = "Scoreboard" ]]; then
SCOREBOARD=`fgrep -w "$STATNAME:" $STATUSFILE | sed "s/$STATNAME: //"`
echo -n "\"$ZABBIXSOURCE\" \"custom.apache[WaitingForConnection]\" " >> $SENDERFILE
echo $SCOREBOARD | awk '{print split($1,dummy,"_")}' >> $SENDERFILE
echo -n "\"$ZABBIXSOURCE\" \"custom.apache[StartingUp]\" " >> $SENDERFILE
echo $SCOREBOARD | awk '{print split($1,dummy,"S")}' >> $SENDERFILE
echo -n "\"$ZABBIXSOURCE\" \"custom.apache[ReadingRequest]\" " >> $SENDERFILE
echo $SCOREBOARD | awk '{print split($1,dummy,"R")}' >> $SENDERFILE
echo -n "\"$ZABBIXSOURCE\" \"custom.apache[SendingReply]\" " >> $SENDERFILE
echo $SCOREBOARD | awk '{print split($1,dummy,"W")}' >> $SENDERFILE
echo -n "\"$ZABBIXSOURCE\" \"custom.apache[KeepAlive]\" " >> $SENDERFILE
echo $SCOREBOARD | awk '{print split($1,dummy,"K")}' >> $SENDERFILE
echo -n "\"$ZABBIXSOURCE\" \"custom.apache[DNSLookup]\" " >> $SENDERFILE
echo $SCOREBOARD | awk '{print split($1,dummy,"D")}' >> $SENDERFILE
echo -n "\"$ZABBIXSOURCE\" \"custom.apache[ClosingConnection]\" " >> $SENDERFILE
echo $SCOREBOARD | awk '{print split($1,dummy,"C")}' >> $SENDERFILE
echo -n "\"$ZABBIXSOURCE\" \"custom.apache[Logging]\" " >> $SENDERFILE
echo $SCOREBOARD | awk '{print split($1,dummy,"L")}' >> $SENDERFILE
echo -n "\"$ZABBIXSOURCE\" \"custom.apache[GracefullyFinishing]\" " >> $SENDERFILE
echo $SCOREBOARD | awk '{print split($1,dummy,"G")}' >> $SENDERFILE
echo -n "\"$ZABBIXSOURCE\" \"custom.apache[IdleCleanupOfWorker]\" " >> $SENDERFILE
echo $SCOREBOARD | awk '{print split($1,dummy,"I")}' >> $SENDERFILE
echo -n "\"$ZABBIXSOURCE\" \"custom.apache[OpenSlotWithNoCurrentProcess]\" " >> $SENDERFILE
echo $SCOREBOARD | awk '{print split($1,dummy,".")}' >> $SENDERFILE
echo -n "\"$ZABBIXSOURCE\" \"custom.apache[TotalWorkers]\" " >> $SENDERFILE
echo $SCOREBOARD | awk '{print split($1,dummy,"")}' >> $SENDERFILE
else
echo -n "\"$ZABBIXSOURCE\" \"custom.apache[$STATNAME]\" " >> $SENDERFILE
LINE=`fgrep -w "$STATNAME:" $STATUSFILE`
if [[ $? = 0 ]]; then
echo "$LINE" | head -1 | sed "s/$STATNAME: //" >> $SENDERFILE
else
echo 0 >> $SENDERFILE;
fi
fi
done < $CONFIGFILE
else
echo "\"$ZABBIXSOURCE\" \"custom.apache[failed]\" 1" >> $SENDERFILE
fi
# Send all information to Zabbix using zbbix_sender
SENDER_CMD="$SENDER -v --zabbix-server $ZABBIXSERVER --port $ZABBIXPORT --input-file $SENDERFILE"
# DEBUG
if [[ $DEBUG = 1 ]]; then
echo -e "\n########################################\n"
echo -e "Statistics available on your server:\n"
cat $STATUSFILE | awk -F: '{print $1}'
echo -e "\n########################################\n"
echo -e "Statistics that I am collecting:\n"
cat $CONFIGFILE
echo -e "\n########################################\n"
echo -e "Metrics to be send\n"
SENDERFILEDEBUG=`mktemp`
echo '"SERVER" "METRIC" VALUE' >> $SENDERFILEDEBUG
cat $SENDERFILE >> $SENDERFILEDEBUG
column -t -s'\"' $SENDERFILEDEBUG
rm -f $SENDERFILEDEBUG
echo -e "\n########################################\n"
echo -e "Parameters\n"
echo "--host=$HOST"
echo "--port=$PORT"
echo "--proto=$PROTO"
echo "--zabbixserver=$ZABBIXSERVER"
echo "--sender=$SENDER"
echo "--zabbixport=$ZABBIXPORT"
echo "--zabbixsource=$ZABBIXSOURCE"
echo "--url=$URL"
echo "--user=$APACHEUSER"
echo "--passwd=$APACHEPASSWD"
echo "--timeout=$TIMEOUT"
echo "Final URL: $STATUS_URL"
echo -e "\n########################################\n"
echo -e "Command line to collect data from Apache\n"
echo -e "$GET_CMD\n"
echo -e "Command line to send data to Zabbix\n"
echo -e "$SENDER_CMD\n"
if [[ $TEST = 0 ]]; then
echo -e "\n########################################\n"
echo -e "zabbix_sender output\n"
eval $SENDER_CMD
echo -e "\n"
fi
else
[[ $TEST = 0 ]] && eval $SENDER_CMD > /dev/null
fi
# Housekeeping
rm -f $STATUSFILE
rm -f $SENDERFILE
if [[ -n $tmpCONFIGFILE ]]; then
rm -f $CONFIGFILE
fi