Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ctor 1321 plugin apps backup veeam vbem restapi mode jobs list jobs handle replication jobs #5489

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/apps/backup/veeam/vbem/restapi/custom/api.pm
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,18 @@ sub cache_backup_job_session {
return $datas;
}

sub cache_replica_job_session {
my ($self, %options) = @_;

my $datas = $self->get_replica_job_session(disable_cache => 1, timeframe => $options{timeframe});
$self->write_cache_file(
statefile => 'replica_job_session',
response => $datas
);

return $datas;
}

sub cache_repository {
my ($self, %options) = @_;

Expand Down Expand Up @@ -281,6 +293,24 @@ sub get_backup_job_session {
);
}

sub get_replica_job_session {
my ($self, %options) = @_;

return $self->get_cache_file_response(statefile => 'replica_job_session')
if (defined($self->{option_results}->{cache_use}) && !defined($options{disable_cache}));

my $creation_time = DateTime->now->subtract(seconds => $options{timeframe})->iso8601();

return $self->request_api(
endpoint => '/api/query',
get_param => [
'type=ReplicaJobSession',
'format=Entities',
'filter=CreationTime>=' . $creation_time
]
);
}

sub get_repository {
my ($self, %options) = @_;

Expand Down
47 changes: 46 additions & 1 deletion src/apps/backup/veeam/vbem/restapi/mode/jobs.pm
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ sub manage_selection {
my ($self, %options) = @_;

my $jobs_exec = $options{custom}->get_backup_job_session(timeframe => $self->{option_results}->{timeframe});
my $jobs_replica = $options{custom}->get_replica_job_session(timeframe => $self->{option_results}->{timeframe});

my $ctime = time();

Expand Down Expand Up @@ -278,6 +279,50 @@ sub manage_selection {
$self->{jobs}->{ $job->{JobUid} }->{failed}->{failed}++;
}
}
foreach my $job (@{$jobs_replica->{Entities}->{ReplicaJobSessions}->{ReplicaJobSessions}}) {
next if (defined($self->{option_results}->{filter_uid}) && $self->{option_results}->{filter_uid} ne '' && $job->{JobUid} !~ /$self->{option_results}->{filter_uid}/);
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && $job->{JobName} !~ /$self->{option_results}->{filter_name}/);
next if (defined($self->{option_results}->{filter_type}) && $self->{option_results}->{filter_type} ne '' && $job->{JobType} !~ /$self->{option_results}->{filter_type}/);

if (!defined($self->{jobs}->{ $job->{JobUid} })) {
$self->{jobs}->{ $job->{JobUid} } = {
name => $job->{JobName},
type => $job->{JobType},
failed => { name => $job->{JobName}, total => 0, failed => 0 }
};
$self->{global}->{detected}++;
}

$job->{CreationTimeUTC} =~ /^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)/;
my $dt = DateTime->new(year => $1, month => $2, day => $3, hour => $4, minute => $5, second => $6);
my $epoch = $dt->epoch();

if (!defined($self->{jobs}->{ $job->{JobUid} }->{executions}) || $epoch > $self->{jobs}->{ $job->{JobUid} }->{executions}->{last}->{epoch}) {
$self->{jobs}->{ $job->{JobUid} }->{executions}->{last} = {
jobName => $job->{JobName},
started => $job->{CreationTimeUTC},
status => $job->{Result},
epoch => $epoch
};

$self->{jobs}->{ $job->{JobUid} }->{timers} = {
name => $job->{JobName},
lastExecSeconds => $ctime - $epoch,
lastExecHuman => centreon::plugins::misc::change_seconds(value => $ctime - $epoch)
};

if ($job->{State} =~ /Starting|Working|Resuming/i) {
my $duration = $ctime - $epoch;
$self->{jobs}->{ $job->{JobUid} }->{timers}->{durationSeconds} = $duration;
$self->{jobs}->{ $job->{JobUid} }->{timers}->{durationHuman} = centreon::plugins::misc::change_seconds(value => $duration);
}
}

$self->{jobs}->{ $job->{JobUid} }->{failed}->{total}++;
if (defined($job->{Result}) && $job->{Result} =~ /Failed/i) {
$self->{jobs}->{ $job->{JobUid} }->{failed}->{failed}++;
}
}

