[Asterisk] Manager module for Asterisk-perl Jean-Denis Girard asterisk@marko.net Mon, 04 Nov 2002 17:11:58 -1000 * Previous message: [Asterisk] [PATCH] Dial + URL, Queue +URL <005732.html> * Next message: [Asterisk] Which steps for getting start with Asterisk ? <005671.html> * Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] ------------------------------------------------------------------------ This is a multi-part message in MIME format. --------------090600030806080008050306 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Hi James, I started to work on a new module for Asterisk-perl: Manager, that obviously talks to the Asterisk's Manager sub system. It probably needs more work, but I'd appreciate early comments, so here it is, along with an example program. Jean-Denis --------------090600030806080008050306 Content-Type: text/plain; name="astmon2.pl" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="astmon2.pl" #! /usr/bin/perl # Sample script to demonstrate Asterisk pbx monitoring with Asterisk::Monitor # Jean-Denis Girard > http://www.esoft.pf/ use strict; use warnings; $|++; use Asterisk::Manager; my $astmon = new Asterisk::Manager; $astmon->connect( '', '', 'astman', 'asterisk' ) or die "Could not connect !\n"; my $rpeers = $astmon->iax_peers; my $rqueues = $astmon->queues; print "Peers:\n"; print "\t$_ => ", join( ', ', @{$$rpeers{$_}}), "\n" foreach sort keys %$rpeers; print "\nQueues:\n"; foreach ( sort keys %$rqueues ) { print "\t$_ => Calls: ", ${$$rqueues{$_}}[0], "\n"; print "\t Members: "; foreach ( @{${$$rqueues{$_}}[1]}) { if( s/IAX\/// ) { if( defined ${$$rpeers{$_}}[1] ) { print "IAX:$_ (logged) "; } else { print "IAX:$_ (not here) "; } } else { print "$_ "; } } print "\n"; print "\t Callers: ", join( ', ', @{${$$rqueues{$_}}[2]}), "\n"; } print "\nNow waiting for * events..."; $astmon->status( \&status_cb ); $astmon->logoff; # Useless here because status never returns... sub status_cb { # Subroutine called by Asterisk::Monitor::status on each new event my $rs = shift; print "\nNew event:\n"; print "\t$_ => $$rs{$_}\n" foreach keys %$rs; } --------------090600030806080008050306 Content-Type: text/plain; name="Manager.pm" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="Manager.pm" package Asterisk::Manager; # Asterisk PBX monitoring module # Author: Jean-Denis Girard > http://www.esoft.pf/ require 5.004; use Asterisk; use IO::Socket; use strict; use warnings; my $EOL = "\015\012"; my $BLANK = $EOL x 2; my $VERSION = '0.01'; sub version { $VERSION; } sub new { my ($class, %args) = @_; my $self = {}; $self->{_CONN} = undef; $self->{_PROTOVERS} = undef; bless $self, ref $class || $class; return $self; } sub DESTROY { } sub _read_response { my( $conn ) = @_; my @resp = (); while( <$conn> ) { last if $_ eq $EOL; s/$EOL//g; chomp; push @resp, $_ if $_; } return wantarray ? @resp:$resp[0]; } # connect( $host, $port, $user, $secret) # Make connection to Asterisk server $host on port $port # and login with username $user, password $secret sub connect { my ($self, $host, $port, $user, $secret) = @_; # Connect... my $conn = new IO::Socket::INET( Proto => "tcp", PeerAddr => $host || 'localhost', PeerPort => $port || 5038 ) or die "Connection refused ($host:$port)\n"; $conn->autoflush(1); my $in = <$conn>; $in =~ s/$EOL//g; my( $manager, $version ) = split '/', $in; die "Unknown protocol\n" unless $manager eq 'Asterisk Call Manager'; $self->{_PROTOVERS} = $version; $self->{_CONN} = $conn; # Login... print $conn "Action: Login${EOL}Username: $user${EOL}Secret: $secret$BLANK"; my %resp = map { split(': ', $_); } _read_response $conn; die "Authentification failed for user $user\n" unless $resp{Response} eq 'Success' && $resp{Message} eq 'Authentication accepted'; return 1; } sub logoff { my( $self ) = @_; my $conn = $self->{_CONN}; print $conn "Action: Logoff$BLANK"; my %resp = map { split(': ', $_); } _read_response $conn; return 0 unless $resp{Response} eq 'Goodbye'; return 1; } # Queues Status: return a hash reference: # queue_name => [active_calls, # [member1, member2... ], # [call1, call2... ] # ] sub queues { my( $self ) = @_; my $conn = $self->{_CONN}; print $conn "Action: Queues$BLANK"; my @lines = _read_response $conn; # Sample response (to explain how parsing of @lines to %queues is done) #queue has 1 calls (max unlimited) # Members: # IAX/jdg # IAX/fpo # Callers: # 1. Zap/2-1 (wait: 0:02) #queue_op1 has 0 calls (max unlimited) # Members: # IAX/jdg # No Callers my %queues; for( my $i=0; $i<@lines; ) { my( $l, @members, @callers ); $l = $lines[$i++]; my( $q, undef, $n ) = split /\s+/, $l; # Queue name unless( $l =~ /No\sMembers/ ) { # Scan callers $i++; # Forget 'Members:' line for( ; $i<@lines; ){ $l=$lines[$i++]; last if $l =~ /Callers/; $l =~ s/^\s+//; push @members, $l; } } unless( $l =~ /No\sCallers/ ) { # Scan callers for( ; $i<@lines; ){ $l=$lines[$i++]; last if $l =~ /^\S/; $l =~ s/^\s+//; push @callers, $l; } $i--; } $queues{$q} = [ $n, \@members, \@callers] } return \%queues; } # IAX peers: return a hash reference: # peer_name => [ username, host, dynamic, mask, port ] sub iax_peers { my( $self ) = @_; my $conn = $self->{_CONN}; print $conn "Action: IAXpeers$BLANK"; my %peers = map { my @p = split( /\s+/, $_); my $k = shift @p; $p[1] = undef if $p[1] eq '(Unspecified)'; $p[2] = 0 if $p[2] eq '(S)'; $p[2] = 1 if $p[2] eq '(D)'; ($k, \@p ); } _read_response $conn; # Sample response (to explain how %peers is built) #Name Username Host Mask Port #jdg jdg 192.168.10.100 (D) 255.255.255.255 5036 #fpo fpo (Unspecified) (D) 255.255.255.255 0 delete $peers{Name}; return \%peers; } # Send event status to a callback routine that should be accept a hash # reference as argument. # If successfull, this function never returns... sub status { my( $self, $callback ) = @_; my $conn = $self->{_CONN}; print $conn "Action: Status$BLANK"; my %resp = map { split(': ', $_); } _read_response $conn; return 0 unless $resp{Response} eq 'Success' && $resp{Message} eq 'Channel status will follow'; while( 1 ) { %resp = map { split(': ', $_); } _read_response $conn; &$callback( \%resp ); } } 1; --------------090600030806080008050306-- ------------------------------------------------------------------------ * Previous message: [Asterisk] [PATCH] Dial + URL, Queue +URL <005732.html> * Next message: [Asterisk] Which steps for getting start with Asterisk ? <005671.html> * Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]