-
Notifications
You must be signed in to change notification settings - Fork 12
/
check_freeswitch_health.pl
executable file
·299 lines (260 loc) · 9.55 KB
/
check_freeswitch_health.pl
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
#! /usr/bin/perl -w
# check_freeswitch_health.pl
#
# Written by Khalid J Hosein, Platform28, http://platform28.com
# July 2013
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# Many thanks to Ton Voon for writing the Nagios::Plugin Perl module
# http://search.cpan.org/~tonvoon/Nagios-Plugin-0.36/
#
# Remember to modify the $fs_cli_location variable below to suit your install.
#
# The queries that you can pass to this plugin *resemble* but *do not*
# completely match queries that you can give fs_cli (in the -x argument)
# The reason for this is that those queries sometimes spit back too
# much data to process in one Nagios check. Additionally, they've all
# been transformed to hyphenated versions in order not to trip up NRPE.
#
# Note that since it's less complicated for Nagios to deal with one check at a
# time, this script only accepts one (1) -q query.
#
# Checks that you can run currently and what type of results to expect:
# sofia-status-internal - looks for the 'internal' Name and expects to
# find a state of RUNNING. Sets the result to 1 if successful, 0 otherwise.
# You'll need to set -c 1:1 (or -w 1:1) in Nagios if you want to
# alert on it. See Nagios Thresholds for more info:
# http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT
# This check also returns # of calls as performance data.
# sofia-status-external - looks for the 'external' Name and expects to
# find a state of RUNNING. Same format as the 'internal' test above.
# show-calls-count - reports total # of current calls.
# sofia-status-profile-internal-failed-calls-in - reports the FAILED-CALLS-IN
# parameter in the 'sofia status profile internal' query.
# sofia-status-profile-internal-failed-calls-out - reports the FAILED-CALLS-OUT
# parameter in the 'sofia status profile internal' query.
# show-registrations-count - reports total # of current registrations.
#
# TO DO IN FUTURE VERSIONS:
# 1. (DONE) Include an option (perhaps -a) to list all allowed queries.
# Decided to refer the user back to the docs in the comments.
# 2. (DONE) Remove excess whitespace from $rawdata
# 3. Refine the use of the $perfdatatitle (better logic on selecting the title)
# 4. Look for fs_cli, and report back via cmd line output and perfdata if can't find
# I. Prologue
use strict;
use warnings;
# Look for 'feature' pragma (Perl 5.10+), otherwise use Switch module (Perl 5.8)
eval {
# require feature 'switch';
require feature;
feature->import();
};
unless($@) {
use Switch 'Perl6';
}
use Nagios::Plugin;
# use vars qw($VERSION $PROGNAME $result);
our ( $VERSION, $PROGNAME, $result, $rawdata );
$VERSION = '0.5';
# get the base name of this script for use in the examples
use File::Basename;
$PROGNAME = basename( $0 );
# Fully qualified path to fs_cli. Modify this to suit:
my $fs_cli_location = "/usr/bin/fs_cli";
# Declare some vars
my @fs_cli_output;
my $subquery;
my $result2;
my $label2;
# Currently processed fs_cli queries:
my @allowed_checks = (
"show-calls-count",
"show-registrations-count",
"sofia-status-internal",
"sofia-status-external",
"sofia-status-profile-internal-failed-calls-in",
"sofia-status-profile-internal-failed-calls-out"
);
# II. Usage/Help
my $p = Nagios::Plugin->new(
usage => "Usage: %s
[ -q|--query=These are mapped to specific fs_cli -x checks
e.g. show-calls-count is mapped to 'show calls count'
[ -w|--warning=threshold that generates a Nagios warning ]
[ -c|--critical=threshold that generates a Nagios critical warning ]
[ -f|--perfdatatitle=title for Nagios Performance Data.
Note: don't use spaces. ]
See the documentation in this script's comments for accepted queries.
For example, you can run 'head -n 50 check_freeswitch_health.pl'
",
version => $VERSION,
blurb => "This plugin requires the FreeSWITCH fs_cli command to perform checks.",
extra => qq(
An example query:
./check_freeswitch_health.pl -q show-calls-count -w 100 -c 150 -f Total_Calls
),
license =>
"This Nagios plugin is subject to the terms of the Mozilla Public License, v. 2.0.",
);
# III. Command line arguments/options
# See Getopt::Long for more
$p->add_arg(
spec => 'query|q=s',
required => 1,
help => "-q, --query=STRING
What check to run. E.g. show-calls-count, sofia-status-internal, etc.
REQUIRED."
);
$p->add_arg(
spec => 'warning|w=s',
help => "-w, --warning=INTEGER:INTEGER
Minimum and maximum number of allowable result, outside of which a
warning will be generated. If omitted, no warning is generated."
);
$p->add_arg(
spec => 'critical|c=s',
help => "-c, --critical=INTEGER:INTEGER
Minimum and maximum number of allowable result, outside of which a
an alert will be generated. If omitted, no alert is generated."
);
$p->add_arg(
spec => 'perfdatatitle|f=s',
required => 0,
help => "-f, --perfdatatitle=STRING
If you want to collect Nagios Performance Data, you may
give the check an appropriate name. OPTIONAL"
);
# Parse arguments and process standard ones (e.g. usage, help, version)
$p->getopts;
# IV. Sanity check the command line arguments
# Ensure that only one of the supported fs_cli queries are called:
my $query = $p->opts->query;
unless ( grep /^$query$/i, @allowed_checks ) {
$p->nagios_die( "Sorry, that's not an allowed check (yet?)!" );
}
# V. Check the stuff
# Set up and run the specific queries
given ( $query ) {
# Perform a 'show calls count'
when ( "show-calls-count" ) {
@fs_cli_output = `$fs_cli_location -x "show calls count"`;
foreach ( @fs_cli_output ) {
if ( /total/i ) {
my @temp = split( /\s+/, $_ );
$rawdata = $_;
$result = $temp[0];
last;
}
}
}
when ( "sofia-status-internal" ) {
@fs_cli_output = `$fs_cli_location -x "sofia status"`;
$subquery = 'internal';
foreach ( @fs_cli_output ) {
if ( /internal/i ) {
my @temp = split( /\s+/, $_ );
if ( $temp[1] eq 'internal' ) {
$rawdata = $_;
$temp[5] =~ s/[^0-9]//g; # strip out parens
$result2 = $temp[5];
$label2 = "# of Calls";
if ( $temp[4] =~ /^running$/i ) {
$result = 1;
} else {
$result = 0;
}
last;
}
}
}
}
when ( "sofia-status-external" ) {
@fs_cli_output = `$fs_cli_location -x "sofia status"`;
$subquery = 'external';
foreach ( @fs_cli_output ) {
if ( /external/i ) {
my @temp = split( /\s+/, $_ );
if ( $temp[1] eq 'external' ) {
$rawdata = $_;
$temp[5] =~ s/[^0-9]//g; # strip out parens
$result2 = $temp[5];
$label2 = "# of Calls";
if ( $temp[4] =~ /^running$/i ) {
$result = 1;
} else {
$result = 0;
}
last;
}
}
}
}
when ( "sofia-status-profile-internal-failed-calls-in" ) {
@fs_cli_output = `$fs_cli_location -x "sofia status profile internal"`;
$subquery = 'failed-calls-in';
foreach ( @fs_cli_output ) {
if ( /failed-calls-in/i ) {
my @temp = split( /\s+/, $_ );
$rawdata = $_;
$result = $temp[1];
}
}
}
when ( "sofia-status-profile-internal-failed-calls-out" ) {
@fs_cli_output = `$fs_cli_location -x "sofia status profile internal"`;
$subquery = 'failed-calls-out';
foreach ( @fs_cli_output ) {
if ( /failed-calls-out/i ) {
my @temp = split( /\s+/, $_ );
$rawdata = $_;
$result = $temp[1];
}
}
}
when ( "show-registrations-count" ) {
@fs_cli_output = `$fs_cli_location -x "show registrations"`;
foreach ( @fs_cli_output ) {
if ( /total/i ) {
my @temp = split( /\s+/, $_ );
$rawdata = $_;
$result = $temp[0];
last;
}
}
}
}
# VI. Performance Data gathering
my $threshold = $p->set_thresholds(
warning => $p->opts->warning,
critical => $p->opts->critical
);
my $perfdatatitle = $query;
if ( defined $p->opts->perfdatatitle ) {
$perfdatatitle = $p->opts->perfdatatitle;
}
$perfdatatitle =~ s/\s/_/g; # replace whitespaces with underscores
$p->add_perfdata(
label => $perfdatatitle,
value => $result,
threshold => $threshold,
uom => "", # Bug in Nagios::Plugin version 0.15
);
# is there a 2nd set of performance data:
if ( defined $result2 ) {
$p->add_perfdata(
label => $label2,
value => $result2,
uom => "", # Bug in Nagios::Plugin version 0.15
);
}
# VIII. Exit Code
# Output in Nagios format and exit.
# remove excess whitespace:
$rawdata =~ s/\s+/ /g;
$p->nagios_exit(
return_code => $p->check_threshold( $result ),
message => "Result of check is: $rawdata",
);