enh(versa): add cache system + paths mode (#2806)

This commit is contained in:
qgarnier 2021-05-19 09:24:59 +02:00 committed by GitHub
parent fb442bb775
commit def5ed1352
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 904 additions and 182 deletions

View File

@ -26,6 +26,7 @@ use centreon::plugins::http;
use centreon::plugins::statefile;
use JSON::XS;
use Digest::MD5 qw(md5_hex);
use centreon::plugins::misc;
sub new {
my ($class, %options) = @_;
@ -49,11 +50,11 @@ sub new {
'api-username:s' => { name => 'api_username' },
'api-password:s' => { name => 'api_password' },
'timeout:s' => { name => 'timeout' },
'reload-cache-time:s' => { name => 'reload_cache_time' },
'ignore-unknown-errors' => { name => 'ignore_unknown_errors' },
'unknown-http-status:s' => { name => 'unknown_http_status' },
'warning-http-status:s' => { name => 'warning_http_status' },
'critical-http-status:s' => { name => 'critical_http_status' }
'critical-http-status:s' => { name => 'critical_http_status' },
'cache-use' => { name => 'cache_use' }
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
@ -61,7 +62,6 @@ sub new {
$self->{output} = $options{output};
$self->{http} = centreon::plugins::http->new(%options);
$self->{cache} = centreon::plugins::statefile->new(%options);
$self->{cache_checked} = 0;
return $self;
}
@ -80,12 +80,11 @@ sub check_options {
$self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : '';
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 9182;
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 50;
$self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : '';
$self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : '';
$self->{reload_cache_time} = (defined($self->{option_results}->{reload_cache_time})) ? $self->{option_results}->{reload_cache_time} : 180;
$self->{ignore_unknown_errors} = (defined($self->{option_results}->{ignore_unknown_errors})) ? 1 : 0;
my $default_unknown = '(%{http_code} < 200 or %{http_code} >= 300)';
if ($self->{ignore_unknown_errors} == 1) {
$default_unknown = '(%{http_code} < 200 or %{http_code} >= 300) and %{http_code} != 404';
@ -107,6 +106,8 @@ sub check_options {
$self->{output}->option_exit();
}
# we force to use storable module
$self->{option_results}->{statefile_storable} = 1;
$self->{cache}->check_options(option_results => $self->{option_results});
return 0;
}
@ -165,7 +166,7 @@ sub bouchon {
my $content = do {
local $/ = undef;
if (!open my $fh, "<", $options{file}) {
if (!open my $fh, '<', $options{file}) {
$self->{output}->add_option_msg(short_msg => "Could not open file $options{file} : $!");
$self->{output}->option_exit();
}
@ -189,6 +190,7 @@ sub request_api {
$self->settings();
my $response = $self->{http}->request(
url_path => $options{endpoint},
get_param => $options{get_param},
critical_status => $self->{critical_http_status},
warning_status => $self->{warning_http_status},
unknown_status => $self->{unknown_http_status}
@ -209,95 +211,189 @@ sub request_api {
return ($content);
}
sub cache_versa_entities {
sub call_organizations {
my ($self, %options) = @_;
return if ($self->{cache_checked} == 1);
#my $datas = $self->bouchon(file => '/home/qgarnier/clients/bpce/Plugin Versa V0/cleans/vnms_organization_orgs.txt');
my $datas = $self->request_api(
endpoint => '/vnms/organization/orgs',
get_param => ['primary=true', 'offset=0', 'limit=200']
);
$self->{cache_checked} = 1;
my $has_cache_file = $self->{cache}->read(statefile => 'cache_versa_' . $self->get_hostname());
my $timestamp_cache = $self->{cache}->get(name => 'last_timestamp');
$self->{cache_organizations} = $self->{cache}->get(name => 'organizations');
$self->{cache_appliances} = $self->{cache}->get(name => 'appliances');
my $orgs = { entries => {}, names => {} };
foreach (@{$datas->{organizations}}) {
$orgs->{entries}->{ $_->{uuid} } = {
uuid => $_->{uuid},
depth => $_->{depth},
name => $_->{name},
globalOrgId => $_->{globalOrgId}
};
$orgs->{names}->{ $_->{name} } = $_->{uuid};
}
if ($has_cache_file == 0 || !defined($timestamp_cache) || ((time() - $timestamp_cache) > (($self->{reload_cache_time}) * 60))) {
$self->{cache_organizations} = $self->get_organizations(
disable_cache => 1
);
$self->{cache_appliances} = $self->get_appliances(
disable_cache => 1
);
return $orgs;
}
$self->{cache}->write(data => {
last_timestamp => time(),
organizations => $self->{cache_organizations},
appliances => $self->{cache_appliances}
});
sub call_devices {
my ($self, %options) = @_;
#my $datas = $self->bouchon(file => '/home/qgarnier/clients/bpce/Plugin Versa V0/cleans/vnms_appliance_filter.txt');
my $datas = $self->request_api(
endpoint => '/vnms/appliance/filter/' . $options{org_name},
get_param => ['offset=0', 'limit=5000']
);
my $devices = { entries => {}, names => {}, types => {} };
foreach (@{$datas->{appliances}}) {
$devices->{entries}->{ $_->{uuid} } = {
uuid => $_->{uuid},
name => $_->{name},
ipAddress => $_->{ipAddress},
type => $_->{type},
location => $_->{location},
orgs => $_->{orgs},
pingStatus => $_->{'ping-status'},
syncStatus => $_->{'sync-status'},
servicesStatus => $_->{'services-status'},
pathStatus => $_->{'path-status'},
controllerStatus => $_->{'controll-status'},
alarmSummary => $_->{alarmSummary},
cpeHealth => $_->{cpeHealth},
policyViolation => $_->{policyViolation}
};
if (defined($_->{Hardware})) {
$devices->{entries}->{ $_->{uuid} }->{hardware} = {
memory => $_->{Hardware}->{memory},
freeMemory => $_->{Hardware}->{freeMemory},
diskSize => $_->{Hardware}->{diskSize},
freeDisk => $_->{Hardware}->{freeDisk}
};
}
$devices->{names}->{ $_->{name} } = $_->{uuid};
$devices->{types}->{ $_->{type} } = {} if (!defined($devices->{types}->{ $_->{type} }));
$devices->{types}->{ $_->{type} }->{ $_->{name} } = $_->{uuid};
}
return $devices;
}
sub call_device_paths {
my ($self, %options) = @_;
#my $datas = $self->bouchon(file => '/home/qgarnier/clients/bpce/Plugin Versa V0/cleans/vnms_dashboard_health_path.txt');
my $datas = $self->request_api(
endpoint => '/vnms/dashboard/health/path',
get_param => ['deviceName=' . $options{device_name}, 'tenantName=' . $options{org_name}, 'offset=0', 'limit=5000']
);
my $paths = { org_name => $options{org_name}, device_name => $options{device_name}, entries => []};
if (defined($datas->[0])) {
foreach (@{$datas->[0]->{details}}) {
my $remote_wan_link = centreon::plugins::misc::trim($_->{remoteWanLink});
my $local_wan_link = centreon::plugins::misc::trim($_->{localWanLink});
push @{$paths->{entries}}, {
remoteSiteName => $_->{remoteSiteName},
localWanLink => $local_wan_link ne '' ? $local_wan_link : 'unknown',
remoteWanLink => $remote_wan_link ne '' ? $remote_wan_link : 'unknown',
connState => $_->{connState}
};
}
}
return $paths;
}
sub cache_organizations {
my ($self, %options) = @_;
my $orgs = $self->call_organizations();
$self->write_cache_file(
statefile => 'orgs',
response => $orgs
);
return $orgs;
}
sub cache_devices {
my ($self, %options) = @_;
my $devices = $self->call_devices(%options);
$self->write_cache_file(
statefile => 'devices_' . $options{org_name},
response => $devices
);
return $devices;
}
sub cache_device_paths {
my ($self, %options) = @_;
my $paths = $self->call_device_paths(%options);
$self->write_cache_file(
statefile => 'device_paths_' . $options{org_name} . '_' . $options{device_name},
response => $paths
);
return $paths;
}
sub write_cache_file {
my ($self, %options) = @_;
$self->{cache}->read(statefile => 'cache_versa_' . $self->get_hostname() . '_' . $options{statefile});
$self->{cache}->write(data => {
update_time => time(),
response => $options{response}
});
}
sub get_cache_file_response {
my ($self, %options) = @_;
$self->{cache}->read(statefile => 'cache_versa_' . $self->get_hostname() . '_' . $options{statefile});
my $response = $self->{cache}->get(name => 'response');
if (!defined($response)) {
$self->{output}->add_option_msg(short_msg => 'Cache file missing');
$self->{output}->option_exit();
}
return $response;
}
sub find_root_organization_name {
my ($self, %options) = @_;
foreach (values %{$options{orgs}->{entries}}) {
return $_->{name} if ($_->{depth} == 1);
}
}
sub get_organizations {
my ($self, %options) = @_;
#my $datas = $self->bouchon(file => '/home/qgarnier/clients/plugins/todo/versa/Versa-Centreon/organizations.json');
$self->cache_versa_entities();
return $self->{cache_organizations} if (!defined($options{disable_cache}) || $options{disable_cache} == 0);
my $datas = $self->request_api(endpoint => '/api/config/nms/provider/organizations/organization');
my $results = { entries => {}, names => { } };
if (defined($datas->{organization})) {
foreach (@{$datas->{organization}}) {
$results->{entries}->{ $_->{uuid} } = $_;
$results->{names}->{ $_->{name} } = $_->{uuid};
}
}
return $results;
return $self->get_cache_file_response(statefile => 'orgs')
if (defined($self->{option_results}->{cache_use}));
return $self->call_organizations();
}
sub get_appliances {
sub get_devices {
my ($self, %options) = @_;
#my $datas = $self->bouchon(file => '/home/qgarnier/clients/plugins/todo/versa/Versa-Centreon/appliances.json');
$self->cache_versa_entities();
return $self->{cache_appliances} if (!defined($options{disable_cache}) || $options{disable_cache} == 0);
my $datas = $self->request_api(endpoint => '/api/config/nms/provider/appliances/appliance');
my $results = { entries => {}, names => { }, types => { } };
if (defined($datas->{appliance})) {
foreach (@{$datas->{appliance}}) {
$results->{entries}->{ $_->{uuid} } = $_;
$results->{names}->{ $_->{name} } = $_->{uuid};
$results->{types}->{ $_->{type} } = {} if (!defined($results->{types}->{ $_->{type} }));
$results->{types}->{ $_->{type} }->{ $_->{name} } = $_->{uuid};
}
}
return $results;
return $self->get_cache_file_response(statefile => 'devices_' . $options{org_name})
if (defined($self->{option_results}->{cache_use}));
return $self->call_devices(org_name => $options{org_name});
}
sub get_appliances_status_under_organization {
sub get_device_paths {
my ($self, %options) = @_;
#my $datas = $self->bouchon(file => '/home/qgarnier/clients/plugins/todo/versa/Versa-Centreon/tutu.txt.pretty');
my $datas = $self->request_api(endpoint => '/vnms/dashboard/tenantDetailAppliances/' . $options{organization});
return $datas;
}
sub execute {
my ($self, %options) = @_;
$self->cache_versa_entities();
my $results = $self->request_api(
endpoint => $options{endpoint},
return $self->get_cache_file_response(statefile => 'device_paths_' . $options{org_name} . '_' . $options{device_name})
if (defined($self->{option_results}->{cache_use}));
return $self->call_device_paths(
org_name => $options{org_name},
device_name => $options{device_name}
);
return $results;
}
1;
@ -340,14 +436,14 @@ Versa Director API password.
Set HTTP timeout
=item B<--reload-cache-time>
Time in minutes before reloading cache file (default: 180).
=item B<--ignore-unknown-errors>
Ignore unknown errors (404 status code).
=item B<--cache-use>
Use the cache file (created with cache mode).
=back
=head1 DESCRIPTION

View File

@ -0,0 +1,102 @@
#
# Copyright 2021 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 network::versa::director::restapi::mode::cache;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments => {
'filter-org-name:s' => { name => 'filter_org_name' },
'filter-device-name:s' => { name => 'filter_device_name' },
'paths-by-orgs' => { name => 'paths_by_orgs' }
});
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
my $orgs = $options{custom}->cache_organizations();
my $root_org_name = $options{custom}->find_root_organization_name(orgs => $orgs);
foreach my $org (values %{$orgs->{entries}}) {
if (defined($self->{option_results}->{filter_org_name}) && $self->{option_results}->{filter_org_name} ne '' &&
$org->{name} !~ /$self->{option_results}->{filter_org_name}/) {
$self->{output}->output_add(long_msg => "skipping org '" . $org->{name} . "': no matching filter name.", debug => 1);
next;
}
my $devices = $options{custom}->cache_devices(org_name => $org->{name});
foreach my $device (values %{$devices->{entries}}) {
if (defined($self->{option_results}->{filter_device_name}) && $self->{option_results}->{filter_device_name} ne '' &&
$device->{name} !~ /$self->{option_results}->{filter_device_name}/) {
$self->{output}->output_add(long_msg => "skipping device '" . $device->{name} . "': no matching filter name.", debug => 1);
next;
}
next if (!defined($self->{option_results}->{paths_by_orgs}) && $org->{name} ne $root_org_name);
# we check all paths from the root org
$options{custom}->cache_device_paths(
org_name => $org->{name},
device_name => $device->{name}
);
}
}
$self->{output}->output_add(
severity => 'OK',
short_msg => 'Cache files created successfully'
);
}
1;
__END__
=head1 MODE
Create cache files (other modes could use it with --cache-use option).
=over 8
=item B<--filter-org-name>
Filter organizations by name (Can be a regexp).
=item B<--filter-device-name>
Filter devices by name (Can be a regexp).
=item B<--paths-by-orgs>
Create paths cache files by organizations.
=back
=cut

View File

@ -24,7 +24,7 @@ use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
use centreon::plugins::misc;
use Digest::MD5 qw(md5_hex);
@ -67,6 +67,48 @@ sub custom_disk_output {
);
}
sub device_long_output {
my ($self, %options) = @_;
return "checking device '" . $options{instance_value}->{display} . "' [type: " . $options{instance_value}->{type} . ']';
}
sub prefix_device_output {
my ($self, %options) = @_;
return "Device '" . $options{instance_value}->{display} . "' ";
}
sub prefix_global_output {
my ($self, %options) = @_;
return 'Devices ';
}
sub prefix_alarm_output {
my ($self, %options) = @_;
return 'alarms ';
}
sub prefix_path_output {
my ($self, %options) = @_;
return 'paths ';
}
sub prefix_policy_output {
my ($self, %options) = @_;
return 'policy violation ';
}
sub prefix_health_output {
my ($self, %options) = @_;
return "health monitor '" . $options{instance_value}->{display} . "' ";
}
sub set_counters {
my ($self, %options) = @_;
@ -78,6 +120,7 @@ sub set_counters {
{ name => 'device_memory', type => 0, skipped_code => { -10 => 1 } },
{ name => 'device_disk', type => 0, skipped_code => { -10 => 1 } },
{ name => 'device_alarms', type => 0, cb_prefix_output => 'prefix_alarm_output', skipped_code => { -10 => 1 } },
{ name => 'device_paths', type => 0, cb_prefix_output => 'prefix_path_output', skipped_code => { -10 => 1 } },
{ name => 'device_policy', type => 0, cb_prefix_output => 'prefix_policy_output', skipped_code => { -10 => 1 } },
{ name => 'device_bgp_health', cb_prefix_output => 'prefix_health_output', type => 0, skipped_code => { -10 => 1 } },
{ name => 'device_config_health', cb_prefix_output => 'prefix_health_output', type => 0, skipped_code => { -10 => 1 } },
@ -86,7 +129,7 @@ sub set_counters {
{ name => 'device_port_health', cb_prefix_output => 'prefix_health_output', type => 0, skipped_code => { -10 => 1 } },
{ name => 'device_path_health', cb_prefix_output => 'prefix_health_output', type => 0, skipped_code => { -10 => 1 } },
{ name => 'device_reachability_health', cb_prefix_output => 'prefix_health_output', type => 0, skipped_code => { -10 => 1 } },
{ name => 'device_service_health', cb_prefix_output => 'prefix_health_output', type => 0, skipped_code => { -10 => 1 } },
{ name => 'device_service_health', cb_prefix_output => 'prefix_health_output', type => 0, skipped_code => { -10 => 1 } }
]
}
];
@ -103,7 +146,7 @@ sub set_counters {
];
$self->{maps_counters}->{device_status} = [
{ label => 'status', threshold => 0, set => {
{ label => 'status', type => 2, critical_default => '%{ping_status} ne "reachable" or %{services_status} ne "good"', set => {
key_values => [
{ name => 'ping_status' }, { name => 'sync_status' },
{ name => 'services_status' }, { name => 'path_status' },
@ -111,7 +154,7 @@ sub set_counters {
],
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
}
];
@ -184,6 +227,25 @@ sub set_counters {
};
}
$self->{maps_counters}->{device_paths} = [
{ label => 'paths-up', nlabel => 'paths.up.count', set => {
key_values => [ { name => 'up' }, { name => 'display' } ],
output_template => 'up: %s',
perfdatas => [
{ template => '%d', min => 0, label_extra_instance => 1 }
]
}
},
{ label => 'paths-down', nlabel => 'paths.down.count', set => {
key_values => [ { name => 'down' }, { name => 'display' } ],
output_template => 'down: %s',
perfdatas => [
{ template => '%d', min => 0, label_extra_instance => 1 }
]
}
}
];
$self->{maps_counters}->{device_policy} = [
{ label => 'packets-dropped-novalidlink', nlabel => 'policy.violation.packets.dropped.novalidlink.count', set => {
key_values => [ { name => 'dropped_novalidlink', diff => 1 }, { name => 'display' } ],
@ -219,42 +281,6 @@ sub set_counters {
}
}
sub device_long_output {
my ($self, %options) = @_;
return "checking device '" . $options{instance_value}->{display} . "' [type: " . $options{instance_value}->{type} . ']';
}
sub prefix_device_output {
my ($self, %options) = @_;
return "Device '" . $options{instance_value}->{display} . "' ";
}
sub prefix_global_output {
my ($self, %options) = @_;
return 'Devices ';
}
sub prefix_alarm_output {
my ($self, %options) = @_;
return 'alarms ';
}
sub prefix_policy_output {
my ($self, %options) = @_;
return 'policy violation ';
}
sub prefix_health_output {
my ($self, %options) = @_;
return "health monitor '" . $options{instance_value}->{display} . "' ";
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
@ -262,39 +288,46 @@ sub new {
$options{options}->add_options(arguments => {
'organization:s' => { name => 'organization' },
'filter-org-name:s' => { name => 'filter_org_name' },
'filter-device-name:s' => { name => 'filter_device_name' },
'filter-device-type:s' => { name => 'filter_device_type' },
'unknown-status:s' => { name => 'unknown_status', default => '' },
'warning-status:s' => { name => 'warning_status', default => '' },
'critical-status:s' => { name => 'critical_status', default => '%{ping_status} ne "reachable" or %{services_status} ne "good"' }
'add-paths' => { name => 'add_paths' }
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
if (!defined($self->{option_results}->{organization}) || $self->{option_results}->{organization} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --organization option");
$self->{output}->option_exit();
}
$self->change_macros(macros => [
'unknown_status', 'warning_status', 'critical_status'
]);
}
sub manage_selection {
my ($self, %options) = @_;
my $devices = $options{custom}->get_appliances_status_under_organization(organization => $self->{option_results}->{organization});
my ($orgs, $root_org_name);
my $devices = {};
if (defined($self->{option_results}->{organization}) && $self->{option_results}->{organization} ne '') {
my $result = $options{custom}->get_devices(org_name => $self->{option_results}->{organization});
$devices = $result->{entries};
} else {
$orgs = $options{custom}->get_organizations();
$root_org_name = $options{custom}->find_root_organization_name(orgs => $orgs);
foreach my $org (values %{$orgs->{entries}}) {
if (defined($self->{option_results}->{filter_org_name}) && $self->{option_results}->{filter_org_name} ne '' &&
$org->{name} !~ /$self->{option_results}->{filter_org_name}/) {
$self->{output}->output_add(long_msg => "skipping org '" . $org->{name} . "': no matching filter name.", debug => 1);
next;
}
my $result = $options{custom}->get_devices(org_name => $org->{name});
$devices = { %$devices, %{$result->{entries}} };
}
}
if (defined($self->{option_results}->{add_paths}) && !defined($root_org_name)) {
$orgs = $options{custom}->get_organizations();
$root_org_name = $options{custom}->find_root_organization_name(orgs => $orgs);
}
$self->{global} = { total => 0 };
$self->{devices} = {};
foreach my $device (@$devices) {
foreach my $device (values %$devices) {
if (defined($self->{option_results}->{filter_device_name}) && $self->{option_results}->{filter_device_name} ne '' &&
$device->{name} !~ /$self->{option_results}->{filter_device_name}/) {
$self->{output}->output_add(long_msg => "skipping device '" . $device->{name} . "': no matching filter name.", debug => 1);
@ -306,13 +339,13 @@ sub manage_selection {
next;
}
#"ping-status": "REACHABLE",
#"sync-status": "OUT_OF_SYNC",
#"services-status": "GOOD",
#"overall-status": "POWERED_ON",
#"controller-status": "Unavailable",
#"path-status": "Unavailable",
#"Hardware": {
#"pingStatus": "REACHABLE",
#"syncStatus": "OUT_OF_SYNC",
#"servicesStatus": "GOOD",
#"overallStatus": "POWERED_ON",
#"controllerStatus": "Unavailable",
#"pathStatus": "Unavailable",
#"hardware": {
# "memory": "7.80GiB",
# "freeMemory": "1.19GiB",
# "diskSize": "80G",
@ -324,11 +357,11 @@ sub manage_selection {
type => $device->{type},
device_status => {
display => $device->{name},
ping_status => lc($device->{'ping-status'}),
sync_status => lc($device->{'sync-status'}),
services_status => lc($device->{'services-status'}),
path_status => lc($device->{'path-status'}),
controller_status => defined($device->{'controller-status'}) ? lc($device->{'controller-status'}) : '-'
ping_status => lc($device->{pingStatus}),
sync_status => lc($device->{syncStatus}),
services_status => lc($device->{servicesStatus}),
path_status => lc($device->{pathStatus}),
controller_status => defined($device->{controllerStatus}) ? lc($device->{controllerStatus}) : '-'
},
device_alarms => {
display => $device->{name}
@ -342,13 +375,13 @@ sub manage_selection {
};
my ($total, $free);
if (defined($device->{Hardware}->{memory})) {
if (defined($device->{hardware}->{memory})) {
$total = centreon::plugins::misc::convert_bytes(
value => $device->{Hardware}->{memory},
value => $device->{hardware}->{memory},
pattern => '([0-9\.]+)(.*)$'
);
$free = centreon::plugins::misc::convert_bytes(
value => $device->{Hardware}->{freeMemory},
value => $device->{hardware}->{freeMemory},
pattern => '([0-9\.]+)(.*)$'
);
$self->{devices}->{ $device->{name} }->{device_memory} = {
@ -361,13 +394,13 @@ sub manage_selection {
};
}
if (defined($device->{Hardware}->{diskSize})) {
if (defined($device->{hardware}->{diskSize})) {
$total = centreon::plugins::misc::convert_bytes(
value => $device->{Hardware}->{diskSize},
value => $device->{hardware}->{diskSize},
pattern => '([0-9\.]+)(.*)$'
);
$free = centreon::plugins::misc::convert_bytes(
value => $device->{Hardware}->{freeDisk},
value => $device->{hardware}->{freeDisk},
pattern => '([0-9\.]+)(.*)$'
);
$self->{devices}->{ $device->{name} }->{device_disk} = {
@ -404,13 +437,32 @@ sub manage_selection {
total => $_->{columnValues}->[0] + $_->{columnValues}->[1] + $_->{columnValues}->[2]
};
}
if (defined($self->{option_results}->{add_paths})) {
$self->{devices}->{ $device->{name} }->{device_paths} = {
display => $device->{name},
up => 0,
down => 0
};
# we want all paths. So we check from root org
my $paths = $options{custom}->get_device_paths(
org_name => $root_org_name,
device_name => $device->{name}
);
foreach (@{$paths->{entries}}) {
$self->{devices}->{ $device->{name} }->{device_paths}->{ $_->{connState} }++;
}
}
$self->{global}->{total}++;
}
$self->{cache_name} = 'versa_' . $self->{mode} . '_' . $options{custom}->get_hostname() . '_' . $self->{option_results}->{organization} . '_' .
$self->{cache_name} = 'versa_' . $self->{mode} . '_' . $options{custom}->get_hostname() . '_' .
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
(defined($self->{option_results}->{organization}) ? md5_hex($self->{option_results}->{organization}) : md5_hex('all')) . '_' .
(defined($self->{option_results}->{filter_org_name}) ? md5_hex($self->{option_results}->{filter_org_name}) : md5_hex('all')) . '_' .
(defined($self->{option_results}->{filter_device_name}) ? md5_hex($self->{option_results}->{filter_device_name}) : md5_hex('all')) . '_' .
(defined($self->{option_results}->{filter_device_type}) ? md5_hex($self->{option_results}->{filter_device_type}) : md5_hex('all'))
(defined($self->{option_results}->{filter_device_type}) ? md5_hex($self->{option_results}->{filter_device_type}) : md5_hex('all'));
}
1;
@ -425,7 +477,11 @@ Check devices.
=item B<--organization>
Check device under a organization (Required).
Check device under an organization name.
=item B<--filter-org-name>
Filter organizations by name (Can be a regexp).
=item B<--filter-device-name>
@ -435,6 +491,10 @@ Filter device by name (Can be a regexp).
Filter device by type (Can be a regexp).
=item B<--add-paths>
Add path statuses count.
=item B<--unknown-status>
Set unknown threshold for status.
@ -464,7 +524,8 @@ Can be: 'total','memory-usage', 'memory-usage-free', 'memory-usage-prct',
'interface-health-up' 'interface-health-down' 'interface-health-disabled'
'ike-health-up' 'ike-health-down' 'ike-health-disabled'
'config-health-up' 'config-health-down' 'config-health-disabled'
'packets-dropped-novalidlink', 'packets dropped by sla action'.
'packets-dropped-novalidlink', 'packets dropped by sla action',
'paths-up', 'paths-down'.
=back

View File

@ -61,12 +61,8 @@ sub discovery_devices {
$node->{uuid} = $device->{uuid};
$node->{name} = $device->{name};
$node->{type} = $device->{type};
$node->{ip} = $device->{'ip-address'};
$node->{organizations} = [];
foreach (@{$device->{orgs}}) {
push @{$node->{organizations}}, $options{organizations}->{entries}->{ $_->{uuid} }->{name};
}
$node->{ip} = $device->{ipAddress};
$node->{organizations} = $device->{orgs};
push @$disco_data, $node;
}
@ -77,13 +73,18 @@ sub discovery_organizations {
my ($self, %options) = @_;
my $disco_data = [];
foreach my $org (values %{$options{organizations}->{entries}}) {
foreach my $org (values %{$options{orgs}->{entries}}) {
my $node = {};
$node->{uuid} = $org->{uuid};
$node->{name} = $org->{name};
$node->{devices} = [];
foreach (@{$org->{appliances}}) {
push @{$node->{devices}}, $options{devices}->{entries}->{ $_ }->{name};
foreach my $device (values %{$options{devices}->{entries}}) {
foreach (@{$device->{orgs}}) {
if ($_ eq $org->{name}) {
push @{$node->{devices}}, $device->{name};
last;
}
}
}
push @$disco_data, $node;
@ -98,19 +99,20 @@ sub run {
my $disco_stats;
$disco_stats->{start_time} = time();
my $organizations = $options{custom}->get_organizations(disable_cache => 1);
my $devices = $options{custom}->get_appliances(disable_cache => 1);
my $orgs = $options{custom}->get_organizations();
# find top level org with depth attribute
my $root_org_name = $options{custom}->find_root_organization_name(orgs => $orgs);
my $devices = $options{custom}->get_devices(org_name => $root_org_name);
my $results = [];
if ($self->{option_results}->{resource_type} eq 'device') {
$results = $self->discovery_devices(
devices => $devices,
organizations => $organizations
devices => $devices
);
} else {
$results = $self->discovery_organizations(
devices => $devices,
organizations => $organizations
orgs => $orgs
);
}
@ -130,7 +132,7 @@ sub run {
if ($@) {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
$self->{output}->exit();

View File

@ -44,7 +44,10 @@ sub check_options {
sub manage_selection {
my ($self, %options) = @_;
return $options{custom}->get_appliances(disable_cache => 1);
my $orgs = $options{custom}->get_organizations();
# find top level org with depth attribute
my $root_org_name = $options{custom}->find_root_organization_name(orgs => $orgs);
return $options{custom}->get_devices(org_name => $root_org_name);
}
sub run {
@ -54,11 +57,11 @@ sub run {
foreach (values %{$results->{entries}}) {
$self->{output}->output_add(
long_msg => sprintf(
'[uuid = %s][name = %s][type = %s][ip address = %s]',
'[uuid = %s][name = %s][type = %s][ip_address = %s]',
$_->{uuid},
$_->{name},
$_->{type},
$_->{'ip-address'}
$_->{ipAddress}
)
);
}
@ -74,7 +77,7 @@ sub run {
sub disco_format {
my ($self, %options) = @_;
$self->{output}->add_disco_format(elements => ['uuid', 'name', 'type', 'ipaddress']);
$self->{output}->add_disco_format(elements => ['uuid', 'name', 'type', 'ip_address']);
}
sub disco_show {
@ -86,7 +89,7 @@ sub disco_show {
uuid => $_->{uuid},
name => $_->{name},
type => $_->{type},
ipaddress => $_->{'ip-address'}
ip_address => $_->{ipAddress}
);
}
}

View File

@ -0,0 +1,147 @@
#
# Copyright 2021 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 network::versa::director::restapi::mode::listpaths;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments => {
'filter-device-name:s' => { name => 'filter_device_name' },
'filter-device-type:s' => { name => 'filter_device_type' }
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
sub manage_selection {
my ($self, %options) = @_;
my $orgs = $options{custom}->get_organizations();
my $root_org_name = $options{custom}->find_root_organization_name(orgs => $orgs);
my $devices = $options{custom}->get_devices(org_name => $root_org_name);
my $results = {};
foreach my $device (values %{$devices->{entries}}) {
if (defined($self->{option_results}->{filter_device_name}) && $self->{option_results}->{filter_device_name} ne '' &&
$device->{name} !~ /$self->{option_results}->{filter_device_name}/) {
$self->{output}->output_add(long_msg => "skipping device '" . $device->{name} . "': no matching filter name.", debug => 1);
next;
}
if (defined($self->{option_results}->{filter_device_type}) && $self->{option_results}->{filter_device_type} ne '' &&
$device->{type} !~ /$self->{option_results}->{filter_device_type}/) {
$self->{output}->output_add(long_msg => "skipping device '" . $device->{name} . "': no matching filter type.", debug => 1);
next;
}
my $paths = $options{custom}->get_device_paths(
org_name => $root_org_name,
device_name => $device->{name}
);
my $i = 0;
foreach (@{$paths->{entries}}) {
$results->{ $device->{name} . ':' . $i } = {
device_name => $device->{name},
device_type => $device->{type},
remote_site_name => $_->{remoteSiteName},
local_wan_link => $_->{localWanLink},
remote_wan_link => $_->{remoteWanLink}
};
$i++;
}
}
return $results;
}
sub run {
my ($self, %options) = @_;
my $results = $self->manage_selection(%options);
foreach (values %$results) {
$self->{output}->output_add(
long_msg => sprintf(
'[device_name: %s][device_type: %s][remote_site_name: %s][local_wan_link: %s][remote_wan_link: %s]',
$_->{device_name},
$_->{device_type},
$_->{remote_site_name},
$_->{local_wan_link},
$_->{remote_wan_link}
)
);
}
$self->{output}->output_add(
severity => 'OK',
short_msg => 'List device paths:'
);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit();
}
sub disco_format {
my ($self, %options) = @_;
$self->{output}->add_disco_format(elements => ['device_name', 'device_type', 'remote_site_name', 'local_wan_link', 'remote_wan_link']);
}
sub disco_show {
my ($self, %options) = @_;
my $results = $self->manage_selection(%options);
foreach (values %$results) {
$self->{output}->add_disco_entry(%$_);
}
}
1;
__END__
=head1 MODE
List paths by devices.
=over 8
=item B<--filter-device-name>
Filter device by name (Can be a regexp).
=item B<--filter-device-type>
Filter device by type (Can be a regexp).
=back
=cut

View File

@ -0,0 +1,308 @@
#
# Copyright 2021 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 network::versa::director::restapi::mode::paths;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
sub grp1_long_output {
my ($self, %options) = @_;
return "checking '" . $options{instance_value}->{name} . "'";
}
sub prefix_grp1_output {
my ($self, %options) = @_;
return "'" . $options{instance_value}->{name} . "' ";
}
sub prefix_global_output {
my ($self, %options) = @_;
return 'Total paths ';
}
sub prefix_grp1_paths_output {
my ($self, %options) = @_;
return 'paths ';
}
sub prefix_grp2_output {
my ($self, %options) = @_;
return "'" . $options{instance_value}->{name} . "' ";
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', skipped_code => { -10 => 1 } },
{ name => 'grp1', type => 3, cb_prefix_output => 'prefix_grp1_output', cb_long_output => 'grp1_long_output', indent_long_output => ' ', message_multiple => 'All group paths are ok',
group => [
{ name => 'grp1_paths', type => 0, cb_prefix_output => 'prefix_grp1_paths_output', skipped_code => { -10 => 1 } },
{ name => 'grp2', type => 1, display_long => 1, cb_prefix_output => 'prefix_grp2_output', message_multiple => 'sub-group paths are ok', skipped_code => { -10 => 1 } }
]
}
];
$self->{maps_counters}->{global} = [
{ label => 'total-paths-up', nlabel => 'paths.up.count', set => {
key_values => [ { name => 'up' } ],
output_template => 'up: %s',
perfdatas => [
{ template => '%d', min => 0 }
]
}
},
{ label => 'total-paths-down', nlabel => 'paths.down.count', set => {
key_values => [ { name => 'down' } ],
output_template => 'down: %s',
perfdatas => [
{ template => '%d', min => 0 }
]
}
}
];
$self->{maps_counters}->{grp1_paths} = [
{ label => 'group-paths-up', nlabel => 'paths.up.count', set => {
key_values => [ { name => 'up' } ],
output_template => 'up: %s',
perfdatas => [
{ template => '%d', min => 0, label_extra_instance => 1 }
]
}
},
{ label => 'group-paths-down', nlabel => 'paths.down.count', set => {
key_values => [ { name => 'down' } ],
output_template => 'down: %s',
perfdatas => [
{ template => '%d', min => 0, label_extra_instance => 1 }
]
}
}
];
$self->{maps_counters}->{grp2} = [
{ label => 'subgroup-paths-up', nlabel => 'paths.up.count', set => {
key_values => [ { name => 'up' } ],
output_template => 'up: %s',
perfdatas => [
{ template => '%d', min => 0, label_extra_instance => 1 }
]
}
},
{ label => 'subgroup-paths-down', nlabel => 'paths.down.count', set => {
key_values => [ { name => 'down' } ],
output_template => 'down: %s',
perfdatas => [
{ template => '%d', min => 0, label_extra_instance => 1 }
]
}
}
];
}
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 => {
'group:s' => { name => 'group' },
'organization:s' => { name => 'organization' },
'filter-org-name:s' => { name => 'filter_org_name' },
'filter-device-name:s' => { name => 'filter_device_name' },
'filter-device-type:s' => { name => 'filter_device_type' },
'filter-local-wan-link:s' => { name => 'filter_local_wan_link' },
'filter-remote-site-name:s' => { name => 'filter_remote_site_name' },
'filter-remote-wan-link:s' => { name => 'filter_remote_wan_link' }
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
if (!defined($self->{option_results}->{group}) || $self->{option_results}->{group} eq '') {
$self->{option_results}->{group} = 'remoteSiteName';
}
my $dimensions = { localsitename => 'localSiteName', localwanlink => 'localWanLink', remotesitename => 'remoteSiteName', remotewanlink => 'remoteWanLink' };
my ($grp, $subgrp) = split(/,/, $self->{option_results}->{group});
if (!defined($dimensions->{ lc($grp) })) {
$self->{output}->add_option_msg(short_msg => "Unknown --group name: $grp.");
$self->{output}->option_exit();
}
$self->{grp_name} = $dimensions->{ lc($grp) };
if (defined($subgrp)) {
if (!defined($dimensions->{ lc($subgrp) })) {
$self->{output}->add_option_msg(short_msg => "Unknown --group name: $subgrp.");
$self->{output}->option_exit();
}
$self->{subgrp_name} = $dimensions->{ lc($subgrp) };
}
}
sub manage_selection {
my ($self, %options) = @_;
my $orgs = $options{custom}->get_organizations();
my $root_org_name = $options{custom}->find_root_organization_name(orgs => $orgs);
my $devices = {};
if (defined($self->{option_results}->{organization}) && $self->{option_results}->{organization} ne '') {
my $result = $options{custom}->get_devices(org_name => $self->{option_results}->{organization});
$devices = $result->{entries};
} else {
foreach my $org (values %{$orgs->{entries}}) {
if (defined($self->{option_results}->{filter_org_name}) && $self->{option_results}->{filter_org_name} ne '' &&
$org->{name} !~ /$self->{option_results}->{filter_org_name}/) {
$self->{output}->output_add(long_msg => "skipping org '" . $org->{name} . "': no matching filter name.", debug => 1);
next;
}
my $result = $options{custom}->get_devices(org_name => $org->{name});
$devices = { %$devices, %{$result->{entries}} };
}
}
$self->{global} = { up => 0, down => 0 };
$self->{grp1} = {};
foreach my $device (values %$devices) {
if (defined($self->{option_results}->{filter_device_name}) && $self->{option_results}->{filter_device_name} ne '' &&
$device->{name} !~ /$self->{option_results}->{filter_device_name}/) {
$self->{output}->output_add(long_msg => "skipping device '" . $device->{name} . "': no matching filter name.", debug => 1);
next;
}
if (defined($self->{option_results}->{filter_device_type}) && $self->{option_results}->{filter_device_type} ne '' &&
$device->{type} !~ /$self->{option_results}->{filter_device_type}/) {
$self->{output}->output_add(long_msg => "skipping device '" . $device->{name} . "': no matching filter type.", debug => 1);
next;
}
my $paths = $options{custom}->get_device_paths(
org_name => $root_org_name,
device_name => $device->{name}
);
foreach my $path (@{$paths->{entries}}) {
$path->{localSiteName} = $device->{name};
next if (
defined($self->{option_results}->{filter_local_wan_link}) && $self->{option_results}->{filter_local_wan_link} ne '' &&
$path->{localWanLink} !~ /$self->{option_results}->{filter_local_wan_link}/
);
next if (
defined($self->{option_results}->{filter_remote_site_name}) && $self->{option_results}->{filter_remote_site_name} ne '' &&
$path->{remoteSiteName} !~ /$self->{option_results}->{filter_remote_site_name}/
);
next if (
defined($self->{option_results}->{filter_remote_wan_link}) && $self->{option_results}->{filter_remote_wan_link} ne '' &&
$path->{remoteWanLink} !~ /$self->{option_results}->{filter_remote_wan_link}/
);
if (!defined($self->{grp1}->{ $path->{$self->{grp_name}} })) {
$self->{grp1}->{ $path->{$self->{grp_name}} } = {
name => $path->{ $self->{grp_name} },
grp1_paths => {
up => 0,
down => 0
},
grp2 => {}
};
}
$self->{global}->{ $path->{connState} }++;
$self->{grp1}->{ $path->{$self->{grp_name}} }->{grp1_paths}->{ $path->{connState} }++;
next if (!defined($self->{subgrp_name}));
if (!defined($self->{grp1}->{ $path->{$self->{grp_name}} }->{grp2}->{ $path->{$self->{subgrp_name}} })) {
$self->{grp1}->{ $path->{$self->{grp_name}} }->{grp2}->{ $path->{$self->{subgrp_name}} } = {
name => $path->{$self->{subgrp_name}},
up => 0,
down => 0
};
}
$self->{grp1}->{ $path->{$self->{grp_name}} }->{grp2}->{ $path->{$self->{subgrp_name}} }->{ $path->{connState} }++;
}
}
}
1;
__END__
=head1 MODE
Check paths.
=over 8
=item B<--group>
Choose dimensions to group paths up/down.
Default: --group='remoteSiteName'
=item B<--organization>
Check device under an organization name.
=item B<--filter-org-name>
Filter organizations by name (Can be a regexp).
=item B<--filter-device-name>
Filter devices by name (Can be a regexp).
=item B<--filter-device-type>
Filter devices by type (Can be a regexp).
=item B<--filter-local-wan-link>
Filter paths by localWanLink (Can be a regexp).
=item B<--filter-remote-site-name>
Filter paths by remoteSiteName (Can be a regexp).
=item B<--filter-remote-wan-link>
Filter paths by remoteWanLink (Can be a regexp).
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'total-paths-up', 'total-paths-down',
'group-paths-up', 'group-paths-down',
'subgroup-paths-up', 'subgroup-paths-down'.
=back
=cut

View File

@ -31,9 +31,12 @@ sub new {
$self->{version} = '1.0';
$self->{modes} = {
'cache' => 'network::versa::director::restapi::mode::cache',
'devices' => 'network::versa::director::restapi::mode::devices',
'discovery' => 'network::versa::director::restapi::mode::discovery',
'list-devices' => 'network::versa::director::restapi::mode::listdevices'
'list-devices' => 'network::versa::director::restapi::mode::listdevices',
'list-paths' => 'network::versa::director::restapi::mode::listpaths',
'paths' => 'network::versa::director::restapi::mode::paths'
};
$self->{custom_modes}->{api} = 'network::versa::director::restapi::custom::api';