#!/usr/bin/perl
# By Cléber Campanel
# cleber.campanel@gmail.com
#

# imports
use strict;
use Nagios::Plugin;
use Data::Dumper qw(Dumper);
# variables
my $np;
my $ps;


# plugin setup
$np = Nagios::Plugin->new(
        plugin                   => 'mem_cpu_per_proc',
        shortname        => ' ',
        version                => '1.0',
        usage                   => 'Usage: %s -p <process> [-w <warning %>] [-c <critical %>]',
        blurb                    => 'This plugin checks MEM and CPU the process activity ',
        license                 => 'This nagios plugin is free software, and comes with ABSOLUTELY no WARRANTY!'
);

$np->add_arg(spec => 'warning|w=s',
                          help => "warning threshold, values in percent [num procs,mem,cpu]  (default: 5,25,20)",
                          required => 0,
                          default => '10,25,20');
$np->add_arg(spec => 'critical|c=s',
                          help => "Critical threshold, values in percent [num procs,mem,cpu]  (default: 10,30,25)",
                          required => 0,
                          default => '20,30,25');
$np->add_arg(spec => 'process|p=s',
                          help => "process(es) to be check separed by comma",
                          required => 1,);

# main
$np->getopts;


$ps = `which ps 2> /dev/null`;
chop $ps;

my $warning = $np->opts->warning;
my @arr_warning = split(",",$warning);
my $w_nprocs = @arr_warning[0];
my $w_mem = @arr_warning[1];
my $w_cpu = @arr_warning[2];


my $critical = $np->opts->critical;
my @arr_critical= split(",",$critical);
my $c_nprocs = @arr_critical[0];
my $c_mem = @arr_critical[1];
my $c_cpu = @arr_critical[2];

#print Dumper $w_mem;
#print Dumper $w_cpu;
#print Dumper $c_mem;
#print Dumper $c_cpu;

my $process = $np->opts->process;
my @line;
my $cpu;
my $mem;
my $vsz;
my $rss;
my $nprocs;


foreach my $entry (split("\n",`$ps u -C $process`)) 
{
	@line= split(" ",$entry);
	$cpu+=@line[2];
	$mem+=@line[3];
	$vsz+=@line[4];
	$rss+=@line[5];
	$nprocs++;
}
$nprocs--;

if(($cpu==0)&&($mem==0)){
	$np->nagios_exit (3,"Proc $process not running!!!" );
}

#print Dumper $cpu;
#print Dumper $mem;
#print Dumper $nprocs;
#print Dumper $vsz;
#print Dumper $rss;

# Threshold methods 

#nprocs
my $code_nprocs = $np->check_threshold(
  check => $nprocs,
  warning => $w_nprocs,
  critical => $c_nprocs,
);

#mem
my $code_mem = $np->check_threshold(
	check => $mem,
	warning => $w_mem,
	critical => $c_mem,
);

#print Dumper $code_mem;
#cpu
my $code_cpu = $np->check_threshold(
	check => $cpu,
	warning => $w_cpu,
	critical => $c_cpu,
);
#print Dumper $code_cpu;
my $code = 3;
if($code_nprocs >= $code_cpu and $code_nprocs >= $code_mem){
  $code = $code_nprocs;
}elsif($code_mem >= $code_cpu and $code_mem >= $code_nprocs){
  $code = $code_mem;
}else{
  $code = $code_cpu;
}

#print Dumper $code;
my $msg = $process."=".$nprocs." MEM=".$mem."% CPU=".$cpu."% rss=".$rss."KB vsz=".$vsz."KB";
my $perf = " | 'nprocs'=".$nprocs.";".$w_nprocs.";".$c_nprocs.";0; 'mem'=".$mem."%;".$w_mem.";".$c_mem.";0;100 'cpu'=".$cpu."%;".$w_cpu.";".$c_cpu.";0;100 'vsz'=".$vsz."KB;;;0; 'rss'=".$rss."KB;;;0;";
$np->nagios_exit($code,$msg.$perf);
