From 0596f498e473c68d1c5c4a9b72101de49285b821 Mon Sep 17 00:00:00 2001 From: qgarnier Date: Mon, 2 Aug 2021 14:24:40 +0200 Subject: [PATCH] break(google): rename gsuite to workspace - use history page (#3007) --- .../apps/google/gsuite/mode/applications.pm | 178 ----------------- .../{gsuite => workspace}/custom/api.pm | 51 +++-- .../mode/listservices.pm} | 28 +-- .../apps/google/workspace/mode/services.pm | 179 ++++++++++++++++++ .../google/{gsuite => workspace}/plugin.pm | 10 +- 5 files changed, 232 insertions(+), 214 deletions(-) delete mode 100644 centreon-plugins/apps/google/gsuite/mode/applications.pm rename centreon-plugins/apps/google/{gsuite => workspace}/custom/api.pm (85%) rename centreon-plugins/apps/google/{gsuite/mode/listapplications.pm => workspace/mode/listservices.pm} (74%) create mode 100644 centreon-plugins/apps/google/workspace/mode/services.pm rename centreon-plugins/apps/google/{gsuite => workspace}/plugin.pm (79%) diff --git a/centreon-plugins/apps/google/gsuite/mode/applications.pm b/centreon-plugins/apps/google/gsuite/mode/applications.pm deleted file mode 100644 index 57d12416b..000000000 --- a/centreon-plugins/apps/google/gsuite/mode/applications.pm +++ /dev/null @@ -1,178 +0,0 @@ -# -# 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 apps::google::gsuite::mode::applications; - -use base qw(centreon::plugins::templates::counter); - -use strict; -use warnings; -use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); -use POSIX qw(strftime); - -sub custom_status_output { - my ($self, %options) = @_; - - return sprintf( - "(%s): '%s' (since '%s')", - $self->{result_values}->{status}, - $self->{result_values}->{message}, - $self->{result_values}->{time} - ); -} - -sub prefix_gapps_output { - my ($self, %options) = @_; - - return $options{instance_value}->{name} . " "; -} - -sub set_counters { - my ($self, %options) = @_; - - $self->{maps_counters_type} = [ - { name => 'global', type => 0 }, - { name => 'gapps', type => 1, cb_prefix_output => 'prefix_gapps_output', display_long => 1, cb_long_output => 'long_output', message_multiple => 'All Google Apps are ok' } - ]; - - $self->{maps_counters}->{global} = [ - { label => 'apps', nlabel => 'google.apps.current.count', set => { - key_values => [ { name => 'apps' } ], - output_template => '%s GApps', - perfdatas => [ - { template => '%s', min => 0 } - ] - } - } - ]; - - $self->{maps_counters}->{gapps} = [ - { - label => 'status', - type => 2, - warning_default => '%{status} eq "DEGRADED"', - critical_default => '%{status} eq "UNAVAILABLE"', - set => { - key_values => [ { name => 'status' }, { name => 'name' }, { name => 'message' }, { name => 'time' } ], - closure_custom_output => $self->can('custom_status_output'), - closure_custom_perfdata => sub { return 0; }, - closure_custom_threshold_check => \&catalog_status_threshold_ng - } - } - ]; -} - -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 => { - 'filter-app:s' => { name => 'filter_app' }, - 'display-extra' => { name => 'display_extra'} - }); - - return $self; -} - -my $status_mapping = { - 1 => 'DEGRADED', - 2 => 'UNAVAILABLE', - 3 => 'RESOLVED', - 4 => 'CLOSED' -}; - -sub manage_selection { - my ($self, %options) = @_; - - my $results = $options{custom}->request_api(); - - my $time_now = strftime('%Y-%m-%dT%H:%M:%S', gmtime()); - foreach my $mapping (@{$results->{services}}) { - next if ( - defined($self->{option_results}->{filter_app}) - && $self->{option_results}->{filter_app} ne '' - && $mapping->{name} !~ /(?i)$self->{option_results}->{filter_app}/ - ); - - next if ( - (!defined($self->{option_results}->{display_extra})) - && $mapping->{type} != 0 - ); - - $self->{gapps}->{ $mapping->{id} } = { - name => $mapping->{name}, - type => $mapping->{type}, - status => 'OK', - time => $time_now, - message => '' - }; - }; - - foreach my $issue (@{$results->{messages}}) { - next if (!defined($self->{gapps}->{ $issue->{service} })); - - next if ($issue->{resolved}); - - $self->{gapps}->{ $issue->{service} } = { - name => $self->{gapps}->{ $issue->{service} }->{name}, - status => $status_mapping->{ $issue->{type} }, - time => strftime('%Y-%m-%dT%H:%M:%S', gmtime($issue->{time} / 1000)), - message => $issue->{message}, - resolved => $issue->{resolved}, - type => $self->{gapps}->{ $issue->{service} }->{type} - }; - } - - $self->{global} = { apps => scalar(keys %{$self->{gapps}}) }; -} - -1; - -__END__ - -=head1 MODE - -Check Google Gsuite Applications status - -=over 8 - -=item B<--filter-app> - -Only display the status for a specific app -(Example: --filter-app='gmail') - -=item B<--display-extra> - -Also display the status of the Google Apps not covered by G Suite Service Level Agreement - -=item B<--warning-status> - -Set warning threshold for the application status -(Default: '%{status} eq "DEGRADED"'). - -=item B<--critical-status> - -Set warning threshold for the application status -(Default: '%{status} eq "UNAVALAIBLE"'). - -=back - -=cut diff --git a/centreon-plugins/apps/google/gsuite/custom/api.pm b/centreon-plugins/apps/google/workspace/custom/api.pm similarity index 85% rename from centreon-plugins/apps/google/gsuite/custom/api.pm rename to centreon-plugins/apps/google/workspace/custom/api.pm index 7ac255f44..a127380b8 100644 --- a/centreon-plugins/apps/google/gsuite/custom/api.pm +++ b/centreon-plugins/apps/google/workspace/custom/api.pm @@ -18,7 +18,7 @@ # limitations under the License. # -package apps::google::gsuite::custom::api; +package apps::google::workspace::custom::api; use strict; use warnings; @@ -45,7 +45,6 @@ sub new { 'port:s' => { name => 'port' }, 'proto:s' => { name => 'proto' }, 'timeout:s' => { name => 'timeout' }, - 'language:s' => { name => 'language' }, 'unknown-http-status:s' => { name => 'unknown_http_status' }, 'warning-http-status:s' => { name => 'warning_http_status' }, 'critical-http-status:s' => { name => 'critical_http_status' } @@ -74,7 +73,6 @@ sub check_options { $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443; $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https'; $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 30; - $self->{language} = (defined($self->{option_results}->{language}) && $self->{option_results}->{language} ne '') ? $self->{option_results}->{language} : 'en'; $self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300'; $self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : ''; $self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : ''; @@ -103,7 +101,6 @@ sub settings { return if (defined($self->{settings_done})); $self->build_options_for_httplib(); $self->{http}->add_header(key => 'Accept', value => 'application/json'); - $self->{http}->add_header(key => 'Content-Type', value => 'application/json'); $self->{http}->set_options(%{$self->{option_results}}); $self->{settings_done} = 1; } @@ -114,17 +111,45 @@ sub get_hostname { return $self->{hostname}; } +sub get_services { + my ($self, %options) = @_; + + my $services = { + 'Admin Console' => 1, + 'Classic Hangouts' => 1, + 'Classroom' => 1, + 'Currents' => 1, + 'Gmail' => 1, + 'Google Calendar' => 1, + 'Google Chat' => 1, + 'Google Cloud Search' => 1, + 'Google Docs' => 1, + 'Google Drive' => 1, + 'Google Forms' => 1, + 'Google Groups' => 1, + 'Google Keep' => 1, + 'Google Meet' => 1, + 'Google Sheets' => 1, + 'Google Sites' => 1, + 'Google Slides' => 1, + 'Google Tasks' => 1, + 'Google Vault' => 1, + 'Google Voice' => 1, + 'Google Workspace Support' => 1 + }; + return $services; +} + sub request_api { my ($self, %options) = @_; $self->settings(); - my $content = $self->{http}->request( - url_path => '/appsstatus/json/' . $self->{language}, + my ($content) = $self->{http}->request( + url_path => '/appsstatus/dashboard/incidents.json', unknown_status => $self->{unknown_http_status}, warning_status => $self->{warning_http_status}, critical_status => $self->{critical_http_status} ); - $content =~ s/dashboard.jsonp\((.+)\)\;$/$1/g; if (!defined($content) || $content eq '') { $self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']"); @@ -139,10 +164,6 @@ sub request_api { $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)"); $self->{output}->option_exit(); } - if (defined($decoded->{error_code})) { - $self->{output}->add_option_msg(short_msg => "Error message : " . $decoded->{error}); - $self->{output}->option_exit(); - } return $decoded; } @@ -153,11 +174,11 @@ __END__ =head1 NAME -Gsuite Rest API +Google Workspace Rest API =head1 REST API OPTIONS -Gsuite Rest API +Google Workspace Rest API =over 8 @@ -173,10 +194,6 @@ Port used (Default: 443) Specify https if needed (Default: 'https') -=item B<--language> - -Language (Default: 'en') - =item B<--timeout> Set timeout in seconds (Default: 10). diff --git a/centreon-plugins/apps/google/gsuite/mode/listapplications.pm b/centreon-plugins/apps/google/workspace/mode/listservices.pm similarity index 74% rename from centreon-plugins/apps/google/gsuite/mode/listapplications.pm rename to centreon-plugins/apps/google/workspace/mode/listservices.pm index 3e5b89ebc..2413cfd0e 100644 --- a/centreon-plugins/apps/google/gsuite/mode/listapplications.pm +++ b/centreon-plugins/apps/google/workspace/mode/listservices.pm @@ -18,7 +18,7 @@ # limitations under the License. # -package apps::google::gsuite::mode::listapplications; +package apps::google::workspace::mode::listservices; use base qw(centreon::plugins::mode); @@ -45,28 +45,28 @@ sub check_options { sub manage_selection { my ($self, %options) = @_; - my $results = $options{custom}->request_api(); - my $applications = {}; - foreach my $application (@{$results->{services}}) { + my $results = $options{custom}->get_services(); + my $services = {}; + foreach my $name (keys %$results) { next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' - && $application->{name} !~ /$self->{option_results}->{filter_name}/); - $applications->{ $application->{id} } = $application->{name}; + && $name !~ /$self->{option_results}->{filter_name}/); + $services->{$name} = $name; } - return $applications; + return $services; } sub run { my ($self, %options) = @_; - my $applications = $self->manage_selection(%options); - foreach (keys %$applications) { + my $services = $self->manage_selection(%options); + foreach (sort keys %$services) { $self->{output}->output_add( - long_msg => sprintf("[name = %s]", $applications->{$_}) + long_msg => sprintf("[name = %s]", $services->{$_}) ); } - $self->{output}->output_add(severity => 'OK', short_msg => 'Google Gsuite Applications:'); + $self->{output}->output_add(severity => 'OK', short_msg => 'Google workspace service:'); $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); $self->{output}->exit(); } @@ -80,10 +80,10 @@ sub disco_format { sub disco_show { my ($self, %options) = @_; - my $applications = $self->manage_selection(%options); - foreach (keys %$applications) { + my $services = $self->manage_selection(%options); + foreach (sort keys %$services) { $self->{output}->add_disco_entry( - name => $applications->{$_} + name => $services->{$_} ); } } diff --git a/centreon-plugins/apps/google/workspace/mode/services.pm b/centreon-plugins/apps/google/workspace/mode/services.pm new file mode 100644 index 000000000..93b152691 --- /dev/null +++ b/centreon-plugins/apps/google/workspace/mode/services.pm @@ -0,0 +1,179 @@ +# +# 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 apps::google::workspace::mode::services; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); +use DateTime; +use centreon::plugins::misc; + +sub custom_status_output { + my ($self, %options) = @_; + + my $msg = 'status is ' . $self->{result_values}->{status}; + if ($self->{result_values}->{since} ne '') { + $msg .= sprintf( + ' [since: %s][message: %s]', + $self->{result_values}->{since}, + $self->{result_values}->{message}, + ); + } + return $msg; +} + +sub prefix_service_output { + my ($self, %options) = @_; + + return "Service '" . $options{instance_value}->{name} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0 }, + { name => 'services', type => 1, cb_prefix_output => 'prefix_service_output', display_long => 1, cb_long_output => 'long_output', message_multiple => 'All Google workspace services are ok' } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'services', nlabel => 'google.workspace.services.count', display_ok => 0, set => { + key_values => [ { name => 'total' } ], + output_template => '%s Google workspace services', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + } + ]; + + $self->{maps_counters}->{services} = [ + { + label => 'status', + type => 2, + warning_default => '%{status} eq "disruption"', + critical_default => '%{status} eq "outage"', + set => { + key_values => [ { name => 'status' }, { name => 'name' }, { name => 'message' }, { name => 'since' } ], + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + } + ]; +} + +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 => { + 'filter-name:s' => { name => 'filter_name' } + }); + + return $self; +} + +my $status_mapping = { + SERVICE_DISRUPTION => 'disruption', + SERVICE_OUTAGE => 'outage', + SERVICE_INFORMATION => 'information' +}; + +sub manage_selection { + my ($self, %options) = @_; + + my $services = $options{custom}->get_services(); + my $history = $options{custom}->request_api(); + + $self->{services} = {}; + foreach my $name (keys %$services) { + next if ( + defined($self->{option_results}->{filter_name}) + && $self->{option_results}->{filter_name} ne '' + && $name !~ /$self->{option_results}->{filter_name}/i + ); + + $self->{services}->{lc($name)} = { + name => $name, + status => 'available', + message => '-', + since => '' + }; + } + + foreach my $entry (@$history) { + next if (defined($entry->{end}) && $entry->{end} =~ /^\d+-\d+-\d+T\d+:\d+:\d+(.*)$/); + next if (!defined($entry->{begin}) || $entry->{begin} !~ /^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)(.*)$/); + + my $dt = DateTime->new( + year => $1, + month => $2, + day => $3, + hour => $4, + minute => $5, + second => $6, + time_zone => $7 + ); + + my $diff_time = time() - $dt->epoch(); + foreach (@{$entry->{affected_products}}) { + next if (!defined($self->{services}->{ lc($_->{title}) })); + $self->{services}->{ lc($_->{title}) }->{status} = $status_mapping->{ $entry->{status_impact} }; + $self->{services}->{ lc($_->{title}) }->{message} = $entry->{external_desc}; + $self->{services}->{ lc($_->{title}) }->{since} = centreon::plugins::misc::change_seconds(value => $diff_time); + } + } + + $self->{global} = { total => scalar(keys %{$self->{services}}) }; +} + +1; + +__END__ + +=head1 MODE + +Check Google workspace service status. + +=over 8 + +=item B<--filter-name> + +Only display the status for a specific servie +(Example: --filter-service='gmail') + +=item B<--warning-status> + +Set warning threshold for the service status +(Default: '%{status} eq "disruption"'). + +=item B<--critical-status> + +Set warning threshold for the service status +(Default: '%{status} eq "outage"'). + +=back + +=cut diff --git a/centreon-plugins/apps/google/gsuite/plugin.pm b/centreon-plugins/apps/google/workspace/plugin.pm similarity index 79% rename from centreon-plugins/apps/google/gsuite/plugin.pm rename to centreon-plugins/apps/google/workspace/plugin.pm index d2506d086..0055a01fe 100644 --- a/centreon-plugins/apps/google/gsuite/plugin.pm +++ b/centreon-plugins/apps/google/workspace/plugin.pm @@ -18,7 +18,7 @@ # limitations under the License. # -package apps::google::gsuite::plugin; +package apps::google::workspace::plugin; use strict; use warnings; @@ -31,11 +31,11 @@ sub new { $self->{version} = '0.1'; $self->{modes} = { - 'applications' => 'apps::google::gsuite::mode::applications', - 'list-applications' => 'apps::google::gsuite::mode::listapplications' + 'services' => 'apps::google::workspace::mode::services', + 'list-services' => 'apps::google::workspace::mode::listservices' }; - $self->{custom_modes}->{api} = 'apps::google::gsuite::custom::api'; + $self->{custom_modes}->{api} = 'apps::google::workspace::custom::api'; return $self; } @@ -45,6 +45,6 @@ __END__ =head1 PLUGIN DESCRIPTION -Check Google GApps status. +Check Google Workspace status. =cut