#!/usr/bin/perl -w
#
# check_cpuwaitdisk.pl - nagios plugin
#
# Copyright (C) 2008 Markus Grabosch,
#
# 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.
#
# Date : 08 08 2008
# Author  : Markus Grabosch (mg@grabosch.de)
# Licence : GPL - http://www.fsf.org/licenses/gpl.txt
# TODO : 
# - look at the version-string and think about it! -
# RRD-Datastorage
#################################################################

 
use strict;
use Getopt::Long;
use vars qw($PROGNAME);
use lib "/usr/local/nagios/libexec"; # Pfad zur util.pm !!
use utils qw ($TIMEOUT %ERRORS &print_revision &support);
 
sub print_help ();
sub print_usage ();
 
my ($opt_V, $opt_h, $opt_C, $opt_P, $opt_H, $opt_p, $opt_o, $opt_r, $opt_w, $opt_c, $opt_t);
my ($result, $message, $age, $size, $st);
 
$PROGNAME="check_cpuwaitdisk";
 
$opt_C="public";
$opt_P="1";
$opt_H="";
$opt_p=161;
$opt_o="";
$opt_r="";
$opt_w="5";
$opt_c="20";
$opt_t="10";
 
Getopt::Long::Configure('bundling');
GetOptions(
        "V"   => \$opt_V, "version"     => \$opt_V,
        "h"   => \$opt_h, "help"        => \$opt_h,
        "C=s" => \$opt_C, "community"        => \$opt_C,
        "P=s" => \$opt_P, "protocol=s" => \$opt_P,
	"H=s" => \$opt_H, "hostname"	=> \$opt_H,
	"p=i" => \$opt_p, "port"	=> \$opt_p,
	"o=s" => \$opt_o, "oid"		=> \$opt_o,
	"r=s" => \$opt_r, "regex"	=> \$opt_r,
        "w=s" => \$opt_w, "warning=s" => \$opt_w,
        "c=s" => \$opt_c, "critical=s" => \$opt_c,
        "t=i" => \$opt_t, "timeout" => \$opt_t);
 
if ($opt_t) {
        $TIMEOUT=$opt_t;
}
 
# Just in case of problems, let's not hang Nagios
$SIG{'ALRM'} = sub {
        print "UNKNOWN - Plugin Timed out\n";
        exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);
 
if ($opt_V) {
        exit $ERRORS{'OK'};
}
 
if ($opt_h) {
        print_help();
        exit $ERRORS{'OK'};
}
  
sub print_usage () {
        print "Usage:\n";
        print "  $PROGNAME -H <hostname> -o <OID> -r <regex> [-C <COMMUNITY>] [-p <port>] [-P <snmp-version>] [-w <INTEGER>] [-c <INTEGER>]\n";
        print "  $PROGNAME [-h | --help]\n";
        print "  $PROGNAME [-V | --version]\n";
	print "\n\nOptions:\n";
	print "  -H, --hostname\n";
	print "     Host name or IP Address\n";
        print "  -o, --oid\n";
        print "     Object identifier parent tree whose values you wish to query\n";
        print "  -r, --regex\n";
        print "     Regex like PoolName you want to check\n";
        print "  -C, --community\n";
        print "     Optional community string for SNMP communication\n";
	print "     (default is \"public\")\n";
        print "  -p, --port\n";
        print "     Port number (default: 161)\n";
        print "  -P, --protocol\n";
        print "     SNMP protocol version [1,2c]\n";
	print "     (SNMP Version 3 is not supported yet)\n";
        print "  -w, --warning\n";
        print "     FIXME: Some usefull explain for warning threshold\n";
        print "  -c, --critical\n";
        print "     FIXME: Some usefull explain for critical threshold\n";
        print "  -h, --help\n";
        print "     Print detailed help screen\n";
        print "  -V, --version\n";
        print "     Print version information\n\n";
 
}
 
