#!/usr/bin/perl -w
############## check_cisco_tp #########################
# Author  : Jim Clark ( banditbbs at gmail.com )
# Help    : http://forum.nagios.com
# Licence : GPL - http://www.fsf.org/licenses/gpl.txt
# Contrib : 
# ToDo    : N/A
# Check Cisco Telepresence
#######################################################
# Help : ./check_cisci_tp.pl -h
#
use strict;
use Net::SNMP;
use Getopt::Long;

#### BASE DIRECTORY FOR TEMP FILE ####
my $o_base_dir="/tmp/tmp_cisco_tp.";
my $final_state = undef;
my $session = undef;
my $result = undef;
#### Nagios specific ####
use lib "/usr/local/nagios/libexec";
use utils qw(%ERRORS $TIMEOUT);

#### SNMP Datas
my $base_table= '.1.3.6.1.4.1.9.9.644';

#### Globals
my $Version   = '1.04';
my $lastindex = undef;
my $audioonly = 0;
my @calltypes;
my @totalrx1;
my @totalrx2;
my @totalrx3;
my @totaltx1;
my @totaltx2;
my @totaltx3;
my @active_calls;
my @callstates;
my $oldest_current = undef;

# Standard options
my $o_host     = undef; # hostname
my $o_port     = 161; 	# port
my $o_mode     = undef; # description filter
my $o_verb=		undef;	# verbose mode
my $o_help     = undef; # wan't some help ?
my $o_version  = undef;	# print version
my $o_warn_opt = undef; # warning options
my $o_crit_opt = undef; # critical options
my $o_timeout  = undef;	# Timeout (Default 5)

# Login options specific
my $o_community = 	undef; 	# community
my $o_version2	= 	undef;	# use snmp v2c
my $o_login     =	undef;	# Login for snmpv3
my $o_passwd	=	undef;	# Pass for snmpv3
my $v3protocols	=	undef;	# V3 protocol list.
my $o_authproto	=	'md5';	# Auth protocol
my $o_privproto	=	'des';	# Priv protocol
my $o_privpass	= 	undef;	# priv password

# functions
sub p_version { print "check_cisco_tp version : $Version\n"; }

sub print_usage {
    print "Usage: $0 -H <host> -C <snmp_community> [-2] | (-l login -x passwd [-X pass -L <authp>,<privp>)  [-p <port>]] -m <mode> -w <warn levels> -c <crit levels> [-t <timeout>] [-V]\n";
}

sub isnnum { # Return true if arg is not a number
  my $num = shift;
  if ( $num =~ /^(\d+\.?\d*)|(^\.\d+)$/ ) { return 0 ;}
  return 1;
}

sub help {
   print "\nCisco Telepresence Monitor for Nagios version ",$Version,"\n";
   print "GPL licence, (c)2013-2014 Jim Clark\n\n";
   print_usage();
   print <<EOT;
-h, --help
   print this help message
-H, --hostname=HOST
   name or IP address of host to check
-C, --community=COMMUNITY NAME
   community name for the host's SNMP agent (implies v1 protocol)
-l, --login=LOGIN ; -x, --passwd=PASSWD, -2, --v2c
   Login and auth password for snmpv3 authentication 
   If no priv password exists, implies AuthNoPriv 
   -2 : use snmp v2c
-X, --privpass=PASSWD
   Priv password for snmpv3 (AuthPriv protocol)
-L, --protocols=<authproto>,<privproto>
   <authproto> : Authentication protocol (md5|sha : default md5)
   <privproto> : Priv protocole (des|aes : default des) 
-P, --port=PORT
   SNMP port (Default 161)
-m, --mode=<mode>
   registered : Checks if registered with call manager
   last       : dispalys last called number
   state      : Current calls state(i.e 'inprogress')
   duration   : Current/last call duration in seconds
   type       : Current call type
   bitrate    : compares initial to current bitrate 
   end        : What type of endpoint is it connected to
   jitter     : check call jitter
   rxpix      : Show rx horizontal pixels
   anomolies  : Show any Out of Order, Late or Duplicate RX packets
   info	      : Show state/last/type/end together(Informational)
   ploss       : Show both RX and TX packet loss(requires w and c values i.e. -w .5 -c 1)
   fps        : Show both RX and TX FPS(requires w and c values)
   codec      : Show both RX and TX codec(Informational)
-w, --warning=input,output[,error in,error out,discard in,discard out]
   warning level for input / output bandwidth (0 for no warning)
     unit depends on B,M,G,u options
   warning for error & discard input / output in error/min (need -q)
-c, --critical=input,output[,error in,error out,discard in,discard out]
   critical level for input / output bandwidth (0 for no critical)
     unit depends on B,M,G,u options
   critical for error & discard input / output in error/min (need -q)
-t, --timeout=INTEGER
   timeout for SNMP in seconds (Default: 5)   
-V, --version
   prints version number
EOT
}

sub check_options { #### Check Options
    Getopt::Long::Configure ("bundling");
	GetOptions(
        'h'     => \$o_help,    	'help'        	=> \$o_help,
        'H:s'   => \$o_host,		'hostname:s'	=> \$o_host,
        'p:i'   => \$o_port,   		'port:i'		=> \$o_port,
		'm:s'   => \$o_mode,        'mode:s'        => \$o_mode,
        'C:s'   => \$o_community,	'community:s'	=> \$o_community,
		'2'		=> \$o_version2,	'v2c'			=> \$o_version2,		
		'l:s'	=> \$o_login,		'login:s'		=> \$o_login,
		'x:s'	=> \$o_passwd,		'passwd:s'		=> \$o_passwd,
		'X:s'	=> \$o_privpass,	'privpass:s'	=> \$o_privpass,
		'L:s'	=> \$v3protocols,	'protocols:s'	=> \$v3protocols,   
        't:i'   => \$o_timeout,    	'timeout:i'		=> \$o_timeout,
		'V'		=> \$o_version,		'version'		=> \$o_version,
        'w:s'   => \$o_warn_opt,    'warning:s'   	=> \$o_warn_opt,
        'c:s'   => \$o_crit_opt,    'critical:s'   	=> \$o_crit_opt
    );
    if (defined($o_help)) { help(); exit $ERRORS{"UNKNOWN"}};
    if (defined($o_version)) { p_version(); exit $ERRORS{"UNKNOWN"}};
    if ( ! defined($o_mode) ||  ! defined($o_host) ) # check host and filter 
	{ print_usage(); exit $ERRORS{"UNKNOWN"}}
    #### check snmp information
    if ( !defined($o_community) && (!defined($o_login) || !defined($o_passwd)) )
		{ print "Put snmp login info!\n"; print_usage(); exit $ERRORS{"UNKNOWN"}}
	if ((defined($o_login) || defined($o_passwd)) && (defined($o_community) || defined($o_version2)) )
		{ print "Can't mix snmp v1,2c,3 protocols!\n"; print_usage(); exit $ERRORS{"UNKNOWN"}}
	if (defined ($v3protocols)) {
		if (!defined($o_login)) { print "Put snmp V3 login info with protocols!\n"; print_usage(); exit $ERRORS{"UNKNOWN"}}
		my @v3proto=split(/,/,$v3protocols);
		if ((defined ($v3proto[0])) && ($v3proto[0] ne "")) {$o_authproto=$v3proto[0];	}	# Auth protocol
		if (defined ($v3proto[1])) {$o_privproto=$v3proto[1];	}	# Priv  protocol
		if ((defined ($v3proto[1])) && (!defined($o_privpass))) {
			print "Put snmp V3 priv login info with priv protocols!\n"; print_usage(); exit $ERRORS{"UNKNOWN"}}
	}
	if (defined($o_timeout) && (isnnum($o_timeout) || ($o_timeout < 2) || ($o_timeout > 60))) 
	  { print "Timeout must be >1 and <60 !\n"; print_usage(); exit $ERRORS{"UNKNOWN"}}
	if (!defined($o_timeout)) {$o_timeout=5;}
}

