1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-22 14:16:00 +00:00

asterisk: refactor command/response handling.

Instead of having a dumb readreply, already drop some of the unneeded
lines of the command when sending the request, and handle errors in
place.

This simplifies the code a little, even though it requires a few more
"discard loops" in the code (to avoid garbage in Asterisk's logs).
This commit is contained in:
Diego Elio Pettenò 2012-12-30 13:36:36 -08:00
parent b77eef4a62
commit 1a98faf11c

View file

@ -67,16 +67,31 @@ use strict;
use Munin::Plugin; use Munin::Plugin;
use IO::Socket; use IO::Socket;
sub readreply { sub asterisk_command {
my $socket = shift; my ($socket, $command) = @_;
my $line; my $line, my $reply;
my $return;
while ( $line = $socket->getline and $line ne "\r\n" ) { $socket->print("Action: command\nCommand: $command\n\n");
$return .= $line;
# Response: (Error|Follows|???)
$line = $socket->getline;
if ($line ne "Response: Follows\r\n") {
while ( $line = $socket->getline and $line ne "\r\n" ) {}
return undef;
} }
return $return; # Privilege: Command
$line = $socket->getline;
# Until we get the --END COMMAND-- marker, it's the command's output.
while ( $line = $socket->getline and $line ne "--END COMMAND--\r\n" ) {
$reply .= $line;
}
# And then wait for the empty line that says we're done
while ( $line = $socket->getline and $line ne "\r\n" ) {}
return $reply;
} }
$0 =~ /asterisk(?:_(.+))$/; $0 =~ /asterisk(?:_(.+))$/;
@ -101,14 +116,15 @@ if ( $socket ) {
$socket->getline; $socket->getline;
$socket->print("Action: login\nUsername: $username\nSecret: $secret\nEvents: off\n\n"); $socket->print("Action: login\nUsername: $username\nSecret: $secret\nEvents: off\n\n");
my $auth_status = readreply $socket; my $response_status = $socket->getline;
if ( $auth_status !~ /^Response: Success/ ) { if ( $response_status ne "Response: Success\r\n" ) {
$auth_status =~ s/.*\nMessage: (.*)\r\n/$1/; my $response_message = $socket->getline;
$error = "Asterisk authentication error: " . $auth_status; $response_message =~ s/Message: (.*)\r\n/$1/;
$socket->close(); $error = "Asterisk authentication error: " . $response_message;
$socket = undef;
} }
while ( $line = $socket->getline and $line ne "\r\n" ) {}
} }
if ( $ARGV[0] and $ARGV[0] eq 'autoconf' ) { if ( $ARGV[0] and $ARGV[0] eq 'autoconf' ) {
@ -155,28 +171,20 @@ END
} }
# if we arrive here and we don't have a socket, it's time to exit. # if we arrive here and we don't have a socket, it's time to exit.
die $error unless $socket; die $error if $error;
$socket->print("Action: command\nCommand: core show channels\n\n"); my $channels_response = asterisk_command($socket, "core show channels");
#Response: Follows
#Channel Location State Application(Data) #Channel Location State Application(Data)
#Zap/pseudo-198641660 s@frompstn:1 Rsrvd (None) #Zap/pseudo-198641660 s@frompstn:1 Rsrvd (None)
#Zap/1-1 4@frompstn:1 Up MeetMe(5500) #Zap/1-1 4@frompstn:1 Up MeetMe(5500)
#2 active channels #2 active channels
#1 active call #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"); my $voicemail_response = asterisk_command($socket, "voicemail show users");
#Response: Follows
#Context Mbox User Zone NewMsg #Context Mbox User Zone NewMsg
#default 1234 Example Mailbox 1 #default 1234 Example Mailbox 1
#other 1234 Company2 User 0 #other 1234 Company2 User 0
#2 voicemail users configured. #2 voicemail users configured.
#--END COMMAND--
my $voicemail_response = readreply $socket;
$voicemail_response = undef if $voicemail_response =~ /Response: Error/;
$socket->close(); $socket->close();