# ---------------------------------------------------------------
# command
# ---------------------------------------------------------------
# 
# Description
# 
# This module is used to execute an arbitrary command
# this command must return status 0 if test is ok,
# and status != 0 if it is wrong.
#
# All text messages send to standar output is send as status
# messages to bsdisplay.
# 
# note that command are exec with "/" base directory.
# uxmon-net example:
#
# localhost exec="/opt/bs/otros/test.pl test1" time=10 item=test command
# 
# Parameters
# 
# exec  command to execute
# item	The item to report (default: command)
# time	The number of seconds to wait for a response before
# 		timing out.  (default: 5)
# 
# Manuel de Vega Barreiro
# Madrid, Spain
# mbarreiro@red.madritel.es
#
# Altered by Thomas Aeby - all bugs are mine :-)
#
# 
# ---------------------------------------------------------------

# Load the monitor
my $command;
my $timeout = ($args{"time"})?($args{"time"}):5;

unless( $command = $memory{"command_${timeout}_$args{'frequency'}"} ) {
    &uxmon::load_module( "cmd" );
    &uxmon::checker(
        $memory{"command_${timeout}_$args{'frequency'}"} = $command = Monitor::cmd->new($timeout)
    );
    $command->sched_frequency( $args{"frequency"} );
}

#
# $args{"alias"} is always set and meant to be the name we want to report results as
my $host = $args{"alias"};
my $exec = $args{"exec"};
my $item = $args{"item"} ? $args{"item"} : 'command';
$item = "$args{alias}.$item";

my $rules = $args{"results"};

my @rules = (
    [ "0", "green", "OK: $exec" ],
    [ "timeout", "red", "timed out: $exec" ],
    [ "*", "red", "error: $exec" ]
);

if( $rules ) {
    @rules = ();
    foreach my $rule (split( ";", $rules)) {
	next unless( $rule =~ /^(.*?):(.*?):(.*)$/ );
	push( @rules, [ $1, $2, $3 ] );
    }
}

$command->add_check($item,$exec,@rules);

1;