sub print_help () {
        print "Copyright (c) 200x SOME AUTHOR\n\n";
        print_usage();
        print "\n";
        print "  host  Give me some Host to work with\n";
        print "  <help>  This output\n";
        print "\n";
        support();
}
 
 
 
#print "Given Options:\n";
#printf ("Hostname: %s\n",$opt_H);
#printf ("OID: %s\n",$opt_o);
#printf ("Regex: %s\n",$opt_r);
#printf ("Community: %s\n",$opt_C);
#printf ("Port: %i\n",$opt_p);
#printf ("SNMP Version: %s\n",$opt_P);
#printf ("Warning %s\n",$opt_w);
#printf ("Critical %s\n",$opt_c);

my $Version='0.2';

use strict;

my $_cmd_sar ="sar";
my $_sar_opt =" -P ALL";
my $_sar_out;
my @sar_array;
my $sar_array_length;
my @cpu_array; #Werte der einzelnen CPUs
my $cpu_count; #Anzahl der CPUs im System
my @tmp_array;
my $tmp_s;
my $critical = $opt_c; 
my $warn = $opt_w;
my $_status = "OK"; #Statusmeldung an Nagios
my $_message; #Nachricht an Nagios
my $_rc; #Return-Code für Nagios

#Kommandozeilenparameter für "critical" und "warn"
#einlesen


#"sar"-Kommando ausführen und Ausgabe zeilenweise
#in ein Array ablegen
$_sar_out = qx($_cmd_sar $_sar_opt);
@sar_array = split(/\n/,$_sar_out);
$sar_array_length = @sar_array;

#Anzahl der CPUs bestimmen
#letzte Zeile der sar-Ausgabe ansehen
$tmp_s =  $sar_array[$sar_array_length-1];
#die vielen Leerzeichen zwischen den Werten auf 
#jeweils ein Leerzeichen dezimieren
$tmp_s =~ tr/ / /s;
#die Werte der Zeile nun in ein Array schreiben
@tmp_array = split(/\s/,$tmp_s);
#der zweite Wert repräsentiert die CPU-Zahl
$cpu_count = $tmp_array[1]+1;

#jetzt für jede CPU "critical" und "warn" überprüfen
#wenn Grenzwert erreicht: CPU melden
for(my $i=1;$i<=$cpu_count;$i++)
{
  #analog zum CPU-Count oben die 6te Spalte
  #als "%ioidle" auslesen
  $tmp_s =  $sar_array[$sar_array_length-$i];
  $tmp_s =~ tr/ / /s;
  @tmp_array = split(/\s/,$tmp_s);
  $cpu_array[$cpu_count+1-$i] = $tmp_array[5];
  $cpu_array[$cpu_count+1-$i] =~ tr/,/./;
  if (($cpu_array[$cpu_count+1-$i] >= $warn) && ($_status ne "Critical"))
  {
    $_status = "Warn";
  }
  if ($cpu_array[$cpu_count+1-$i] >= $critical)
  {
    $_status = "Critical";
  }
}

#Durchschnitt über alle CPUs auslesen
$tmp_s =  $sar_array[$sar_array_length-$cpu_count-1];
$tmp_s =~ tr/ / /s;
@tmp_array = split(/\s/,$tmp_s);
$cpu_array[0] = $tmp_array[5];
$cpu_array[0] =~ tr/,/./;

#Nagios-Meldung zusammenbauen
$_message = $_status.": ALL:".$cpu_array[0]." | ";

#Performance-Daten der CPU(s)
for(my $i=1;$i<=$cpu_count;$i++)
{
  $_message = $_message."CPU".$i."=".$cpu_array[$i]."%;";
  $_message = $_message.$warn.";".$critical.";";
  if($i<$cpu_count){$_message=$_message." ";}
}

print $_message."\n";

$_rc = 3;
if ($_status eq "Critical") {$_rc = 2;}
if ($_status eq "Warn") {$_rc = 1;}
if ($_status eq "OK") {$_rc = 0;}
exit $_rc;