sub verb { my $t=shift; print $t,"\n" if defined($o_verb) ; }

sub registered {
	my @oids = "1.3.6.1.4.1.9.9.644.1.2.6.1.4.1";
	my @results = undef;
	$result = $session->get_request(@oids);
	@results = $result->{"1.3.6.1.4.1.9.9.644.1.2.6.1.4.1"};

	if (!defined($result)) { printf("ERROR: %s.\n", $session->error); $session->close;
		exit $ERRORS{"UNKNOWN"};
		}

	$session->close;
	alarm(0);

	if (@results = 5) {
		print "OK: Device is registered \n";
		exit $ERRORS{"OK"};
		} else {
		print "CRITICAL: Device is not registered(@results) \n";
		exit $ERRORS{"CRITICAL"};
		}
}

sub lastcall {
        my $lastdialed = undef;
		my $output = "";
        my $oid_lastdialed = "1.3.6.1.4.1.9.9.644.1.4.8.1.2.";
		my $i = 0;
		my $index = undef;
		my $oid = undef;

		while ($i < 5) {
			$index = ($lastindex - $i);
			$oid = $oid_lastdialed.$index;
			$result = $session->get_request($oid);
			$lastdialed = $result->{$oid};
			if (!defined($lastdialed)) { printf("ERROR: %s.\n", $session->error); $session->close;
                $session->close;
                exit $ERRORS{"UNKNOWN"};
			}
			$output = $output . " " . $lastdialed;
			$i++;
			$lastdialed = undef;
		}

        return "Last 5 Numbers Dialed: $output";
}

sub state {
	my $i = $lastindex;
	my @states = ('unknown','unknown(1)','other(2)','noMgmtSysConn(3)','noDialTone(4)','invalidNumber(5)','ringing(6)','noAnswer(7)','inProgress(8)','remoteHold(9)','shareLineActive(10)','inLocalConference(11)','terminatedbyError(12)','localHold(13)','terminatedNormally(14)','answer(15)','resume(16)','busy(17)','pause(18)','playback(19)','recording(20)');
	if ($oldest_current) { # Check all connections from oldest_current to lastindex
		while ($i >= $oldest_current) {
			if (( $callstates[$lastindex]  == 3 ) || ( $callstates[$lastindex] == 4 ) || ( $callstates[$lastindex] == 12 )) {
				return ("CRITICAL: A call state is $states[$callstates[$i]]",2);
			}
			$i--;
		}
	} else { # Check lastindex
		if (( $callstates[$lastindex]  == 3 ) || ( $callstates[$lastindex] == 4 ) || ( $callstates[$lastindex] == 12 )) {
			return ("CRITICAL: Call State is $states[$callstates[$lastindex]]", 2);
		}
	}
	return ("OK: All current connections are OK",0);
}

