| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Asterisk Queue Viewer |
| 4 |
# Uses management interface to query call queues on a machine |
| 5 |
# (C) 2003 David C. Troy -- [email protected] |
| 6 |
# |
| 7 |
# This program is free software, distributed under the terms of the |
| 8 |
# GNU General Public License |
| 9 |
# |
| 10 |
|
| 11 |
use IO::Socket; |
| 12 |
use CGI qw(:standard); |
| 13 |
use CGI::Carp qw/fatalsToBrowser/; |
| 14 |
|
| 15 |
$host = "asterisk.yourdomain.com"; |
| 16 |
$port = 5038; |
| 17 |
$user = "manager_user"; |
| 18 |
$secret = "Manager_secret"; |
| 19 |
$EOL = "\015\012"; |
| 20 |
$BLANK = $EOL x 2; |
| 21 |
$queue = param('queue'); |
| 22 |
|
| 23 |
$remote = IO::Socket::INET->new( |
| 24 |
Proto => 'tcp', # protocol |
| 25 |
PeerAddr=> $host, # Address of server |
| 26 |
PeerPort=> $port, # port of server |
| 27 |
Reuse => 1 |
| 28 |
) or die "$!"; |
| 29 |
|
| 30 |
$remote->autoflush(1); # Send immediately |
| 31 |
|
| 32 |
# Login and get our booty from Asterisk |
| 33 |
$logres = send_cmd("Action: Login${EOL}Username: $user${EOL}Secret: $secret$BLANK"); |
| 34 |
$qinfo = send_cmd("Action: queues$BLANK$EOL"); |
| 35 |
$logres = send_cmd("Action: Logoff$BLANK"); |
| 36 |
close $remote; # Close socket |
| 37 |
|
| 38 |
my %qcalls = map { /(\S+)\s+has (\d+) calls.*?\n\n/sg; } $qinfo; |
| 39 |
my %qmax = map { /(\S+)\s+has \d+ calls \(max (\S+)\).*?\n\n/sg; } $qinfo; |
| 40 |
my %qstrat = map { /(\S+)\s+has \d+ calls \(max \S+\) in (\S+) strategy.*?\n\n/sg; } $qinfo; |
| 41 |
my %qmems = map { /(\S+)\s+has \d+ calls.*?Members:.*?\s{6}(.*?)\s{3}\S*?\s*?Callers/sg; } $qinfo; |
| 42 |
my %qcallers = map { /(\S+)\s+has \d+ calls.*?([No ]*Callers.*?)\n\n/sg; } $qinfo; |
| 43 |
|
| 44 |
print header(); |
| 45 |
print start_html(-head=>meta({-http_equiv=>'Refresh', -content=>'120'}), |
| 46 |
-title=>"PBX Queue Viewer", |
| 47 |
-style=>{'src'=>'/pbxinfo.css'}); |
| 48 |
print "<table width=850><tr>"; |
| 49 |
|
| 50 |
$col = 0; |
| 51 |
|
| 52 |
foreach $q (keys %qcalls) { |
| 53 |
|
| 54 |
$mems = $qmems{$q}; |
| 55 |
$mems =~ s/ //g; |
| 56 |
$mems =~ s/\n/<br>\n/g; |
| 57 |
$callers = $qcallers{$q}; |
| 58 |
$callers =~ s/ //g; |
| 59 |
$callers =~ s/Callers:.*\n//g; |
| 60 |
$callers =~ s/\n/<br>/g; |
| 61 |
|
| 62 |
print qq{<td valign=top width=48%><table width=100%> |
| 63 |
<tr><th colspan=2><A HREF=/mrtg/qmon-$q.html>$q</A> $qcalls{$q} calls (max $qmax{$q}), $qstrat{$q} strategy</th></tr> |
| 64 |
<tr><td valign=top width=55%>$mems</td><td valign=top width=45%>$callers</td></tr> |
| 65 |
</table></td> |
| 66 |
}; |
| 67 |
|
| 68 |
print "</tr><tr>" if $col; |
| 69 |
$col = 0 if $col++; |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
print "</table>"; |
| 74 |
|
| 75 |
print end_html(); |
| 76 |
|
| 77 |
exit(0); |
| 78 |
|
| 79 |
sub read_conn { |
| 80 |
|
| 81 |
my $buf=""; |
| 82 |
while (<$remote>) { |
| 83 |
last if $_ eq $EOL; |
| 84 |
s/$EOL/\n/g; |
| 85 |
$buf .= $_; |
| 86 |
} |
| 87 |
|
| 88 |
return $buf |
| 89 |
} |
| 90 |
|
| 91 |
sub send_cmd { |
| 92 |
my $cmd = @_[0]; |
| 93 |
|
| 94 |
my $buf=""; |
| 95 |
print $remote $cmd; |
| 96 |
|
| 97 |
$buf = read_conn(); |
| 98 |
|
| 99 |
return $buf; |
| 100 |
} |