-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck_rabbitmq_consumers.sh
executable file
·113 lines (97 loc) · 2.25 KB
/
check_rabbitmq_consumers.sh
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
#!/bin/bash
#
# check_rabbitmq_consumers.sh
# https://github.com/sol1/icinga-check-rabbitmq
#
# Standard Icinga return codes
E_SUCCESS="0"
E_WARNING="1"
E_CRITICAL="2"
E_UNKNOWN="3"
usage() {
cat << EOU
Usage: $0 <OPTIONS>
-u <RabbitMQ API username>
-p <RabbitMQ API password>
-a <API URL, e.g. http://localhost:15672/api/consumers>
-q <RabbitMQ queue name, e.g. remote_deliveries>
-c <Number of consumers when CRITICAL or less>
-w <Number of consumers when WARNING or less>
Example:
$0 -u guest -p guest -a "http://localhost:15672/api/consumers" -q remote_deliveries -c 1 -w 2
EOU
exit ${E_UNKNOWN}
}
while getopts ":u:p:a:q:c:w:" o; do
case "${o}" in
u)
u=${OPTARG}
;;
p)
p=${OPTARG}
;;
a)
a=${OPTARG}
;;
q)
q=${OPTARG}
;;
c)
c=${OPTARG}
;;
w)
w=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z "${u}" ] || [ -z "${p}" ] || [ -z "${a}" ] || [ -z "${q}" ] || [ -z "${c}" ] || [ -z "${w}" ]; then
usage
fi
curl=`which curl`
jq=`which jq`
if [ -z "${curl}" ] || ! [ -x "${curl}" ]; then
echo "UNKNOWN: curl not found"
exit ${E_UNKNOWN}
fi
if [ -z "${jq}" ] || ! [ -x "${jq}" ]; then
echo "UNKNOWN: jq not found"
exit ${E_UNKNOWN}
fi
response=`curl -s -u $u:$p -o - "$a"`
# Check for a simple error provided by the API
if [ -z "$response" ]; then
api_error=`echo $response | jq ".error?"`
else
api_error='';
fi
if ! [ -z "${api_error}" ]; then
echo "CRITICAL: API returned: ${api_error}"
exit ${E_CRITICAL}
fi
if [ $? != 0 ]; then
echo "CRITICAL: Bad response from RabbitMQ HTTP API"
exit ${E_CRITICAL}
else
consumers=`echo $response | jq "map(select(.queue.name == \"$q\")) | length"`
fi
if [ -z "${consumers}" ]; then
echo "CRITICAL: No response from RabbitMQ HTTP API"
exit ${E_CRITICAL}
fi
if [ $consumers -gt $w ]; then
echo "OK: ${q} has ${consumers} consumers"
exit ${E_SUCCESS}
fi
if [ $consumers -le $c ]; then
echo "CRITICAL: ${q} has ${consumers} consumers"
exit ${E_CRITICAL}
fi
if [ $consumers -le $w ]; then
echo "WARNING: ${q} has ${consumers} consumers"
exit ${E_WARNING}
fi
exit ${E_UNKNOWN}