diff --git a/apps/backup/veeam/local/mode/jobstatus.pm b/apps/backup/veeam/local/mode/jobstatus.pm index 2abbbea83..2603d7473 100644 --- a/apps/backup/veeam/local/mode/jobstatus.pm +++ b/apps/backup/veeam/local/mode/jobstatus.pm @@ -113,7 +113,7 @@ sub new { 'filter-start-time:s' => { name => 'filter_start_time' }, 'ok-status:s' => { name => 'ok_status', default => '' }, 'warning-status:s' => { name => 'warning_status', default => '' }, - 'critical-status:s' => { name => 'critical_status', default => '%{is_running} == 0 and not %{status} =~ /Success/i' }, + 'critical-status:s' => { name => 'critical_status', default => '%{is_running} == 0 and not %{status} =~ /success/i' }, 'warning-long:s' => { name => 'warning_long' }, 'critical-long:s' => { name => 'critical_long' } }); diff --git a/apps/backup/veeam/local/mode/resources/types.pm b/apps/backup/veeam/local/mode/resources/types.pm index f6887571a..b3b4a224c 100644 --- a/apps/backup/veeam/local/mode/resources/types.pm +++ b/apps/backup/veeam/local/mode/resources/types.pm @@ -97,10 +97,11 @@ $job_type = { }; $job_result = { - 0 => 'Success', - 1 => 'Warning', - 2 => 'Failed', - -1 => 'None' + 0 => 'success', + 1 => 'warning', + 2 => 'failed', + -1 => 'none', + -10 => 'never' }; $job_tape_type = { @@ -114,7 +115,7 @@ $job_tape_type = { }; $job_tape_result = { - 0 => 'None', 1 => 'Success', 2 => 'Warning', 3 => 'Failed' + 0 => 'none', 1 => 'success', 2 => 'warning', 3 => 'failed' }; $job_tape_state = { diff --git a/apps/backup/veeam/local/mode/vsbjobs.pm b/apps/backup/veeam/local/mode/vsbjobs.pm new file mode 100644 index 000000000..bb9041260 --- /dev/null +++ b/apps/backup/veeam/local/mode/vsbjobs.pm @@ -0,0 +1,291 @@ +# +# Copyright 2022 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package apps::backup::veeam::local::mode::vsbjobs; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::common::powershell::veeam::vsbjobs; +use apps::backup::veeam::local::mode::resources::types qw($job_type $job_result); +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); +use centreon::plugins::misc; +use JSON::XS; + +sub custom_status_output { + my ($self, %options) = @_; + + return sprintf( + 'last status: %s [duration: %s]', + $self->{result_values}->{status}, + centreon::plugins::misc::change_seconds(value => $self->{result_values}->{duration}) + ); +} + +sub prefix_job_output { + my ($self, %options) = @_; + + return sprintf( + "SureBackup job '%s' [type: %s] ", + $options{instance_value}->{name}, + $options{instance_value}->{type} + ); +} + +sub prefix_global_output { + my ($self, %options) = @_; + + return 'Number of SureBackup jobs '; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', }, + { name => 'jobs', type => 1, cb_prefix_output => 'prefix_job_output', message_multiple => 'All SureBackup jobs are ok', skipped_code => { -10 => 1 } } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'jobs-detected', nlabel => 'sure_backup.jobs.detected.count', set => { + key_values => [ { name => 'detected' } ], + output_template => 'detected jobs: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + }, + { label => 'jobs-detected', nlabel => 'sure_backup.jobs.success.count', set => { + key_values => [ { name => 'success' } ], + output_template => 'success: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + }, + { label => 'jobs-failed', nlabel => 'sure_backup.jobs.failed.count', set => { + key_values => [ { name => 'failed' } ], + output_template => 'failed: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + }, + { label => 'jobs-warning', nlabel => 'sure_backup.jobs.warning.count', set => { + key_values => [ { name => 'warning' } ], + output_template => 'warning: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + } + ]; + + $self->{maps_counters}->{jobs} = [ + { label => 'status', type => 2, critical_default => 'not %{status} =~ /success/i', set => { + key_values => [ + { name => 'name' }, { name => 'type' }, + { name => 'status' }, { name => 'duration' } + ], + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + } + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'timeout:s' => { name => 'timeout', default => 50 }, + 'command:s' => { name => 'command', default => 'powershell.exe' }, + 'command-path:s' => { name => 'command_path' }, + 'command-options:s' => { name => 'command_options', default => '-InputFormat none -NoLogo -EncodedCommand' }, + 'no-ps' => { name => 'no_ps' }, + 'ps-exec-only' => { name => 'ps_exec_only' }, + 'ps-display' => { name => 'ps_display' }, + 'filter-name:s' => { name => 'filter_name' }, + 'filter-type:s' => { name => 'filter_type' } + }); + + return $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + if (!defined($self->{option_results}->{no_ps})) { + my $ps = centreon::common::powershell::veeam::vsbjobs::get_powershell(); + if (defined($self->{option_results}->{ps_display})) { + $self->{output}->output_add( + severity => 'OK', + short_msg => $ps + ); + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); + } + + $self->{option_results}->{command_options} .= " " . centreon::plugins::misc::powershell_encoded($ps); + } + + my ($stdout) = centreon::plugins::misc::execute( + output => $self->{output}, + options => $self->{option_results}, + command => $self->{option_results}->{command}, + command_path => $self->{option_results}->{command_path}, + command_options => $self->{option_results}->{command_options} + ); + if (defined($self->{option_results}->{ps_exec_only})) { + $self->{output}->output_add( + severity => 'OK', + short_msg => $stdout + ); + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); + } + + my $decoded; + eval { + $decoded = JSON::XS->new->decode($stdout); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); + $self->{output}->option_exit(); + } + + #[ + # { name: 'xxxx', type: 0, result: 0, creationTimeUTC: 1512875246.2, endTimeUTC: 1512883615.377 }, + # { name: 'xxxx', type: 0, result: 1, creationTimeUTC: '', endTimeUTC: '' }, + # { name: 'xxxx', type: 1, result: 0, creationTimeUTC: 1513060425.027, endTimeUTC: -2208992400 } + #] + + $self->{global} = { detected => 0, success => 0, failed => 0, warning => 0 }; + $self->{jobs} = {}; + my $current_time = time(); + foreach my $job (@$decoded) { + $job->{creationTimeUTC} =~ s/,/\./; + $job->{endTimeUTC} =~ s/,/\./; + + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $job->{name} !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping job '" . $job->{name} . "': no matching filter.", debug => 1); + next; + } + if (defined($self->{option_results}->{filter_type}) && $self->{option_results}->{filter_type} ne '' && + $job->{type} !~ /$self->{option_results}->{filter_type}/) { + $self->{output}->output_add(long_msg => "skipping job '" . $job->{name} . "': no matching filter type.", debug => 1); + next; + } + # Sometimes we may get such JSON: [{"lastResult":null,"name":null,"lastState":null,"type":null,"enabled":null}] + if (!defined($job->{name})) { + $self->{output}->output_add(long_msg => "skipping nulled job (empty json)", debug => 1); + next; + } + + my $elapsed_time = 0; + $elapsed_time = $current_time - $job->{creationTimeUTC} if ($job->{creationTimeUTC} =~ /[0-9]/); + + my $status = defined($job_result->{ $job->{result} }) && $job_result->{ $job->{result} } ne '' ? $job_result->{ $job->{result} } : '-'; + $self->{jobs}->{ $job->{name} } = { + name => $job->{name}, + type => defined($job_type->{ $job->{type} }) ? $job_type->{ $job->{type} } : 'unknown', + duration => $elapsed_time, + status => $status + }; + $self->{global}->{$status}++ if (defined($self->{global}->{$status})); + $self->{global}->{detected}++; + } +} + +1; + +__END__ + +=head1 MODE + +Check SureBackup jobs. + +=over 8 + +=item B<--timeout> + +Set timeout time for command execution (Default: 50 sec) + +=item B<--no-ps> + +Don't encode powershell. To be used with --command and 'type' command. + +=item B<--command> + +Command to get information (Default: 'powershell.exe'). +Can be changed if you have output in a file. To be used with --no-ps option!!! + +=item B<--command-path> + +Command path (Default: none). + +=item B<--command-options> + +Command options (Default: '-InputFormat none -NoLogo -EncodedCommand'). + +=item B<--ps-display> + +Display powershell script. + +=item B<--ps-exec-only> + +Print powershell output. + +=item B<--filter-name> + +Filter job name (can be a regexp). + +=item B<--filter-type> + +Filter job type (can be a regexp). + +=item B<--unknown-status> + +Set unknown threshold for status. +Can used special variables like: %{name}, %{type}, %{status}, %{duration}. + +=item B<--warning-status> + +Set warning threshold for status. +Can used special variables like: %{name}, %{type}, %{status}, %{duration}. + +=item B<--critical-status> + +Set critical threshold for status (Default: 'not %{status} =~ /success/i'). +Can used special variables like: %{name}, %{type}, %{status}, %{duration}. + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'jobs-detected', 'jobs-success', 'jobs-warning', 'jobs-failed'. + +=back + +=cut diff --git a/apps/backup/veeam/local/plugin.pm b/apps/backup/veeam/local/plugin.pm index 5a2783ee8..4706f7076 100644 --- a/apps/backup/veeam/local/plugin.pm +++ b/apps/backup/veeam/local/plugin.pm @@ -34,7 +34,8 @@ sub new { 'job-status' => 'apps::backup::veeam::local::mode::jobstatus', 'licenses' => 'apps::backup::veeam::local::mode::licenses', 'list-jobs' => 'apps::backup::veeam::local::mode::listjobs', - 'tape-jobs' => 'apps::backup::veeam::local::mode::tapejobs' + 'tape-jobs' => 'apps::backup::veeam::local::mode::tapejobs', + 'vsb-jobs' => 'apps::backup::veeam::local::mode::vsbjobs' }; return $self; diff --git a/centreon/common/powershell/veeam/jobstatus.pm b/centreon/common/powershell/veeam/jobstatus.pm index 5b542b40e..c66dcebdf 100644 --- a/centreon/common/powershell/veeam/jobstatus.pm +++ b/centreon/common/powershell/veeam/jobstatus.pm @@ -47,21 +47,31 @@ Try { $items = New-Object System.Collections.Generic.List[Hashtable]; - $jobs = Get-VBRJob - foreach ($job in $jobs) { + $sessions = @{} + Get-VBRBackupSession | Sort CreationTimeUTC -Descending | ForEach-Object { + $jobId = $_.jobId.toString() + if (-not $sessions.ContainsKey($jobId)) { + $sessions[$jobId] = @{} + $sessions[$jobId].result = $_.Result.value__ + $sessions[$jobId].creationTimeUTC = (get-date -date $_.CreationTime.ToUniversalTime() -Uformat ' . "'%s'" . ') + $sessions[$jobId].endTimeUTC = (get-date -date $_.EndTime.ToUniversalTime() -Uformat ' . "'%s'" . ') + } + } + + Get-VBRJob | ForEach-Object { $item = @{} - $item.name = $job.Name - $item.type = $job.JobType.value__ - $item.isRunning = $job.isRunning + $item.name = $_.Name + $item.type = $_.JobType.value__ + $item.isRunning = $_.isRunning $item.result = -10 $item.creationTimeUTC = "" $item.endTimeUTC = "" - $lastsession = $job.findlastsession() - if ($lastsession) { - $item.result = $lastsession.Result.value__ - $item.creationTimeUTC = (get-date -date $lastsession.creationTime.ToUniversalTime() -Uformat ' . "'%s'" . ') - $item.endTimeUTC = (get-date -date $lastsession.EndTime.ToUniversalTime() -Uformat ' . "'%s'" . ') + $guid = $_.Id.Guid.toString() + if ($sessions.ContainsKey($guid)) { + $item.result = $sessions[$guid].result + $item.creationTimeUTC = $sessions[$guid].creationTimeUTC + $item.endTimeUTC = $sessions[$guid].endTimeUTC } $items.Add($item) diff --git a/centreon/common/powershell/veeam/vsbjobs.pm b/centreon/common/powershell/veeam/vsbjobs.pm new file mode 100644 index 000000000..7e5bb8935 --- /dev/null +++ b/centreon/common/powershell/veeam/vsbjobs.pm @@ -0,0 +1,100 @@ +# +# Copyright 2022 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package centreon::common::powershell::veeam::vsbjobs; + +use strict; +use warnings; +use centreon::common::powershell::functions; +use centreon::common::powershell::veeam::functions; + +sub get_powershell { + my (%options) = @_; + + my $ps = ' +$ProgressPreference = "SilentlyContinue" +$WarningPreference = "SilentlyContinue" + +$culture = new-object "System.Globalization.CultureInfo" "en-us" +[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture +'; + + $ps .= centreon::common::powershell::functions::escape_jsonstring(%options); + $ps .= centreon::common::powershell::functions::convert_to_json(%options); + $ps .= centreon::common::powershell::veeam::functions::powershell_init(); + + $ps .= ' + +Try { + $ErrorActionPreference = "Stop" + + $items = New-Object System.Collections.Generic.List[Hashtable]; + + $sessions = @{} + Get-VSBSession | Sort CreationTimeUTC -Descending | ForEach-Object { + $jobId = $_.jobId.toString() + if (-not $sessions.ContainsKey($jobId)) { + $sessions[$jobId] = @{} + $sessions[$jobId].result = $_.Result.value__ + $sessions[$jobId].creationTimeUTC = (get-date -date $_.CreationTime.ToUniversalTime() -Uformat ' . "'%s'" . ') + $sessions[$jobId].endTimeUTC = (get-date -date $_.EndTime.ToUniversalTime() -Uformat ' . "'%s'" . ') + } + } + + Get-VSBJob | ForEach-Object { + $item = @{} + $item.name = $_.Name + $item.type = $_.JobType.value__ + $item.result = -10 + $item.creationTimeUTC = "" + $item.endTimeUTC = "" + + $guid = $_.Id.Guid.toString() + if ($sessions.ContainsKey($guid)) { + $item.result = $sessions[$guid].result + $item.creationTimeUTC = $sessions[$guid].creationTimeUTC + $item.endTimeUTC = $sessions[$guid].endTimeUTC + } + + $items.Add($item) + } + + $jsonString = $items | ConvertTo-JSON-20 -forceArray $true + Write-Host $jsonString +} Catch { + Write-Host $Error[0].Exception + exit 1 +} + +exit 0 +'; + + return $ps; +} + +1; + +__END__ + +=head1 DESCRIPTION + +Method to get veeam SureBackup jobs informations. + +=cut