#!/usr/bin/perl
# Author: Michal Zyzak
# Email: michal.zyzak@gmail.com
#    This script 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 script 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.

use SNMP;

$script    = "check_snmp_AP_channel";
$script_version = "2.0";

$ARGV[0]; # hostname
$ARGV[1]; # community
$ARGV[2]; # SNMP version - 1 or 2c (not 3)
$ARGV[3]; # AP number
$ARGV[4]; # Rx warning lvl
$ARGV[5]; # Rx critical lvl
$ARGV[6]; # Rx warning lvl
$ARGV[7]; # Rx critical lvl

$hostname = $ARGV[0];
$community = $ARGV[1];
$version = $ARGV[2];

# Mapping OIDs to APs
$AP7id    = ".0.31.157.35.136.80";
$AP8id    = ".0.30.19.122.46.64";
$AP9id    = ".0.29.161.144.169.208";
$AP10id   = ".0.29.161.144.189.240";
$AP11id   = ".0.29.161.144.166.16";
$AP12id   = ".0.29.161.144.193.16";
$AP13id   = ".0.30.19.122.64.16";
$AP14id   = ".0.29.161.144.217.0";
$AP15id   = ".0.29.162.136.135.16";

# Options check
if ( $ARGV[0] == "-v" ) {
     print "Script: check_snmp_AP_clients.pl \nVersion: ".$script_version."\n";
     exit(0);
}
elsif ( $ARGV[0] == "-h" ) {
     print "\nThis script gather information (via SNMP) from Cisco 4400 Series Wireless Controller \nabout traffic utilisation of Access Point in question\n\n";
     print "Usage: \n check_snmp_AP_clients IP_Address Community_String SNMP_Version AP_number Warning_lvl Critical_lvl \n";
     print "Or:  -h for this help\n     -v for script version\n\n";
     exit(0);
}
elsif ( !($ARGV[0]) || !($ARGV[7]) ) {
     print "\nNot enough parameters were given!\n\n";
     print "Usage: \n check_snmp_AP_clients IP_Address Community_String SNMP_Version AP_number Warning_lvl Critical_lvl \n";
     print "Or:  -h for help\n     -v for script version\n\n";
     exit(0);
}
if ( $ARGV[5] <= $ARGV[4] || $ARGV[7] <= $ARGV[6] ) {
     print "\nWarning level must by lower than critical level!\n\n";
     exit(0);
}

# Decide on which AP to operate
SWITCH: {
  	$ARGV[3] == 7 && do { $APid = $AP7id; last SWITCH; };
  	$ARGV[3] == 8 && do { $APid = $AP8id; last SWITCH; };
  	$ARGV[3] == 9 && do { $APid = $AP9id; last SWITCH; };
	$ARGV[3] == 10 && do { $APid = $AP10id; last SWITCH; };
  	$ARGV[3] == 11 && do { $APid = $AP11id; last SWITCH; };
  	$ARGV[3] == 12 && do { $APid = $AP12id; last SWITCH; };
  	$ARGV[3] == 13 && do { $APid = $AP13id; last SWITCH; };
	$ARGV[3] == 14 && do { $APid = $AP14id; last SWITCH; };
	$ARGV[3] == 15 && do { $APid = $AP15id; last SWITCH; };
	print "\$ARGV[3] is not within 7 to 15 \n";
}

# Get the IODs
$oid_AP_AdminStatus                = "1.3.6.1.4.1.14179.2.2.1.1.37";
$oid_AP_OperStatus                 = "1.3.6.1.4.1.14179.2.2.1.1.6";
$oid_AP_UsedChannel                = "1.3.6.1.4.1.14179.2.2.2.1.4";
$oid_AP_TX_Utilisation		     = "1.3.6.1.4.1.14179.2.2.13.1.1";
$oid_AP_RX_Utilisation		     = "1.3.6.1.4.1.14179.2.2.13.1.2";
$oid_AP_ChannelUtilization         = "1.3.6.1.4.1.14179.2.2.13.1.3";



# Basic Status Data
$oid_temp1 	     = $oid_AP_AdminStatus . $APid;
chomp($admstat     = `snmpget -v 2c -c $community -Ovq $hostname $oid_temp1`);
$oid_temp2 	     = $oid_AP_OperStatus . $APid;
chomp($operstat 	= `snmpget -v 2c -c $community -Ovq $hostname $oid_temp2`);
$oid_temp6 	     = $oid_AP_UsedChannel . $APid . ".0";
chomp($channum 	= `snmpget -v 2c -c $community -Ovq $hostname $oid_temp6`);

# Traffic Data
$oid_temp3 	     = $oid_AP_TX_Utilisation . $APid . ".0";
chomp($txutil 	     = `snmpget -v 2c -c $community -Ovq $hostname $oid_temp3`);
$oid_temp4 	     = $oid_AP_RX_Utilisation . $APid .".0";
chomp($rxutil 	     = `snmpget -v 2c -c $community -Ovq $hostname $oid_temp4`);
$oid_temp5 	     = $oid_AP_ChannelUtilization . $APid . ".0";
chomp($chutil 	     = `snmpget -v 2c -c $community -Ovq $hostname $oid_temp5`);



# Check state and output
if ( $admstat != 1 ) {
     print "This Ap is administativly disabled\n";
     exit(0);
}
elsif ( $admstat != $operstat ) {
     print "CRITICAL: AP is not associated with WLC\n";
     exit(2);
}
else {
     if ( $rxutil > $ARGV[5] || $txutil > $ARGV[7] ) {
          print "CRITICAL: AP works on channel: ".$channum." - traffic utilisation: Rx: ".$rxutil." %, Tx: ".$txutil." %, channel utilisation: ".$chutil." %";
          print "| Rx: ".$rxutil." %, Tx: ".$txutil." %, channel utilisation: ".$chutil." %\n";
          exit(2);
     }
     elsif ( $rxutil > $ARGV[4] || $txutil > $ARGV[6] ) {
          print "WARNING: AP works on channel: ".$channum." - traffic utilisation: Rx: ".$rxutil." %, Tx: ".$txutil." %, channel utilisation: ".$chutil." %";
          print "| Rx: ".$rxutil." %, Tx: ".$txutil." %, channel utilisation: ".$chutil." %\n";
          exit(1);
     }
     else {
          print "OK: AP works on channel: ".$channum." - traffic utilisation: Rx: ".$rxutil." %, Tx: ".$txutil." %, channel utilisation: ".$chutil." %";
          print "| Rx: ".$rxutil." %, Tx: ".$txutil." %, channel utilisation: ".$chutil." %\n";
          exit(0);
     }
}