foreach my $uid (keys %{$self->{jobs}}) {
$self->{jobs}->{$uid}->{failed}->{failedPrct} = $self->{jobs}->{$uid}->{failed}->{total} > 0 ? $self->{jobs}->{$uid}->{failed}->{failed} * 100 / $self->{jobs}->{$uid}->{failed}->{total} : 0;
Expand Down Expand Up @@ -308,7 +353,7 @@ Filter jobs by type.

=item B<--timeframe>

Timeframe to get BackupJobSession (in seconds. Default: 86400).
Timeframe to get BackupJobSession and ReplicaJobSession (in seconds. Default: 86400).

=item B<--unit>

Expand Down
15 changes: 13 additions & 2 deletions src/apps/backup/veeam/vbem/restapi/mode/listjobs.pm
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ sub manage_selection {

my $results = {};
my $jobs_exec = $options{custom}->cache_backup_job_session(timeframe => $self->{option_results}->{timeframe});
my $jobs_replica = $options{custom}->get_replica_job_session(timeframe => $self->{option_results}->{timeframe});

foreach my $job (@{$jobs_exec->{Entities}->{BackupJobSessions}->{BackupJobSessions}}) {
next if (defined($results->{ $job->{JobUid} }));

Expand All @@ -60,14 +62,23 @@ sub manage_selection {
}
}

foreach my $job (@{$jobs_replica->{Entities}->{ReplicaJobSessions}->{ReplicaJobSessions}}) {
next if (defined($results->{ $job->{JobUid} }));

$results->{ $job->{JobUid} } = {
jobName => $job->{JobName},
jobType => $job->{JobType}
}
}

return $results;
}

sub run {
my ($self, %options) = @_;

my $results = $self->manage_selection(%options);
foreach my $uid (keys %$results) {
foreach my $uid (sort keys %$results) {
$self->{output}->output_add(
long_msg => sprintf(
'[uid: %s][jobName: %s][jobType: %s]',
Expand Down Expand Up @@ -117,7 +128,7 @@ List jobs.

=item B<--timeframe>

Timeframe to get BackupJobSession (in seconds. Default: 86400).
Timeframe to get BackupJobSession and ReplicaJobSession (in seconds. Default: 86400).

=back

Expand Down
51 changes: 51 additions & 0 deletions tests/apps/backup/veeam/vbem/restapi/jobs.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
*** Settings ***
Documentation Check Veeam Backup Enterprise Manager using Rest API,Check jobs.

Resource ${CURDIR}${/}..${/}..${/}..${/}..${/}..${/}resources/import.resource

Suite Setup Start Mockoon ${MOCKOON_JSON}
Suite Teardown Stop Mockoon
Test Timeout 120s


*** Variables ***
${MOCKOON_JSON} ${CURDIR}${/}restapi.json

${cmd} ${CENTREON_PLUGINS}
... --plugin=apps::backup::veeam::vbem::restapi::plugin
... --hostname=${HOSTNAME}
... --api-username='username'
... --api-password='password'
... --proto='http'
... --port=${APIPORT}

*** Test Cases ***
Create cache from API
[Tags] apps backup veeam vbem restapi jobs cache
${output} Run
... ${CMD} --mode=cache --proto=http --port=${APIPORT} --hostname=${HOSTNAME}

Log ${output}
Should Contain ${output} OK: Cache files created successfully

jobs ${tc}
[Tags] apps backup veeam vbem restapi jobs

${command} Catenate
... ${cmd}
... --mode=jobs
... --cache-use
... ${extraoptions}

Ctn Verify Command Output ${command} ${expected_result}

Examples: tc extraoptions expected_result --
... 1 ${EMPTY} OK: All jobs are ok | 'jobs.executions.detected.count'=2;;;0; 'Backup client 2 - Tous les jours#job.executions.failed.percentage'=5.26%;;;0;100 'client 6 - Backup - VM Test et Lab#job.execution.last.seconds'
... 2 --critical-execution-status='\\\%{status} eq "Success"' CRITICAL: job 'Backup client 2 - Tous les jours' [type: Backup] execution started: 2025-02-19T11:30:08.103Z status: Success - job 'PROD Job 1' [type: Backup] execution started: 2025-02-19T12:00:11.94Z status: Success | 'jobs.executions.detected.count'=2;;;0;
... 3 --warning-execution-status='\\\%{status} eq "Success"' WARNING: job 'Backup client 2 - Tous les jours' [type: Backup] execution started: 2025-02-19T11:30:08.103Z status: Success - job 'PROD Job 1' [type: Backup] execution started: 2025-02-19T12:00:11.94Z status: Success | 'jobs.executions.detected.count'=2;;;0;
... 4 --filter-uid='urn:veeam:Job' OK: All jobs are ok | 'jobs.executions.detected.count'=2;;;0; 'Backup client 2 - Tous les jours#job.executions.failed.percentage'=5.26%;;;0;100 'client 6 - Backup - VM Test et Lab#job.execution.last.seconds'
... 5 --filter-name='PROD Job 1' CRITICAL: job 'PROD Job 1' [type: Backup] execution started: 2025-02-19T03:51:03.037Z status: Failed | 'jobs.executions.detected.count'=2;;;0; 'PROD Job 1#job.executions.failed.percentage'=50.00%;;;0;100
... 6 --filter-type='toto' OK: | 'jobs.executions.detected.count'=0;;;0;
... 7 --timeframe='0' OK: All jobs are ok | 'jobs.executions.detected.count'=2;;;0; 'Backup client 2 - Tous les jours#job.executions.failed.percentage'=5.26%;;;0;100 'client 6 - Backup - VM Test et Lab#job.execution.last.seconds'
... 8 --unknown-execution-status='\\\%{status} eq "Success"' --filter-name='client 6' --filter-type='Backup' UNKNOWN: job 'client 6 - Backup - Infrastructure g0t0-oob' [type: Backup] execution started: 2025-02-19T11:30:08.103Z status: Success - job 'client 6 - Backup - Infrastructure g0t0-bck' [type: Backup] execution started: 2025-02-19T12:00:11.94Z status: Success | 'jobs.executions.detected.count'=2;;;0;
... 9 --warning-job-executions-failed-prct=0 --critical-job-executions-failed-prct=10 CRITICAL: job 'PROD Job 1' [type: Backup] number of failed executions: 60.00 % WARNING: job 'Backup client 2 - Tous les jours' [type: Backup]
34 changes: 34 additions & 0 deletions tests/apps/backup/veeam/vbem/restapi/list-jobs.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
*** Settings ***
Documentation Check Veeam Backup Enterprise Manager using Rest API,Check jobs.

Resource ${CURDIR}${/}..${/}..${/}..${/}..${/}..${/}resources/import.resource

Suite Setup Start Mockoon ${MOCKOON_JSON}
Suite Teardown Stop Mockoon
Test Timeout 120s


*** Variables ***
${MOCKOON_JSON} ${CURDIR}${/}restapi.json

${cmd} ${CENTREON_PLUGINS}
... --plugin=apps::backup::veeam::vbem::restapi::plugin
... --mode=list-jobs
... --hostname=${HOSTNAME}
... --api-username='username'
... --api-password='password'
... --proto='http'
... --port=${APIPORT}

*** Test Cases ***
list-jobs ${tc}
[Tags] apps backup veeam vbem restapi list-jobs

${command} Catenate
... ${cmd}
... ${extraoptions}

Ctn Verify Command Output ${command} ${expected_result}

Examples: tc extraoptions expected_result --
... 1 --timeframe='' List jobs: [uid: urn:veeam:Job][jobName: Backup client 2 - Tous les jours][jobType: Backup] [uid: urn:veeam:Job:xxxxxxxx-yyyy-zzzz-1111-aaaaaaaaaaaa][jobName: PROD Job 1][jobType: Backup]
159 changes: 159 additions & 0 deletions tests/apps/backup/veeam/vbem/restapi/restapi.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions tests/resources/spellcheck/stopwords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ aws
AWSCLI
--aws-role-arn
Backbox
BackupJobSession
base64
blocked-by-uf
BPL
Expand Down Expand Up @@ -215,6 +216,7 @@ QoS
Qtree
queue-messages-inflighted
raidvolume
ReplicaJobSession
RestAPI
RFC1628
RRDCached
Expand Down
Loading