use warnings;

#Nagios Return Codes
$ng_ok = 0;
$ng_warn = 1;
$ng_crit = 2;
$ng_err = 3;

#Input
unless ($#ARGV == 0){
   print "Input must be: 'list of essential services'\n";
   exit $ng_err;
}

$essential = shift @ARGV;
@essential = split " ", $essential;

@proclist = `tasklist /nh`;
$plural = '';
@notrunning = ();

foreach $needed(@essential){
   $there = 0;
   foreach $process(@proclist){
      chomp $process;
      next unless $process;
      ($name) = split ' ', $process;
      $name =~ s/\..+$//;
      if ($process =~ /$needed/i){
         $there = 1;
      }
   }
   push (@notrunning, $needed) if ($there == 0);
}

#Report
if (@notrunning){
   $notrunning = $#notrunning + 1;
   print "Critical: $notrunning essential processes are not running\n";
   print join(' ', @notrunning);
   print "\n";
   exit $ng_crit;
} else {
   print "OK: All essential processes appear to be running\n";
   exit $ng_ok
}
