#!/usr/bin/perl -w
#
# check_traffic v 0.90b - Nagios(r) network traffic monitor plugin
#
# Copyright (c) 2003 Adrian Wieczorek, <ads (at) irc.pila.pl>
# modified by Kevin Calhoon for 
# Distributed Computing Services of Michigan Technolgical University
# Send me bug reports, questions and comments about this plugin.
# Latest version of this software: http://adi.blink.pl/nagios
#
#
# 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
#
# Edited by Kevin Calhoon 2004
#    -changed to work with net::snmp and RRDs libs
#
use strict;
use English;
use lib "/usr/local/rrdtool-1.0.43/lib/perl/";
use Getopt::Long;
use Net::SNMP;
use RRDs;
use lib "/dcs/local/nagios/nagios-2.0/libexec/";
use utils qw($TIMEOUT %ERRORS &print_revision &support);
use vars qw($opt_H $opt_w $opt_c $opt_h $opt_V $opt_v $opt_C $PROGRNAME);

sub print_help ();
sub print_usage ();

$ENV{'PATH'}='';
$ENV{'BASH_ENV'}=''; 
$ENV{'ENV'}=''; 

Getopt::Long::Configure('bundling');
GetOptions
        ("V"    => \$opt_V,  "version"     => \$opt_V,
         "h"    => \$opt_h,  "help"        => \$opt_h,
         "H=s"  => \$opt_H,  "host"    	   => \$opt_H,
         "v:s"  => \$opt_v,  "snmpVersion" => \$opt_v,
         "C:s"  => \$opt_C,  "community"   => \$opt_C,
         "w:s"  => \$opt_w,  "warning"     => \$opt_w,
         "c:s"  => \$opt_c,  "critical"	   => \$opt_c);

my $VERSION     = "0.90b";
my $PROGNAME	= "check_traffic";

my $TRAFFIC_FILE = "/tmp/traffic/traffic";

# SNMP stuff:
my $COMMUNITY    = ();
my $version    = ();
my $snmp_session = ();
my $error 	 = ();
my $result 	 = ();

#
# RRD support:
# set to 0 to not create and/or update an rrd file
#
my $WITH_RRD     = 1;
my $DB_PATH      = "/dcs/local/nagios/nagios-2.0/check_traffic-$VERSION/db";

my $host_address;
my $in_cpu = 0;

my $warn = 80;
my $critical = 90;

if ($opt_V) 
{
  print_revision($PROGNAME,'$Revision: 1.1 $VERSION $ ');
  exit $ERRORS{'OK'};
}
if ($opt_h) 
{
  print_help();
  exit $ERRORS{'OK'};
}
if($opt_H =~ /([-.A-Za-z0-9]+)/)
{ 
  $host_address = $opt_H;
}
else
{
  print_help();
  exit $ERRORS{'CRITICAL'};
}
if(defined($opt_w))
{
  $warn = $opt_w;
}
else
{
  $warn = 80;
}
if(defined($opt_c))
{
  $critical = $opt_c;
}
else
{
  $critical = 90;
}
if(defined($opt_C))
{
  $COMMUNITY = $opt_C;
}
else
{
  $COMMUNITY = "public";
}
if(defined($opt_v))
{
  $version = $opt_v;
}
else
{
  $version = "2c";
}

#
#create an snmp_session
#
($snmp_session, $error) = Net::SNMP->session(
	-hostname => $host_address,
        -community => $COMMUNITY,
        -version => $version,
        -port => 161);

if (! $snmp_session)
{
        print "Error: $error\n";
        exit $ERRORS{'UNKNOWN'};
}

#
#OID values
#
# cpmCPUTotal1minRev = 1.3.6.1.4.1.9.9.109.1.1.1.1.7.1
#

$result = $snmp_session->get_request(-varbindlist => ["1.3.6.1.4.1.9.9.109.1.1.1.1.7.1"]);

if (!defined($result)) {
      printf("ERROR: %s.\n", $snmp_session->error);
      $snmp_session->close;
      exit $ERRORS{'CRITICAL'};
   }

$in_cpu = $result->{"1.3.6.1.4.1.9.9.109.1.1.1.1.7.1"};

$snmp_session->close();

my $update_time = time;
my $time = localtime;

if($WITH_RRD)
{
  my $db_file = $host_address."cpu".".rrd";
  #print "db file = $DB_PATH/$db_file\n";

  #
  #       If the file doesn't exist, make it.
  #
  #       This file will contain an archive of:
  #
  #       - Daily archive of all data points retrieved
  #         in a 1 minute interval.
  #       - Weekly archive of all data points retrieved
  #         in a 5 min interval.
  #       - Monthly archive of all data points retrieved
  #         in a 10 min interval.
  #       - Yearly archive of all data points retrieved
  #         in a 30 min interval.
  if (! -e "$DB_PATH/$db_file")
  {
        RRDs::create("$DB_PATH/$db_file",
                "--step", "60",
                "DS:cpu:GAUGE:120:U:U",
                "RRA:AVERAGE:0.5:1:1440",
                "RRA:AVERAGE:0.5:5:2016",
                "RRA:AVERAGE:0.5:10:9072",
                "RRA:AVERAGE:0.5:30:17520",
                "RRA:MAX:0.5:1:1440",
                "RRA:MAX:0.5:5:2016",
                "RRA:MAX:0.5:10:9072",
                "RRA:MAX:0.5:30:17520"
        );
        $error = RRDs::error();
        if ($error)
        {
                print "ERROR_while_updating$DB_PATH/$db_file:$error\n";
                exit ($ERRORS{"UNKNOWN"});
        }
  }

  RRDs::update("$DB_PATH/$db_file", "$update_time:$in_cpu");
  $error = RRDs::error();
  if ($error)
  {
        print "ERROR while updating $DB_PATH/$db_file: $error\n";
        exit $ERRORS{'UNKNOWN'};
  }
}

if($in_cpu > $critical)
{
  print "Critical CPU utilization $in_cpu";
  exit $ERRORS{'CRITICAL'};
}
elsif($in_cpu > $warn)
{
  print "Warning CPU utilization $in_cpu";
  exit $ERRORS{'WARNING'};
}
else
{
  print "OK CPU utilization $in_cpu";
  exit $ERRORS{'OK'};
}

sub print_usage ()
{
 print "Usage: check_traffic -H host {-V snmp version] [-C snmp community] [ -w warn % ] [ -c critical % ]\n";
}

sub print_help () 
{
 print_usage();
 print "Options:\n";
 print " -h this help";
 print " -V version";
 print " -H --host STRING or IPADDRESS\n";
 print " -V --snmp version 1, 2c, 3\n";
 print " -C --snmp community\n";
 print " -w --cpu warning level\n";
 print " -c --cpu critical level\n";
 exit $ERRORS{'UNKNOWN'};
}

