#!/usr/bin/php
<?php

@$host=$argv[1];
@$community=$argv[2];
@$warn=intval($argv[3]);
@$critical=intval($argv[4]);
@$action=$argv[5]; // "checkall" or "total"
$grandtotal = 0; $warn_aps = ""; $okay_aps = "All APs within spec. Click to view.\n"; $crit_aps = "";
$are_warn = false; $are_crit = false;
$perfdata = " | ";

// nagios exit codes
$e_okay = 0;
$e_warn = 1;
$e_crit = 2;
$e_unkn = 3;

if (!$action || !$host || !$community || !$warn || !$critical) {
		print "Usage:\ncheck_cisco_wlc <hostname> <community> <warn value> <critical value> <'checkall' or 'total'>\n";
		exit($e_unkn);
}

if ($warn > $critical) {
		print "Warning value must be less than the critical value!\n";
		exit($e_unkn);
}

$aps = snmp2_real_walk($host, $community, '1.3.6.1.4.1.14179.2.2.1.1.3') or exit($e_unkn);

foreach ($aps as $key => $name) {
		preg_match("^(.+?)2.2.1.1.3.(.+?)$^" , $key, $match);
		$oid = trim($match[2]);
		preg_match("#STRING: \"(.*?)\"#",$name,$match);
		$name = trim($match[1]);

		$numassoc1 = snmp2_get($host,$community,'1.3.6.1.4.1.14179.2.2.13.1.4.'.$oid.'.0'); //SSID #1
		preg_match("#[0-9]+#",$numassoc1,$match);
		$num1 = @intval($match[0]);

		$numassoc2 = snmp2_get($host,$community,'1.3.6.1.4.1.14179.2.2.13.1.4.'.$oid.'.1'); //SSID #2
		preg_match("#[0-9]+#",$numassoc2,$match);
		$num2 = @intval($match[0]);

		$total = $num1+$num2;
		$perfdata .= "$name=$total;$warn;$critical;0;";
		if ($action == "checkall") {
			if ($total < $warn) {
					$okay_aps .= "OK: $name has $total client(s).\n";
			}
			if ($total >= $warn && $total < $critical) {
					$warn_aps .= "WARNING: $name has $total clients.\n";
					$are_warn = true;
			}
			if ($total >= $critical) {
					$crit_aps .= "CRITICAL: $name has $total clients!\n";
					$are_crit = true;
			}
		}

		$grandtotal = $grandtotal+$total;

	}
if ($action == "checkall") {
	if ($total < 1) {
		print "OK: No clients."; exit($e_okay);
	}
	if ($are_crit) {
		print $crit_aps.$perfdata; exit($e_crit);
	}
	if ($are_warn) {
		print $warn_aps.$perfdata; exit($e_warn);
	}
	print $okay_aps.$perfdata; exit($e_okay);
}
if ($action == "total") {
		if ($grandtotal == 0) {
				print "OK: There are no clients.";
				exit($e_okay);
		}
		if ($grandtotal < $warn) {
				print "OK: $grandtotal associated clients. | clients=$grandtotal;$warn;$critical;0;";
				exit($e_okay);
		}
		if ($grandtotal >= $warn && $grandtotal < $critical) {
				print "WARNING: $grandtotal associated clients. | clients=$grandtotal;$warn;$critical;0;";
				exit($e_warn);
		}
		if ($grandtotal >= $critical) {
				print "CRITICAL: $grandtotal associated clients. | clients=$grandtotal;$warn;$critical;0;";
				exit($e_crit);
		}
}
?>
