#!/usr/bin/perl
use Switch;

($op, $warn, $crit) = @ARGV;
switch ($op){
   case "procs"  { $s=0; $e=1 ; $label='PROCS'; @itens=("run queue","sleep"); $mt=""}
   case "memory" { $s=2; $e=5; $label='MEMORY'; @itens=("Swap used","Mem free","Buff used","Cache used"); $mt="MB"}
   case "swap"   { $s=6; $e=7; $label='SWAP'; @itens=("Swapped from disk","Swapped to disk"); $mt="/s"}
   case "io"     { $s=8; $e=9; $label='IO'; @itens=("Blocks received","Blocks sent"); $mt="/s"}
   case "system" { $s=10; $e=11; $label='SYSTEM'; @itens=("Interrupts","TCS"); $mt="/s"}
   case "cpu"    { $s=12; $e=15; $label='CPU'; @itens=("user","system","idle","waiting"); $mt="%"}
   case "cpu_utilization" { $s=12; $e=15; $label='CPU Utilization'; @itens=("user","system","idle","waiting","1"); $mt="%"}
   else {
        print "Usage:\n\t$0 [option] [warning] [critical]\n\nOPTIONS:\n";
	print "procs\n\tr: The number of processes waiting for run time(run queue size).\n\tb: The number of processes in uninterruptible sleep.\n\n";
	print "memory\n\tswpd: the amount of virtual memory used.\n\tfree: the amount of idle memory.\n\tbuff: the amount of memory used as buffers.\n\tcache: the amount of memory used as cache.\n\n";
	print "swap\n\tsi: Amount of memory swapped in from disk (/s).\n\tso: Amount of memory swapped to disk (/s).\n\n";
	print "io\n\tbi: Blocks received from a block device (blocks/s).\n\tbo: Blocks sent to a block device (blocks/s).\n\n";
	print "system\n\tin: The number of interrupts per second, including the clock.\n\tcs: The number of context switches per second.\n\n";
	print "cpu\n\tus: Time spent running non-kernel code. (user time, including nice time)\n\tsy: Time spent running kernel code. (system time)\n\tid: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.\n\twa: Time spent waiting for IO. Prior to Linux 2.5.41, shown as zero.\n\n";
	print "cpu_utilization\n\tThese are percentages of total CPU time.\n\n";
	exit (1);
	}
}

if ($#ARGV < 1) {
        print "Usage:\n\t$0 [option] [warning] [critical]\n";
        exit(-1);
}

@total;
$average = 0;
$utilization = 0;
@command = `/usr/bin/vmstat 1 10`;
#print @command;
chomp (@command);

for ($i=2; $i <= $#command; $i++){
    @temp = split (" ", $command[$i]);
    for ($j=$s; $j <= $e; $j++){
    	$total[$j] = $total[$j] + $temp[$j];
    }
}

$it = 0;
for ($j=$s; $j <= $e; $j++){
    $total[$j] = $total[$j] / 10;

    if ( $s == 2 ){
	$total[$j] = $total[$j] / 1024;
        $total[$j] = sprintf ("%i",$total[$j]);
    }

    if ( $itens[4] == 1 ){
	$utilization = $utilization + $total[$j];
    }else {
	$return .= " $itens[$it]:$total[$j]$mt";
    }
    $perf .= " '$itens[$it]'=$total[$j]$mt;$warn;$crit;0;;";
    $it++;

    if (($itens[$it] == "Mem free")||($itens[$it] == "idle")){
	next;
    }

    if ($total[$j] >= $crit){
	$average = 2;
	next;
    }
    if ($total[$j] >= $warn){
	$average = 1;
    }
}

if ( $itens[4] == 1 ){
    $utilization = $utilization - $total[14];	
    $utilization = sprintf ("%.1f",$utilization);
    $return .= " $utilization$mt";
    if ($utilization >= $crit){
	$average = 2;
    }elsif ($utilization >= $warn){
	$average = 1;
	}
}

if ($average == 2){
    print "CRITICAL - Average $label:$return|$perf\n";
    exit(2);
}

if ($average == 1){
    print "WARNING - Average $label:$return|$perf\n";
    exit(1);
}

print "OK - Average $label:$return|$perf\n";
exit (0);
