add azure vpn gateway, fix expressroute circuit status

This commit is contained in:
Colin Gagnaire 2019-01-25 12:05:16 +01:00
parent 495e0c915f
commit 07992b2a76
7 changed files with 627 additions and 227 deletions

View File

@ -219,6 +219,8 @@ sub request_api {
$self->settings();
$self->{output}->output_add(long_msg => "URL: '" . $options{full_url} . "'", debug => 1);
my $content = $self->{http}->request(%options);
my $decoded;
@ -290,7 +292,7 @@ sub azure_get_metrics {
my $metric_name = lc($metric->{name}->{value});
$metric_name =~ s/ /_/g;
$results->{$metric_name} = { points => 0 };
$results->{$metric_name} = { points => 0, name => $metric->{name}->{localizedValue} };
foreach my $timeserie (@{$metric->{timeseries}}) {
foreach my $point (@{$timeserie->{data}}) {
if (defined($point->{average})) {
@ -349,8 +351,7 @@ sub azure_list_resources_set_url {
sub azure_list_resources {
my ($self, %options) = @_;
my $results = {};
my $full_url = $self->azure_list_resources_set_url(%options);
my $response = $self->request_api(method => 'GET', full_url => $full_url, hostname => '');
@ -370,7 +371,6 @@ sub azure_list_vms_set_url {
sub azure_list_vms {
my ($self, %options) = @_;
my $results = {};
my $full_url = $self->azure_list_vms_set_url(%options);
my $response = $self->request_api(method => 'GET', full_url => $full_url, hostname => '');
@ -388,7 +388,6 @@ sub azure_list_groups_set_url {
sub azure_list_groups {
my ($self, %options) = @_;
my $results = {};
my $full_url = $self->azure_list_groups_set_url(%options);
my $response = $self->request_api(method => 'GET', full_url => $full_url, hostname => '');
@ -407,7 +406,6 @@ sub azure_list_deployments_set_url {
sub azure_list_deployments {
my ($self, %options) = @_;
my $results = {};
my $full_url = $self->azure_list_deployments_set_url(%options);
my $response = $self->request_api(method => 'GET', full_url => $full_url, hostname => '');
@ -427,7 +425,6 @@ sub azure_list_vaults_set_url {
sub azure_list_vaults {
my ($self, %options) = @_;
my $results = {};
my $full_url = $self->azure_list_vaults_set_url(%options);
my $response = $self->request_api(method => 'GET', full_url => $full_url, hostname => '');
@ -447,7 +444,6 @@ sub azure_list_backup_jobs_set_url {
sub azure_list_backup_jobs {
my ($self, %options) = @_;
my $results = {};
my $full_url = $self->azure_list_backup_jobs_set_url(%options);
my $response = $self->request_api(method => 'GET', full_url => $full_url, hostname => '');
@ -467,7 +463,6 @@ sub azure_list_backup_items_set_url {
sub azure_list_backup_items {
my ($self, %options) = @_;
my $results = {};
my $full_url = $self->azure_list_backup_items_set_url(%options);
my $response = $self->request_api(method => 'GET', full_url => $full_url, hostname => '');
@ -487,13 +482,30 @@ sub azure_list_expressroute_circuits_set_url {
sub azure_list_expressroute_circuits {
my ($self, %options) = @_;
my $results = {};
my $full_url = $self->azure_list_expressroute_circuits_set_url(%options);
my $response = $self->request_api(method => 'GET', full_url => $full_url, hostname => '');
return $response->{value};
}
sub azure_list_vpn_gateways_set_url {
my ($self, %options) = @_;
my $url = $self->{management_endpoint} . "/subscriptions/" . $self->{subscription} . "/resourcegroups/" .
$options{resource_group} . "/providers/Microsoft.Network/virtualNetworkGateways?api-version=" . $self->{api_version};
return $url;
}
sub azure_list_vpn_gateways {
my ($self, %options) = @_;
my $full_url = $self->azure_list_vpn_gateways_set_url(%options);
my $response = $self->request_api(method => 'GET', full_url => $full_url, hostname => '');
return $response->{value};
}
1;
__END__

View File

@ -110,6 +110,32 @@ sub check_options {
return 0;
}
sub execute {
my ($self, %options) = @_;
$self->{output}->output_add(long_msg => "Command line: '" . $self->{option_results}->{command} . " " . $options{cmd_options} . "'", debug => 1);
my ($response) = centreon::plugins::misc::execute(
output => $self->{output},
options => $self->{option_results},
sudo => $self->{option_results}->{sudo},
command => $self->{option_results}->{command},
command_path => $self->{option_results}->{command_path},
command_options => $options{cmd_options});
my $raw_results;
eval {
$raw_results = JSON::XS->new->utf8->decode($response);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
}
return $raw_results;
}
sub convert_duration {
my ($self, %options) = @_;
@ -153,31 +179,13 @@ sub azure_get_metrics {
my $end_time = DateTime->now->iso8601.'Z';
my $cmd_options = $self->azure_get_metrics_set_cmd(%options, start_time => $start_time, end_time => $end_time);
my ($response) = centreon::plugins::misc::execute(
output => $self->{output},
options => $self->{option_results},
sudo => $self->{option_results}->{sudo},
command => $self->{option_results}->{command},
command_path => $self->{option_results}->{command_path},
command_options => $cmd_options);
my $raw_results;
my $command_line = $self->{option_results}->{command} . " " . $cmd_options;
eval {
$raw_results = JSON::XS->new->utf8->decode($response);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
}
my $raw_results = $self->execute(cmd_options => $cmd_options);
foreach my $metric (@{$raw_results->{value}}) {
my $metric_name = lc($metric->{name}->{value});
$metric_name =~ s/ /_/g;
$results->{$metric_name} = { points => 0 };
$results->{$metric_name} = { points => 0, name => $metric->{name}->{localizedValue} };
foreach my $timeserie (@{$metric->{timeseries}}) {
foreach my $point (@{$timeserie->{data}}) {
if (defined($point->{average})) {
@ -205,8 +213,8 @@ sub azure_get_metrics {
$results->{$metric_name}->{average} /= $results->{$metric_name}->{points};
}
}
return $results, $raw_results, $command_line;
return $results, $raw_results;
}
sub azure_list_resources_set_cmd {
@ -226,29 +234,9 @@ sub azure_list_resources_set_cmd {
sub azure_list_resources {
my ($self, %options) = @_;
my $results = {};
my $cmd_options = $self->azure_list_resources_set_cmd(%options);
my ($response) = centreon::plugins::misc::execute(
output => $self->{output},
options => $self->{option_results},
sudo => $self->{option_results}->{sudo},
command => $self->{option_results}->{command},
command_path => $self->{option_results}->{command_path},
command_options => $cmd_options);
my $raw_results;
my $command_line = $self->{option_results}->{command} . " " . $cmd_options;
eval {
$raw_results = JSON::XS->new->utf8->decode($response);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
}
my $raw_results = $self->execute(cmd_options => $cmd_options);
return $raw_results;
}
@ -266,29 +254,9 @@ sub azure_list_vm_sizes_set_cmd {
sub azure_list_vm_sizes {
my ($self, %options) = @_;
my $results = {};
my $cmd_options = $self->azure_list_vm_sizes_set_cmd(%options);
my ($response) = centreon::plugins::misc::execute(
output => $self->{output},
options => $self->{option_results},
sudo => $self->{option_results}->{sudo},
command => $self->{option_results}->{command},
command_path => $self->{option_results}->{command_path},
command_options => $cmd_options);
my $raw_results;
my $command_line = $self->{option_results}->{command} . " " . $cmd_options;
eval {
$raw_results = JSON::XS->new->utf8->decode($response);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
}
my $raw_results = $self->execute(cmd_options => $cmd_options);
return $raw_results;
}
@ -308,29 +276,9 @@ sub azure_list_vms_set_cmd {
sub azure_list_vms {
my ($self, %options) = @_;
my $results = {};
my $cmd_options = $self->azure_list_vms_set_cmd(%options);
my ($response) = centreon::plugins::misc::execute(
output => $self->{output},
options => $self->{option_results},
sudo => $self->{option_results}->{sudo},
command => $self->{option_results}->{command},
command_path => $self->{option_results}->{command_path},
command_options => $cmd_options);
my $raw_results;
my $command_line = $self->{option_results}->{command} . " " . $cmd_options;
eval {
$raw_results = JSON::XS->new->utf8->decode($response);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
}
my $raw_results = $self->execute(cmd_options => $cmd_options);
return $raw_results;
}
@ -348,29 +296,9 @@ sub azure_list_groups_set_cmd {
sub azure_list_groups {
my ($self, %options) = @_;
my $results = {};
my $cmd_options = $self->azure_list_groups_set_cmd(%options);
my ($response) = centreon::plugins::misc::execute(
output => $self->{output},
options => $self->{option_results},
sudo => $self->{option_results}->{sudo},
command => $self->{option_results}->{command},
command_path => $self->{option_results}->{command_path},
command_options => $cmd_options);
my $raw_results;
my $command_line = $self->{option_results}->{command} . " " . $cmd_options;
eval {
$raw_results = JSON::XS->new->utf8->decode($response);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
}
my $raw_results = $self->execute(cmd_options => $cmd_options);
return $raw_results;
}
@ -379,7 +307,7 @@ sub azure_list_deployments_set_cmd {
my ($self, %options) = @_;
return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '');
my $cmd_options = "group deployment list --resource-group '$options{resource_group}' --output json";
$cmd_options .= " --subscription '$self->{subscription}'" if (defined($self->{subscription}) && $self->{subscription} ne '');
@ -388,29 +316,9 @@ sub azure_list_deployments_set_cmd {
sub azure_list_deployments {
my ($self, %options) = @_;
my $results = {};
my $cmd_options = $self->azure_list_deployments_set_cmd(%options);
my ($response) = centreon::plugins::misc::execute(
output => $self->{output},
options => $self->{option_results},
sudo => $self->{option_results}->{sudo},
command => $self->{option_results}->{command},
command_path => $self->{option_results}->{command_path},
command_options => $cmd_options);
my $raw_results;
my $command_line = $self->{option_results}->{command} . " " . $cmd_options;
eval {
$raw_results = JSON::XS->new->utf8->decode($response);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
}
my $raw_results = $self->execute(cmd_options => $cmd_options);
return $raw_results;
}
@ -419,7 +327,7 @@ sub azure_list_vaults_set_cmd {
my ($self, %options) = @_;
return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '');
my $cmd_options = "backup vault list --output json";
$cmd_options .= " --resource-group '$options{resource_group}'" if (defined($options{resource_group}) && $options{resource_group} ne '');
$cmd_options .= " --subscription '$self->{subscription}'" if (defined($self->{subscription}) && $self->{subscription} ne '');
@ -429,29 +337,9 @@ sub azure_list_vaults_set_cmd {
sub azure_list_vaults {
my ($self, %options) = @_;
my $results = {};
my $cmd_options = $self->azure_list_vaults_set_cmd(%options);
my ($response) = centreon::plugins::misc::execute(
output => $self->{output},
options => $self->{option_results},
sudo => $self->{option_results}->{sudo},
command => $self->{option_results}->{command},
command_path => $self->{option_results}->{command_path},
command_options => $cmd_options);
my $raw_results;
my $command_line = $self->{option_results}->{command} . " " . $cmd_options;
eval {
$raw_results = JSON::XS->new->utf8->decode($response);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
}
my $raw_results = $self->execute(cmd_options => $cmd_options);
return $raw_results;
}
@ -460,7 +348,7 @@ sub azure_list_backup_jobs_set_cmd {
my ($self, %options) = @_;
return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '');
my $cmd_options = "backup job list --resource-group '$options{resource_group}' --vault-name '$options{vault_name}' --output json";
$cmd_options .= " --subscription '$self->{subscription}'" if (defined($self->{subscription}) && $self->{subscription} ne '');
@ -469,29 +357,9 @@ sub azure_list_backup_jobs_set_cmd {
sub azure_list_backup_jobs {
my ($self, %options) = @_;
my $results = {};
my $cmd_options = $self->azure_list_backup_jobs_set_cmd(%options);
my ($response) = centreon::plugins::misc::execute(
output => $self->{output},
options => $self->{option_results},
sudo => $self->{option_results}->{sudo},
command => $self->{option_results}->{command},
command_path => $self->{option_results}->{command_path},
command_options => $cmd_options);
my $raw_results;
my $command_line = $self->{option_results}->{command} . " " . $cmd_options;
eval {
$raw_results = JSON::XS->new->utf8->decode($response);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
}
my $raw_results = $self->execute(cmd_options => $cmd_options);
return $raw_results;
}
@ -500,7 +368,7 @@ sub azure_list_backup_items_set_cmd {
my ($self, %options) = @_;
return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '');
my $cmd_options = "backup item list --resource-group '$options{resource_group}' --vault-name '$options{vault_name}' --output json";
$cmd_options .= " --subscription '$self->{subscription}'" if (defined($self->{subscription}) && $self->{subscription} ne '');
@ -509,29 +377,9 @@ sub azure_list_backup_items_set_cmd {
sub azure_list_backup_items {
my ($self, %options) = @_;
my $results = {};
my $cmd_options = $self->azure_list_backup_items_set_cmd(%options);
my ($response) = centreon::plugins::misc::execute(
output => $self->{output},
options => $self->{option_results},
sudo => $self->{option_results}->{sudo},
command => $self->{option_results}->{command},
command_path => $self->{option_results}->{command_path},
command_options => $cmd_options);
my $raw_results;
my $command_line = $self->{option_results}->{command} . " " . $cmd_options;
eval {
$raw_results = JSON::XS->new->utf8->decode($response);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
}
my $raw_results = $self->execute(cmd_options => $cmd_options);
return $raw_results;
}
@ -540,7 +388,7 @@ sub azure_list_expressroute_circuits_set_cmd {
my ($self, %options) = @_;
return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '');
my $cmd_options = "network express-route list --output json";
$cmd_options .= " --resource-group '$options{resource_group}'" if (defined($options{resource_group}) && $options{resource_group} ne '');
$cmd_options .= " --subscription '$self->{subscription}'" if (defined($self->{subscription}) && $self->{subscription} ne '');
@ -550,29 +398,29 @@ sub azure_list_expressroute_circuits_set_cmd {
sub azure_list_expressroute_circuits {
my ($self, %options) = @_;
my $results = {};
my $cmd_options = $self->azure_list_expressroute_circuits_set_cmd(%options);
my $raw_results = $self->execute(cmd_options => $cmd_options);
my ($response) = centreon::plugins::misc::execute(
output => $self->{output},
options => $self->{option_results},
sudo => $self->{option_results}->{sudo},
command => $self->{option_results}->{command},
command_path => $self->{option_results}->{command_path},
command_options => $cmd_options);
return $raw_results;
}
my $raw_results;
my $command_line = $self->{option_results}->{command} . " " . $cmd_options;
sub azure_list_vpn_gateways_set_cmd {
my ($self, %options) = @_;
return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '');
my $cmd_options = "network vnet-gateway list --resource-group '$options{resource_group}' --output json";
$cmd_options .= " --subscription '$self->{subscription}'" if (defined($self->{subscription}) && $self->{subscription} ne '');
eval {
$raw_results = JSON::XS->new->utf8->decode($response);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
}
return $cmd_options;
}
sub azure_list_vpn_gateways {
my ($self, %options) = @_;
my $cmd_options = $self->azure_list_vpn_gateways_set_cmd(%options);
my $raw_results = $self->execute(cmd_options => $cmd_options);
return $raw_results;
}

View File

@ -106,7 +106,7 @@ sub manage_selection {
my $circuits = $options{custom}->azure_list_expressroute_circuits(resource_group => $self->{option_results}->{resource_group});
foreach my $circuit (@{$circuits}) {
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
&& $circuit->{properties}->{entityFriendlyName} !~ /$self->{option_results}->{filter_name}/);
&& $circuit->{name} !~ /$self->{option_results}->{filter_name}/);
next if (defined($self->{option_results}->{location}) && $self->{option_results}->{location} ne ''
&& $circuit->{location} !~ /$self->{option_results}->{location}/);

View File

@ -39,7 +39,7 @@ sub custom_traffic_calc {
$self->{result_values}->{stat} = $options{new_datas}->{$self->{instance} . '_stat'};
$self->{result_values}->{metric_perf} = lc($options{extra_options}->{metric_perf});
$self->{result_values}->{metric_label} = lc($options{extra_options}->{metric_label});
$self->{result_values}->{metric_name} = $options{extra_options}->{metric_name};
$self->{result_values}->{metric_name} = $options{new_datas}->{$self->{instance} . '_' . $self->{result_values}->{metric_perf} . '_' . $self->{result_values}->{stat} . '_name'};
$self->{result_values}->{timeframe} = $options{new_datas}->{$self->{instance} . '_timeframe'};
$self->{result_values}->{value} = $options{new_datas}->{$self->{instance} . '_' . $self->{result_values}->{metric_perf} . '_' . $self->{result_values}->{stat}};
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
@ -93,10 +93,10 @@ sub set_counters {
$metric_label =~ s/ /-/g;
my $entry = { label => $metric_label . '-' . $aggregation, set => {
key_values => [ { name => $metric_perf . '_' . $aggregation }, { name => 'display' },
{ name => 'stat' }, { name => 'timeframe' } ],
{ name => 'stat' }, { name => 'stat' }, { name => $metric_perf . '_' . $aggregation . '_name' } ],
closure_custom_calc => $self->can('custom_traffic_calc'),
closure_custom_calc_extra_options => { metric_perf => $metric_perf,
metric_label => $metric_label, metric_name => $metric },
metric_label => $metric_label },
closure_custom_output => $self->can('custom_traffic_output'),
closure_custom_perfdata => $self->can('custom_traffic_perfdata'),
closure_custom_threshold_check => $self->can('custom_traffic_threshold'),
@ -190,6 +190,7 @@ sub manage_selection {
$self->{metric}->{$resource_name . "_" . lc($aggregation)}->{display} = $resource_name;
$self->{metric}->{$resource_name . "_" . lc($aggregation)}->{timeframe} = $self->{az_timeframe};
$self->{metric}->{$resource_name . "_" . lc($aggregation)}->{stat} = lc($aggregation);
$self->{metric}->{$resource_name . "_" . lc($aggregation)}->{$metric_name . "_" . lc($aggregation) . '_name'} = $metric_results{$resource_name}->{$metric_name}->{name};
$self->{metric}->{$resource_name . "_" . lc($aggregation)}->{$metric_name . "_" . lc($aggregation)} = defined($metric_results{$resource_name}->{$metric_name}->{lc($aggregation)}) ? $metric_results{$resource_name}->{$metric_name}->{lc($aggregation)} : 0;
}
}

View File

@ -0,0 +1,316 @@
#
# Copyright 2019 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 cloud::azure::network::vpngateway::mode::tunneltraffic;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
my $instance_mode;
sub prefix_metric_output {
my ($self, %options) = @_;
return "VPN Gateway '" . $options{instance_value}->{display} . "' " . $options{instance_value}->{stat} . " ";
}
sub custom_metric_calc {
my ($self, %options) = @_;
$self->{result_values}->{stat} = $options{new_datas}->{$self->{instance} . '_stat'};
$self->{result_values}->{metric_perf} = lc($options{extra_options}->{metric_perf});
$self->{result_values}->{metric_label} = lc($options{extra_options}->{metric_label});
$self->{result_values}->{metric_name} = $options{new_datas}->{$self->{instance} . '_' . $self->{result_values}->{metric_perf} . '_' . $self->{result_values}->{stat} . '_name'};
$self->{result_values}->{timeframe} = $options{new_datas}->{$self->{instance} . '_timeframe'};
$self->{result_values}->{value} = $options{new_datas}->{$self->{instance} . '_' . $self->{result_values}->{metric_perf} . '_' . $self->{result_values}->{stat}};
$self->{result_values}->{value_per_sec} = $self->{result_values}->{value} / $self->{result_values}->{timeframe};
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
return 0;
}
sub custom_metric_threshold {
my ($self, %options) = @_;
my $exit = $self->{perfdata}->threshold_check(value => defined($instance_mode->{option_results}->{per_sec}) ? $self->{result_values}->{value_per_sec} : $self->{result_values}->{value},
threshold => [ { label => 'critical-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}, exit_litteral => 'critical' },
{ label => 'warning-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}, exit_litteral => 'warning' } ]);
return $exit;
}
sub custom_traffic_perfdata {
my ($self, %options) = @_;
my $extra_label = '';
$extra_label = '_' . lc($self->{result_values}->{display}) if (!defined($options{extra_instance}) || $options{extra_instance} != 0);
$self->{output}->perfdata_add(label => $self->{result_values}->{metric_perf} . "_" . $self->{result_values}->{stat} . $extra_label,
unit => defined($instance_mode->{option_results}->{per_sec}) ? 'B/s' : 'B',
value => sprintf("%.2f", defined($instance_mode->{option_results}->{per_sec}) ? $self->{result_values}->{value_per_sec} : $self->{result_values}->{value}),
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}),
min => 0
);
}
sub custom_traffic_output {
my ($self, %options) = @_;
my $msg = "";
if (defined($instance_mode->{option_results}->{per_sec})) {
my ($value, $unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{value_per_sec});
$msg = $self->{result_values}->{metric_name} . ": " . $value . $unit . "/s";
} else {
my ($value, $unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{value});
$msg = $self->{result_values}->{metric_name} . ": " . $value . $unit;
}
return $msg;
}
sub custom_packets_perfdata {
my ($self, %options) = @_;
my $extra_label = '';
$extra_label = '_' . lc($self->{result_values}->{display}) if (!defined($options{extra_instance}) || $options{extra_instance} != 0);
$self->{output}->perfdata_add(label => $self->{result_values}->{metric_perf} . "_" . $self->{result_values}->{stat} . $extra_label,
unit => defined($instance_mode->{option_results}->{per_sec}) ? 'packets/s' : 'packets',
value => sprintf("%.2f", defined($instance_mode->{option_results}->{per_sec}) ? $self->{result_values}->{value_per_sec} : $self->{result_values}->{value}),
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}),
min => 0
);
}
sub custom_packets_output {
my ($self, %options) = @_;
my $msg ="";
if (defined($instance_mode->{option_results}->{per_sec})) {
$msg = sprintf("%s: %.2f packets/s", $self->{result_values}->{metric_name}, $self->{result_values}->{value_per_sec});
} else {
$msg = sprintf("%s: %.2f packets", $self->{result_values}->{metric_name}, $self->{result_values}->{value});
}
return $msg;
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'metric', type => 1, cb_prefix_output => 'prefix_metric_output', message_multiple => "All traffic metrics are ok", skipped_code => { -10 => 1 } },
];
foreach my $aggregation ('minimum', 'maximum', 'average', 'total') {
foreach my $metric ('TunnelIngressBytes', 'TunnelEgressBytes') {
my $metric_perf = lc($metric);
my $metric_label = lc($metric);
$metric_perf =~ s/ /_/g;
$metric_label =~ s/ /-/g;
my $entry = { label => $metric_label . '-' . $aggregation, set => {
key_values => [ { name => $metric_perf . '_' . $aggregation }, { name => 'display' },
{ name => 'stat' }, { name => $metric_perf . '_' . $aggregation . '_name' }, { name => 'timeframe' } ],
closure_custom_calc => $self->can('custom_metric_calc'),
closure_custom_calc_extra_options => { metric_perf => $metric_perf,
metric_label => $metric_label },
closure_custom_output => $self->can('custom_traffic_output'),
closure_custom_perfdata => $self->can('custom_traffic_perfdata'),
closure_custom_threshold_check => $self->can('custom_metric_threshold'),
}
};
push @{$self->{maps_counters}->{metric}}, $entry;
}
foreach my $metric ('TunnelIngressPackets', 'TunnelEgressPackets') {
my $metric_perf = lc($metric);
my $metric_label = lc($metric);
$metric_perf =~ s/ /_/g;
$metric_label =~ s/ /-/g;
my $entry = { label => $metric_label . '-' . $aggregation, set => {
key_values => [ { name => $metric_perf . '_' . $aggregation }, { name => 'display' },
{ name => 'stat' }, { name => $metric_perf . '_' . $aggregation . '_name' }, { name => 'timeframe' } ],
closure_custom_calc => $self->can('custom_metric_calc'),
closure_custom_calc_extra_options => { metric_perf => $metric_perf,
metric_label => $metric_label },
closure_custom_output => $self->can('custom_packets_output'),
closure_custom_perfdata => $self->can('custom_packets_perfdata'),
closure_custom_threshold_check => $self->can('custom_metric_threshold'),
}
};
push @{$self->{maps_counters}->{metric}}, $entry;
}
}
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
"resource:s@" => { name => 'resource' },
"resource-group:s" => { name => 'resource_group' },
"filter-metric:s" => { name => 'filter_metric' },
"per-sec" => { name => 'per_sec' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
if (!defined($self->{option_results}->{resource})) {
$self->{output}->add_option_msg(short_msg => "Need to specify either --resource <name> with --resource-group option or --resource <id>.");
$self->{output}->option_exit();
}
$self->{az_resource} = $self->{option_results}->{resource};
$self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group}));
$self->{az_resource_type} = 'virtualNetworkGateways';
$self->{az_resource_namespace} = 'Microsoft.Network';
$self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900;
$self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT5M";
$self->{az_aggregations} = ['Total'];
if (defined($self->{option_results}->{aggregation})) {
$self->{az_aggregations} = [];
foreach my $stat (@{$self->{option_results}->{aggregation}}) {
if ($stat ne '') {
push @{$self->{az_aggregations}}, ucfirst(lc($stat));
}
}
}
foreach my $metric ('TunnelIngressBytes', 'TunnelEgressBytes', 'TunnelIngressPackets', 'TunnelEgressPackets') {
next if (defined($self->{option_results}->{filter_metric}) && $self->{option_results}->{filter_metric} ne ''
&& $metric !~ /$self->{option_results}->{filter_metric}/);
push @{$self->{az_metrics}}, $metric;
}
$instance_mode = $self;
}
sub manage_selection {
my ($self, %options) = @_;
my %metric_results;
foreach my $resource (@{$self->{az_resource}}) {
my $resource_group = $self->{az_resource_group};
my $resource_name = $resource;
if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Network\/virtualNetworkGateways\/(.*)$/) {
$resource_group = $1;
$resource_name = $2;
}
($metric_results{$resource_name}, undef) = $options{custom}->azure_get_metrics(
resource => $resource_name,
resource_group => $resource_group,
resource_type => $self->{az_resource_type},
resource_namespace => $self->{az_resource_namespace},
metrics => $self->{az_metrics},
aggregations => $self->{az_aggregations},
timeframe => $self->{az_timeframe},
interval => $self->{az_interval},
);
foreach my $metric (@{$self->{az_metrics}}) {
my $metric_name = lc($metric);
$metric_name =~ s/ /_/g;
foreach my $aggregation (@{$self->{az_aggregations}}) {
next if (!defined($metric_results{$resource_name}->{$metric_name}->{lc($aggregation)}) && !defined($self->{option_results}->{zeroed}));
$self->{metric}->{$resource_name . "_" . lc($aggregation)}->{display} = $resource_name;
$self->{metric}->{$resource_name . "_" . lc($aggregation)}->{timeframe} = $self->{az_timeframe};
$self->{metric}->{$resource_name . "_" . lc($aggregation)}->{stat} = lc($aggregation);
$self->{metric}->{$resource_name . "_" . lc($aggregation)}->{$metric_name . "_" . lc($aggregation) . '_name'} = $metric_results{$resource_name}->{$metric_name}->{name};
$self->{metric}->{$resource_name . "_" . lc($aggregation)}->{$metric_name . "_" . lc($aggregation)} = defined($metric_results{$resource_name}->{$metric_name}->{lc($aggregation)}) ? $metric_results{$resource_name}->{$metric_name}->{lc($aggregation)} : 0;
}
}
}
if (scalar(keys %{$self->{metric}}) <= 0) {
$self->{output}->add_option_msg(short_msg => 'No metrics. Check your options or use --zeroed option to set 0 on undefined values');
$self->{output}->option_exit();
}
}
1;
__END__
=head1 MODE
Check VPN gateway tunnels traffic metrics.
Example:
Using resource name :
perl centreon_plugins.pl --plugin=cloud::azure::network::vpngateway::plugin --custommode=azcli --mode=tunnel-traffic
--resource=MyResource --resource-group=MYRGROUP --aggregation='total' --critical-tunnelingressbytes-total='10'
--verbose
Using resource id :
perl centreon_plugins.pl --plugin=cloud::azure::network::vpngateway::plugin --custommode=azcli --mode=tunnel-traffic
--resource='/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Network/virtualNetworkGateways/xxx'
--aggregation='total' --critical-tunnelingressbytes-total='10' --verbose
Default aggregation: 'total' / All aggregations are valid.
=over 8
=item B<--resource>
Set resource name or id (Required).
=item B<--resource-group>
Set resource group (Required if resource's name is used).
=item B<--filter-metric>
Filter metrics (Can be: 'TunnelIngressBytes', 'TunnelEgressBytes', 'TunnelIngressPackets',
'TunnelEgressPackets') (Can be a regexp).
=item B<--warning-$metric$-$aggregation$>
Thresholds warning ($metric$ can be: 'tunnelingressbytes', 'tunnelegressbytes',
'tunnelingresspackets', 'tunnelegresspackets', $aggregation$ can be: 'total',
'minimum', 'maximum', 'average').
=item B<--critical-$metric$-$aggregation$>
Thresholds critical ($metric$ can be: 'tunnelingressbytes', 'tunnelegressbytes',
'tunnelingresspackets', 'tunnelegresspackets', $aggregation$ can be: 'total',
'minimum', 'maximum', 'average').
=item B<--per-sec>
Change the data to be unit/sec.
=back
=cut

View File

@ -0,0 +1,161 @@
#
# Copyright 2019 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 cloud::azure::network::vpngateway::mode::vpngatewaystatus;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
sub custom_status_output {
my ($self, %options) = @_;
my $msg = sprintf("Provisioning State '%s' [Gateway type: %s] [VPN type: %s]",
$self->{result_values}->{provisioning_state},
$self->{result_values}->{gateway_type},
$self->{result_values}->{vpn_type});
return $msg;
}
sub custom_status_calc {
my ($self, %options) = @_;
$self->{result_values}->{provisioning_state} = $options{new_datas}->{$self->{instance} . '_provisioning_state'};
$self->{result_values}->{gateway_type} = $options{new_datas}->{$self->{instance} . '_gateway_type'};
$self->{result_values}->{vpn_type} = $options{new_datas}->{$self->{instance} . '_vpn_type'};
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
return 0;
}
sub prefix_vpn_output {
my ($self, %options) = @_;
return "VPN Gateway '" . $options{instance_value}->{display} . "' ";
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'vpns', type => 1, cb_prefix_output => 'prefix_vpn_output', message_multiple => 'All VPN gateways are ok' },
];
$self->{maps_counters}->{vpns} = [
{ label => 'status', threshold => 0, set => {
key_values => [ { name => 'provisioning_state' }, { name => 'gateway_type' }, { name => 'vpn_type' },
{ name => 'display' } ],
closure_custom_calc => $self->can('custom_status_calc'),
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold,
}
},
];
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
"resource-group:s" => { name => 'resource_group' },
"filter-name:s" => { name => 'filter_name' },
"warning-status:s" => { name => 'warning_status', default => '' },
"critical-status:s" => { name => 'critical_status', default => '%{provisioning_state} ne "Succeeded"' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
if (!defined($self->{option_results}->{resource_group}) || $self->{option_results}->{resource_group} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --resource-group option");
$self->{output}->option_exit();
}
$self->change_macros(macros => ['warning_status', 'critical_status']);
}
sub manage_selection {
my ($self, %options) = @_;
$self->{vpns} = {};
my $vpns = $options{custom}->azure_list_vpn_gateways(resource_group => $self->{option_results}->{resource_group});
foreach my $vpn (@{$vpns}) {
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
&& $vpn->{name} !~ /$self->{option_results}->{filter_name}/);
$self->{vpns}->{$vpn->{id}} = {
display => $vpn->{name},
provisioning_state => ($vpn->{provisioningState}) ? $vpn->{provisioningState} : $vpn->{properties}->{provisioningState},
gateway_type => ($vpn->{gatewayType}) ? $vpn->{gatewayType} : $vpn->{properties}->{gatewayType},
vpn_type => ($vpn->{vpnType}) ? $vpn->{vpnType} : $vpn->{properties}->{vpnType},
};
}
if (scalar(keys %{$self->{vpns}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No VPN gateways found.");
$self->{output}->option_exit();
}
}
1;
__END__
=head1 MODE
Check VPN gateways status.
Example:
perl centreon_plugins.pl --plugin=cloud::azure::network::vpngateway::plugin --custommode=azcli --mode=vpn-gateways-status
--resource-group='MYRESOURCEGROUP' --verbose
=over 8
=item B<--resource-group>
Set resource group (Required).
=item B<--filter-name>
Filter vpn name (Can be a regexp).
=item B<--warning-status>
Set warning threshold for status (Default: '').
Can used special variables like: %{provisioning_state}, %{gateway_type}, %{vpn_type}, %{display}
=item B<--critical-status>
Set critical threshold for status (Default: '%{provisioning_state} ne "Succeeded"').
Can used special variables like: %{provisioning_state}, %{gateway_type}, %{vpn_type}, %{display}
=back
=cut

View File

@ -0,0 +1,62 @@
#
# Copyright 2019 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 cloud::azure::network::vpngateway::plugin;
use strict;
use warnings;
use base qw(centreon::plugins::script_custom);
sub new {
my ( $class, %options ) = @_;
my $self = $class->SUPER::new( package => __PACKAGE__, %options );
bless $self, $class;
$self->{version} = '0.1';
%{ $self->{modes} } = (
'vpn-gateway-status' => 'cloud::azure::network::vpngateway::mode::vpngatewaystatus',
'tunnel-traffic' => 'cloud::azure::network::vpngateway::mode::tunneltraffic',
);
$self->{custom_modes}{azcli} = 'cloud::azure::custom::azcli';
$self->{custom_modes}{api} = 'cloud::azure::custom::api';
return $self;
}
sub init {
my ($self, %options) = @_;
$self->{options}->add_options(arguments =>
{
'api-version:s' => { name => 'api_version', default => '2018-01-01' },
});
$self->SUPER::init(%options);
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check Microsoft Azure VPN gateways.
=cut