use warnings;

#Nagios Return Codes
$ng_ok = 0;
$ng_warn = 1;
$ng_crit = 2;
$ng_err = 3;

#Input
unless ($#ARGV == 2){
   print "Input must be: 'warnnum critnum allowedlist'\n";
   exit $ng_err;
}

($warn, $crit, $allowedstring) = @ARGV;

@proclist = `tasklist /nh`;
@badproc = ();
$plural = '';

foreach (@proclist){
   chomp;
   next unless $_;
   ($name) = split ' ';
   $name =~ s/\..+$//;
   if ($allowedstring !~ /\Q$name\E/i){
      push @badproc, $name;
   }
}

$badprocs = $#badproc + 1;
$plural = 'es' if ($badprocs > 1);
$type = $ng_ok;

if ($badprocs >= $warn && $badprocs < $crit){
   print "Warning: $badprocs Unknown process$plural Running\n";
   $type = $ng_warn;
}
if ($badprocs >= $crit){
   print "Critical: $badprocs Unknown process$plural Running\n";
   $type = $ng_crit;
}   

if ($type == $ng_warn || $type == $ng_crit){
   foreach (@badproc){
      print "$_ ";
   }
   print "\n";
   exit $type;
}

print "OK: No Unknown Services Running\n";
exit $ng_ok
