[Nagiosplug-devel] Nagios plugin for monitoring number of messages in an asterisk mailbox
Ted Cabeen
secabeen at pobox.com
Fri Mar 19 22:19:01 CET 2004
For any of you who want it, here's a nagios plugin that allows you to
monitor the number of unread messages in an asterisk mailbox through
the monitor interface.
Thanks to the authors of check_breeze (Jeffrey Blank & Karl
DeBisschop) for a nice perl plugin that I could modify to my needs.
#! /usr/bin/perl -w
# ------------------------------------------------------------------------
#
# check_asterisk_vm -- A program to check the number of unread
# messages in an asterisk voicemail mailbox
#
# Copyright 2000-2004 Jeffrey Blank/Karl DeBisschop & Ted Cabeen
# All Rights Reserved
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
# USA; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# -----------------------------------------------------------------------
use strict;
use Getopt::Long;
use IO::Socket;
use vars qw($opt_V $opt_h $opt_H $opt_w $opt_c $opt_u $opt_p $opt_m $PROGNAME);
use lib "/usr/local/nagios/libexec" ;
use utils qw(%ERRORS &print_revision &support &usage);
$PROGNAME = "check_asterisk_vm";
my $port = 5038;
my $EOL = "\015\012";
my $BLANK = $EOL x 2;
my $context = "default";
sub print_help ();
sub print_usage ();
$ENV{'PATH'}='';
$ENV{'BASH_ENV'}='';
$ENV{'ENV'}='';
Getopt::Long::Configure('bundling');
GetOptions
("V" => \$opt_V, "version" => \$opt_V,
"h" => \$opt_h, "help" => \$opt_h,
"w=s" => \$opt_w, "warning=s" => \$opt_w,
"c=s" => \$opt_c, "critical=s" => \$opt_c,
"H=s" => \$opt_H, "hostname=s" => \$opt_H,
"u=s" => \$opt_u, "user=s" => \$opt_u,
"p=s" => \$opt_p, "password=s" => \$opt_p,
"m=s" => \$opt_m, "mailbox=s" => \$opt_m);
if ($opt_V) {
print_revision($PROGNAME,'$Revision: 1.0 $');
exit $ERRORS{'OK'};
}
if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
($opt_H) || usage("Host name/address not specified\n");
my $host = $1 if ($opt_H =~ /([-.A-Za-z0-9]+)/);
($host) || usage("Invalid host: $opt_H\n");
($opt_w) || usage("Warning threshold not specified\n");
my $warning = $1 if ($opt_w =~ /([0-9]{1,2}|100)+/);
($warning) || usage("Invalid warning threshold: $opt_w\n");
($opt_c) || usage("Critical threshold not specified\n");
my $critical = $1 if ($opt_c =~ /([0-9]{1,2}|100)/);
($critical) || usage("Invalid critical threshold: $opt_c\n");
($opt_u) || usage("Username not specified\n");
my $username = $1 if ($opt_u =~ /([-.A-Za-z0-9]+)/);
($username) || usage("Invalid username: $opt_u\n");
($opt_p) || usage("Password not specified\n");
my $password = $1 if ($opt_p =~ /([-.A-Za-z0-9]+)/);
($password) || usage("Invalid password: $opt_p\n");
($opt_m) || usage("Mailbox not specified\n");
my $mbox = $1 if ($opt_m =~ /([0-9]{1,5})/);
($mbox) || usage("Invalid mailbox: $opt_m\n");
my $remote = IO::Socket::INET->new(
Proto => 'tcp', # protocol
PeerAddr=> $host, # Address of server
PeerPort=> $port, # port of server
Reuse => 1
) or die "$!";
$remote->autoflush(1); # Send immediately
# Login and get our booty from Asterisk
my $logres = send_cmd("Action: Login${EOL}Username: $username${EOL}Secret: $password$BLANK");
my $vminfo = send_cmd("Action: MailboxStatus${EOL}Mailbox: $mbox$BLANK");
$logres = send_cmd("Action: Logoff$BLANK");
close $remote; # Close socket
my $newmess = 0;
if ($vminfo =~ /Waiting:\s+(\d+)\n/) {
$newmess = $1;
}
if ($newmess >= $critical) {
print "CRITICAL: Mailbox $mbox has $newmess messages waiting\n";
exit $ERRORS{'CRITICAL'};
} elsif ($newmess >= $warning) {
print "WARNING: Mailbox $mbox has $newmess messages waiting\n";
exit $ERRORS{'WARNING'};
} else {
print "OK: Mailbox $mbox has $newmess messages waiting\n";
exit $ERRORS{'OK'};
}
sub print_usage () {
print "Usage: $PROGNAME -H <host> -u <username> -p <password> -m <mailbox> -w <warn> -c <crit>\n";
}
sub print_help () {
print_revision($PROGNAME,'$Revision: 1.0 $');
print "Copyright (c) 2000-2004 Jeffrey Blank/Karl DeBisschop & Ted Cabeen
This plugin reports the number of messages in an asterisk queue
The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute
copies of the plugins under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.
";
print_usage();
print "
-H, --hostname=HOST
Name or IP address of host to check
-u, --username=username
Manager Username
-p, --password=password
Manager Password
-m, --mailbox=mailbox
Mailbox number or name
-w, --warning=INTEGER
generate warning state if message count equals or exceeds this number
-c, --critical=INTEGER
generate critical state if message count equals or exceeds this number
";
support();
}
sub read_conn {
my $buf="";
while (<$remote>) {
last if $_ eq $EOL;
s/$EOL/\n/g;
$buf .= $_;
}
return $buf
}
sub send_cmd {
my $cmd = $_[0];
my $buf="";
print $remote $cmd;
$buf = read_conn();
return $buf;
}
--
Ted Cabeen http://www.pobox.com/~secabeen ted at impulse.net
Check Website or Keyserver for PGP/GPG Key BA0349D2 secabeen at pobox.com
"I have taken all knowledge to be my province." -F. Bacon secabeen at cabeen.org
"Human kind cannot bear very much reality."-T.S.Eliot cabeen at netcom.com
More information about the Devel
mailing list