#! /usr/bin/perl
###############################################################################
#                                                                             #
#  Check a Symbol Spectrum24 direct sequence wireless access point via SNMP   #
#                                                                             #
###############################################################################
#
# Copyright (c) 2005 Simon Butcher <simon@butcher.name>
#  
# This program 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 program 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.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA    02111-1307    USA
#
##############################################################################
#
#
# Performance data:
#    +-------+-----------------------------------------------+
#    | Field | Description                                   |
#    +-------+-----------------------------------------------+
#    |   1   | Device Type ("AP-4131", etc)                  |
#    |   2   | Uptime                                        |
#    |   3   | Associated MUs (Mobile Units)                 |
#    |   4   | Location of Access Point                      |
#    |   5   | SSID/Network ID being served ("101", etc)     |
#    +-------+-----------------------------------------------+
#
##############################################################################


use Net::SNMP;
use Getopt::Std;

$script = "check_snmp_symbol_spectrum_t3dsap";
$script_title =
  "Monitor the status of a Symbol Spectrum24 direct sequence WLAN access point";
$script_version = "20051203";

# SNMP options
$version = "1";
$timeout = 2;

# Various SNMP OID prefixes to look at..
$oid_ap_name            = ".1.3.6.1.2.1.1.5.0";
$oid_ap_radiostatus     = ".1.3.6.1.4.1.388.1.8.1.4.1.0";
$oid_ap_location        = ".1.3.6.1.2.1.1.6.0";
$oid_ap_ssid            = ".1.3.6.1.4.1.388.1.8.1.4.2.0";
$oid_ap_devicetype      = ".1.3.6.1.4.1.388.1.8.1.1.1.0";
$oid_ap_uptime          = ".1.3.6.1.2.1.1.3.0";
$oid_ap_assocmus        = ".1.3.6.1.4.1.388.1.8.2.8.1.0";
$oid_ap_assocmus_max    = ".1.3.6.1.4.1.388.1.8.1.2.27.0";

# The information we returned
$ap_radiostatus = "";
$ap_location = "";
$ap_ssid = "";
$ap_channel = "";
$ap_powerlevel = "";
$ap_devicetype = "";
$ap_uptime = "";
$ap_assocmus = "";

# Our return status - we start with 0 (OK) and hope for the best
$status = 0;

# Our return string with lots of interesting stuff in it
$returnstr = "";

# The SNMP hostname and community to use for the query
$hostname = "";
$community = "public";


# Grab the command line options
getopts("h:H:C:w:c:");

# If we didn't get any options, show some help and quit
if ($opt_h){
    usage();
    exit(-1); # Unknown
}

# Get the hostname, if it was given..
if (defined($opt_H)){
    $hostname = $opt_H;
} else {
    # We really need a hostname
    usage();
    exit(-1); # Unknown
}

# Get the SNMP community
if (defined($opt_C)){
    $community = $opt_C;
}

# Grab the warning and critical MU association levels, if provided
if (defined($opt_w)) {
    $mu_warning = $opt_w;

    # Make sure this is a number, and a valid one at that..
    if ($mu_warning <= 0) {
	print "Warning level must be a number of mobile units.\n";
	exit(-1); # Unknown
    }
} else {
    $mu_warning = -1;
}
if (defined($opt_c)) {
    $mu_critical = $opt_c;

    # Make sure this is a number, and a valid one at that..
    if ($mu_critical <= 0) {
	print "Critical level must be a number of mobile units.\n";
	exit(-1); # Unknown
    }
} else {
    $mu_critical = -1;
}

# If both the critical and warning levels were given, check warning is lower..
if (($mu_warning >= 0) && ($mu_critical >= 0) &&
    ($mu_warning > $mu_critical)) {
    print "Warning level is greater than the critical level!\n";
    exit(-1); # Unknown
}

# Initialise the SNMP session via the Net::SNMP perl module
my ($snmp_session, $snmp_error) = Net::SNMP->session(
    -community => $community,
    -hostname => $hostname,
    -version => $version,
    -timeout => $timeout,
);

# Grab interesting details from the access point
check_device();
    
# Shut down the SNMP session
$snmp_session->close();

