#!/usr/bin/perl -w
#
# Copyright (C) 2005-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 dated June,
# 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# If you improve this script please send your version to my email address
# with the copyright notice upgrade with your name.
#
# Plugin to monitor number of active channels by codec used, use the 
# asterisk's manager API to fecth datas.
#
# $Log$
# Revision 1.1  2005/10/28 21:04:01  rodo
# Created by Rodolphe Quiedeville
#
# Parameters mandatory:
#
# 	username
# 	secret
#
#%# family=asterisk
#%# capabilities=autoconf

use strict;

my $ret = undef;
if (! eval "require Net::Telnet;")
{
    $ret = "Net::Telnet not found";
}

#my @codecs = ("g723","gsm","ulaw","alaw","g726","adpcm","slin","lpc10","g729","speex","ilbc");

# At this time we only use theses

my @CODECS = exists $ENV{'codecs'} ? split ' ',$ENV{'codecs'} : qw(gsm ulaw alaw g729 g722 speex);

if ($ARGV[0] and $ARGV[0] eq "config")
{
    print "graph_title Asterisk channels/codecs\n";
    print "graph_args --base 1000 -l 0\n";
    print "graph_vlabel channels\n";
    print "graph_category asterisk\n";
    foreach my $codec (@CODECS) {
	if ($codec eq $CODECS[0]) {
	    print "$codec.draw AREA\n";
	}
	else{
	    print "$codec.draw STACK\n";
	}
	print "$codec.label $codec\n";
    }
    exit 0;
}

my $host = exists $ENV{'host'} ? $ENV{'host'} : "127.0.0.1";
my $port = exists $ENV{'port'} ? $ENV{'port'} : "5038";

my $username = $ENV{'username'};
my $secret   = $ENV{'secret'};

my $pop = new Net::Telnet (Telnetmode => 0);
$pop->open(Host => $host,
	   Port => $port);

## Read connection message.
my $line = $pop->getline;
die $line unless $line =~ /^Asterisk/;

## Send user name.
$pop->print("Action: login");
$pop->print("Username: $username");
$pop->print("Secret: $secret");
$pop->print("Events: off");
$pop->print("");

#Response: Success
#Message: Authentication accepted
while (($line = $pop->getline) and ($line ne "\n"))
{}

## Request status of messages.
$pop->print("Action: command");
$pop->print("Command: sip show channels");
$pop->print("");

#Response: Success
#Message: Command output follows
#Output: Peer             User/ANR         Call ID          Format           Hold     Last Message    Expiry     Peer
#Output: 192.168.17.99    4b1ljr           313434383032383  (alaw)           No       Tx: ACK                    4b1ljr
#Output: 192.168.17.101   65kvmb           313434383032383  (alaw)           No       Tx: ACK                    65kvmb
#Output: 2 active SIP dialogs

my @results;
my ($i, $start)=(0,0);
foreach my $codec (@CODECS) {
    $results[$i] = 0;
    $i++;
}

my @fields;
while (($line = $pop->getline) and ($line !~ /active SIP dialog/o))
{
    $i = 0;
    if ($start) {
	$line =~ s/^Output: //;
	@fields = (split ' ', $line);
	foreach my $codec (@CODECS) {
	    $results[$i] = $results[$i] + 1 if ($fields[3] eq "($codec)");
	    $i++;
	}
    }

    $start = 1 if ($line =~ /Peer/o);
}
while (($line = $pop->getline) and ($line ne "\n"))
{}

## Request status of messages.
$pop->print("Action: command");
$pop->print("Command: iax2 show channels");
$pop->print("");

#Response: Success
#Message: Command output follows
#Output: Channel               Peer                                      Username    ID (Lo/Rem)  Seq (Tx/Rx)  Lag      Jitter  JitBuf  Format  FirstMsg    LastMsg
#Output: IAX2/trunkast         192.168.32.248   trunkast    06737/07371  00002/00003  00000ms  -0001ms  0000ms  Unknow  Tx:NEW      Tx:ACK
#Output: IAX2/barotrunka       192.168.51.252   barotrunka  10108/02659  00004/00005  00000ms  -0001ms  0000ms  Unknow  Tx:NEW      Rx:ACK
#Output: 2 active IAX channels

$start = 0;
while (($line = $pop->getline) and ($line !~ /active IAX channel/o))
{
    $i = 0;
    if ($start) {
        $line =~ s/^Output: //;
        @fields = (split ' ', $line);
        foreach my $codec (@CODECS) {
            $results[$i] = $results[$i] + 1 if ($fields[8] eq "$codec");
            $i++;
        }
    }
    $start = 1 if ($line =~ /Channel/o);
}
while (($line = $pop->getline) and ($line ne "\n"))
{}

# Logoff
$pop->print("Action: logoff");
$pop->print("");
while ($line = $pop->getline)
{}
$pop->close();

$i = 0;
foreach my $codec (@CODECS) {
    print "$codec.value $results[$i]\n";
    $i++;
}

# vim:syntax=perl
