diff --git a/apps/vtom/restapi/custom/api.pm b/apps/vtom/restapi/custom/api.pm index 3a8629712..a5477323d 100644 --- a/apps/vtom/restapi/custom/api.pm +++ b/apps/vtom/restapi/custom/api.pm @@ -133,6 +133,48 @@ sub settings { $self->{http}->set_options(%{$self->{option_results}}); } +sub cache_environment { + my ($self, %options) = @_; + + my $has_cache_file = $options{statefile}->read(statefile => 'cache_vtom_env_' . $self->{hostname} . '_' . $self->{port}); + my $timestamp_cache = $options{statefile}->get(name => 'last_timestamp'); + my $environments = $options{statefile}->get(name => 'environments'); + if ($has_cache_file == 0 || !defined($timestamp_cache) || ((time() - $timestamp_cache) > (($options{reload_cache_time}) * 60))) { + $environments = {}; + my $datas = { last_timestamp => time(), environments => $environments }; + my $result = $self->get(path => '/api/environment/list'); + if (defined($result->{result}->{rows})) { + foreach (@{$result->{result}->{rows}}) { + $environments->{$_->{id}} = $_->{name}; + } + } + $options{statefile}->write(data => $datas); + } + + return $environments; +} + +sub cache_application { + my ($self, %options) = @_; + + my $has_cache_file = $options{statefile}->read(statefile => 'cache_vtom_app_' . $self->{hostname} . '_' . $self->{port}); + my $timestamp_cache = $options{statefile}->get(name => 'last_timestamp'); + my $applications = $options{statefile}->get(name => 'applications'); + if ($has_cache_file == 0 || !defined($timestamp_cache) || ((time() - $timestamp_cache) > (($options{reload_cache_time}) * 60))) { + $applications = {}; + my $datas = { last_timestamp => time(), applications => $applications }; + my $result = $self->get(path => '/api/application/list'); + if (defined($result->{result}->{rows})) { + foreach (@{$result->{result}->{rows}}) { + $applications->{$_->{id}} = { name => $_->{name}, envSId => $_->{envSId} }; + } + } + $options{statefile}->write(data => $datas); + } + + return $applications; +} + sub get { my ($self, %options) = @_; diff --git a/apps/vtom/restapi/mode/jobstatus.pm b/apps/vtom/restapi/mode/jobstatus.pm index ec3eb05f0..5f63bbf91 100644 --- a/apps/vtom/restapi/mode/jobstatus.pm +++ b/apps/vtom/restapi/mode/jobstatus.pm @@ -25,6 +25,7 @@ use base qw(centreon::plugins::templates::counter); use strict; use warnings; use centreon::plugins::misc; +use centreon::plugins::statefile; my $instance_mode; @@ -66,7 +67,9 @@ sub custom_status_calc { my ($self, %options) = @_; $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'}; - $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; + $self->{result_values}->{name} = $options{new_datas}->{$self->{instance} . '_name'}; + $self->{result_values}->{environment} = $options{new_datas}->{$self->{instance} . '_environment'}; + $self->{result_values}->{application} = $options{new_datas}->{$self->{instance} . '_application'}; $self->{result_values}->{exit_code} = $options{new_datas}->{$self->{instance} . '_exit_code'}; $self->{result_values}->{family} = $options{new_datas}->{$self->{instance} . '_family'}; $self->{result_values}->{information} = $options{new_datas}->{$self->{instance} . '_information'}; @@ -109,7 +112,9 @@ sub custom_long_calc { my ($self, %options) = @_; $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'}; - $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; + $self->{result_values}->{name} = $options{new_datas}->{$self->{instance} . '_name'}; + $self->{result_values}->{environment} = $options{new_datas}->{$self->{instance} . '_environment'}; + $self->{result_values}->{application} = $options{new_datas}->{$self->{instance} . '_application'}; $self->{result_values}->{elapsed} = $options{new_datas}->{$self->{instance} . '_elapsed'}; $self->{result_values}->{family} = $options{new_datas}->{$self->{instance} . '_family'}; @@ -128,7 +133,8 @@ sub set_counters { $self->{maps_counters}->{job} = [ { label => 'status', threshold => 0, set => { - key_values => [ { name => 'status' }, { name => 'display' }, { name => 'exit_code' }, { name => 'family' }, { name => 'information' } ], + key_values => [ { name => 'status' }, { name => 'name' }, { name => 'environment' }, + { name => 'application' }, { name => 'exit_code' }, { name => 'family' }, { name => 'information' } ], closure_custom_calc => $self->can('custom_status_calc'), closure_custom_output => $self->can('custom_status_output'), closure_custom_perfdata => sub { return 0; }, @@ -136,7 +142,8 @@ sub set_counters { } }, { label => 'long', threshold => 0, set => { - key_values => [ { name => 'status' }, { name => 'display' }, { name => 'elapsed' }, { name => 'family' } ], + key_values => [ { name => 'status' }, { name => 'name' }, { name => 'environment' }, + { name => 'application' }, { name => 'elapsed' }, { name => 'family' } ], closure_custom_calc => $self->can('custom_long_calc'), closure_custom_output => $self->can('custom_long_output'), closure_custom_perfdata => sub { return 0; }, @@ -202,14 +209,18 @@ sub new { $self->{version} = '1.0'; $options{options}->add_options(arguments => { + "filter-application:s" => { name => 'filter_application' }, "filter-name:s" => { name => 'filter_name' }, "filter-family:s" => { name => 'filter_family' }, "warning-status:s" => { name => 'warning_status' }, "critical-status:s" => { name => 'critical_status', default => '%{status} =~ /Error/i' }, "warning-long:s" => { name => 'warning_long' }, "critical-long:s" => { name => 'critical_long' }, + "reload-cache-time:s" => { name => 'reload_cache_time', default => 180 }, }); - + $self->{statefile_cache_app} = centreon::plugins::statefile->new(%options); + $self->{statefile_cache_env} = centreon::plugins::statefile->new(%options); + return $self; } @@ -217,6 +228,8 @@ sub check_options { my ($self, %options) = @_; $self->SUPER::check_options(%options); + $self->{statefile_cache_app}->check_options(%options); + $self->{statefile_cache_env}->check_options(%options); $instance_mode = $self; $self->change_macros(); } @@ -230,7 +243,7 @@ sub prefix_global_output { sub prefix_job_output { my ($self, %options) = @_; - return "job '" . $options{instance_value}->{display} . "' "; + return "job '" . $options{instance_value}->{environment} . '/' . $options{instance_value}->{application} . '/' . $options{instance_value}->{name} . "' "; } sub change_macros { @@ -254,39 +267,57 @@ my %mapping_job_status = ( sub manage_selection { my ($self, %options) = @_; + my $environments = $options{custom}->cache_environment(statefile => $self->{statefile_cache_env}, + reload_cache_time => $self->{option_results}->{reload_cache_time}); + my $applications = $options{custom}->cache_application(statefile => $self->{statefile_cache_app}, + reload_cache_time => $self->{option_results}->{reload_cache_time}); + $self->{job} = {}; $self->{global} = { total => 0, running => 0, unplanned => 0, finished => 0, coming => 0, error => 0 }; - my $result = $options{custom}->get(path => '/api/job/getAll'); - + my $path = '/api/job/getAll'; + if (defined($self->{option_results}->{filter_application}) && $self->{option_results}->{filter_application} ne '') { + $path = '/api/job/list?applicationName=' . $self->{option_results}->{filter_application}; + } + my $result = $options{custom}->get(path => $path); + my $entries = defined($result->{result}) && ref($result->{result}) eq 'ARRAY' ? + $result->{result} : (defined($result->{result}->{rows}) ? + $result->{result}->{rows} : []); + my $current_time = time(); - if (defined($result->{result})) { - foreach my $entry (@{$result->{result}}) { - if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && - $entry->{name} !~ /$self->{option_results}->{filter_name}/) { - $self->{output}->output_add(long_msg => "skipping '" . $entry->{name} . "': no matching filter.", debug => 1); - next; - } - my $family = defined($entry->{family}) ? $entry->{family} : '-'; - if (defined($self->{option_results}->{filter_family}) && $self->{option_results}->{filter_family} ne '' && - $family !~ /$self->{option_results}->{filter_family}/) { - $self->{output}->output_add(long_msg => "skipping '" . $family . "': no matching filter.", debug => 1); - next; - } - - my $information = defined($entry->{information}) ? $entry->{information} : ''; - $information =~ s/\|/-/msg; - my $elapsed_time = $current_time - $entry->{timeBegin}; - - $self->{global}->{total} += 1; - $self->{global}->{lc($mapping_job_status{$entry->{status}})} += 1; - $self->{job}->{$entry->{id}} = { - display => $entry->{name}, - status => $mapping_job_status{$entry->{status}}, information => $information, - exit_code => $entry->{retcode}, - family => $family, - elapsed => $elapsed_time - }; + foreach my $entry (@{$entries}) { + my $application_sid = defined($entry->{applicationSId}) ? $entry->{applicationSId} : + (defined($entry->{appSId}) ? $entry->{appSId} : undef); + my $application = defined($application_sid) && defined($applications->{$application_sid}) ? + $applications->{$application_sid}->{name} : 'unknown'; + my $environment = defined($application_sid) && defined($applications->{$application_sid}) && defined($environments->{$applications->{$application_sid}->{envSId}}) ? + $environments->{$applications->{$application_sid}->{envSId}} : 'unknown'; + my $display = $environment . '/' . $application . '/' . $entry->{name}; + + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $display !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping '" . $display . "': no matching filter.", debug => 1); + next; } + my $family = defined($entry->{family}) ? $entry->{family} : '-'; + if (defined($self->{option_results}->{filter_family}) && $self->{option_results}->{filter_family} ne '' && + $family !~ /$self->{option_results}->{filter_family}/) { + $self->{output}->output_add(long_msg => "skipping '" . $family . "': no matching filter.", debug => 1); + next; + } + + + my $information = defined($entry->{information}) ? $entry->{information} : ''; + $information =~ s/\|/-/msg; + + $self->{global}->{total} += 1; + $self->{global}->{lc($mapping_job_status{$entry->{status}})} += 1; + $self->{job}->{$entry->{id}} = { + name => $entry->{name}, + status => $mapping_job_status{$entry->{status}}, information => $information, + exit_code => defined($entry->{retcode}) ? $entry->{retcode} : '-', + family => $family, application => $application, environment => $environment, + elapsed => defined($entry->{timeBegin}) ? ( $current_time - $entry->{timeBegin}) : undef, + }; } if (scalar(keys %{$self->{job}}) <= 0) { @@ -305,6 +336,10 @@ Check job status. =over 8 +=item B<--filter-application> + +Filter application name (cannot be a regexp). + =item B<--filter-name> Filter name (can be a regexp). @@ -333,24 +368,30 @@ Can be: 'total-error', 'total-running', 'total-unplanned', =item B<--warning-status> Set warning threshold for status (Default: -) -Can used special variables like: %{display}, %{status}, -%{exit_code}, %{family}, %{information} +Can used special variables like: %{name}, %{status}, +%{exit_code}, %{family}, %{information}, %{environment}, %{application} =item B<--critical-status> Set critical threshold for status (Default: '%{exit_code} =~ /Error/i'). -Can used special variables like: %{display}, %{status}, -%{exit_code}, %{family}, %{information} +Can used special variables like: %{name}, %{status}, +%{exit_code}, %{family}, %{information}, %{environment}, %{application} =item B<--warning-long> Set warning threshold for long jobs (Default: none) -Can used special variables like: %{display}, %{status}, %{elapsed}, %{family} +Can used special variables like: %{name}, %{status}, %{elapsed}, +%{family}, %{environment}, %{application} =item B<--critical-long> Set critical threshold for long jobs (Default: none). -Can used special variables like: %{display}, %{status}, %{elapsed}, %{family} +Can used special variables like: %{name}, %{status}, %{elapsed}, +%{family}, %{environment}, %{application} + +=item B<--reload-cache-time> + +Time in seconds before reloading cache file (default: 180). =back