#!/usr/bin/perl -w
#
# Copyright (C) 2005 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.
#
#
# $Log: not supported by cvs2svn $
# Revision 1.1  2005/10/22 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";
}

if ($#ARGV < 3)
{
    print "asterisk_iaxlag.pl lag_critical jitter_critical lag_warning jitter_warning\n";
    exit 3;
}
my $lag_critical=$ARGV[0];
my $jitter_critical=$ARGV[1];
my $lag_warning=$ARGV[2];
my $jitter_warning=$ARGV[3];

my $host = "127.0.0.1";
my $port = "5038";

my $username = "admin";
my $secret   = "chopp";

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

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

#Response: Follows
#Channel               Peer             Username    ID (Lo/Rem)  Seq (Tx/Rx)  Lag      Jitter  JitBuf  Format
#IAX2/rodolphe@rodolp  10.8.53.6        rodolphe    00003/01287  00006/00004  00000ms  0148ms  0000ms  gsm
#1 active IAX channel(s)
#--END COMMAND--

my ($lag, $jitter, $start, $channels)=(0,0,0,0);
my @fields;
my @cha;
my $warning=0;
my $critical=0;
while (($line = $pop->getline) and ($line !~ /active IAX channel/o))
{
    if ($start) {
	@fields = (split ' ', $line);
	$_ = $fields[5];
	s/ms//;
	$lag = $lag + $_;
	if ($_>$lag_critical) {
		$critical=1;
	}
        if ($_>$lag_warning) {
                $warning=1;
        }

	$_ = $fields[6];
	s/ms//;
	$jitter = $jitter + $_;
        if ($_>$jitter_critical) {
                $critical=1;
        }
        if ($_>$jitter_warning) {
                $warning=1;
        }

	push @cha, "$fields[0] lag  $fields[5] Jitter $fields[6] / ";

	$channels++;
    }

    $start = 1 if ($line =~ /Channel/o);
}

$pop->print("Action: logoff");
$pop->print("");

my $avg_lag=0;
my $avg_jitter=0;
if ($channels) {
	$avg_lag=$lag/$channels;
	$avg_jitter=$jitter/$channels;
}
else {
	$avg_lag=0;
	$avg_jitter=0;
}
my $unit="ms";
if ($critical==1) {
	print "CRITICAL - @cha | lag=$avg_lag$unit;0;;; jitter=$avg_jitter$unit;0;;;\n";
	exit(2);
}
if ($warning==1) {
        print "CRITICAL - @cha | lag=$avg_lag$unit;0;;; jitter=$avg_jitter$unit;0;;;\n";
        exit(1);
}

if ($channels) {
	print "OK - @cha  | lag=$avg_lag$unit;0;;; jitter=$avg_jitter$unit;0;;;\n";
}
else {
	print "OK - no channles active  | lag=$avg_lag$unit;0;;; jitter=$avg_jitter$unit;0;;;\n";
}
exit(0);

# vim:syntax=perl

