#!/usr/bin/perl
# Editado - 15-04-2016 - Cleber Campanel - Troca de switch por if para não precisar instalar modulo do perl
# Editado - 04-05-2015 - Cleber Campanel


($op, $warn, $crit) = @ARGV;
if ( $op eq "procs"){ $s=0; $e=1 ; $label='PROCS'; @itens=("run queue","sleep"); $mt=""}
elsif( $op eq "memory") { $s=2; $e=5; $label='MEMORY'; @itens=("Swap used","Mem free","Buff used","Cache used"); $mt="MB" }
elsif( $op eq "swap")   { $s=6; $e=7; $label='SWAP'; @itens=("Swapped from disk","Swapped to disk"); $mt="/s"}
elsif( $op eq "io")     { $s=8; $e=9; $label='IO'; @itens=("Blocks received","Blocks sent"); $mt="/s"}
elsif( $op eq "system") { $s=10; $e=11; $label='SYSTEM'; @itens=("Interrupts","TCS"); $mt="/s"}
elsif( $op eq "cpu")    { $s=12; $e=15; $label='CPU'; @itens=("user","system","idle","waiting"); $mt="%"}
elsif( $op eq "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;;;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);
    #print "aki: $utilization \n";
    $perfutilization = "'cpu_utilization'=$utilization%;$warn;$crit;0;100";
    $return .= " $utilization$mt";
    if ($utilization >= $crit){
	$average = 2;
    }elsif ($utilization >= $warn){
	$average = 1;
	}
}

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

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

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