#!/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 type, use the 
# asterisk's manager API to fetch datas.
#
# $Log$
#
# Revision 1.4	2020/12/24 11:56:12 -0200	Etienne Allovon <eallovon@avencall.com>
#   Remove iax2 channel type (iax2 no longer exists in XiVO)
#
# Revision 1.3	2016/10/12 20:12:17 -0400	Etienne Lessard <elessard@proformatique.com>
#   Update plugin to work with asterisk >= 14
#
# Revision 1.2  2010/11/04 14:28     Guillaume Bour <gbour@proformatique.com>
#   Zap channels now replaced by Dahdi ones
#
# Revision 1.1  2006/03/06 12: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 @CHANNELS = exists $ENV{'channels'} ? split ' ',$ENV{'channels'} : qw(DAHDI SIP);

if ($ARGV[0] and $ARGV[0] eq "config")
{
    print "graph_title Asterisk channels\n";
    print "graph_args --base 1000 -l 0\n";
    print "graph_vlabel channels\n";
    print "graph_category asterisk\n";
    foreach my $channel (@CHANNELS) {
	if ($channel eq $CHANNELS[0]) {
	    print "$channel.draw AREA\n";
	}
	else{
	    print "$channel.draw STACK\n";
	}
	print "$channel.label $channel\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: core show channels");
$pop->print("");

#Response: Follows
#Channel              Location             State   Application(Data)
#Zap/pseudo-198641660 s@frompstn:1         Rsrvd   (None)
#Zap/1-1              4@frompstn:1         Up      MeetMe(5500)
#2 active channels
#1 active call
#--END COMMAND--

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

my @fields;
while (($line = $pop->getline) and ($line !~ /active channel/o))
{
    $i = 0;
    if ($start) {
	$line =~ s/^Output: //;
	@fields = (split '/', $line);
        foreach my $channel (@CHANNELS) {
            $results[$i] = $results[$i] + 1 if ($fields[0] eq "$channel");
            $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 $channel (@CHANNELS) {
    print "$channel.value $results[$i]\n";
    $i++;
}

# vim:syntax=perl
