-
Notifications
You must be signed in to change notification settings - Fork 3
/
check_asterisk_ami
executable file
·296 lines (261 loc) · 10.1 KB
/
check_asterisk_ami
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
#!/bin/bash
#
# Program : check_asterisk_ami
# :
# Author : Jason Rivers <[email protected]>
# :
# Purpose : Nagios plugin to return Information from an Asterisk host using AMI
# :
# Parameters : --help
# : --version
# :
# Returns : Standard Nagios status_* codes as defined in utils.sh
# :
# Licence : GPL
#
# Notes : See --help for details
#============:==============================================================
PROGNAME=`basename $0`
PROGPATH=`echo $0 | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION=`echo '$Revision: 1.2.0.0 $' | sed -e 's/[^0-9.]//g'`
. $PROGPATH/utils.sh
print_usage() {
echo "Usage: $PROGNAME [-H hostname] [-q query] [-u username] [-p password] [-P port] [-w warning] [-c critical]"
echo " -H Hostname"
echo " -q Command to query"
echo " -u AMI Username"
echo " -p AMI Password"
echo " -P (optional) AMI PORT"
echo " -w (optional) warning threshold"
echo " -c (optional) critical threshold"
echo ""
echo "SupportedCommands:"
echo " channels (check number of current channels in-use)"
echo " calls (check number of current calls)"
echo " sippeers (check number of SIP peers)"
echo " iaxpeers (check number of IAX peers)"
echo " pjsip (check number of PJSIP endpoints)"
echo ""
echo "Usage: $PROGNAME --help"
echo "Usage: $PROGNAME --version"
}
print_help() {
print_revision $PROGNAME $REVISION
echo ""
echo "Nagios Plugin to check Asterisk using AMI"
echo ""
print_usage
echo ""
echo "Asterisk Call Status Check. © Jason Rivers 2011"
echo ""
exit 0
# support
}
# If we have arguments, process them.
#
exitstatus=$STATE_WARNING #default
while test -n "$1"; do
case "$1" in
--help)
print_help
exit $STATE_OK
;;
-h)
print_help
exit $STATE_OK
;;
--version)
print_revision $PROGNAME $REVISION
exit $STATE_OK
;;
-V)
print_revision $PROGNAME $REVISION
exit $STATE_OK
;;
-H)
REMOTEHOST=$2;
shift;
;;
-P) AMIPORT=$2;
shift;
;;
-u) AMIUSER=$2;
shift;
;;
-p) AMIPASS=$2;
shift;
;;
-q) CHECK=$2;
shift;
;;
-c)
CRITICALNUMBER=$2
shift;
;;
-w)
WARNINGNUMBER=$2;
shift;
;;
*)
echo "Unknown argument: $1"
print_usage
exit $STATE_UNKNOWN
;;
esac
shift
done
##Find netcat
if [ -f /usr/bin/nc ]; then
NC=/usr/bin/nc
elif [ -f /bin/nc ]; then
NC=/bin/nc
elif [ -f /usr/bin/netcat ]; then
NC=/usr/bin/netcat
elif [ -f /bin/netcat ]; then
NC=/bin/netcat
else
echo "Couldn't find Netcat on your system, Please install the netcat package:"
echo ""
echo "for RedHat based distros use: yum install nc"
echo "for Debian based distros use: apt-get install netcat"
exit $STATE_CRITICAL
fi
if [ "${AMIPORT}" = "" ]; then
AMIPORT="5038"
fi
if [ "${CHECK}" = "channels" ]; then
##WARNING
if [ "$WARNINGNUMBER" = "" ]; then
WARNINGNUMBER=10
fi
if [ "$CRITICALNUMBER" = "" ]; then
CRITICALNUMBER=20
fi
## Checking active chanels on Asterisk
CHANNELS=`/bin/echo -e "Action: login\r\nUsername: ${AMIUSER}\r\nSecret: ${AMIPASS}\r\nEvents: off\r\n\r\nAction: CoreShowChannels\r\n\r\nAction: Logoff\r\n\r\n" | ${NC} $REMOTEHOST ${AMIPORT} | sed 's/Output: //' | awk '/^ListItems/ {print $2}'|tr -d "\r"`
if [ "$CHANNELS" = "" ]; then
echo "UNKNOWN: Unable to get number of Channels"
exit $STATUS_UNKNOWN
fi
if [ $CHANNELS -lt $WARNINGNUMBER ]; then
exitstatus=$STATU_OK
MSG="OK: ${CHANNELS} Asterisk calls active|channels=${CHANNELS}"
elif [ $CHANNELS -lt $CRITICALNUMBER ]; then
exitstatus=$STATU_WARNING
MSG="WARNING: ${CHANNELS} Asterisk calls active|channels=${CHANNELS}"
elif [ $CHANNELS -ge $CRITICALNUMBER ]; then
exitstatus=$STATU_CRITICAL
MSG="CRITICAL: ${CHANNELS} Asterisk calls active|channels=${CHANNELS}"
fi
elif [ "${CHECK}" = "calls" ]; then
##WARNING
if [ "$WARNINGNUMBER" = "" ]; then
WARNINGNUMBER=5
fi
if [ "$CRITICALNUMBER" = "" ]; then
CRITICALNUMBER=10
fi
CALLS=`/bin/echo -e "Action: login\r\nUsername: ${AMIUSER}\r\nSecret: ${AMIPASS}\r\nEvents: off\r\n\r\nAction: Command\r\ncommand: Core Show Channels\r\n\r\nAction: Logoff\r\n\r\n" | ${NC} ${REMOTEHOST} ${AMIPORT} | sed 's/Output: //' | awk '/active call/ {print $1}' | tr -d "\r"`
if [ "$CALLS" = "" ]; then
echo "UNKNOWN: Unable to get number of calls"
exit $STATUS_UNKNOWN
fi
if [ $CALLS -lt $WARNINGNUMBER ]; then
exitstatus=$STATE_OK
MSG="OK: ${CALLS} Asterisk calls active|calls=${CALLS}"
elif [ $CALLS -lt $CRITICALNUMBER ]; then
exitstatus=$STATE_WARNING
MSG="WARNING: ${CALLS} Asterisk calls active|calls=${CALLS}"
elif [ $CALLS -ge $CRITICALNUMBER ]; then
exitstatus=$STATE_CRITICAL
MSG="CRITICAL: ${CALLS} Asterisk calls active|calls=${CALLS}"
fi
if [ "$CALLS" = "" ]; then
CALLS=0
fi
elif [ "${CHECK}" = "iaxpeers" ]; then
##WARNING
if [ "$WARNINGNUMBER" = "" ]; then
WARNINGNUMBER=5
fi
if [ "$CRITICALNUMBER" = "" ]; then
CRITICALNUMBER=10
fi
IAXpeers=`/bin/echo -e "Action: login\r\nUsername: ${AMIUSER}\r\nSecret: ${AMIPASS}\r\nEvents: off\r\n\r\nAction: Command\r\ncommand: iax2 show peers\r\n\r\nAction: Logoff\r\n\r\n" | ${NC} ${REMOTEHOST} ${AMIPORT} | sed 's/Output: //' | awk '/online/ {print $0}' | tr -d "\r"`
ONLINE=`echo $IAXpeers | sed 's/.*\[\(.*\) online.*unmonitored.*/\1/'`
OFFLINE=`echo $IAXpeers | sed 's/.*online, \(.*\) offline.*unmonitored.*/\1/'`
if [ "$OFFLINE" = "" ]; then
echo "UNKNOWN: Unable to get number of IAX Peers"
exit $STATUS_UNKNOWN
fi
if [ $OFFLINE -lt $WARNINGNUMBER ]; then
exitstatus=$STATE_OK
MSG="OK: ${ONLINE} online, ${OFFLINE} offline IAX2 peers|online=${ONLINE} offline=${OFFLINE}"
elif [ $OFFLINE -lt $CRITICALNUMBER ]; then
exitstatus=$STATE_WARNING
MSG="WARNING: ${ONLINE} online, ${OFFLINE} offline IAX2 peers|online=${ONLINE} offline=${OFFLINE}"
elif [ $OFFLINE -ge $CRITICALNUMBER ]; then
exitstatus=$STATE_CRITICAL
MSG="CRITICAL: ${ONLINE} online, ${OFFLINE} offline IAX2 peers|online=${ONLINE} offline=${OFFLINE}"
fi
elif [ "${CHECK}" = "sippeers" ]; then
##WARNING
if [ "$WARNINGNUMBER" = "" ]; then
WARNINGNUMBER=5
fi
if [ "$CRITICALNUMBER" = "" ]; then
CRITICALNUMBER=10
fi
SIPpeers=`/bin/echo -e "Action: login\r\nUsername: ${AMIUSER}\r\nSecret: ${AMIPASS}\r\nEvents: off\r\n\r\nAction: Command\r\ncommand: sip show peers\r\n\r\nAction: Logoff\r\n\r\n" | ${NC} ${REMOTEHOST} ${AMIPORT} | sed 's/Output: //' | awk '/online/ {print $0}' | tr -d "\r"`
ONLINE=`echo $SIPpeers | sed 's/.*Monitored: \(.*\) online.*Unmonitored.*/\1/'`
OFFLINE=`echo $SIPpeers | sed 's/.*online, \(.*\) offline.*Unmonitored.*/\1/'`
if [ "$OFFLINE" = "" ]; then
echo "UNKNOWN: Unable to get number of SIP Peers"
exit $STATUS_UNKNOWN
fi
if [ $OFFLINE -lt $WARNINGNUMBER ]; then
exitstatus=$STATE_OK
MSG="OK: ${ONLINE} online, ${OFFLINE} offline SIP peers|online=${ONLINE} offline=${OFFLINE}"
elif [ $OFFLINE -lt $CRITICALNUMBER ]; then
exitstatus=$STATE_WARNING
MSG="WARNING: ${ONLINE} online, ${OFFLINE} offline SIP peers|online=${ONLINE} offline=${OFFLINE}"
elif [ $OFFLINE -ge $CRITICALNUMBER ]; then
exitstatus=$STATE_CRITICAL
MSG="CRITICAL: ${ONLINE} online, ${OFFLINE} offline SIP peers|online=${ONLINE} offline=${OFFLINE}"
fi
elif [ "${CHECK}" = "pjsip" ]; then
EP=0
while read L; do
let EP="$EP + $L"
done < <(/bin/echo -e "Action: login\r\nUsername: ${AMIUSER}\r\nSecret: ${AMIPASS}\r\nEvents: off\r\n\r\nAction: Command\r\ncommand: pjsip show endpoints\r\n\r\nAction: Logoff\r\n\r\n" | ${NC} ${REMOTEHOST} ${AMIPORT} | sed 's/Output: //' | awk '/Aor/ {print $3}' | grep -v "MaxContact" | grep -v "Hash" | tr -d "\r")
PJSIP_TOTAL_ENDPOINTS=$EP
PJSIP_CONTACTS=`/bin/echo -e "Action: login\r\nUsername: ${AMIUSER}\r\nSecret: ${AMIPASS}\r\nEvents: off\r\n\r\nAction: Command\r\ncommand: pjsip show contacts\r\n\r\nAction: Logoff\r\n\r\n" | ${NC} ${REMOTEHOST} ${AMIPORT} | sed 's/Output: //' | awk '/Objects/ {print $3}' | tr -d "\r"`
ONLINE=$PJSIP_CONTACTS
OFFLINE=$(( $PJSIP_TOTAL_ENDPOINTS - $PJSIP_CONTACTS ))
if [ "$ONLINE" = "" ]; then
echo "UNKNOWN: Unable to get number of PJSIP Peers"
exit $STATUS_UNKNOWN
fi
if [ "$WARNINGNUMBER" = "" ] || [ "$CRITICALNUMBER" = "" ]; then
MSG_PRE="OK:"
exitstatus=$STATE_OK
else
if [ $OFFLINE -lt $WARNINGNUMBER ]; then
exitstatus=$STATE_OK
MSG_PRE="OK:"
elif [ $OFFLINE -lt $CRITICALNUMBER ]; then
exitstatus=$STATE_WARNING
MSG_PRE="WARNING:"
elif [ $ONLINE -ge $CRITICALNUMBER ]; then
exitstatus=$STATE_CRITICAL
MSG_PRE="CRITICAL:"
fi
fi
MSG="${MSG_PRE} ${ONLINE} online, ${OFFLINE} offline PJSIP peers|online=${ONLINE} offline=${OFFLINE}"
else
echo="CRITICAL: Unknown command"
print_help
exit=$STATE_CRITICAL
fi
echo $MSG
exit $exitstatus