#!/usr/bin/perl -w
############## check_wireless_clients #########################
# Version : 0.9
# Date    : March 20, 2014
# 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 Wireless
#######################################################
# Help : ./check_wireless_clients.pl -h
#
use strict;
use Net::SNMP;
use Getopt::Long;

#### BASE DIRECTORY FOR TEMP FILE ####
my @results = undef;
my $connections = 0;
my @split = undef;
use lib "/usr/local/nagios/libexec";
use utils qw(%ERRORS $TIMEOUT);

#### Globals
my $Version   = '0.9';

# Standard options
my $o_host     = undef; # hostname
my $o_help     = undef; # wan't some help ?
my $o_crit_opt = undef; # critical options
my $o_warn_opt = undef; 

# Login options specific
my $o_community = 	undef; 	# community

# functions
sub print_usage {
    print "Usage: $0 -H <host> -C <snmp_community> -w <warning levels> -c <crit levels>\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_usage();
}

sub check_options { #### Check Options
    Getopt::Long::Configure ("bundling");
	GetOptions(
        'h'     => \$o_help,    	'help'        	=> \$o_help,
        'H:s'   => \$o_host,		'hostname:s'	=> \$o_host,
        'C:s'   => \$o_community,	'community:s'	=> \$o_community,
        'c:s'   => \$o_crit_opt,    'critical:s'   	=> \$o_crit_opt,
        'w:s'   => \$o_warn_opt,    'warning:s'        => \$o_warn_opt
    );
    if (defined($o_help)) { help(); exit $ERRORS{"UNKNOWN"}};
}

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

@results = `snmpwalk -v 2c -c $o_community $o_host .1.3.6.1.4.1.14179.2.1.1.1.38`;
my $i = 0;
foreach (@results) {
	@split = split(" ", $results[$i]);
	$connections = $connections + $split[3];
	$i++;
}

if ($connections > $o_crit_opt ) {
        print "CRITICAL: Greater than $o_crit_opt | Connections=$connections\n";
        exit $ERRORS{"CRITICAL"};
	} elsif ($connections > $o_warn_opt ) {
	        print "WARNING: Connections greater than $o_warn_opt | Connections=$connections\n";
        	exit $ERRORS{"WARNING"};
	} else {
	        print "OK: Less than $o_warn_opt | Connections=$connections\n";
        	exit $ERRORS{"OK"};
        }
