#!/usr/bin/perl
use Getopt::Long;
use strict;

our ($warning,$critical,$help,$path);
our ($check_procs) = "/usr/local/opmon/libexec/check_procs";

sub init {
        my $status = GetOptions(
                "w=i" => \$warning,"warning=i" => \$warning,
                "c=i" => \$critical, "critical=i" => \$critical,
		"p=s" => \$check_procs, "path=s" => \$check_procs,
                "h" => \$help,
                );

	help() if (defined($help));
        usage() if (!defined($warning) || !defined($critical));
        main();
}


sub main {

	if (!-f $check_procs) {
		print "UNKNOWN - Impossivel encontrar $check_procs\n";
		exit 3;
	}

	my @ret = `$check_procs -w $warning -c $critical -s Z`;
	if ($ret[0] !~ "process") {
		print "UNKNOWN - Problema desconhecido - abortando\n";
		exit 3;
	}
	$ret[0] =~ m/^.+:\s*(\d+)\s+p.+$/;

	if (!defined($1)) {
		print "UNKNOWN - Problema desconhecido - abortando\n";
		exit 3;
	}

	if ($1 > $critical) {
		print "CRITICAL - Processos zombies = $1 |procs=$1;$warning;$critical;;\n";
		exit 2;
	}

	if ($1 > $warning) {
                print "WARNING - Processos zombies = $1 |procs=$1;$warning;$critical;;\n";
                exit 1;
	}

        print "OK - Processos zombies = $1 |procs=$1;$warning;$critical;;\n";
        exit 0;




}

sub help {
	print "This plugin verifies the amount of zombie process\n";
	print "It receives two parameters(-w and -c) for warning and critical\n";
	print "A third parameter points to nagios-plugins 'check_procs'(default location under /usr/local/opmon/libexec/)\n\n";
	usage();
}


sub usage {
	print "Usage:\n\t$0 -w <warning> -c <critical> [-h] [-p /path/to/check_procs]\n\n";
	exit 3;
}

init();
