#!/usr/bin/perl

# nagios: +epn

=pod

=head1 NAME

check_tp_cpu.pl - Check CPU pressure on a Tipping Point device.

=head1 DESCRIPTION

The SNMP agent on Bluecoat devices allows us to query it to see what
thresholds have been set by the Bluecoat administrator.  For this reason
we do not need to have the user specify warning nor critical thresholds.

The script will return perfdata for CPU utilization for all Bluecoat 
devices; for SGOS 5.x and later this script will also return idle and
busy % CPU utilization.

=cut

sub check_tp_cpu {

    use strict;
    use FindBin;
    use lib "$FindBin::Bin/lib";

    use Net::SNMP;
    use Nagios::Plugin::SNMP;

    my $LABEL = 'TPT-CPU';

    my $plugin = Nagios::Plugin::SNMP->new(
        'shortname' => $LABEL,
        'usage' => 'USAGE: %s'
    );

    $plugin->getopts;

    my $DEBUG = $plugin->opts->get('snmp-debug');

    #  Return from current state will be one of these, each has
    #  a value that matches the index position in the array below.
    my @states = qw(ok low-warning warning high-warning
                    low-critical critical high-critical);

    my %oids = qw(
        resourceHPCPUBusyPercent.0   .1.3.6.1.4.1.10734.3.3.2.5.4.1.0
        cpuWarningThreshold.0     .1.3.6.1.4.1.10734.3.3.2.5.4.2.0
        cpuCriticalThreshold.0    .1.3.6.1.4.1.10734.3.3.2.5.4.3.0
    );

    my $result = $plugin->get(values %oids);

    #  Close and destroy session
    $plugin->close();

    my @perf_data;

    my %cpu;

    $cpu{'busy'} = $result->{$oids{'resourceHPCPUBusyPercent.0'}};
    $cpu{'warn'} = $result->{$oids{'cpuWarningThreshold.0'}};
    $cpu{'crit'} = $result->{$oids{'cpuCriticalThreshold.0'}};


    my $level = OK;

    my $output = "$LABEL ";

    if($cpu{'busy'}<$cpu{'warn'}){
        $output .= "OK - Utilization $cpu{'busy'}% ";
        }

    if(($cpu{'busy'}>=$cpu{'crit'}) && ($cpu{'busy'}>=$cpu{'warn'})){
	$output .= "CRITICAL - Utilization $cpu{'busy'}% >= $cpu{'crit'}% ";
	$level = CRITICAL;
	}
    if(($cpu{'busy'}>=$cpu{'warn'}) && ($cpu{'busy'}<$cpu{'crit'})){
	$output .= "WARNING  - Utilization $cpu{'busy'}% >= $cpu{'warn'}% ";
	 $level = WARNING;
	}
	
   
    print "$output | utilization=$cpu{'busy'}%;$cpu{'warn'};$cpu{'crit'};0;100\n";
    return $level;

    sub debug {

        return unless $DEBUG == 1;

        my $msg = shift;

        print STDERR scalar(localtime()) . ": $msg\n";

    }

}

exit check_tp_cpu();