# Return the return string and return status
print "$returnstr\n";
exit($status);


##############################################################################
##############################################################################
#
# Change the status level
#
sub status {
    my $newstatus = $_[0];

    # If the new status is greater than the old status, change the old status
    if ($newstatus > $status) {
	$status = $newstatus;
    }
}


##############################################################################
##############################################################################
#
# Grab a value from snmp
#
sub grab_snmp_value {
    my $this_oid = $_[0];
    my $this_value = "";

    # Try to grab the OID's value, if it exists
    if (defined($snmp_session->get_request($this_oid))) {
        foreach ($snmp_session->var_bind_names()) {
            $this_value = $snmp_session->var_bind_list()->{$_};
        }
    }

    # Return the value we got..
    return $this_value;
}


##############################################################################
##############################################################################
#
# Grab lots of interesting info about the access point :)
#
sub check_device {
    # Grab some values
    $ap_radiostatus = grab_snmp_value($oid_ap_radiostatus);
    $ap_location = grab_snmp_value($oid_ap_location);
    $ap_ssid = grab_snmp_value($oid_ap_ssid);
    $ap_devicetype = grab_snmp_value($oid_ap_devicetype);
    $ap_uptime = grab_snmp_value($oid_ap_uptime);
    $ap_assocmus = grab_snmp_value($oid_ap_assocmus);

    # If we have the device type name, start with that, other use "AP"
    if ($ap_devicetype eq "") {
	$returnstr = "AP";
    } else {
        $returnstr = "$ap_devicetype";
    }

    # Work out the radio status
    if ($ap_radiostatus == 0) {         # Off
	status(2); # Critical
	$returnstr = "$returnstr RF Off";
    } elsif ($ap_radiostatus == 1) {   # On
	status(0); # OK
	$returnstr = "$returnstr RF On";
    } else {                            # Unknown!
	status(1); # Warning
	$returnstr = "$returnstr UNKNOWN";
    }

    # If we have the MU count, add that
    if ($ap_assocmus >= 0) {
	# Add a comma to the return string
	$returnstr = "$returnstr,";

	# If we have a critical or warning level to watch, check it now..
	if (($mu_critical >= 0) &&
	    ($ap_assocmus >= $mu_critical)) {
	    # Prefix a critical label to the MU count and change the status
	    status(2); # Critical
	    $returnstr = "$returnstr CRITICAL:";
	    $mu_is_critical = 1;
	} else {
	    $mu_is_critical = 0;
	}

	# If we have a warning level to watch, check it too..
	if (($mu_warning >= 0) &&
	    ($mu_is_critical != 1) &&
            ($ap_assocmus >= $mu_warning)) {
	    status(1); # Warning
	    $returnstr = "$returnstr WARNING: <$mu_warning>";
	}

	# Add the MU count
	$returnstr = 
	    "$returnstr " .
	    "$ap_assocmus MUs";
    }

    # If we have SSID name, add that
    if (!($ap_ssid eq "")) {
	$returnstr =
	    "$returnstr, " .
	    "SSID: $ap_ssid";
    }

    # If we have the uptime, add that
    if (!($ap_uptime eq "")) {
	$returnstr =
	    "$returnstr, " .
	    "Uptime: $ap_uptime";
    }

    # Add on some performance monitoring stuff to the return string :)
    $returnstr =
	"$returnstr" .
	"|\"$ap_devicetype\";\"$ap_uptime\";$ap_assocmus;\"$ap_location\"" .
	";\"$ap_ssid\"" .
	";";
}


##############################################################################
##############################################################################
#
# Usage information
#
sub usage {
    print << "USAGE";
$script ($script_version)
Copyright (c) 2005 Simon Butcher <simon@butcher.name>

$script_title

Usage: $script -H <hostname> [-C <community>] [-w <#>] [-c <#>]

Options:
    -H 	Hostname or IP address of the access point
    -C 	SNMP read community (default is $community)
    -w  Number of associated mobile units to begin warning status (eg. 100)
    -c  Number of associated mobile units to begin critical status (eg. 120)

USAGE
     exit(-1); # Unknown
}
