#!@@PERL@@ -w
# -*- perl -*-

=head1 NAME

asterisk_channelstypes - Plugin to graph the number of channel types
in use

=head1 ABOUT

This plugin uses the asterisk manager API to fetch data.

=head1 CONFIGURATION

The following configuration parameters are used by this plugin

 [asterisk_channelstypes]
  env.host     - hostname to connect to
  env.port     - port number to connect to
  env.username - username used for authentication
  env.secret   - secret used for authentication
  env.channels - The channel types to look for

The "username" and "secret" parameters are mandatory, and have no
defaults.

=head2 DEFAULT CONFIGURATION

 [asterisk_channelstypes]
  env.host 127.0.0.1
  env.port 5038
  env.channels Zap IAX2 SIP

=head1 AUTHOR

Copyright (C) 2005-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>

=head1 LICENSE

Gnu GPLv2

=begin comment

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.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

If you improve this script please send your version to my email address
with the copyright notice upgrade with your name.

=end comment

=head1 MAGIC MARKERS

 #%# family=contrib

=cut

use IO::Socket;
use strict;

my @CHANNELS = exists $ENV{'channels'} ? split ' ',$ENV{'channels'} : qw(Zap IAX2 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 IO::Socket::INET (PeerAddr => $host,
				PeerPort => $port,
				Proto => 'tcp');
die "Could not create socket: $!\n" unless $pop;

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

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

#Response: Success
#Message: Authentication accepted

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

#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 channels/o))
{
    $i = 0;
    if ($start) {
	@fields = (split '/', $line);
	foreach my $channel (@CHANNELS) {
	    $results[$i] = $results[$i] + 1 if ($fields[0] eq $channel);
	    $i++;
	}
    }
    $start = 1 if ($line =~ /Channel/o);
}


# Logoff
$pop->print("Action: logoff\n");
$pop->print("\n");

## Exhaust buffer before closing (to avoid polluting Asterisk's logs)
while ($line = $pop->getline) {}

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

# vim:syntax=perl
