#!/usr/bin/perl -wT
#
# check_maxchannels.pl - nagios plugin 
# 
#
# Copyright (C) 2000 Christoph Kron
#
# 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; either version 2
# of the License, or (at your option) any later version.
#
# 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.
#
#
# Report bugs to: ck@zet.net
#
# 11.01.2000 Version 1.0

require 5.004;
use strict;
use lib utils.pm ;
use Plugin;
use Plugin::Parameter qw(:DEFAULT :snmp);

use Net::SNMP;

use vars qw($opt_H $opt_C $opt_p $opt_t $PROGNAME);

use utils qw(%ERRORS &usage);

$p_opt->default(161);
my $plugin = new Plugin(-revision => '$Revision: 1.1.1.1 $',
			-copyright => "2000 Christoph Kron, 2004 Howard Wilkinson <howard\@cohtech.com>",
			-shortcomment => "Perl Check maxchannels plugin for Nagios. Monitors ISDN lines and modems on Ascend MAX 2000/4000/6000/TNT",
			-parameterlists => [ [ $H_opt, $C_opt, $p_opt, $t_opt ], $h_opts, $V_opts ]);

$plugin->init();
	
# free channels
my $snmpWanAvailableChannels = '1.3.6.1.4.1.529.4.23.0';
# maximum channels
my $snmpWanSwitchedChannels = '1.3.6.1.4.1.529.4.24.0';
my $snmpWanDisabledChannels = '1.3.6.1.4.1.529.4.25.0';
my $snmpWanActiveChannels = '1.3.6.1.4.1.529.4.26.0';
my $snmpWanNailedChannels = '1.3.6.1.4.1.529.4.27.0';
my $snmpWanOutOfServiceChannels = '1.3.6.1.4.1.529.4.28.0';
my $snmpEventCurrentActiveSessions = '1.3.6.1.4.1.529.10.6.0';
# since startup
my $snmpEventTotalNoModems = '1.3.6.1.4.1.529.10.15.0';
# lan modem
my $snmpDeadLanModem = '1.3.6.1.4.1.529.15.7.0';
my $snmpDisabledLanModem = '1.3.6.1.4.1.529.15.5.0';
my $snmpSuspectLanModem = '1.3.6.1.4.1.529.15.3.0';
my $snmpAvailLanModem = '1.3.6.1.4.1.529.15.1.0';
my $snmpBusyLanModem = '1.3.6.1.4.1.529.15.9.0';
# max modems
my $snmpMdmNumber  = '1.3.6.1.2.1.38.1.1.0';

my @snmpoids = ( $snmpWanAvailableChannels,
		 $snmpWanSwitchedChannels,
		 $snmpWanDisabledChannels,
		 $snmpWanActiveChannels,
		 $snmpWanNailedChannels,
		 $snmpWanOutOfServiceChannels,
		 $snmpEventCurrentActiveSessions,
		 $snmpEventTotalNoModems,
		 $snmpDeadLanModem,
		 $snmpDisabledLanModem,
		 $snmpSuspectLanModem,
		 $snmpAvailLanModem,
		 $snmpBusyLanModem,
		 $snmpMdmNumber);

my $state = "UNKNOWN";
my $answer = "";

my ($session, $error) = Net::SNMP->session(-hostname  => $opt_H,
					   -community => $opt_C,
					   -port      => $opt_p,
					   -timeout   => $opt_t
					  );

if (!defined($session)) {
  $state='UNKNOWN';
  $answer=$error;
  print ("$PROGNAME $state: $answer");
  exit $ERRORS{$state};
}

my $response = $session->get_request(@snmpoids);
if (!defined $response) {
  $answer=$session->error;
  $session->close;
  $state = 'CRITICAL';
  print ("$PROGNAME $state: $answer,$opt_C");
  exit $ERRORS{$state};
}

my $WanAvailableChannels = $response->{$snmpWanAvailableChannels};
my $WanSwitchedChannels = $response->{$snmpWanSwitchedChannels};
my $WanDisabledChannels = $response->{$snmpWanDisabledChannels};
my $WanActiveChannels = $response->{$snmpWanActiveChannels};
my $WanNailedChannels = $response->{$snmpWanNailedChannels};
my $WanOutOfServiceChannels = $response->{$snmpWanOutOfServiceChannels};
my $EventCurrentActiveSessions = $response->{$snmpEventCurrentActiveSessions};
my $EventTotalNoModems = $response->{$snmpEventTotalNoModems};
my $DeadLanModem = $response->{$snmpDeadLanModem};
my $DisabledLanModem = $response->{$snmpDisabledLanModem};
my $SuspectLanModem = $response->{$snmpSuspectLanModem};
my $AvailLanModem = $response->{$snmpAvailLanModem};
my $BusyLanModem = $response->{$snmpBusyLanModem};
my $MdmNumber = $response->{$snmpMdmNumber};

# less than 50% -> WARNING
if ( 0 < $WanOutOfServiceChannels 
     && $WanOutOfServiceChannels < ($snmpWanSwitchedChannels * 0.5) ) {
   $state = 'WARNING';
}
elsif ($WanOutOfServiceChannels > 0) {
   $state = 'CRITICAL';
}
elsif ($DeadLanModem > 0) {
   $state = 'CRITICAL';
}
elsif ($SuspectLanModem > 0) {
   $state = 'WARNING';
}
elsif ($AvailLanModem == 0) {
   $state = 'WARNING';
}
else {
   $state = 'OK';
}


$answer =  sprintf("active sessions: %d (%d), active modems: %d (%d)<BR>",
		   $EventCurrentActiveSessions,
		   $WanSwitchedChannels,
		   $BusyLanModem,
		   $MdmNumber);
		
$answer .= sprintf("channels available: %d, disabled: %d",
		   $WanAvailableChannels,
		   $WanDisabledChannels);

$answer .= sprintf(", out of service: %d, nailed: %d<BR>",
		   $WanOutOfServiceChannels,
		   $WanNailedChannels);

$answer .= sprintf("modems avail.: %d, disabled: %d, suspect: %d, dead: %d<BR>",
		   $AvailLanModem,
		   $DisabledLanModem,
		   $SuspectLanModem,
		   $DeadLanModem);

$answer .= sprintf("unserviced modem calls: %d (since startup)\n",
		   $EventTotalNoModems);

$session->close;

print ("$PROGNAME $state: $answer");
exit $ERRORS{$state};
