From e462159030bc1dac033179f3fd5bcceb494f3071 Mon Sep 17 00:00:00 2001 From: Colin Gagnaire Date: Fri, 25 Jan 2019 18:40:56 +0100 Subject: [PATCH] add azure sql server and database services --- cloud/azure/custom/api.pm | 39 ++++ cloud/azure/custom/azcli.pm | 41 ++++ .../database/sqldatabase/mode/databasesize.pm | 203 ++++++++++++++++++ .../sqldatabase/mode/databasestatus.pm | 166 ++++++++++++++ .../sqldatabase/mode/listdatabases.pm | 155 +++++++++++++ cloud/azure/database/sqldatabase/plugin.pm | 63 ++++++ .../database/sqlserver/mode/listservers.pm | 148 +++++++++++++ .../database/sqlserver/mode/serverstatus.pm | 159 ++++++++++++++ cloud/azure/database/sqlserver/plugin.pm | 62 ++++++ 9 files changed, 1036 insertions(+) create mode 100644 cloud/azure/database/sqldatabase/mode/databasesize.pm create mode 100644 cloud/azure/database/sqldatabase/mode/databasestatus.pm create mode 100644 cloud/azure/database/sqldatabase/mode/listdatabases.pm create mode 100644 cloud/azure/database/sqldatabase/plugin.pm create mode 100644 cloud/azure/database/sqlserver/mode/listservers.pm create mode 100644 cloud/azure/database/sqlserver/mode/serverstatus.pm create mode 100644 cloud/azure/database/sqlserver/plugin.pm diff --git a/cloud/azure/custom/api.pm b/cloud/azure/custom/api.pm index 736537a21..1de936339 100644 --- a/cloud/azure/custom/api.pm +++ b/cloud/azure/custom/api.pm @@ -545,6 +545,45 @@ sub azure_list_vnet_peerings { return $response->{value}; } +sub azure_list_sqlservers_set_url { + my ($self, %options) = @_; + + my $url = $self->{management_endpoint} . "/subscriptions/" . $self->{subscription}; + $url .= "/resourceGroups/" . $options{resource_group} if (defined($options{resource_group}) && $options{resource_group} ne ''); + $url .= "/providers/Microsoft.Sql/servers?api-version=" . $self->{api_version}; + + return $url; +} + +sub azure_list_sqlservers { + my ($self, %options) = @_; + + my $full_url = $self->azure_list_sqlservers_set_url(%options); + my $response = $self->request_api(method => 'GET', full_url => $full_url, hostname => ''); + + return $response->{value}; +} + +sub azure_list_sqldatabases_set_url { + my ($self, %options) = @_; + + my $url = $self->{management_endpoint} . "/subscriptions/" . $self->{subscription}; + $url .= "/resourceGroups/" . $options{resource_group} if (defined($options{resource_group}) && $options{resource_group} ne ''); + $url .= "/providers/Microsoft.Sql/servers/" . $options{server} if (defined($options{server}) && $options{server} ne ''); + $url .= "/databases?api-version=" . $self->{api_version}; + + return $url; +} + +sub azure_list_sqldatabases { + my ($self, %options) = @_; + + my $full_url = $self->azure_list_sqldatabases_set_url(%options); + my $response = $self->request_api(method => 'GET', full_url => $full_url, hostname => ''); + + return $response->{value}; +} + 1; __END__ diff --git a/cloud/azure/custom/azcli.pm b/cloud/azure/custom/azcli.pm index 9121fc65e..4bd4f70ff 100644 --- a/cloud/azure/custom/azcli.pm +++ b/cloud/azure/custom/azcli.pm @@ -466,6 +466,47 @@ sub azure_list_vnet_peerings { return $raw_results; } +sub azure_list_sqlservers_set_cmd { + my ($self, %options) = @_; + + return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne ''); + + my $cmd_options = "sql server 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 ''); + + return $cmd_options; +} + +sub azure_list_sqlservers { + my ($self, %options) = @_; + + my $cmd_options = $self->azure_list_sqlservers_set_cmd(%options); + my $raw_results = $self->execute(cmd_options => $cmd_options); + + return $raw_results; +} + +sub azure_list_sqldatabases_set_cmd { + my ($self, %options) = @_; + + return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne ''); + + my $cmd_options = "sql db list --resource-group '$options{resource_group}' --server '$options{server}' --output json"; + $cmd_options .= " --subscription '$self->{subscription}'" if (defined($self->{subscription}) && $self->{subscription} ne ''); + + return $cmd_options; +} + +sub azure_list_sqldatabases { + my ($self, %options) = @_; + + my $cmd_options = $self->azure_list_sqldatabases_set_cmd(%options); + my $raw_results = $self->execute(cmd_options => $cmd_options); + + return $raw_results; +} + 1; __END__ diff --git a/cloud/azure/database/sqldatabase/mode/databasesize.pm b/cloud/azure/database/sqldatabase/mode/databasesize.pm new file mode 100644 index 000000000..f0c1757b5 --- /dev/null +++ b/cloud/azure/database/sqldatabase/mode/databasesize.pm @@ -0,0 +1,203 @@ +# +# 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::database::sqldatabase::mode::databasesize; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; + +sub prefix_metric_output { + my ($self, %options) = @_; + + return "Database '" . $options{instance_value}->{display} . "' " . $options{instance_value}->{stat} . " "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'metric', type => 1, cb_prefix_output => 'prefix_metric_output', message_multiple => "All usage metrics are ok", skipped_code => { -10 => 1 } }, + ]; + + foreach my $aggregation ('minimum', 'maximum', 'average') { + foreach my $metric ('storage_percent') { + 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' } ], + output_template => 'Database size percentage: %.2f %%', + perfdatas => [ + { label => $metric_perf . '_' . $aggregation, value => $metric_perf . '_' . $aggregation . '_absolute', + template => '%.2f', unit => '%', label_extra_instance => 1, instance_use => 'display_absolute', + min => 0, max => 100 }, + ], + } + }; + 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' }, + }); + + 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 with --resource-group option or --resource ."); + $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} = 'servers'; + $self->{az_resource_namespace} = 'Microsoft.Sql'; + $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} = ['Maximum']; + 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 ('storage_percent') { + 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; + } +} + +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\.Sql\/servers\/(.*)\/databases\/(.*)$/) { + $resource_group = $1; + $resource_name = $2 . '/databases/' . $3; + } + + ($metric_results{$resource_name}, undef, 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)}->{stat} = lc($aggregation); + $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 databases usage metrics. + +Example: + +Using resource name : + +perl centreon_plugins.pl --plugin=cloud::azure::database::sqldatabase::plugin --custommode=azcli --mode=database-size +--resource=MYDATABASE --resource-group=MYHOSTGROUP --aggregation='maximum' +--critical-storage-percent-maximum='รง0' --verbose + +Using resource id : + +perl centreon_plugins.pl --plugin=cloud::azure::compute::virtualmachine::plugin --custommode=azcli --mode=cpu +--resource='/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Sql/servers/xxx/databases/xxx' +--aggregation='maximum' --critical-storage-percent-maximum='90' --verbose + +Default aggregation: 'maximum' / 'average', 'minimum' and 'maximum' 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: 'storage_percent') (Can be a regexp). + +=item B<--warning-$metric$-$aggregation$> + +Thresholds warning ($metric$ can be: 'cpu-credits-remaining', 'cpu-credits-consumed', +'percentage-cpu', $aggregation$ can be: 'minimum', 'maximum', 'average'). + +=item B<--critical-$metric$-$aggregation$> + +Thresholds critical ($metric$ can be: 'storage-percent', +$aggregation$ can be: 'minimum', 'maximum', 'average'). + +=back + +=cut diff --git a/cloud/azure/database/sqldatabase/mode/databasestatus.pm b/cloud/azure/database/sqldatabase/mode/databasestatus.pm new file mode 100644 index 000000000..1531898db --- /dev/null +++ b/cloud/azure/database/sqldatabase/mode/databasestatus.pm @@ -0,0 +1,166 @@ +# +# 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::database::sqldatabase::mode::databasestatus; + +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("Status '%s'", + $self->{result_values}->{status}); + return $msg; +} + +sub custom_status_calc { + my ($self, %options) = @_; + + $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'}; + $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; + return 0; +} + +sub prefix_database_output { + my ($self, %options) = @_; + + return "Database '" . $options{instance_value}->{display} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'databases', type => 1, cb_prefix_output => 'prefix_database_output', message_multiple => 'All databases are ok' }, + ]; + + $self->{maps_counters}->{databases} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'status' }, { 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' }, + "server:s" => { name => 'server' }, + "filter-name:s" => { name => 'filter_name' }, + "warning-status:s" => { name => 'warning_status', default => '' }, + "critical-status:s" => { name => 'critical_status', default => '%{status} ne "Online"' }, + }); + + 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(); + } + if (!defined($self->{option_results}->{server}) || $self->{option_results}->{server} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --server option"); + $self->{output}->option_exit(); + } + + $self->change_macros(macros => ['warning_status', 'critical_status']); +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{databases} = {}; + my $databases = $options{custom}->azure_list_sqldatabases( + resource_group => $self->{option_results}->{resource_group}, + server => $self->{option_results}->{server} + ); + foreach my $database (@{$databases}) { + next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' + && $database->{name} !~ /$self->{option_results}->{filter_name}/); + + $self->{databases}->{$database->{id}} = { + display => $database->{name}, + status => ($database->{status}) ? $database->{status} : $database->{properties}->{status}, + }; + } + + if (scalar(keys %{$self->{databases}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No SQL databases found."); + $self->{output}->option_exit(); + } +} + +1; + +__END__ + +=head1 MODE + +Check SQL databases status. + +Example: +perl centreon_plugins.pl --plugin=cloud::azure::database::sqldatabase::plugin --custommode=azcli --mode=database-status +--resource-group='MYRESOURCEGROUP' --server='MyServer' --verbose + +=over 8 + +=item B<--resource-group> + +Set resource group (Required). + +=item B<--server> + +Set SQL server (Required). + +=item B<--filter-name> + +Filter database name (Can be a regexp). + +=item B<--warning-status> + +Set warning threshold for status (Default: ''). +Can used special variables like: %{status}, %{display} + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{status} ne "Online"'). +Can used special variables like: %{status}, %{display} + +=back + +=cut diff --git a/cloud/azure/database/sqldatabase/mode/listdatabases.pm b/cloud/azure/database/sqldatabase/mode/listdatabases.pm new file mode 100644 index 000000000..2c99eb97f --- /dev/null +++ b/cloud/azure/database/sqldatabase/mode/listdatabases.pm @@ -0,0 +1,155 @@ +# +# 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::database::sqldatabase::mode::listdatabases; + +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; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "resource-group:s" => { name => 'resource_group' }, + "server:s" => { name => 'server' }, + "filter-name:s" => { name => 'filter_name' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%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(); + } + + if (!defined($self->{option_results}->{server}) || $self->{option_results}->{server} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --server option"); + $self->{output}->option_exit(); + } +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{databases} = $options{custom}->azure_list_sqldatabases( + resource_group => $self->{option_results}->{resource_group}, + server => $self->{option_results}->{server} + ); +} + +sub run { + my ($self, %options) = @_; + + $self->manage_selection(%options); + foreach my $database (@{$self->{databases}}) { + next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' + && $database->{name} !~ /$self->{option_results}->{filter_name}/); + my $resource_group = '-'; + $resource_group = $database->{resourceGroup} if (defined($database->{resourceGroup})); + $resource_group = $1 if ($resource_group eq '-' && defined($database->{id}) && $database->{id} =~ /resourceGroups\/(.*)\/providers/); + + my @tags; + foreach my $tag (keys %{$database->{tags}}) { + push @tags, $tag . ':' . $database->{tags}->{$tag}; + } + + $self->{output}->output_add(long_msg => sprintf("[name = %s][kind = %s][resourcegroup = %s][location = %s][id = %s][tags = %s]", + $database->{name}, + ($database->{kind}) ? $database->{kind} : $database->{properties}->{kind}, + $resource_group, + $database->{location}, + $database->{id}, + join(',', @tags), + )); + } + + $self->{output}->output_add(severity => 'OK', + short_msg => 'List SQL databases:'); + $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 => ['name', 'kind', 'resourcegroup', 'location', 'id', 'tags']); +} + +sub disco_show { + my ($self, %options) = @_; + + $self->manage_selection(%options); + foreach my $database (@{$self->{databases}}) { + my $resource_group = '-'; + $resource_group = $database->{resourceGroup} if (defined($database->{resourceGroup})); + $resource_group = $1 if ($resource_group eq '-' && defined($database->{id}) && $database->{id} =~ /resourceGroups\/(.*)\/providers/); + + my @tags; + foreach my $tag (keys %{$database->{tags}}) { + push @tags, $tag . ':' . $database->{tags}->{$tag}; + } + + $self->{output}->add_disco_entry( + name => $database->{name}, + kind => ($database->{kind}) ? $database->{kind} : $database->{properties}->{kind}, + resourcegroup => $resource_group, + location => $database->{location}, + id => $database->{id}, + tags => join(',', @tags), + ); + } +} + +1; + +__END__ + +=head1 MODE + +List SQL databases. + +=over 8 + +=item B<--resource-group> + +Set resource group (Required). + +=item B<--resource-group> + +Set SQL erver (Required). + +=item B<--filter-name> + +Filter resource name (Can be a regexp). + +=back + +=cut diff --git a/cloud/azure/database/sqldatabase/plugin.pm b/cloud/azure/database/sqldatabase/plugin.pm new file mode 100644 index 000000000..d3a33055d --- /dev/null +++ b/cloud/azure/database/sqldatabase/plugin.pm @@ -0,0 +1,63 @@ +# +# 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::database::sqldatabase::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} } = ( + 'database-size' => 'cloud::azure::database::sqldatabase::mode::databasesize', + 'database-status' => 'cloud::azure::database::sqldatabase::mode::databasestatus', + 'list-databases' => 'cloud::azure::database::sqldatabase::mode::listdatabases', + ); + + $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-06-01-preview' }, + }); + + $self->SUPER::init(%options); +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Microsoft Azure SQL Server. + +=cut diff --git a/cloud/azure/database/sqlserver/mode/listservers.pm b/cloud/azure/database/sqlserver/mode/listservers.pm new file mode 100644 index 000000000..f7dd62e2f --- /dev/null +++ b/cloud/azure/database/sqlserver/mode/listservers.pm @@ -0,0 +1,148 @@ +# +# 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::database::sqlserver::mode::listservers; + +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; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "resource-group:s" => { name => 'resource_group' }, + "location:s" => { name => 'location' }, + "filter-name:s" => { name => 'filter_name' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{servers} = $options{custom}->azure_list_sqlservers( + resource_group => $self->{option_results}->{resource_group} + ); +} + +sub run { + my ($self, %options) = @_; + + $self->manage_selection(%options); + foreach my $server (@{$self->{servers}}) { + next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' + && $server->{name} !~ /$self->{option_results}->{filter_name}/); + next if (defined($self->{option_results}->{location}) && $self->{option_results}->{location} ne '' + && $server->{location} !~ /$self->{option_results}->{location}/); + my $resource_group = '-'; + $resource_group = $server->{resourceGroup} if (defined($server->{resourceGroup})); + $resource_group = $1 if ($resource_group eq '-' && defined($server->{id}) && $server->{id} =~ /resourceGroups\/(.*)\/providers/); + + my @tags; + foreach my $tag (keys %{$server->{tags}}) { + push @tags, $tag . ':' . $server->{tags}->{$tag}; + } + + $self->{output}->output_add(long_msg => sprintf("[name = %s][fqdn = %s][version = %s][resourcegroup = %s][location = %s][id = %s][tags = %s]", + $server->{name}, + ($server->{fullyQualifiedDomainName}) ? $server->{fullyQualifiedDomainName} : $server->{properties}->{fullyQualifiedDomainName}, + ($server->{version}) ? $server->{version} : $server->{properties}->{version}, + $resource_group, + $server->{location}, + $server->{id}, + join(',', @tags), + )); + } + + $self->{output}->output_add(severity => 'OK', + short_msg => 'List SQL servers:'); + $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 => ['name', 'fqdn', 'version', 'resourcegroup', 'location', 'id', 'tags']); +} + +sub disco_show { + my ($self, %options) = @_; + + $self->manage_selection(%options); + foreach my $server (@{$self->{servers}}) { + my $resource_group = '-'; + $resource_group = $server->{resourceGroup} if (defined($server->{resourceGroup})); + $resource_group = $1 if ($resource_group eq '-' && defined($server->{id}) && $server->{id} =~ /resourceGroups\/(.*)\/providers/); + + my @tags; + foreach my $tag (keys %{$server->{tags}}) { + push @tags, $tag . ':' . $server->{tags}->{$tag}; + } + + $self->{output}->add_disco_entry( + name => $server->{name}, + fqdn => ($server->{fullyQualifiedDomainName}) ? $server->{fullyQualifiedDomainName} : $server->{properties}->{fullyQualifiedDomainName}, + version => ($server->{version}) ? $server->{version} : $server->{properties}->{version}, + resourcegroup => $resource_group, + location => $server->{location}, + id => $server->{id}, + tags => join(',', @tags), + ); + } +} + +1; + +__END__ + +=head1 MODE + +List SQL servers. + +=over 8 + +=item B<--resource-group> + +Set resource group. + +=item B<--location> + +Set resource location. + +=item B<--filter-name> + +Filter resource name (Can be a regexp). + +=back + +=cut diff --git a/cloud/azure/database/sqlserver/mode/serverstatus.pm b/cloud/azure/database/sqlserver/mode/serverstatus.pm new file mode 100644 index 000000000..b28a34411 --- /dev/null +++ b/cloud/azure/database/sqlserver/mode/serverstatus.pm @@ -0,0 +1,159 @@ +# +# 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::database::sqlserver::mode::serverstatus; + +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("State '%s' [FQDN: %s]", + $self->{result_values}->{state}, + $self->{result_values}->{fqdn}); + return $msg; +} + +sub custom_status_calc { + my ($self, %options) = @_; + + $self->{result_values}->{state} = $options{new_datas}->{$self->{instance} . '_state'}; + $self->{result_values}->{fqdn} = $options{new_datas}->{$self->{instance} . '_fqdn'}; + $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; + return 0; +} + +sub prefix_server_output { + my ($self, %options) = @_; + + return "Server '" . $options{instance_value}->{display} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'servers', type => 1, cb_prefix_output => 'prefix_server_output', message_multiple => 'All servers are ok' }, + ]; + + $self->{maps_counters}->{servers} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'state' }, { name => 'fqdn' }, { 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' }, + "location:s" => { name => 'location' }, + "filter-name:s" => { name => 'filter_name' }, + "warning-status:s" => { name => 'warning_status', default => '' }, + "critical-status:s" => { name => 'critical_status', default => '%{state} ne "Ready"' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $self->change_macros(macros => ['warning_status', 'critical_status']); +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{servers} = {}; + my $servers = $options{custom}->azure_list_sqlservers(resource_group => $self->{option_results}->{resource_group}); + foreach my $server (@{$servers}) { + next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' + && $server->{name} !~ /$self->{option_results}->{filter_name}/); + next if (defined($self->{option_results}->{location}) && $self->{option_results}->{location} ne '' + && $server->{location} !~ /$self->{option_results}->{location}/); + + $self->{servers}->{$server->{id}} = { + display => $server->{name}, + state => ($server->{state}) ? $server->{state} : $server->{properties}->{state}, + fqdn => ($server->{fullyQualifiedDomainName}) ? $server->{fullyQualifiedDomainName} : $server->{properties}->{fullyQualifiedDomainName}, + }; + } + + if (scalar(keys %{$self->{servers}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No SQL servers found."); + $self->{output}->option_exit(); + } +} + +1; + +__END__ + +=head1 MODE + +Check SQL servers status. + +Example: +perl centreon_plugins.pl --plugin=cloud::azure::database::sqlserver::plugin --custommode=azcli --mode=server-status +--resource-group='MYRESOURCEGROUP' --verbose + +=over 8 + +=item B<--resource-group> + +Set resource group. + +=item B<--location> + +Set resource location. + +=item B<--filter-name> + +Filter server name (Can be a regexp). + +=item B<--warning-status> + +Set warning threshold for status (Default: ''). +Can used special variables like: %{state}, %{fqdn}, %{display} + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{state} ne "Ready"'). +Can used special variables like: %{state}, %{fqdn}, %{display} + +=back + +=cut diff --git a/cloud/azure/database/sqlserver/plugin.pm b/cloud/azure/database/sqlserver/plugin.pm new file mode 100644 index 000000000..8d744f8ec --- /dev/null +++ b/cloud/azure/database/sqlserver/plugin.pm @@ -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::database::sqlserver::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} } = ( + 'server-status' => 'cloud::azure::database::sqlserver::mode::serverstatus', + 'list-servers' => 'cloud::azure::database::sqlserver::mode::listservers', + ); + + $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-06-01-preview' }, + }); + + $self->SUPER::init(%options); +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Microsoft Azure SQL Server. + +=cut