1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-22 02:51:03 +00:00
Munin-Contrib/plugins/asterisk/asterisk

210 lines
5 KiB
Perl
Executable file

#!/usr/bin/perl -w
# -*- cperl -*-
=head1 NAME
asterisk - Multigraph-capable plugin to monitor Asterisk
=head1 NOTES
This plugin will produce multiple graphs showing:
- total number of active channels (replaces asterisk_channels),
together with breakdown of specific channel types (replaces
asterisk_channelstypes);
- the number of messages in all voicemail boxes (replaces
asterisk_voicemail).
=head1 CONFIGURATION
The following configuration parameters are used by this plugin
[asterisk]
env.host - hostname to connect to
env.port - port number to connect to
env.username - username used for authentication
env.secret - secret used for authentication
env.channels - The channel types to look for
The "username" and "secret" parameters are mandatory, and have no
defaults.
=head2 DEFAULT CONFIGURATION
[asterisk]
env.host 127.0.0.1
env.port 5038
env.channels Zap IAX2 SIP
=head2 WILDCARD CONFIGURATION
It's possible to use the plugin in a virtual-node capacity, in which
case the host configuration will default to the hostname following the
underscore:
[asterisk_someserver]
env.host someserver
env.port 5038
=head1 AUTHOR
Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
Copyright (C) 2012 Diego Elio Pettenò <flameeyes@flameeyes.eu>
=head1 LICENSE
GPLv2
=head1 MAGIC MARKERS
#%# family=auto
#%# capabilities=autoconf
=cut
use strict;
use Munin::Plugin;
use IO::Socket;
sub readreply {
my $socket = shift;
my $line;
my $return;
while ( $line = $socket->getline and $line ne "\r\n" ) {
$return .= $line;
}
return $return;
}
$0 =~ /asterisk(?:_(.+))$/;
my $hostname = $1;
my $peeraddr = $ENV{'host'} || $hostname || '127.0.0.1';
my $peerport = $ENV{'port'} || '5038';
my $username = $ENV{'username'};
my $secret = $ENV{'secret'};
my @CHANNELS = exists $ENV{'channels'} ? split ' ',$ENV{'channels'} : qw(Zap IAX2 SIP);
my $line, my $error;
my $socket = new IO::Socket::INET(PeerAddr => $peeraddr,
PeerPort => $peerport,
Proto => 'tcp')
or $error = "Could not create socket: $!";
if ( $socket ) {
# This will consume the "Asterisk Call Manager" welcome line.
$socket->getline;
$socket->print("Action: login\nUsername: $username\nSecret: $secret\nEvents: off\n\n");
my $auth_status = readreply $socket;
if ( $auth_status !~ /^Response: Success/ ) {
$auth_status =~ s/.*\nMessage: (.*)\r\n/$1/;
$error = "Asterisk authentication error: " . $auth_status;
$socket->close();
$socket = undef;
}
}
if ( $ARGV[0] and $ARGV[0] eq 'autoconf' ) {
if ( $error ) {
print "no ($error)\n";
} else {
print "yes\n";
}
exit 0;
} elsif ( $ARGV[0] and $ARGV[0] eq 'config' ) {
print "host_name $hostname" if $hostname;
print <<END;
multigraph asterisk_channels
graph_title Asterisk active channels
graph_args --base 1000 -l 0
graph_vlabel channels
graph_category asterisk
total.label channels
END
foreach my $channel (@CHANNELS) {
print <<END;
$channel.draw AREASTACK
$channel.label $channel channels
END
}
print <<END;
multigraph asterisk_voicemail
graph_title Asterisk voicemail messages
graph_args --base 1000 -l 0
graph_vlabel messages
graph_category asterisk
messages.label Total messages
END
unless ( ($ENV{MUNIN_CAP_DIRTYCONFIG} || 0) == 1 ) {
exit 0;
}
}
# if we arrive here and we don't have a socket, it's time to exit.
die $error unless $socket;
$socket->print("Action: command\nCommand: core show channels\n\n");
#Response: Follows
#Channel Location State Application(Data)
#Zap/pseudo-198641660 s@frompstn:1 Rsrvd (None)
#Zap/1-1 4@frompstn:1 Up MeetMe(5500)
#2 active channels
#1 active call
#--END COMMAND--
my $channels_response = readreply $socket;
$channels_response = undef if $channels_response =~ /Response: Error/;
$socket->print("Action: command\nCommand: voicemail show users\n\n");
#Response: Follows
#Context Mbox User Zone NewMsg
#default 1234 Example Mailbox 1
#other 1234 Company2 User 0
#2 voicemail users configured.
#--END COMMAND--
my $voicemail_response = readreply $socket;
$voicemail_response = undef if $voicemail_response =~ /Response: Error/;
$socket->close();
my $active_channels = 'U';
$active_channels = $1 if $channels_response =~ /\n([0-9]+) active channels/;
print <<END;
multigraph asterisk_channels
total.value $active_channels
END
my @channels_list = split(/\r\n/, $channels_response) if $channels_response;
foreach my $channel (@CHANNELS) {
print "$channel.value ";
print $channels_response ? scalar(grep(/^$channel\//, @channels_list)) : "U";
print "\n";
}
print "\nmultigraph asterisk_voicemail\n";
if ( !$voicemail_response or $voicemail_response =~ /no voicemail users/ ) {
print "total.value U\n";
} else {
my $messages = 0;
foreach my $line (split(/\r\n/, $voicemail_response)) {
next unless $line =~ / ([0-9]+)$/;
$messages += $1;
}
print "total.value $messages\n";
}