Skip to content

Commit

Permalink
enh(plugin): add --since-hours to apps::automation::ansible::tower::m…
Browse files Browse the repository at this point in the history
…ode::jobs
  • Loading branch information
Mathieu GOLLAIN-DUPONT committed Apr 11, 2024
1 parent a3e6103 commit 8047def
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/apps/automation/ansible/tower/mode/jobs.pm
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ sub new {

$options{options}->add_options(arguments => {
'filter-name:s' => { name => 'filter_name' },
'since-hours:s' => { name => 'since_hours' },
'display-failed-jobs' => { name => 'display_failed_jobs' },
'memory' => { name => 'memory' }
});
Expand All @@ -82,14 +83,20 @@ sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);

if (defined($self->{option_results}->{memory})) {
if (defined($self->{option_results}->{memory}) || defined($self->{option_results}->{since_hours})) {
$self->{statefile_cache}->check_options(%options);
centreon::plugins::misc::mymodule_load(
output => $self->{output},
module => 'Date::Parse',
error_msg => "Cannot load module 'Date::Parse'."
);
}

if (defined($self->{option_results}->{since_hours})
&& !($self->{option_results}->{since_hours} =~ /^\d+$/ && $self->{option_results}->{since_hours} > 0)) {
$self->{output}->add_option_msg(short_msg => "--since-hours should be a positive integer to count hours");
$self->{output}->option_exit();
}
}

sub manage_selection {
Expand All @@ -111,7 +118,7 @@ sub manage_selection {
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
&& $job->{name} !~ /$self->{option_results}->{filter_name}/);

if (defined($self->{option_results}->{memory}) && defined($job->{finished})) {
if (defined($job->{finished})) {
my $finished_time = Date::Parse::str2time($job->{finished});
if (!defined($finished_time)) {
$self->{output}->output_add(
Expand All @@ -120,7 +127,15 @@ sub manage_selection {
);
next;
}
next if (defined($last_time) && $last_time > $finished_time);

if (defined($self->{option_results}->{memory})) {
next if (defined($last_time) && $last_time > $finished_time);
}

if (defined($self->{option_results}->{since_hours})) {
my $cutoff_time = $current_time - ($self->{option_results}->{since_hours} * 3600);
next if $finished_time < $cutoff_time;
}
}

$self->{global}->{ $job->{status} }++;
Expand Down Expand Up @@ -153,6 +168,10 @@ Check jobs.
Filter job name (can use regexp).
=item B<--since-hours>
Only check jobs that finished less than X hours ago
=item B<--display-failed-jobs>
Display failed jobs list in verbose output.
Expand Down

0 comments on commit 8047def

Please sign in to comment.