#!/usr/bin/perl
# Author: Michal Zyzak - michal.zyzak@gmail.com

use lib qw ( /usr/local/nagios/perl/lib );
use Net::SNMP;

$script = "check_snmp_AP_clients";
$script_version = "2.1";

$ARGV[0]; # hostname
$ARGV[1]; # community
$ARGV[2]; # SNMP version
$ARGV[3]; # AP number
$ARGV[4]; # warning lvl
$ARGV[5]; # critical lvl

$timeout = 20;
$hostname1 = $ARGV[0];
$community = $ARGV[1];
$version = $ARGV[2];

$AP1id = ".0.31.155.25.136.80";
$AP2id = ".0.30.20.122.46.64";
$AP3id = ".0.29.161.177.169.208";


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);
}

SWITCH: {
  	$ARGV[3] == 1 && do { $APid = $AP1id; last SWITCH; };
  	$ARGV[3] == 2 && do { $APid = $AP2id; last SWITCH; };
  	$ARGV[3] == 3 && do { $APid = $AP3id; last SWITCH; };
	print "\nAP Number is not within 1 to 3! \n\n"; exit(0);
}

# OIds for AP name and client count
$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_temp = $oid_AP_NumOfClients . $APid . ".0";
chomp($numofclients = `snmpget -v 2c -c $community -Ovq $hostname1 $oid_temp`);
$oid_temp = $oid_AP_Name . $APid;
chomp($AP_Name = `snmpget -v 2c -c $community -Ovq $hostname1 $oid_temp`);

if ( $numofclients >= $ARGV[5]) {
     print "CRITICAL: ".$AP_Name." - number of clients: ".$numofclients." (>".$ARGV[5].") ";
     print "| Clients: ".$numofclients.";".$ARGV[4].";".$ARGV[5].";0;50\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\n";
     exit(1);
}
else {
     print "OK: ".$AP_Name." - number of clients: ".$numofclients." ";
     print "| Clients: ".$numofclients.";".$ARGV[4].";".$ARGV[5].";0\n";
     exit(0);
}
