#!/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_clients";
$script_version = "3.0";

$ARGV[0]; # hostname
$ARGV[1]; # community
$ARGV[2]; # SNMP version - 1 or 2c (not 3)
$ARGV[3]; # AP number
$ARGV[4]; # warning lvl
$ARGV[5]; # 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 number of currently connected users to 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[5]) ) {
     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] ) {
     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 "\nAP Number is not within 7 to 15! \n\n"; exit(0);
}

# Merge the IODs
$oid_AP_Name = "1.3.6.1.4.1.14179.2.2.1.1.3";
$oid_AP_NumOfClients = "1.3.6.1.4.1.14179.2.2.13.1.4";
$oid_temp1 = $oid_AP_NumOfClients.$APid.".0";
$oid_temp2 = $oid_AP_Name.$APid;

# Create a SNMP session
$session = new SNMP::Session(DestHost => $ARGV[0],
                              Community => $ARGV[1],
                              Version => $ARGV[2],
                              UseEnums => 1,
                              UseSprintValue => 1
                              );
die "Cannot create SNMP::Session (",$SNMP::Session::ErrorStr,")\n" if ( ! defined($session));

# Get the data from supplied OIDs using the SNMP session
$oids = new SNMP::VarList([$oid_temp1],[$oid_temp2]);
($numofclients,$AP_Name)= $session->get($oids);
die $session->{ErrorStr} if ($session->{ErrorStr});


# Check state and output
if ( $numofclients >= $ARGV[5]) {
     print "CRITICAL: ".$AP_Name." - number of clients: ".$numofclients." (>=".$ARGV[5].") ";
     print "| Clients: ".$numofclients.";".$ARGV[4].";".$ARGV[5].";0;25\n";
     exit(2);
}
elsif ( $numofclients >= $ARGV[4]) {
     print "WARNING: ".$AP_Name." - number of clients: ".$numofclients." (>=".$ARGV[4].") ";
     print "| Clients: ".$numofclients.";".$ARGV[4].";".$ARGV[5].";0;25\n";
     exit(1);
}
else {
     print "OK: ".$AP_Name." - number of clients: ".$numofclients." ";
     print "| Clients: ".$numofclients.";".$ARGV[4].";".$ARGV[5].";0;25\n";
     exit(0);
}
