qview.pl 2.4 KB

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