sub duration {
    my $duration = undef;
	my $oid_duration = undef;
	my @duration;
	my $i = 0;
	my $output = "";
	
	if (@active_calls) {
	foreach (@active_calls) {
		$oid_duration = "1.3.6.1.4.1.9.9.644.1.4.8.1.7.".$_;
		$result = $session->get_request($oid_duration);
		push(@duration,$result->{$oid_duration});
		if (!defined($duration[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
			exit $ERRORS{"UNKNOWN"};
		}
		$i++;
	}
	} else { # Exit no active calls
		print "No currently active calls.\n";
		exit $ERRORS{"OK"};
	}
    $session->close;
    alarm(0);

	$i = 0;
	while ($i < @duration) {
		$output = $output . " Call$i=$duration[$i]";
		$i++;
	}
	
    print "Call Duration(s): $output | $output\n";
    exit $ERRORS{"OK"};
}

sub type {      # : Current call type
	my @types = ('blank','audioVideo(1)','audioOnly(2)','unknown(3)');
	my $i = undef;
	my $output = "";

	$i = 0;
	while ($i < @calltypes) {
		$output = $output . " Call$i=$types[$calltypes[$i]]";
		$i++;
	}
    return "Call Type(s): $output";
}

sub bitrate {   # : compares initial to current bitrate
	if (@active_calls == 0) {
		print "No currently active calls.\n";
		exit $ERRORS{"OK"};
	}
	if ( $audioonly == 0) {
		print "OK: Audio only call!\n";
                exit $ERRORS{"OK"};
                }
	if ((!defined($o_warn_opt)) || (!defined($o_crit_opt))) {
		print "UNKNOWN: -w and/or -c values not defined!\n";
		exit $ERRORS{"UNKNOWN"};
		}
	if ( $o_warn_opt < $o_crit_opt ) {
                print "UNKNOWN: Critical needs to be less than warning!\n";
		exit $ERRORS{"UNKNOWN"};
                }

	my @orig;
	my @current;
	my $oid1 = "1.3.6.1.4.1.9.9.644.1.4.8.1.12.";
	my $oid2 = "1.3.6.1.4.1.9.9.644.1.4.8.1.13.";
	my @percent;
	my $i = 0;
	my $y = 0;
	my $x = undef;
	my $crit = 0;
	my $warn = 0;
	my $output = "";
	
	foreach (@active_calls) {
		if ($calltypes[$i] == 1) {
			$result = $session->get_request($oid1.$_);
	        	push(@orig, $result->{$oid1.$_});
			if (!defined($orig[$y])) { printf("ERROR: %s.\n", $session->error); $session->close;
        		    $session->close;
		            exit $ERRORS{"UNKNOWN"};
			}
		        $result = $session->get_request($oid2.$_);
		        push(@current, $result->{$oid2.$_});
			if (!defined($current[$y])) { printf("ERROR: %s.\n", $session->error); $session->close;
		            $session->close;
		            exit $ERRORS{"UNKNOWN"};
			}
			$x = int($current[$y]/$orig[$y]*100);
			push(@percent, $x);
			if (!defined($percent[$y])) { printf("ERROR: %s.\n", $session->error); $session->close;
		            $session->close;
		            exit $ERRORS{"UNKNOWN"};
			}
			$y++;
			}
		$i++;		
	}
    $session->close;
    alarm(0);
	
	$i=0;
	foreach (@percent) {
		if ( $_ < $o_crit_opt ) {
			$crit = 1;
		} elsif ($percent[$i] < $o_warn_opt) {
			$warn = 1;
		}
		$output = $output . " bitrate$i=$percent[$i]%";
		$i++;
	}
	
	if ($crit == 1) {
		print "CRITICAL: A connections bitrate has fallen - $output | $output\n";
		exit $ERRORS{"CRITICAL"};
	}
	if ($warn == 1) {
	    print "WARNING: A connections bitrate has fallen - $output | $output%\n";
		exit $ERRORS{"WARNING"};
	}
	print "Bitrate(s) OK:$output |$output\n";
	exit $ERRORS{"OK"}; 
}

sub end { # : What type of endpoint is it connected to
	if (@active_calls == 0) {
		print "No currently active calls.\n";
		exit $ERRORS{"OK"};
	}
	my @endtypes = ('blank','{unknown(1)','other(2)','audioDevice(3)','videoLegacyDevice(4)','highDefinitionLegacyDevice(5)','singleTelepresence(6)','tripleTelepresence(7)','telepresenceMultipointSwitch(8)','telepresenceRecordingServer(9)','telepresenceTranscodingDevice(10)');

    my @ends;
    my $oids = "1.3.6.1.4.1.9.9.644.1.4.8.1.16.";
	my $output = "";
	my $i = 0;
	
	foreach (@active_calls) {
        $result = $session->get_request($oids.$_);
        push(@ends, $result->{$oids.$_});
		if (!defined($ends[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$i++;
	}
	
	$i = 0;
	while ($i < @ends) {
		if ($i == 0) {
			$output = "Call$i=$endtypes[$ends[$i]]";
		} else {
			$output = $output . " Call$i=$endtypes[$ends[$i]]";
		}
		$i++;
	}
    return "Connected to: $output";
}

sub jitter {    # : check call jitter
	if (@active_calls == 0) {
		print "No currently active calls.\n";
		exit $ERRORS{"OK"};
	}
	if ( $audioonly == 0) {
		print "OK: Audio only call!\n";
        exit $ERRORS{"OK"};
    }
	if ((!defined($o_warn_opt)) || (!defined($o_crit_opt))) {
		print "UNKNOWN: -w and/or -c values not defined!\n";
		exit $ERRORS{"UNKNOWN"};
		}
	if ( $o_warn_opt > $o_crit_opt ) {
        print "UNKNOWN: Critical needs to be greater than warning!\n";
		exit $ERRORS{"UNKNOWN"};
    }

    my $oid1 = undef;
    my $oid2 = undef;
    my $oid3 = undef;
    my $oid4 = undef;
    my $oid5 = undef;
    my $oid6 = undef;
	my @vj1;
	my @vj2;
	my @vj3;
	my @aj1;
	my @aj2;
	my @aj3;
	my $result = undef;
	my $i = 0;
	my $x = 0;
	my $warn = 0;
	my $crit = 0;
	my $output = "";
	
	foreach (@active_calls) {
		if ($calltypes[$x] == 1) {
		$oid1 = "1.3.6.1.4.1.9.9.644.1.4.10.1.23.".$_.".1.1";
		$oid2 = "1.3.6.1.4.1.9.9.644.1.4.10.1.23.".$_.".1.2";
		$oid3 = "1.3.6.1.4.1.9.9.644.1.4.10.1.23.".$_.".1.3";
		$oid4 = "1.3.6.1.4.1.9.9.644.1.4.10.1.23.".$_.".2.1";
		$oid5 = "1.3.6.1.4.1.9.9.644.1.4.10.1.23.".$_.".2.2";
		$oid6 = "1.3.6.1.4.1.9.9.644.1.4.10.1.23.".$_.".2.3";
		$result = $session->get_request($oid1);
		push(@vj1,$result->{$oid1});
		if (!defined($vj1[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid2);
		push(@vj2,$result->{$oid2});
		if (!defined($vj2[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid3);
		push(@vj3,$result->{$oid3});
		if (!defined($vj3[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid4);
		push(@aj1,$result->{$oid4});
		if (!defined($aj1[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid5);
		push(@aj2,$result->{$oid5});
		if (!defined($aj2[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid6);
		push(@aj3,$result->{$oid6});
		if (!defined($aj3[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$i++;
		}
		$x++;		
	}

    $session->close;
    alarm(0);
	$i=0;
	$x=0;
	while ($x < @active_calls) {
		if ($calltypes[$x] == 1) {		
		if (($vj1[$i] > $o_crit_opt) || ($vj2[$i] > $o_crit_opt) || ($vj3[$i] > $o_crit_opt) || ($aj1[$i] > $o_crit_opt) || ($aj2[$i] > $o_crit_opt) || ($aj3[$i] > $o_crit_opt)) {
			$crit = 1;
		} elsif (($vj1[$i] > $o_warn_opt) || ($vj2[$i] > $o_warn_opt) || ($vj3[$i] > $o_warn_opt) || ($aj1[$i] > $o_warn_opt) || ($aj2[$i] > $o_warn_opt) || ($aj3[$i] > $o_warn_opt)) {
			$warn = 1;
		}
		$output = $output . " Video1-$i=$vj1[$i] Video2-$i=$vj2[$i] Video3-$i=$vj3[$i] Audio1-$i=$aj1[$i] Audio1-$i=$aj2[$i] Audio1-$i=$aj3[$i]";
		$i++;
		}
		$x++;
	}

    if ($crit == 1) {
        print "CRITICAL: Jitter high! $output |$output\n";
        exit $ERRORS{"CRITICAL"};
    } elsif ($warn == 1) {
        print "WARNING: Jitter high! $output |$output\n";
        exit $ERRORS{"WARNING"};
    } else {
        print "OK: Jitter normal! $output |$output\n";
        exit $ERRORS{"OK"};
    }
}

sub rxpix {     # : Show rx horizontal pixels
	if (@active_calls == 0) {
		print "No currently active calls.\n";
		exit $ERRORS{"OK"};
	}
	if ( $audioonly == 0) {
		print "OK: Audio only call!\n";
        exit $ERRORS{"OK"};
    }

    my $oid1 = undef;
    my $oid2 = undef;
    my $oid3 = undef;
	my $output = "";
    my @rxvertpix1;
	my @rxvertpix2;
	my @rxvertpix3;
    my $result = undef;
    my $i = 0;
    my $x = 0;	
	
	foreach(@active_calls) {
		if ($calltypes[$x] == 1) {
		$oid1 = "1.3.6.1.4.1.9.9.644.1.4.10.1.33.".$_.".1.1";
		$oid2 = "1.3.6.1.4.1.9.9.644.1.4.10.1.33.".$_.".1.2";
		$oid3 = "1.3.6.1.4.1.9.9.644.1.4.10.1.33.".$_.".1.3";

		$result = $session->get_request($oid1);
		push(@rxvertpix1,$result->{$oid1});
		if (!defined($rxvertpix1[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid2);
		push(@rxvertpix2,$result->{$oid2});
		if (!defined($rxvertpix2[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid3);
		push(@rxvertpix3,$result->{$oid3});
		if (!defined($rxvertpix3[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$output = $output . " rxvertpix1-$i=$rxvertpix1[$i] rxvertpix2-$i=$rxvertpix2[$i] rxvertpix3-$i=$rxvertpix3[$i]";
		$i++;
		}
		$x++;
	}

    $session->close;
    alarm(0);

    print "$output |$output\n";
	exit $ERRORS{"OK"};
}

sub anomolies { # : Show any Out of Order, Late or Duplicate packets
	if (@active_calls == 0) {
		print "No currently active calls.\n";
		exit $ERRORS{"OK"};
	}
	if ( $audioonly == 0) {
		print "OK: Audio only call!\n";
        exit $ERRORS{"OK"};
    }
	if ((!defined($o_warn_opt)) || (!defined($o_crit_opt))) {
		print "UNKNOWN: -w and/or -c values not defined!\n";
		exit $ERRORS{"UNKNOWN"};
		}
	if ( $o_warn_opt > $o_crit_opt ) {
        print "UNKNOWN: Critical needs to be greater than warning!\n";
		exit $ERRORS{"UNKNOWN"};
    }

    my $oid1 = undef;
    my $oid2 = undef;
	my $oid3 = undef;
    my $oid4 = undef;
    my $oid5 = undef;
    my $oid6 = undef;
    my $oid7 = undef;
    my $oid8 = undef;
    my $oid9 = undef;
	my $output = "";
	my $result = undef;
	my @ooo1;
	my @ooo2;
	my @ooo3;
	my @late1;
	my @late2;
	my @late3;
	my @dupe1;
	my @dupe2;
	my @dupe3;
	my $i = 0;
	my $x = 0;
	my $crit = 0;
	my $warn = 0;
	my @oooper1;
	my @lateper1;
	my @dupeper1;
    my @oooper2;
    my @lateper2;
    my @dupeper2;
    my @oooper3;
    my @lateper3;
    my @dupeper3;
		
	foreach(@active_calls) {
	    if ($calltypes[$x] == 1) {
		$oid1 = "1.3.6.1.4.1.9.9.644.1.4.10.1.16.".$_.".1.1";
		$oid2 = "1.3.6.1.4.1.9.9.644.1.4.10.1.16.".$_.".1.2";
		$oid3 = "1.3.6.1.4.1.9.9.644.1.4.10.1.16.".$_.".1.3";
		$oid4 = "1.3.6.1.4.1.9.9.644.1.4.10.1.17.".$_.".1.1";
		$oid5 = "1.3.6.1.4.1.9.9.644.1.4.10.1.17.".$_.".1.2";
		$oid6 = "1.3.6.1.4.1.9.9.644.1.4.10.1.17.".$_.".1.3";
		$oid7 = "1.3.6.1.4.1.9.9.644.1.4.10.1.18.".$_.".1.1";
		$oid8 = "1.3.6.1.4.1.9.9.644.1.4.10.1.18.".$_.".1.2";
		$oid9 = "1.3.6.1.4.1.9.9.644.1.4.10.1.18.".$_.".1.3";
	
		$result = $session->get_request($oid1);
		push(@ooo1,$result->{$oid1});
		if (!defined($ooo1[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}		
		$result = $session->get_request($oid2);
		push(@ooo2,$result->{$oid2});
		if (!defined($ooo2[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid3);
		push(@ooo3,$result->{$oid3});
		if (!defined($ooo3[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid4);
		push(@late1,$result->{$oid4});
		if (!defined($late1[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid5);
		push(@late2,$result->{$oid5});
		if (!defined($late2[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid6);
		push(@late3,$result->{$oid6});
		if (!defined($late3[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid7);
		push(@dupe1,$result->{$oid7});
		if (!defined($dupe1[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid8);
		push(@dupe2,$result->{$oid8});
		if (!defined($dupe2[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid9);
		push(@dupe3,$result->{$oid9});
		if (!defined($dupe3[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		
		push(@oooper1,sprintf("%.2f", ($ooo1[$i]/$totalrx1[$x]*100)));
		push(@lateper1,sprintf("%.2f", ($late1[$i]/$totalrx1[$x]*100)));
		push(@dupeper1,sprintf("%.2f", ($dupe1[$i]/$totalrx1[$x]*100)));
		push(@oooper2,sprintf("%.2f", ($ooo2[$i]/$totalrx2[$x]*100)));
		push(@lateper2,sprintf("%.2f", ($late2[$i]/$totalrx2[$x]*100)));
		push(@dupeper2,sprintf("%.2f", ($dupe2[$i]/$totalrx2[$x]*100)));
		push(@oooper3,sprintf("%.2f", ($ooo3[$i]/$totalrx3[$x]*100)));
		push(@lateper3,sprintf("%.2f", ($late3[$i]/$totalrx3[$x]*100)));
		push(@dupeper3,sprintf("%.2f", ($dupe3[$i]/$totalrx3[$x]*100)));

		$oooper1[$i] =~ s/^(\d+\.\d+)\z/ sprintf '%.2f', $1/e;
		$oooper2[$i] =~ s/^(\d+\.\d+)\z/ sprintf '%.2f', $1/e;
		$oooper3[$i] =~ s/^(\d+\.\d+)\z/ sprintf '%.2f', $1/e;
		$lateper1[$i] =~ s/^(\d+\.\d+)\z/ sprintf '%.2f', $1/e;
		$lateper2[$i] =~ s/^(\d+\.\d+)\z/ sprintf '%.2f', $1/e;
		$lateper3[$i] =~ s/^(\d+\.\d+)\z/ sprintf '%.2f', $1/e;
		$dupeper1[$i] =~ s/^(\d+\.\d+)\z/ sprintf '%.2f', $1/e;
		$dupeper2[$i] =~ s/^(\d+\.\d+)\z/ sprintf '%.2f', $1/e;
		$dupeper3[$i] =~ s/^(\d+\.\d+)\z/ sprintf '%.2f', $1/e;		

		if (( $oooper1[$i] > $o_crit_opt ) || ( $oooper2[$i] > $o_crit_opt ) || ( $oooper3[$i] > $o_crit_opt ) || ( $lateper1[$i] > $o_crit_opt ) || ( $lateper2[$i] > $o_crit_opt ) || ( $lateper3[$i] > $o_crit_opt ) || ( $dupeper1[$i] > $o_crit_opt ) || ( $dupeper2[$i] > $o_crit_opt ) || ( $dupeper3[$i] > $o_crit_opt )) {
			$crit = 1;
		}		
		if (( $oooper1[$i] > $o_warn_opt ) || ( $oooper2[$i] > $o_warn_opt ) || ( $oooper3[$i] > $o_warn_opt ) || ( $lateper1[$i] > $o_warn_opt ) || ( $lateper2[$i] > $o_warn_opt ) || ( $lateper3[$i] > $o_warn_opt ) || ( $dupeper1[$i] > $o_warn_opt ) || ( $dupeper2[$i] > $o_warn_opt ) || ( $dupeper3[$i] > $o_warn_opt )) {
			$warn = 1;
		}
		$output = $output . " oooper1-$i=$oooper1[$i]% oooper2-$i=$oooper2[$i]% oooper3-$i=$oooper3[$i]% lateper1-$i=$lateper1[$i]% lateper2-$i=$lateper2[$i]% lateper3-$i=$lateper3[$i]% dupeper1-$i=$dupeper1[$i]% dupeper2-$i=$dupeper2[$i]% dupeper3-$i=$dupeper3[$i]%\n";
		$i++;
		}
		$x++;
	}
	
    $session->close;
    alarm(0);
	
    if ($crit == 1) {
        print "CRITICAL: Packet anomolie(s) exist! |$output";
        exit $ERRORS{"CRITICAL"};
    } elsif ($warn == 1) {
        print "WARNING: Packet anomolie(s) exist! |$output";
        exit $ERRORS{"WARNING"};
    } else {
        print "OK: No packet anomolie(s) exist! |$output";
        exit $ERRORS{"OK"};
    }
}

sub ploss { 
	if (@active_calls == 0) {
		print "No currently active calls.\n";
		exit $ERRORS{"OK"};
	}
	if ( $audioonly == 0) {
		print "OK: Audio only call!\n";
        exit $ERRORS{"OK"};
    }
	if ((!defined($o_warn_opt)) || (!defined($o_crit_opt))) {
		print "UNKNOWN: -w and/or -c values not defined!\n";
		exit $ERRORS{"UNKNOWN"};
		}
    if ( $o_warn_opt > $o_crit_opt ) {
        print "UNKNOWN: Critical needs to be less than warning!\n";
        exit $ERRORS{"UNKNOWN"};
        }

	my $oid1 = undef;
    my $oid2 = undef;
    my $oid3 = undef;
    my $oid4 = undef;
    my $oid5 = undef;
    my $oid6 = undef;
    my $result = undef;
	my $i = 0;
	my $x = 0;
	my @pl1;
	my @pl2;
	my @pl3;
	my @pl4;
	my @pl5;
	my @pl6;
	my @plper1;
	my @plper2;
	my @plper3;
	my @plper4;
	my @plper5;
	my @plper6;
	my $crit = 0;
	my $warn = 0;
	my $output = "";

	foreach(@active_calls) {
		if ($calltypes[$x] == 1) {
		$oid1 = "1.3.6.1.4.1.9.9.644.1.4.10.1.5.".$_.".1.1";
		$oid2 = "1.3.6.1.4.1.9.9.644.1.4.10.1.5.".$_.".1.2";
		$oid3 = "1.3.6.1.4.1.9.9.644.1.4.10.1.5.".$_.".1.3";
		$oid4 = "1.3.6.1.4.1.9.9.644.1.4.10.1.13.".$_.".1.1";
		$oid5 = "1.3.6.1.4.1.9.9.644.1.4.10.1.13.".$_.".1.2";
		$oid6 = "1.3.6.1.4.1.9.9.644.1.4.10.1.13.".$_.".1.3";

		$result = $session->get_request($oid1);
		push(@pl1,$result->{$oid1});
		if (!defined($pl1[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid2);
		push(@pl2,$result->{$oid2});
		if (!defined($pl2[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid3);
		push(@pl3,$result->{$oid3});
		if (!defined($pl3[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid4);
		push(@pl4,$result->{$oid4});
		if (!defined($pl4[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid5);
		push(@pl5,$result->{$oid5});
		if (!defined($pl5[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid6);
		push(@pl6,$result->{$oid6});
		if (!defined($pl6[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		
	    	$plper1[$i] = sprintf("%.2f", ($pl1[$i]/$totaltx1[$x]*100));
		$plper2[$i] = sprintf("%.2f", ($pl2[$i]/$totaltx2[$x]*100));
		$plper3[$i] = sprintf("%.2f", ($pl3[$i]/$totaltx3[$x]*100));
		$plper4[$i] = sprintf("%.2f", ($pl4[$i]/$totalrx1[$x]*100));
		$plper5[$i] = sprintf("%.2f", ($pl5[$i]/$totalrx2[$x]*100));
		$plper6[$i] = sprintf("%.2f", ($pl6[$i]/$totalrx3[$x]*100));

		$plper1[$i] =~ s/^(\d+\.\d+)\z/ sprintf '%.2f', $1/e;
		$plper2[$i] =~ s/^(\d+\.\d+)\z/ sprintf '%.2f', $1/e;
		$plper3[$i] =~ s/^(\d+\.\d+)\z/ sprintf '%.2f', $1/e;
		$plper4[$i] =~ s/^(\d+\.\d+)\z/ sprintf '%.2f', $1/e;
		$plper5[$i] =~ s/^(\d+\.\d+)\z/ sprintf '%.2f', $1/e;
		$plper6[$i] =~ s/^(\d+\.\d+)\z/ sprintf '%.2f', $1/e;

		if (( $plper1[$i] > $o_crit_opt ) || ( $plper2[$i] > $o_crit_opt ) || ( $plper3[$i] > $o_crit_opt ) || ( $plper4[$i] > $o_crit_opt ) || ( $plper5[$i] > $o_crit_opt ) || ( $plper6[$i] > $o_crit_opt )) {
			$crit = 1;
		} elsif (( $plper1[$i] > $o_warn_opt ) || ( $plper2[$i] > $o_warn_opt ) || ( $plper3[$i] > $o_warn_opt ) || ( $plper4[$i] > $o_warn_opt ) || ( $plper5[$i] > $o_warn_opt ) || ( $plper6[$i] > $o_warn_opt )) {
			$warn = 1;
		}
		$output = $output . " plper1[$i]=$plper1[$i] plper2[$i]=$plper2[$i] plper3[$i]=$plper3[$i] plper4[$i]=$plper4[$i] plper5[$i]=$plper5[$i] plper6[$i]=$plper6[$i]";
		$i++;
		}
		$x++;
}
	
    $session->close;
    alarm(0);

    if ($crit == 1) {
        print "CRITICAL: Packet Loss exists! $output |$output\n";
        exit $ERRORS{"CRITICAL"};
    } elsif ($warn == 1) {
        print "WARNING: Packet Loss exists! $output |$output\n";
        exit $ERRORS{"WARNING"};
    } else {
        print "OK: No Packet Loss exists! $output |$output\n";
        exit $ERRORS{"OK"};
    }
}

sub codec { 
	if (@active_calls == 0) {
		print "No currently active calls.\n";
		exit $ERRORS{"OK"};
	}
	if ( $audioonly == 0) {
		print "OK: Audio only call!\n";
        exit $ERRORS{"OK"};
    }

    my @codecs = ('N/A','unknown(1)','other(2)','aaclc(3)','aacld(4)','g711A(5)','g711U(6)','g722(7)','g7221(8)','g728(9)','g729(10)','h263(11)','h264(12)','aacldLatm(13)');
    my $txoid1 = undef;
    my $txoid2 = undef;
    my $txoid3 = undef;
    my $txoid4 = undef;
    my $txoid5 = undef;
    my $txoid6 = undef;
    my $rxoid1 = undef;
    my $rxoid2 = undef;
    my $rxoid3 = undef;
    my $rxoid4 = undef;
    my $rxoid5 = undef;
    my $rxoid6 = undef;
    my @txc1;
	my @txc2;
	my @txc3;
	my @txc4;
	my @txc5;
	my @txc6;
	my @rxc1;
	my @rxc2;
	my @rxc3;
	my @rxc4;
	my @rxc5;
	my @rxc6;
    my $result = undef;
	my $i = 0;
	my $x = 0;
	my $output = "";

	foreach(@active_calls) {
		if ($calltypes[$x] == 1) {
		$txoid1 = "1.3.6.1.4.1.9.9.644.1.4.10.1.27.".$_.".1.1";
		$txoid2 = "1.3.6.1.4.1.9.9.644.1.4.10.1.27.".$_.".1.2";
		$txoid3 = "1.3.6.1.4.1.9.9.644.1.4.10.1.27.".$_.".1.3";
		$txoid4 = "1.3.6.1.4.1.9.9.644.1.4.10.1.27.".$_.".2.1";
		$txoid5 = "1.3.6.1.4.1.9.9.644.1.4.10.1.27.".$_.".2.2";
		$txoid6 = "1.3.6.1.4.1.9.9.644.1.4.10.1.27.".$_.".2.3";
		$rxoid1 = "1.3.6.1.4.1.9.9.644.1.4.10.1.29.".$_.".1.1";
		$rxoid2 = "1.3.6.1.4.1.9.9.644.1.4.10.1.29.".$_.".1.2";
		$rxoid3 = "1.3.6.1.4.1.9.9.644.1.4.10.1.29.".$_.".1.3";
		$rxoid4 = "1.3.6.1.4.1.9.9.644.1.4.10.1.29.".$_.".2.1";
		$rxoid5 = "1.3.6.1.4.1.9.9.644.1.4.10.1.29.".$_.".2.2";
		$rxoid6 = "1.3.6.1.4.1.9.9.644.1.4.10.1.29.".$_.".2.3";

		$result = $session->get_request($txoid1);
		push(@txc1,$result->{$txoid1});
		if (!defined($txc1[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}		
		$result = $session->get_request($txoid2);
		push(@txc2,$result->{$txoid2});
		if (!defined($txc2[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($txoid3);
		push(@txc3,$result->{$txoid3});
		if (!defined($txc3[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($txoid4);
		push(@txc4,$result->{$txoid4});
		if (!defined($txc4[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($txoid5);
		push(@txc5,$result->{$txoid5});
		if (!defined($txc5[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($txoid6);
		push(@txc6,$result->{$txoid6});
		if (!defined($txc6[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($rxoid1);
		push(@rxc1,$result->{$rxoid1});
		if (!defined($rxc1[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($rxoid2);
		push(@rxc2,$result->{$rxoid2});
		if (!defined($rxc2[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}		
		$result = $session->get_request($rxoid3);
		push(@rxc3,$result->{$rxoid3});
		if (!defined($rxc3[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}		
		$result = $session->get_request($rxoid4);
		push(@rxc4,$result->{$rxoid4});
		if (!defined($rxc4[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}		
		$result = $session->get_request($rxoid5);
		push(@rxc5,$result->{$rxoid5});
		if (!defined($rxc5[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}		
		$result = $session->get_request($rxoid6);
		push(@rxc6,$result->{$rxoid6});
		if (!defined($rxc6[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		
		$output = $output . " RXVCodec1-$i=$codecs[$rxc1[$i]], RXVCodec2-$i=$codecs[$rxc2[$i]], RXVCodec3-$i=$codecs[$rxc3[$i]], RXACodec1-$i=$codecs[$rxc4[$i]], RXACodec2-$i=$codecs[$rxc5[$i]], RXACodec3-$i=$codecs[$rxc6[$i]], TXVCodec1-$i=$codecs[$txc1[$i]], TXVCodec2-$i=$codecs[$txc2[$i]], TXVCodec3-$i=$codecs[$txc3[$i]], TXACodec1-$i=$codecs[$txc4[$i]], TXACodec2-$i=$codecs[$txc5[$i]], TXACodec3-$i=$codecs[$txc6[$i]]";
		$i++;
		}
		$x++;
	}
    
	$session->close;
    alarm(0);
    
    print "$output\n";
    exit $ERRORS{"OK"};
}

sub fps { 
	if (@active_calls == 0) {
		print "No currently active calls.\n";
		exit $ERRORS{"OK"};
	}
	if ( $audioonly == 0) {
		print "OK: Audio only call!\n";
        exit $ERRORS{"OK"};
    }
	if ((!defined($o_warn_opt)) || (!defined($o_crit_opt))) {
		print "UNKNOWN: -w and/or -c values not defined!\n";
		exit $ERRORS{"UNKNOWN"};
		}
    if ( $o_warn_opt < $o_crit_opt ) {
        print "UNKNOWN: Critical needs to be less than warning!\n";
        exit $ERRORS{"UNKNOWN"};
        }

    my $oid1 = undef;
    my $oid2 = undef;
    my $oid3 = undef;
    my $oid4 = undef;
    my $oid5 = undef;
    my $oid6 = undef;
    my @fps1;
	my @fps2;
	my @fps3;
	my @fps4;
	my @fps5;
	my @fps6;
	my $result = undef;
	my $i = 0;
	my $x = 0;
	my $output = "";
    my $crit = 0;
    my $warn = 0;
	
	foreach(@active_calls) {
		if ($calltypes[$x] == 1) {
		my $oid1 = "1.3.6.1.4.1.9.9.644.1.4.10.1.28.".$_.".1.1";
		my $oid2 = "1.3.6.1.4.1.9.9.644.1.4.10.1.28.".$_.".1.2";
		my $oid3 = "1.3.6.1.4.1.9.9.644.1.4.10.1.28.".$_.".1.3";
		my $oid4 = "1.3.6.1.4.1.9.9.644.1.4.10.1.30.".$_.".1.1";
		my $oid5 = "1.3.6.1.4.1.9.9.644.1.4.10.1.30.".$_.".1.2";
		my $oid6 = "1.3.6.1.4.1.9.9.644.1.4.10.1.30.".$_.".1.3";
    
		$result = $session->get_request($oid1);
		push(@fps1,$result->{$oid1});
		if (!defined($fps1[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid2);
		push(@fps2,$result->{$oid2});
		if (!defined($fps2[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid3);
		push(@fps3,$result->{$oid3});
		if (!defined($fps3[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid4);
		push(@fps4,$result->{$oid4});
		if (!defined($fps4[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		$result = $session->get_request($oid5);
		push(@fps5,$result->{$oid5});
		if (!defined($fps5[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}$result = $session->get_request($oid6);
		push(@fps6,$result->{$oid6});
		if (!defined($fps6[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
		
        if (( $fps1[$i] < $o_crit_opt ) && ( $fps1[$i] > 0 ) || ( $fps2[$i] < $o_crit_opt ) && ( $fps2[$i] > 0 ) || ( $fps3[$i] < $o_crit_opt ) && ( $fps3[$i] > 0 ) || ( $fps4[$i] < $o_crit_opt ) && ( $fps4[$i] > 0 ) || ( $fps1[$i] < $o_crit_opt ) && ( $fps5[$i] > 0 ) || ( $fps6[$i] < $o_crit_opt ) && ( $fps6[$i] > 0 )) {
            $crit = 1;
            } elsif (( $fps1[$i] < $o_warn_opt ) && ( $fps1[$i] > 0 ) || ( $fps2[$i] < $o_warn_opt ) && ( $fps2[$i] > 0 ) || ( $fps3[$i] < $o_warn_opt ) && ( $fps3[$i] > 0 ) || ( $fps4[$i] < $o_warn_opt ) && ( $fps4[$i] > 0 ) || ( $fps5[$i] < $o_warn_opt ) && ( $fps5[$i] > 0 ) || ( $fps6[$i] < $o_warn_opt ) && ( $fps6[$i] > 0 )) {
                $warn = 1;
                }
		$output = $output . " TXsecCodec1=$fps1[$i] TXpriCodec=$fps2[$i] TXsecCodec2=$fps3[$i] RXsecCodec1=$fps4[$i] RXpriCodec=$fps5[$i] RXsecCodec2=$fps6[$i]";
		$i++;
		}
		$x++;
	}
		
    $session->close;
    alarm(0);

	my $output3 = $output . " |$output";
    my $output2 = undef;
    if ( $crit == 1 ) {
        $output2 = "CRITICAL: ".$output3 . "\n";
        print $output2;
        exit $ERRORS{"CRITICAL"};
        } elsif ( $warn ==1 ) {
            $output2 = "WARNING: ".$output3 . "\n";
            print $output2;
            exit $ERRORS{"WARNING"};
            }
    $output2 = "OK: ".$output3 . "\n";
    print $output2;
    exit $ERRORS{"OK"};
}

############################################
##############   MAIN   ####################
############################################
check_options();

# Check gobal timeout if snmp screws up
if (defined($TIMEOUT)) {
	verb("Alarm at $TIMEOUT + 5");
	alarm($TIMEOUT+5);
} else {
	verb("no timeout defined : $o_timeout + 10");
	alarm ($o_timeout+10);
}

$SIG{'ALRM'} = sub {
	print "No answer from host\n";
	exit $ERRORS{"UNKNOWN"};
};

# Connect to host
my $error;
if ( defined($o_login) && defined($o_passwd)) {
	#### SNMPv3 login
    if (!defined ($o_privpass)) {
    verb("SNMPv3 AuthNoPriv login : $o_login, $o_authproto");
      ($session, $error) = Net::SNMP->session(
        -hostname   	=> $o_host,
        -version		=> '3',
        -port      	=> $o_port,
        -username		=> $o_login,
        -authpassword	=> $o_passwd,
        -authprotocol	=> $o_authproto,
        -timeout          => $o_timeout
      );  
    } else {
      verb("SNMPv3 AuthPriv login : $o_login, $o_authproto, $o_privproto");
      ($session, $error) = Net::SNMP->session(
        -hostname   	=> $o_host,
        -version		=> '3',
        -username		=> $o_login,
        -port      	=> $o_port,
        -authpassword	=> $o_passwd,
        -authprotocol	=> $o_authproto,
        -privpassword	=> $o_privpass,
	    -privprotocol => $o_privproto,
        -timeout          => $o_timeout
      );
    }
} else {
    if (defined ($o_version2)) {
      # SNMPv2c Login
	  verb("SNMP v2c login");
	  ($session, $error) = Net::SNMP->session(
        -hostname  => $o_host,
	    -version   => 2,
        -community => $o_community,
        -port      => $o_port,
        -timeout   => $o_timeout
      );
    } else {
      # SNMPV1 login
	  verb("SNMP v1 login");
      ($session, $error) = Net::SNMP->session(
        -hostname  => $o_host,
        -community => $o_community,
        -port      => $o_port,
        -timeout   => $o_timeout
      );
    }
}
if (!defined($session)) {
   printf("ERROR opening session: %s.\n", $error);
   exit $ERRORS{"UNKNOWN"};
}
#################################################################################################
my @tindex = undef;
my $oid_lastindex   = undef;
my @descr  = undef;
my $callstate = "" ;
my $i = 0;
my $index = undef;
my $oid_callstate = "1.3.6.1.4.1.9.9.644.1.4.8.1.11.";
my $oid = undef;
my $oid_lasttype = undef;
my $oid1 = undef;
my $oid2 = undef;
my $oid3  = undef;
my $oid4 = undef;
my $oid5 = undef;
my $oid6 = undef;
my $oid_type = undef;
my $lasttype = undef;

##########################
# Get last index number
##########################
$oid_lastindex = "1.3.6.1.4.1.9.9.644.1.4.7.0";
$result = $session->get_request($oid_lastindex);
$lastindex = $result->{$oid_lastindex};
if (!defined($lastindex)) { printf("ERROR: %s.\n", $session->error); $session->close;
	$session->close;
	exit $ERRORS{"UNKNOWN"};
}
#################################
# Get index of all current calls
#################################
while ($callstate ne "noSuchInstance") {
    $index = ($lastindex - $i);
    $oid = $oid_callstate.$index;
    $result = $session->get_request($oid);
    $callstate = $result->{$oid};
	if ($callstate eq "noSuchInstance") {
		last;
	}
    if (!defined($callstate)) { printf("ERROR: %s.\n", $session->error); $session->close;
        $session->close;
        exit $ERRORS{"UNKNOWN"};
	}
	if (( $callstate == 8 ) || ( $callstate == 9 ) || ( $callstate == 10 ) || ( $callstate == 11 ) || ( $callstate == 13 )) {
        push(@active_calls,$index);
	}
    if ($callstate =~ m/[1][1]/ ) {
        $oldest_current = $index;
	}
	$callstates[$index] = $callstate;
    $i++;
}

###################################
# Get last/current call(s) type(s)
###################################

foreach (@active_calls) {
	$oid_lasttype = "1.3.6.1.4.1.9.9.644.1.4.8.1.8.".$_;
	$result = $session->get_request($oid_lasttype);
	$lasttype = $result->{$oid_lasttype};
	if (!defined($lasttype)) { printf("ERROR: %s.\n", $session->error); $session->close;
        exit $ERRORS{"UNKNOWN"};
    }
	if ($lasttype eq "noSuchInstance") {
		$lasttype = 3;
	}
	if ($lasttype == 1) {
		$audioonly = 1;
	}
	push(@calltypes,$lasttype);
}

############################
# Get RX and TX Packets
############################
$i = 0;
if (@calltypes) {
foreach (@active_calls) {
if ( $calltypes[$i] == 1 ) {
	$oid1 = "1.3.6.1.4.1.9.9.644.1.4.10.1.12.".$_.".1.1";
	$oid2 = "1.3.6.1.4.1.9.9.644.1.4.10.1.12.".$_.".1.2";
	$oid3 = "1.3.6.1.4.1.9.9.644.1.4.10.1.12.".$_.".1.3";
    $oid4 = "1.3.6.1.4.1.9.9.644.1.4.10.1.4.".$_.".1.1";
    $oid5 = "1.3.6.1.4.1.9.9.644.1.4.10.1.4.".$_.".1.2";
    $oid6 = "1.3.6.1.4.1.9.9.644.1.4.10.1.4.".$_.".1.3";
	$result = $session->get_request($oid1);
	$totalrx1[$i] = $result->{$oid1};
		if (!defined($totalrx1[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
    $result = $session->get_request($oid2);
	$totalrx2[$i] = $result->{$oid2};
		if (!defined($totalrx2[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
    $result = $session->get_request($oid3);
	$totalrx3[$i] = $result->{$oid3};
		if (!defined($totalrx3[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
    $result = $session->get_request($oid4);
    $totaltx1[$i] = $result->{$oid4};
    	if (!defined($totaltx1[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
	$result = $session->get_request($oid5);
    $totaltx2[$i] = $result->{$oid5};
    	if (!defined($totaltx2[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
	$result = $session->get_request($oid6);
    $totaltx3[$i] = $result->{$oid6};
		if (!defined($totaltx3[$i])) { printf("ERROR: %s.\n", $session->error); $session->close;
            $session->close;
            exit $ERRORS{"UNKNOWN"};
		}
	if ($totalrx1[$i] == 0) {$totalrx1[$i] = 1000000}
	if ($totalrx2[$i] == 0) {$totalrx2[$i] = 1000000}
	if ($totalrx3[$i] == 0) {$totalrx3[$i] = 1000000}
	if ($totaltx1[$i] == 0) {$totaltx1[$i] = 1000000}
	if ($totaltx2[$i] == 0) {$totaltx2[$i] = 1000000}
	if ($totaltx3[$i] == 0) {$totaltx3[$i] = 1000000}
}
	$i++;
}
}
##############################################################
#### Modes
##############################################################	
if ($o_mode eq "registered" ){
   registered()
}

if ($o_mode eq "last" ){
    my $last = lastcall();
        $session->close;
        alarm(0);

	print "$last\n";
	exit $ERRORS{"OK"};	
}

if ($o_mode eq "state" ){
    my ($state, $status) = state();
        $session->close;
        alarm(0);

	print "$state\n";
	if ($status == 2) {
		exit $ERRORS{"CRITICAL"};
	} else {
		exit $ERRORS{"OK"};
	}
}

if ($o_mode eq "duration" ){
   duration()
}

if ($o_mode eq "type" ){
	if (@active_calls == 0) {
		print "No currently active calls.\n";
		exit $ERRORS{"OK"};
	}
	my $type = type();
        $session->close;
        alarm(0);

	print "$type\n";
	exit $ERRORS{"OK"};
}

if ($o_mode eq "bitrate" ){
   bitrate()
}

if ($o_mode eq "end" ){
	if (@active_calls == 0) {
		print "No currently active calls.\n";
		exit $ERRORS{"OK"};
	}
	my $end = end();
        $session->close;
        alarm(0);

	print "$end\n";
	exit $ERRORS{"OK"};
}

if ($o_mode eq "jitter" ){
   jitter()
}

if ($o_mode eq "rxpix" ){
   rxpix()
}

if ($o_mode eq "anomolies" ){
   anomolies()
}

if ($o_mode eq "info" ){
	my $end = undef;
	my $type = undef;
	my $output = undef;
	my $status = undef;
	my $state = undef;
	my $last = undef;

	if (@active_calls != 0) {
		$end = end();
		$type = type();
		($state, $status) = state();
		$last = lastcall();
		$output = "$state, $last, $end, $type\n";
	} else {
		$output = "No current call\n";
		$status = 0;
	}

        $session->close;
        alarm(0);

	print "$output";
	if ($status == 2) {
		exit $ERRORS{"CRITICAL"};
	} else {
		exit $ERRORS{"OK"};
	}
}

if ($o_mode eq "ploss" ){
   ploss()
}

if ($o_mode eq "fps" ){
   fps()
}

if ($o_mode eq "codec" ){
   codec()
}

$session->close;
alarm(0);
print "Not a correct mode defined!\n";
exit(1);
