#!/usr/bin/perl

use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/../../lib";
use Wigo::Probe qw/:all/;

###
# DEFAULT CONFIG
###

my $conf = {
    'processToCheck' => [
        '/usr/sbin/sshd'
    ],
};

init( config => $conf );

# Exec
my @inactiveProcesses;

for my $processName ( @{ config->{'processList'} } )
{
    my $fnret = `ps ax | grep -v grep | grep "$processName"`;

    if ( $? )
    {
        push @inactiveProcesses, $processName; 
    }
}

my $countInactiveProcesses = scalar @inactiveProcesses;
if ( $countInactiveProcesses )
{
    status  300 + $countInactiveProcesses;
    message $countInactiveProcesses . " process not running : " . join( "," , @inactiveProcesses );
}
else
{
    status  100;
    message "All monitored process are running";
}

detail->{'not_running'} = \@inactiveProcesses;
detail->{'running'}     = [ grep { my $p = $_ ; ! grep { $p eq $_ } @inactiveProcesses } @{config->{'processList'}} ];

output 0;
