#!/usr/bin/perl -w

use strict;
use Net::SNMP;
use Getopt::Long qw(:config no_ignore_case bundling);

my $version="0.1alpha";
$0 =~ /.*\/([^\/]*)$/;
my $ME = $1;


	  my $hostname = 'localhost';
	  my $port = 3401;
	  my $community = 'public';
	  my $verbose = '';
	  my $help = '';
	  my $Ccpu = 20;
	  my $Wcpu = 10;
	  my $Csto = 65536*1024;
	  my $Wsto = $Csto-8192;
	  my $Cobj = 1024*1024;
	  my $Wobj = $Cobj-16384;
	  my $Cip = 4096;
	  my $Wip = $Cip-512;
	  my $Ccli = 2048;
	  my $Wcli = $Ccli - 256;

	  GetOptions('verbose|v'=>\$verbose,
		     'help|h'=>\$help,
		     'hostname|H=s'=>\$hostname,
		     'port|p=i'=>\$port,
		     'community|C=s'=>\$community,
		     'wC=i'=>\$Wcpu,
		     'cC=i'=>\$Ccpu,
		     'ws=i'=>\$Wsto,
		     'cs=i'=>\$Csto,
		     'wo=i'=>\$Wobj,
		     'co=i'=>\$Cobj,
		     'wi=i'=>\$Wip,
		     'ci=i'=>\$Cip,
		     'wc=i'=>\$Wcli,
		     'cc=i'=>\$Ccli
	  );

	  if ($help) {
            print<<EOF;
$ME (C) Arno Lehmann, IT-Service Lehmann, 2005

See source code for usage details.
No warranties for anything. Use on your own risk.

Permission to use, modify and distribute this script is granted
under the three-clause BSD license:

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in
  the documentation and/or other materials provided with the
   distribution.
* Neither the name of the IT-Service Lehmann nor the names of its
  contributors may be used to endorse or promote products derived
  from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

EOF
	    exit(3);
	  }

	  if (($Wcpu > $Ccpu) or ($Wsto > $Csto) or ($Wobj > $Cobj) or
	      ($Wip > $Cip) or ($Wcli > $Ccli)) {
	     print("ERROR: Warning threshold must be lower or equal to critical threshold.\n");
	     exit 3;
	  }

          my ($session, $error) = Net::SNMP->session(
             -hostname  => $hostname,
             -community => $community,
             -port      => $port
          );

          if (!defined($session)) {
             printf("ERROR: %s.\n", $error);
             exit 3;
          }

          my $CacheUpTime = '1.3.6.1.4.1.3495.1.1.3.0';
	  my $CacheCpuUsage = '1.3.6.1.4.1.3495.1.3.1.5.0';
          my $CacheSysStorage = '1.3.6.1.4.1.3495.1.1.2.0';
 	  my $CacheNumObjCount = '1.3.6.1.4.1.3495.1.3.1.7.0';
	  my $CacheRequestHitRatio5 = '1.3.6.1.4.1.3495.1.3.2.2.1.9.5';
	  my $CacheIpEntries = '1.3.6.1.4.1.3495.1.4.1.1.0';
	  my $CacheClients = '1.3.6.1.4.1.3495.1.3.2.1.15.0';

          my $result = $session->get_request(
             -varbindlist => [$CacheUpTime, $CacheCpuUsage, $CacheSysStorage,
			     $CacheNumObjCount, $CacheRequestHitRatio5,
			     $CacheIpEntries, $CacheClients]
          );

          if (!defined($result)) {
             printf("SNMP ERROR: %s.\n", $session->error);
             $session->close;
             exit 3;
          }

	  if ($verbose) {
	     printf("CacheUpTime for host ’%s’ is %s\nCPU usage: %s (%s:%s)\nStorage: %skB (%s:%s)\n" .
		    "Objects: %s (%s:%s) \nHit ratio: %s%%\nIPs cached: %s (%s:%s) \nClients: %s (%s:%s)\n",
		    $session->hostname, $result->{$CacheUpTime}, $result->{$CacheCpuUsage},
		    $Wcpu, $Ccpu, $result->{$CacheSysStorage}, $Wsto, $Csto, $result->{$CacheNumObjCount},
		    $Wobj, $Cobj, $result->{$CacheRequestHitRatio5}, $result->{$CacheIpEntries},
	            $Wip, $Cip, $result->{$CacheClients}, $Wcli, $Ccli
	           );
	  }

	  my $T = $result->{$CacheUpTime};
	  my $C = $result->{$CacheCpuUsage};
	  my $S = $result->{$CacheSysStorage};
	  my $O = $result->{$CacheNumObjCount};
	  my $H = $result->{$CacheRequestHitRatio5};
	  my $I = $result->{$CacheIpEntries};
	  my $Cl = $result->{$CacheClients};
	  my $Err = 0;
          my $RU = 'k';
	  my $RS = $S;
          if ($RS >= 1024) {
             $RU = 'M';
             $RS = sprintf("%.2f", $RS/1024);
	     if ($RS >= 1024.0) {
		$RU = 'G';
		$RS = sprintf("%.2f", $S/1024/1024);
	     }
	  }

	  my $Msg = 'Up ' . $T . ', ' . $RS . ' ' . $RU . 'B, ' . $O . ' objects, ' . $Cl . ' clients. Hit rate ' . $H . '%';
          my $MErr = '';

	  if ($C >= $Ccpu) {
	     $Err = 2;
	     $MErr = "CPU usage $C >= $Ccpu ";
	  } elsif ($C >= $Wcpu) {
	     $Err = 1 if (0 == $Err);
	     $MErr = "CPU usage $Ccpu > $C >= $Wcpu ";
	  }
	  if ($S >= $Csto) {
             $Err = 2;
             $MErr .= "Storage usage $S >= $Csto kB ";
          } elsif ($S >= $Wsto) {
             $Err = 1 if (0 == $Err);
             $MErr .= "Storage usage $Csto > $S >= $Wsto kB ";
          }
	  if ($O >= $Cobj) {
             $Err = 2;
             $MErr .= "Storage objects $S >= $Cobj ";
          } elsif ($O >= $Wobj) {
             $Err = 1 if (0 == $Err);
             $MErr .= "Storage objects $Wsto > $O >= $Wobj ";
          }
	  if ($I >= $Cip) {
             $Err = 2;
             $MErr .= "IPs $I >= $Cip ";
          } elsif ($I >= $Wip) {
             $Err = 1 if (0 == $Err);
             $MErr .= "IPs $Cip > $I >= $Wip ";
          }
	  if ($Cl >= $Ccli) {
             $Err = 2;
             $MErr .= "Clients $Cl >= $Ccli ";
          } elsif ($Cl >= $Wcli) {
             $Err = 1 if (0 == $Err);
             $MErr .= "Clients $Ccli > $Cl >= $Wcli ";
          }

	  my $Stat = sprintf("|CPU=%s%%;%s;%s;; Storage=%s;%s;%s;; Objects=%s;%s;%s;; HitRatio=%s%%;;;; IPs=%s;%s;%s;; Clients=%s;%s;%s;;",
		             $C, $Wcpu, $Ccpu, $S*1024, $Wsto*1024, $Csto*1024, $O, $Wobj, $Cobj, $H, $I, $Wip, $Cip, $Cl, $Wcli, $Ccli);

          $session->close;
          if (0 == $Err) {
	     $Msg = "Squid OK: $Msg";
          } elsif (1 == $Err) {
             $Msg = "Squid Warning: $MErr";
	  } elsif (2 == $Err) {
	     $Msg = "Squid CRITICAL: $MErr";
	  } else {
	     $Msg = "Squid UNKNOWN: $Msg";
	     $Err = 3;
	  }
	  print $Msg . $Stat;
          exit $Err;

