Merge pull request #2434 from centreon/enh-velocloud

refacto custom velocloud
This commit is contained in:
qgarnier 2020-12-16 13:59:08 +01:00 committed by GitHub
commit 983e849c60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 215 additions and 158 deletions

View File

@ -24,6 +24,8 @@ use strict;
use warnings; use warnings;
use centreon::plugins::http; use centreon::plugins::http;
use JSON::XS; use JSON::XS;
use Digest::MD5 qw(md5_hex);
use centreon::plugins::statefile;
use DateTime; use DateTime;
sub new { sub new {
@ -42,21 +44,27 @@ sub new {
if (!defined($options{noptions})) { if (!defined($options{noptions})) {
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
'hostname:s' => { name => 'hostname' }, 'hostname:s' => { name => 'hostname' },
'port:s' => { name => 'port' }, 'port:s' => { name => 'port' },
'proto:s' => { name => 'proto' }, 'proto:s' => { name => 'proto' },
'username:s' => { name => 'username' }, 'username:s' => { name => 'username' },
'password:s' => { name => 'password' }, 'password:s' => { name => 'password' },
'operator-user' => { name => 'operator_user' }, 'operator-user' => { name => 'operator_user' },
'api-path:s' => { name => 'api_path' }, 'api-path:s' => { name => 'api_path' },
'timeframe:s' => { name => 'timeframe' }, 'timeframe:s' => { name => 'timeframe' },
'timeout:s' => { name => 'timeout' } 'timeout:s' => { name => 'timeout' },
'cache-expires-in:s' => { name => 'cache_expires_in' },
'unknown-http-status:s' => { name => 'unknown_http_status' },
'warning-http-status:s' => { name => 'warning_http_status' },
'critical-http-status:s' => { name => 'critical_http_status' }
}); });
} }
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
$self->{output} = $options{output}; $self->{output} = $options{output};
$self->{http} = centreon::plugins::http->new(%options); $self->{http} = centreon::plugins::http->new(%options);
$self->{cache_cookie} = centreon::plugins::statefile->new(%options);
$self->{cache_app} = centreon::plugins::statefile->new(%options);
return $self; return $self;
} }
@ -72,30 +80,53 @@ sub set_defaults {}
sub check_options { sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : undef; $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : '';
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https'; $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443; $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
$self->{username} = (defined($self->{option_results}->{username})) ? $self->{option_results}->{username} : undef; $self->{username} = (defined($self->{option_results}->{username})) ? $self->{option_results}->{username} : '';
$self->{password} = (defined($self->{option_results}->{password})) ? $self->{option_results}->{password} : undef; $self->{password} = (defined($self->{option_results}->{password})) ? $self->{option_results}->{password} : '';
$self->{api_path} = (defined($self->{option_results}->{api_path})) ? $self->{option_results}->{api_path} : '/portal/rest'; $self->{api_path} = (defined($self->{option_results}->{api_path})) ? $self->{option_results}->{api_path} : '/portal/rest';
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10; $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
$self->{cache_expires_in} = (defined($self->{option_results}->{cache_expires_in})) && $self->{option_results}->{cache_expires_in} =~ /(\d+)/ ?
$1 : 7200;
$self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300';
$self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : '';
$self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : '';
if (!defined($self->{hostname}) || $self->{hostname} eq '') { if ($self->{hostname} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --hostname option."); $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option.");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
if (!defined($self->{username}) || $self->{username} eq '') { if ($self->{username} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --username option."); $self->{output}->add_option_msg(short_msg => "Need to specify --username option.");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
if (!defined($self->{password}) || $self->{password} eq '') { if ($self->{password} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --password option."); $self->{output}->add_option_msg(short_msg => "Need to specify --password option.");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
$self->{cache_cookie}->check_options(option_results => $self->{option_results});
$self->{cache_app}->check_options(option_results => $self->{option_results});
return 0; return 0;
} }
sub json_decode {
my ($self, %options) = @_;
my $decoded;
eval {
$decoded = JSON::XS->new->utf8->decode($options{content});
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
}
return $decoded;
}
sub get_connection_infos { sub get_connection_infos {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -109,9 +140,6 @@ sub build_options_for_httplib {
$self->{option_results}->{timeout} = $self->{timeout}; $self->{option_results}->{timeout} = $self->{timeout};
$self->{option_results}->{port} = $self->{port}; $self->{option_results}->{port} = $self->{port};
$self->{option_results}->{proto} = $self->{proto}; $self->{option_results}->{proto} = $self->{proto};
$self->{option_results}->{unknown_status} = '';
$self->{option_results}->{warning_status} = '';
$self->{option_results}->{critical_status} = '';
} }
sub settings { sub settings {
@ -120,131 +148,150 @@ sub settings {
$self->build_options_for_httplib(); $self->build_options_for_httplib();
$self->{http}->add_header(key => 'Accept', value => 'application/json'); $self->{http}->add_header(key => 'Accept', value => 'application/json');
$self->{http}->add_header(key => 'Content-Type', value => 'application/json'); $self->{http}->add_header(key => 'Content-Type', value => 'application/json');
if (defined($self->{session_cookie})) {
$self->{http}->add_header(key => 'Cookie', value => 'velocloud.session=' . $self->{session_cookie});
}
$self->{http}->set_options(%{$self->{option_results}}); $self->{http}->set_options(%{$self->{option_results}});
} }
sub clean_session {
my ($self, %options) = @_;
my $datas = {};
$options{statefile}->write(data => $datas);
$self->{session_cookie} = undef;
$self->{http}->add_header(key => 'Cookie', value => undef);
}
sub get_session_cookie { sub get_session_cookie {
my ($self, %options) = @_; my ($self, %options) = @_;
my $form_post = { username => $self->{username}, password => $self->{password} }; my $has_cache_file = $options{statefile}->read(statefile => 'vmware_velocloud_' . md5_hex($self->{option_results}->{hostname}) . '_' . md5_hex($self->{option_results}->{username}));
my $encoded; my $session_cookie = $options{statefile}->get(name => 'session_cookie');
eval {
$encoded = JSON::XS->new->utf8->encode($form_post); if ($has_cache_file == 0 || !defined($session_cookie)) {
}; my $form_post = { username => $self->{username}, password => $self->{password} };
if ($@) { my $encoded;
$self->{output}->add_option_msg(short_msg => 'Cannot encode json request'); eval {
$self->{output}->option_exit(); $encoded = JSON::XS->new->utf8->encode($form_post);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => 'Cannot encode json request');
$self->{output}->option_exit();
}
my $login_url = (defined($self->{option_results}->{operator_user})) ? '/login/operatorLogin' : '/login/enterpriseLogin';
$self->{http}->request(
method => 'POST',
url_path => $self->{api_path} . $login_url,
query_form_post => $encoded,
warning_status => '',
unknown_status => '',
critical_status => ''
);
if ($self->{http}->get_code() != 200) {
$self->{output}->add_option_msg(short_msg => "Authentication error [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
$self->{output}->option_exit();
}
my (@cookies) = $self->{http}->get_header(name => 'Set-Cookie');
my $message = '';
foreach my $cookie (@cookies) {
$message = $1 if ($cookie =~ /^velocloud\.message=(.+?);/);
$session_cookie = $1 if ($cookie =~ /^velocloud\.session=(.+?);/);
}
if (!defined($session_cookie) || $session_cookie eq '') {
$self->{output}->add_option_msg(short_msg => "Cannot get session cookie: " . $message);
$self->{output}->option_exit();
}
$options{statefile}->write(data => { session_cookie => $session_cookie });
} }
$self->settings(); $self->{session_cookie} = $session_cookie;
$self->{http}->add_header(key => 'Cookie', value => 'velocloud.session=' . $self->{session_cookie});
my $login_url = (defined($self->{option_results}->{operator_user})) ? '/login/operatorLogin' : '/login/enterpriseLogin';
my $content = $self->{http}->request(
method => 'POST',
url_path => $self->{api_path} . $login_url,
query_form_post => $encoded
);
my (@cookies) = $self->{http}->get_header(name => 'Set-Cookie');
my $message = '';
my $session = '';
foreach my $cookie (@cookies) {
$message = $1 if ($cookie =~ /^velocloud\.message=(.+?);/);
$session = $1 if ($cookie =~ /^velocloud\.session=(.+?);/);
}
if (!defined($session) || $session eq '') {
$self->{output}->add_option_msg(short_msg => "Cannot get session cookie: " . $message);
$self->{output}->option_exit();
}
$self->{session_cookie} = $session;
}
sub get_entreprise_id {
my ($self, %options) = @_;
if (!defined($self->{session_cookie})) {
$self->get_session_cookie();
}
$self->settings();
my $content = $self->{http}->request(
method => 'POST',
url_path => $self->{api_path} . '/enterprise/getEnterprise'
);
my $decoded;
eval {
$decoded = JSON::XS->new->utf8->decode($content);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response");
$self->{output}->option_exit();
}
$self->{entreprise_id} = $decoded->{id};
} }
sub request_api { sub request_api {
my ($self, %options) = @_; my ($self, %options) = @_;
if (!defined($self->{session_cookie})) {
$self->get_session_cookie();
}
$self->settings(); $self->settings();
if (!defined($self->{session_cookie})) {
$self->get_session_cookie(statefile => $self->{cache_cookie});
}
my $encoded_form_post; my $encoded_form_post;
eval { if (defined($options{query_form_post})) {
$encoded_form_post = JSON::XS->new->utf8->encode($options{query_form_post}); eval {
}; $encoded_form_post = JSON::XS->new->utf8->encode($options{query_form_post});
if ($@) { };
$self->{output}->add_option_msg(short_msg => "Cannot encode json request"); if ($@) {
$self->{output}->option_exit(); $self->{output}->add_option_msg(short_msg => "Cannot encode json request");
$self->{output}->option_exit();
}
} }
my $content = $self->{http}->request( my ($content) = $self->{http}->request(
method => $options{method}, method => $options{method},
url_path => $self->{api_path} . $options{path}, url_path => $self->{api_path} . $options{endpoint},
query_form_post => $encoded_form_post, query_form_post => $encoded_form_post,
critical_status => '', unknown_status => '',
warning_status => '', warning_status => '',
unknown_status => '' critical_status => ''
); );
my $decoded; if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) {
eval { $self->clean_session(statefile => $self->{cache_cookie});
$decoded = JSON::XS->new->utf8->decode($content); $self->get_session_cookie(statefile => $self->{cache_cookie});
}; ($content) = $self->{http}->request(
if ($@) { method => $options{method},
$self->{output}->add_option_msg(short_msg => "Cannot decode json response"); url_path => $self->{api_path} . $options{endpoint},
query_form_post => $encoded_form_post,
unknown_status => '',
warning_status => '',
critical_status => ''
);
}
my $decoded = $self->json_decode(content => $content);
if (!defined($decoded)) {
$self->{output}->add_option_msg(short_msg => 'error while retrieving data (add --debug option for detailed message)');
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
if (ref($decoded) ne 'ARRAY' && defined($decoded->{error})) { if (ref($decoded) ne 'ARRAY' && defined($decoded->{error})) {
$self->{output}->add_option_msg(short_msg => "API returned error code '" . $decoded->{error}->{code} . $self->{output}->add_option_msg(
"', message '" . $decoded->{error}->{message} . "'"); short_msg => sprintf(
"API returned error code '%s', message '%s'",
$decoded->{error}->{code},
$decoded->{error}->{message}
)
);
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
return $decoded; return $decoded;
} }
sub list_edges { sub get_entreprise_id {
my ($self, %options) = @_; my ($self, %options) = @_;
if (!defined($self->{entreprise_id})) { return if (defined($self->{entreprise_id}));
$self->get_entreprise_id();
}
my $response = $self->request_api( my $response = $self->request_api(
method => 'POST', method => 'POST',
path => '/enterprise/getEnterpriseEdges', endpoint => $self->{api_path} . '/enterprise/getEnterprise'
);
$self->{entreprise_id} = $response->{id};
}
sub list_edges {
my ($self, %options) = @_;
$self->get_entreprise_id();
my $response = $self->request_api(
method => 'POST',
endpoint => '/enterprise/getEnterpriseEdges',
query_form_post => { enterpriseId => $self->{entreprise_id} } query_form_post => { enterpriseId => $self->{entreprise_id} }
); );
@ -254,15 +301,13 @@ sub list_edges {
sub list_links { sub list_links {
my ($self, %options) = @_; my ($self, %options) = @_;
if (!defined($self->{entreprise_id})) { $self->get_entreprise_id();
$self->get_entreprise_id();
}
my $start_time = DateTime->now->subtract(seconds => $options{timeframe})->iso8601.'Z'; my $start_time = DateTime->now->subtract(seconds => $options{timeframe})->iso8601.'Z';
my $results = $self->request_api( my $response = $self->request_api(
method => 'POST', method => 'POST',
path => '/metrics/getEdgeLinkMetrics', endpoint => '/metrics/getEdgeLinkMetrics',
query_form_post => { query_form_post => {
enterpriseId => $self->{entreprise_id}, enterpriseId => $self->{entreprise_id},
edgeId => $options{edge_id}, edgeId => $options{edge_id},
@ -273,61 +318,77 @@ sub list_links {
} }
); );
return $results; return $response;
} }
sub get_links_metrics { sub get_links_metrics {
my ($self, %options) = @_; my ($self, %options) = @_;
if (!defined($self->{entreprise_id})) { $self->get_entreprise_id();
$self->get_entreprise_id();
}
my $start_time = DateTime->now->subtract(seconds => $options{timeframe})->iso8601.'Z'; my $start_time = DateTime->now->subtract(seconds => $options{timeframe})->iso8601.'Z';
my $results = $self->request_api( my $response = $self->request_api(
method => 'POST', method => 'POST',
path => '/metrics/getEdgeLinkMetrics', endpoint => '/metrics/getEdgeLinkMetrics',
query_form_post => { query_form_post => {
enterpriseId => $self->{entreprise_id}, enterpriseId => $self->{entreprise_id},
edgeId => $options{edge_id}, edgeId => $options{edge_id},
metrics => [ 'bytesRx', 'bytesTx', 'bestJitterMsRx', 'bestJitterMsTx', metrics => [
'bestLatencyMsRx', 'bestLatencyMsTx', 'bestLossPctRx', 'bestLossPctTx' ], 'bytesRx', 'bytesTx', 'bestJitterMsRx', 'bestJitterMsTx',
'bestLatencyMsRx', 'bestLatencyMsTx', 'bestLossPctRx', 'bestLossPctTx'
],
interval => { interval => {
start => $start_time start => $start_time
} }
} }
); );
return $results; return $response;
} }
sub get_identifiable_applications { sub get_identifiable_applications {
my ($self, %options) = @_; my ($self, %options) = @_;
if (!defined($self->{entreprise_id})) { $self->get_entreprise_id();
$self->get_entreprise_id();
my $has_cache_file = $self->{cache_app}->read(statefile => 'vmware_velocloud_' . md5_hex($self->{option_results}->{hostname}) . '_' . md5_hex($self->{entreprise_id}));
my $updated = $self->{cache_app}->get(name => 'updated');
my $applications = $self->{cache_app}->get(name => 'applications');
if ($has_cache_file == 0 || !defined($updated) || (time() > ($updated + $self->{cache_expires_in}))) {
my $response = $self->request_api(
method => 'POST',
endpoint => '/configuration/getIdentifiableApplications',
query_form_post => {
enterpriseId => $self->{entreprise_id}
}
);
$applications = {};
foreach (@{$response->{applications}}) {
$applications->{ $_->{id} } = $_->{name};
}
$self->{cache_app}->write(data => {
applications => $applications,
updated => time()
});
} }
my $results = $self->request_api( return defined($applications->{ $options{app_id} }) ? $applications->{ $options{app_id} } : undef;
method => 'POST',
path => '/configuration/getIdentifiableApplications',
query_form_post => {
enterpriseId => $self->{entreprise_id}
}
);
return $results;
} }
sub get_apps_metrics { sub get_apps_metrics {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->get_entreprise_id();
my $start_time = DateTime->now->subtract(seconds => $options{timeframe})->iso8601.'Z'; my $start_time = DateTime->now->subtract(seconds => $options{timeframe})->iso8601.'Z';
my $results = $self->request_api( my $response = $self->request_api(
method => 'POST', method => 'POST',
path => '/metrics/getEdgeAppMetrics', endpoint => '/metrics/getEdgeAppMetrics',
query_form_post => { query_form_post => {
enterpriseId => $self->{entreprise_id}, enterpriseId => $self->{entreprise_id},
edgeId => $options{edge_id}, edgeId => $options{edge_id},
@ -338,17 +399,19 @@ sub get_apps_metrics {
} }
); );
return $results; return $response;
} }
sub get_links_qoe { sub get_links_qoe {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->get_entreprise_id();
my $start_time = DateTime->now->subtract(seconds => $options{timeframe})->iso8601.'Z'; my $start_time = DateTime->now->subtract(seconds => $options{timeframe})->iso8601.'Z';
my $results = $self->request_api( my $response = $self->request_api(
method => 'POST', method => 'POST',
path => '/linkQualityEvent/getLinkQualityEvents', endpoint => '/linkQualityEvent/getLinkQualityEvents',
query_form_post => { query_form_post => {
enterpriseId => $self->{entreprise_id}, enterpriseId => $self->{entreprise_id},
edgeId => $options{edge_id}, edgeId => $options{edge_id},
@ -357,21 +420,23 @@ sub get_links_qoe {
maxSamples => '15', maxSamples => '15',
interval => { interval => {
start => $start_time start => $start_time
}, }
} }
); );
return $results; return $response;
} }
sub get_categories_metrics { sub get_categories_metrics {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->get_entreprise_id();
my $start_time = DateTime->now->subtract(seconds => $options{timeframe})->iso8601.'Z'; my $start_time = DateTime->now->subtract(seconds => $options{timeframe})->iso8601.'Z';
my $results = $self->request_api( my $response = $self->request_api(
method => 'POST', method => 'POST',
path => '/metrics/getEdgeCategoryMetrics', endpoint => '/metrics/getEdgeCategoryMetrics',
query_form_post => { query_form_post => {
id => $options{edge_id}, id => $options{edge_id},
metrics => [ 'bytesRx', 'bytesTx', 'packetsRx', 'packetsTx' ], metrics => [ 'bytesRx', 'bytesTx', 'packetsRx', 'packetsTx' ],
@ -381,15 +446,7 @@ sub get_categories_metrics {
} }
); );
return $results; return $response;
}
sub DESTROY {
my $self = shift;
if (defined($self->{session_cookie})) {
$self->{http}->request(method => 'POST', url_path => $self->{api_path} . '/logout');
}
} }
1; 1;
@ -420,6 +477,10 @@ Port used (Default: 443)
Specify https if needed (Default: 'https') Specify https if needed (Default: 'https')
=item B<--cache-expires-in>
Cache (application) expires each X secondes (Default: 7200)
=item B<--username> =item B<--username>
VMware VeloCloud Orchestrator username. VMware VeloCloud Orchestrator username.

View File

@ -118,7 +118,6 @@ sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
my $results = $options{custom}->list_edges(); my $results = $options{custom}->list_edges();
my $config_applications = $options{custom}->get_identifiable_applications();
$self->{edges} = {}; $self->{edges} = {};
foreach my $edge (@{$results}) { foreach my $edge (@{$results}) {
@ -137,18 +136,15 @@ sub manage_selection {
); );
foreach my $app (@{$apps}) { foreach my $app (@{$apps}) {
my $app_name = $app->{application}; my $app_name = $options{custom}->get_application_name(
foreach (@{$config_applications->{applications}}) { app_id => $app->{application}
if ($_->{id} eq $app_name) { );
$app_name = $_->{name}; $app_name = $app->{application} if (!defined($app_name));
last;
}
}
if (defined($self->{option_results}->{filter_application_name}) && if (defined($self->{option_results}->{filter_application_name}) &&
$self->{option_results}->{filter_application_name} ne '' && $self->{option_results}->{filter_application_name} ne '' &&
$app_name !~ /$self->{option_results}->{filter_application_name}/) { $app_name !~ /$self->{option_results}->{filter_application_name}/) {
$self->{output}->output_add(long_msg => "skipping '" . $app_names . "'.", debug => 1); $self->{output}->output_add(long_msg => "skipping '" . $app_name . "'.", debug => 1);
next; next;
} }