rewrite smartemail (#2290)

This commit is contained in:
qgarnier 2020-10-27 11:16:05 +01:00 committed by GitHub
parent 7b261ebe82
commit a4ef2c25d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 703 additions and 844 deletions

View File

@ -1,302 +1,300 @@
# #
# Copyright 2020 Centreon (http://www.centreon.com/) # Copyright 2020 Centreon (http://www.centreon.com/)
# #
# Centreon is a full-fledged industry-strength solution that meets # Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for # the needs in IT infrastructure and application monitoring for
# service performance. # service performance.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Authors : Roman Morandell - ivertix # Authors : Roman Morandell - ivertix
# #
package apps::smartermail::restapi::custom::api; package apps::smartermail::restapi::custom::api;
use strict; use strict;
use warnings; use warnings;
use centreon::plugins::http; use centreon::plugins::http;
use centreon::plugins::statefile; use centreon::plugins::statefile;
use JSON::XS; use JSON::XS;
use Digest::MD5 qw(md5_hex); use Digest::MD5 qw(md5_hex);
sub new { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
my $self = {}; my $self = {};
bless $self, $class; bless $self, $class;
# $options{options} = options object
# $options{output} = output object if (!defined($options{output})) {
# $options{exit_value} = integer print "Class Custom: Need to specify 'output' argument.\n";
# $options{noptions} = integer exit 3;
}
if (!defined($options{output})) { if (!defined($options{options})) {
print "Class Custom: Need to specify 'output' argument.\n"; $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
exit 3; $options{output}->option_exit();
} }
if (!defined($options{options})) {
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument."); if (!defined($options{noptions})) {
$options{output}->option_exit(); $options{options}->add_options(arguments => {
} 'hostname:s' => { name => 'hostname' },
'port:s' => { name => 'port' },
if (!defined($options{noptions})) { 'proto:s' => { name => 'proto' },
$options{options}->add_options(arguments => { 'url-path:s' => { name => 'url_path' },
"hostname:s" => { name => 'hostname' }, 'api-username:s' => { name => 'api_username' },
"port:s" => { name => 'port' }, 'api-password:s' => { name => 'api_password' },
"proto:s" => { name => 'proto' }, 'timeout:s' => { name => 'timeout' },
"url_path:s" => { name => 'url_path' }, });
"username:s" => { name => 'username' }, }
"password:s" => { name => 'password' }, $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
"timeout:s" => { name => 'timeout' },
}); $self->{output} = $options{output};
} $self->{http} = centreon::plugins::http->new(%options);
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); $self->{cache} = centreon::plugins::statefile->new(%options);
$self->{output} = $options{output}; return $self;
$self->{mode} = $options{mode}; }
$self->{http} = centreon::plugins::http->new(%options);
$self->{cache} = centreon::plugins::statefile->new(%options); sub set_options {
my ($self, %options) = @_;
return $self;
} $self->{option_results} = $options{option_results};
}
# Method to manage multiples
sub set_options { sub set_defaults {}
my ($self, %options) = @_;
# options{options_result} sub check_options {
my ($self, %options) = @_;
$self->{option_results} = $options{option_results};
} $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : '';
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
# Method to manage multiples $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
sub set_defaults { $self->{url_path} = (defined($self->{option_results}->{url_path})) ? $self->{option_results}->{url_path} : '/api/v1';
my ($self, %options) = @_; $self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : '';
# options{default} $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : '';
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
# Manage default value
foreach (keys %{$options{default}}) { if ($self->{hostname} eq '') {
if ($_ eq $self->{mode}) { $self->{output}->add_option_msg(short_msg => 'Need to specify hostname option.');
for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) { $self->{output}->option_exit();
foreach my $opt (keys %{$options{default}->{$_}[$i]}) { }
if (!defined($self->{option_results}->{$opt}[$i])) { if ($self->{api_username} eq '') {
$self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt}; $self->{output}->add_option_msg(short_msg => "Need to specify --api-username option.");
} $self->{output}->option_exit();
} }
} if ($self->{api_password} eq '') {
} $self->{output}->add_option_msg(short_msg => "Need to specify --api-password option.");
} $self->{output}->option_exit();
} }
sub check_options { $self->{cache}->check_options(option_results => $self->{option_results});
my ($self, %options) = @_; return 0;
# return 1 = ok still customarg }
# return 0 = no customarg left
sub get_connection_infos {
$self->{hostname} = $self->{option_results}->{hostname}; my ($self, %options) = @_;
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443; return $self->{hostname} . '_' . $self->{http}->get_port();
$self->{url_path} = (defined($self->{option_results}->{url_path})) ? $self->{option_results}->{url_path} : '/api/v1'; }
$self->{username} = (defined($self->{option_results}->{username})) ? $self->{option_results}->{username} : undef;
$self->{password} = (defined($self->{option_results}->{password})) ? $self->{option_results}->{password} : undef; sub get_hostname {
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10; my ($self, %options) = @_;
if (!defined($self->{hostname})) { return $self->{hostname};
$self->{output}->add_option_msg(short_msg => "Need to specify hostname option."); }
$self->{output}->option_exit();
} sub get_port {
my ($self, %options) = @_;
$self->{cache}->check_options(option_results => $self->{option_results});
return $self->{port};
return 0; }
}
sub json_decode {
sub get_connection_infos { my ($self, $content) = @_;
my ($self, %options) = @_;
my $decoded;
return $self->{hostname} . '_' . $self->{http}->get_port(); eval {
} $decoded = JSON::XS->new->utf8->decode($content);
};
sub get_hostname { if ($@) {
my ($self, %options) = @_; $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
return $self->{hostname}; }
}
return $decoded;
sub get_port { }
my ($self, %options) = @_;
sub build_options_for_httplib {
return $self->{port}; my ($self, %options) = @_;
}
$self->{option_results}->{hostname} = $self->{hostname};
sub get_endpoint { $self->{option_results}->{port} = $self->{port};
my ($self, %options) = @_; $self->{option_results}->{proto} = $self->{proto};
}
my $result = $self->request_api(%options);
sub settings {
return $result; my ($self, %options) = @_;
}
$self->build_options_for_httplib();
sub json_decode { $self->{http}->add_header(key => 'Accept', value => 'application/json');
my ($self, $content) = @_; $self->{http}->add_header(key => 'Content-Type', value => 'application/json');
$self->{http}->set_options(%{$self->{option_results}});
my $decoded; }
eval {
$decoded = JSON::XS->new->utf8->decode($content); sub clean_token {
}; my ($self, %options) = @_;
if ($@) {
$self->{output}->output_add(long_msg => $content, debug => 1); my $datas = {};
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); $options{statefile}->write(data => $datas);
$self->{output}->option_exit(); $self->{access_token} = undef;
} $self->{http}->add_header(key => 'Authorization', value => undef);
}
return $decoded;
} sub get_auth_token {
my ($self, %options) = @_;
sub build_options_for_httplib {
my ($self, %options) = @_; my $has_cache_file = $options{statefile}->read(statefile => 'smatermail_api_' . md5_hex($self->{option_results}->{hostname}) . '_' . md5_hex($self->{option_results}->{api_username}));
my $expires_on = $options{statefile}->get(name => 'expires_on');
$self->{option_results}->{hostname} = $self->{hostname}; my $access_token = $options{statefile}->get(name => 'access_token');
$self->{option_results}->{port} = $self->{port};
$self->{option_results}->{proto} = $self->{proto}; # Token expires every 15 minutes
$self->{option_results}->{url_path} = $self->{url_path}; if ($has_cache_file == 0 || !defined($access_token) || (time() > $expires_on)) {
$self->{option_results}->{username} = $self->{username}; my $json_request = { username => $self->{api_username}, password => $self->{api_password} };
$self->{option_results}->{password} = $self->{password}; my $encoded;
$self->{option_results}->{warning_status} = ''; eval {
$self->{option_results}->{critical_status} = ''; $encoded = encode_json($json_request);
} };
if ($@) {
sub settings { $self->{output}->add_option_msg(short_msg => 'cannot encode json request');
my ($self, %options) = @_; $self->{output}->option_exit();
}
$self->build_options_for_httplib();
my ($content) = $self->{http}->request(
$self->{http}->add_header(key => 'Accept', value => 'application/json'); method => 'POST',
if (defined($self->{api_token})) { url_path => $self->{url_path} . '/auth/authenticate-user',
$self->{http}->add_header(key => 'Authorization', value => 'Bearer ' . $self->{api_token}); query_form_post => $encoded,
$self->{http}->add_header(key => 'Content-Type', value => 'application/json'); warning_status => '', unknown_status => '', critical_status => ''
} );
$self->{http}->set_options(%{$self->{option_results}});
} if ($self->{http}->get_code() != 200) {
$self->{output}->add_option_msg(short_msg => "Authentication error [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
sub get_auth_token { $self->{output}->option_exit();
my ($self, %options) = @_; }
my $has_cache_file = $options{statefile}->read(statefile => 'smatermail_api_' . md5_hex($self->{option_results}->{hostname}) . my $decoded = $self->json_decode($content);
'_' . md5_hex($self->{option_results}->{username})); if (!defined($decoded->{accessToken})) {
my $expires_on = $options{statefile}->get(name => 'expires_on'); $self->{output}->add_option_msg(short_msg => "Cannot get token");
my $accessToken = $options{statefile}->get(name => 'accessToken'); $self->{output}->option_exit();
}
if ($has_cache_file == 0 || !defined($accessToken) || (($expires_on - time()) < 60)) {
my $post_param = [ 'username=' . $self->{username}, 'password=' . $self->{password} ]; $access_token = $decoded->{accessToken};
my $datas = {
$self->settings(); access_token => $access_token,
my $url = $self->{url_path} . '/auth/authenticate-user'; expires_on => time() + 900
};
my $content = $self->{http}->request(method => 'POST', url_path => $url, post_param => $post_param); $options{statefile}->write(data => $datas);
}
if ($self->{http}->get_code() != 200) {
$self->{output}->add_option_msg(short_msg => "Authentication error [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']"); $self->{access_token} = $access_token;
$self->{output}->option_exit(); $self->{http}->add_header(key => 'Authorization', value => 'Bearer ' . $self->{acess_token});
} }
my $jsonResponse = $self->json_decode($content); sub request_api {
my ($self, %options) = @_;
if (!defined($jsonResponse->{"resultCode"}) || $jsonResponse->{"resultCode"} ne "200" || !defined($jsonResponse->{accessToken})) {
$self->{output}->output_add(long_msg => $content, debug => 1); $self->settings();
$self->{output}->add_option_msg(short_msg => "Cannot get token"); if (!defined($self->{access_token})) {
$self->{output}->option_exit(); $self->get_auth_token(statefile => $self->{cache});
} }
$accessToken = $jsonResponse->{accessToken}; my $content = $self->{http}->request(
my $datas = { last_timestamp => time(), accessToken => $jsonResponse->{accessToken}, expires_on => time() + 900 }; method => 'GET',
$options{statefile}->write(data => $datas); url_path => $self->{url_path} . $options{endpoint},
} warning_status => '', unknown_status => '', critical_status => ''
);
return $accessToken;
} # Maybe there is an issue with the token. So we retry.
if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) {
sub request_api { $self->clean_token(statefile => $self->{cache});
my ($self, %options) = @_; $self->get_auth_token(statefile => $self->{cache});
$content = $self->{http}->request(
if (!defined($self->{api_token})) { url_path => $self->{url_path} . $options{endpoint},
$self->{api_token} = $self->get_auth_token(statefile => $self->{cache}); warning_status => '', unknown_status => '', critical_status => ''
} );
}
$self->settings();
my $decoded = $self->json_decode(content => $content);
my $content = $self->{http}->request( if (!defined($decoded)) {
method => $options{method}, $self->{output}->add_option_msg(short_msg => 'Error while retrieving data (add --debug option for detailed message)');
url_path => $self->{url_path} . $options{api_path}, $self->{output}->option_exit();
query_form_post => $options{query_form_post}, }
critical_status => '', warning_status => '', unknown_status => ''); if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) {
my $message = 'api request error';
if (defined($decoded->{message})) {
my $jsonResponse = $self->json_decode($content); $message .= ': ' . $decoded->{message};
}
if (!$jsonResponse->{success}) { $self->{output}->add_option_msg(short_msg => $message);
$self->{output}->output_add(long_msg => $content, debug => 1); $self->{output}->option_exit();
$self->{output}->add_option_msg(short_msg => "Request could not be processed: $jsonResponse->{message}"); }
$self->{output}->option_exit();
} return $decoded;
}
return ($jsonResponse, JSON::XS->new->utf8->pretty->encode($jsonResponse));
} 1;
1; __END__
__END__ =head1 NAME
=head1 NAME SmarterMail API
SmarterMail API =head1 SYNOPSIS
=head1 SYNOPSIS smartermail api
smartermail api =head1 REST API OPTIONS
=head1 API OPTIONS =over 8
=over 8 =item B<--hostname>
=item B<--hostname> API hostname.
API hostname. =item B<--url-path>
=item B<--url-path> API url path (Default: '/api/v1')
API url path (Default: '/api/v1') =item B<--port>
=item B<--port> API port (Default: 443)
API port (Default: 443) =item B<--proto>
=item B<--proto> Specify https if needed (Default: 'https')
Specify https if needed (Default: 'https') =item B<--api-username>
=item B<--username> Set API username
Set API username =item B<--api-password>
=item B<--password> Set API password
Set API password =item B<--timeout>
=item B<--timeout> Set HTTP timeout
Set HTTP timeout =back
=back =head1 DESCRIPTION
=head1 DESCRIPTION B<custom>.
B<custom>. =cut
=cut

View File

@ -1,140 +0,0 @@
#
# Copyright 2020 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.
# Authors : Roman Morandell - ivertix
#
package apps::smartermail::restapi::mode::licensenotifications;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use JSON::XS;
sub custom_threshold_output {
my ($self, %options) = @_;
my $status = 'ok';
if ($self->{result_values}->{isUpgradeProtectionExpired_absolute} =~ /Expired/) {
$status = 'critical';
}
return $status;
}
sub custom_status_output {
my ($self, %options) = @_;
my $msg = sprintf("Upgrade protection status is '%s'", $self->{result_values}->{isUpgradeProtectionExpired_absolute});
return $msg;
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'protectionStatus', type => 0, message_separator => ' - ' },
{ name => 'counters', type => 0, message_separator => ' - ' },
];
$self->{maps_counters}->{counters} = [
{ label => 'daysUntilUpgradeProtectionExpires', set => {
key_values => [ { name => 'daysUntilUpgradeProtectionExpires' } ],
output_template => 'expires in %d days',
perfdatas => [
{ label => 'daysUntilUpgradeProtectionExpires', value => 'daysUntilUpgradeProtectionExpires_absolute', template => '%d', min => 0 },
],
}
}
];
$self->{maps_counters}->{protectionStatus} = [
{ label => 'isUpgradeProtectionExpired', threshold => 0, set => {
key_values => [ { name => 'isUpgradeProtectionExpired' } ],
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => $self->can('custom_threshold_output')
}
},
];
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
}
sub manage_selection {
my ($self, %options) = @_;
my $jsonResponse = $options{custom}->get_endpoint(api_path => '/settings/sysadmin/license-notifications');
my $response;
eval {
$response = decode_json($jsonResponse);
};
# the response was checked on "get_endpoint" if contains 'success=true'
if ($@) {
$self->{output}->output_add(long_msg => $jsonResponse, debug => 1);
$self->{output}->add_option_msg(short_msg => "Cannot decode json response");
$self->{output}->option_exit();
}
$self->{counters} = '';
$self->{counters} = {
daysUntilUpgradeProtectionExpires => $response->{daysUntilUpgradeProtectionExpires},
};
$self->{protectionStatus} = '';
$self->{protectionStatus} = {
isUpgradeProtectionExpired => $response->{isUpgradeProtectionExpired} ? "Expired" : "Licensed"
};
}
1;
__END__
=head1 MODE
Check upgrade protection expire
=over 8
=item B<--warning-*>
Threshold warning.
Can be: 'daysUntilUpgradeProtectionExpires'.
=item B<--critical-*>
Threshold critical.
Can be: 'daysUntilUpgradeProtectionExpires'.
=back
=cut

View File

@ -0,0 +1,113 @@
#
# Copyright 2020 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.
# Authors : Roman Morandell - ivertix
#
package apps::smartermail::restapi::mode::licenses;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
sub custom_upgrade_protection_status_output {
my ($self, %options) = @_;
return 'upgrade protection status is ' . $self->{result_values}->{upgrade_protection_status};
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0, message_separator => ' - ' }
];
$self->{maps_counters}->{global} = [
{ label => 'upgrade-protection-status', type => 2, critical_default => '%{upgrade_protection_status} =~ /expired/', set => {
key_values => [ { name => 'upgrade_protection_status' } ],
closure_custom_perfdata => sub { return 0; },
closure_custom_output => $self->can('custom_upgrade_protection_status_output'),
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
},
{ label => 'upgrade-protection-expires-days', nlabel => 'license.upgrade.protection.expires.days.count', set => {
key_values => [ { name => 'upgrade_protection_expires_days' } ],
output_template => 'upgrade protection expires in %d days',
perfdatas => [
{ template => '%d', min => 0 }
]
}
}
];
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
my $result = $options{custom}->request_api(endpoint => '/settings/sysadmin/license-notifications');
$self->{global} = {
upgrade_protection_expires_days => $result->{daysUntilUpgradeProtectionExpires},
upgrade_protection_status => $result->{isUpgradeProtectionExpired} =~ /True|1/i ? 'expired' : 'licensed'
};
}
1;
__END__
=head1 MODE
Check licenses.
=over 8
=item B<--unknown-upgrade-protection-status>
Set unknown threshold for status.
Can used special variables like: %{upgrade_protection_status}
=item B<--warning-upgrade-protection-status>
Set warning threshold for status.
Can used special variables like: %{upgrade_protection_status}
=item B<--critical-upgrade-protection-status>
Set critical threshold for status (Default: '%{upgrade_protection_status} =~ /expired/').
Can used special variables like: %{upgrade_protection_status}
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'upgrade-protection-expires-days'.
=back
=cut

View File

@ -1,155 +1,125 @@
# #
# Copyright 2020 Centreon (http://www.centreon.com/) # Copyright 2020 Centreon (http://www.centreon.com/)
# #
# Centreon is a full-fledged industry-strength solution that meets # Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for # the needs in IT infrastructure and application monitoring for
# service performance. # service performance.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Authors : Roman Morandell - ivertix # Authors : Roman Morandell - ivertix
# #
package apps::smartermail::restapi::mode::services; package apps::smartermail::restapi::mode::services;
use base qw(centreon::plugins::templates::counter); use base qw(centreon::plugins::templates::counter);
use strict; use strict;
use warnings; use warnings;
use JSON::XS; use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_calc);
sub custom_status_output {
sub set_counters { my ($self, %options) = @_;
my ($self, %options) = @_;
return 'state is ' . $self->{result_values}->{state};
$self->{maps_counters_type} = [ }
{ name => 'service', type => 1, cb_prefix_output => 'prefix_service_output',
message_multiple => 'All services are ok' }, sub prefix_service_output {
]; my ($self, %options) = @_;
$self->{maps_counters}->{service} = [ return "Service '" . $options{instance_value}->{display} ."' ";
{ label => 'status', threshold => 0, set => { }
key_values => [ { name => 'state' }, { name => 'display' }],
closure_custom_perfdata => sub { return 0; }, sub set_counters {
closure_custom_output => $self->can('custom_status_output'), my ($self, %options) = @_;
closure_custom_threshold_check => \&catalog_status_threshold,
} $self->{maps_counters_type} = [
}, { name => 'services', type => 1, cb_prefix_output => 'prefix_service_output', message_multiple => 'All services are ok' }
]; ];
}
$self->{maps_counters}->{services} = [
sub custom_status_output { { label => 'status', type => 2, critical_default => '%{state} !~ /running/', set => {
my ($self, %options) = @_; key_values => [ { name => 'state' }, { name => 'display' } ],
closure_custom_perfdata => sub { return 0; },
my $msg .= 'state is ' . $self->{result_values}->{state}; closure_custom_output => $self->can('custom_status_output'),
return $msg; closure_custom_threshold_check => \&catalog_status_threshold_ng
} }
}
sub prefix_service_output { ];
my ($self, %options) = @_; }
return "Service '" . $options{instance_value}->{display} ."' "; sub new {
} my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
sub new { bless $self, $class;
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options); $options{options}->add_options(arguments => {
bless $self, $class; 'filter-service:s' => { name => 'filter_service' }
});
$options{options}->add_options(arguments =>
{ return $self;
"filter-service:s" => { name => 'filter_service' }, }
'unknown-status:s' => { name => 'unknown_status', default => '' },
'warning-status:s' => { name => 'warning_status', default => '' }, sub manage_selection {
'critical-status:s' => { name => 'critical_status', default => '%{state} !~ /running/' }, my ($self, %options) = @_;
});
my $results = $options{custom}->request_api(endpoint => '/settings/sysadmin/services');
return $self;
} $self->{services} = {};
foreach my $svc_name (keys %{$results->{services}}) {
sub check_options { if (defined($self->{option_results}->{filter_service}) && $self->{option_results}->{filter_service} ne '' &&
my ($self, %options) = @_; $svc_name !~ /$self->{option_results}->{filter_service}/) {
$self->SUPER::check_options(%options); $self->{output}->output_add(long_msg => "skipping '" . $svc_name . "': no matching filter.", debug => 1);
next;
$self->change_macros(macros => [ }
'warning_status', 'critical_status', 'unknown_status',
]); $self->{services}->{$svc_name} = {
} display => $svc_name,
state => $results->{services}->{$svc_name} =~ /True|1/i ? 'running' : 'inactive'
sub manage_selection { };
my ($self, %options) = @_; }
}
my $jsonResponse = $options{custom}->get_endpoint(api_path => '/settings/sysadmin/services');
my $response; 1;
eval {
$response = decode_json($jsonResponse); __END__
};
# the response was checked on "get_endpoint" if contains 'success=true' =head1 MODE
if ($@) {
$self->{output}->output_add(long_msg => $jsonResponse, debug => 1); Check services.
$self->{output}->add_option_msg(short_msg => "Cannot decode json response");
$self->{output}->option_exit(); =over 8
}
=item B<--filter-service>
my $jsonServiceStates = $response->{services};
my @services = ('spool', 'smtp', 'pop', 'xmpp', 'imap', 'ldap', 'popretrieval', 'imapretrieval', 'indexing'); Only display some counters (regexp can be used).
(Example: --filter-service='spool|smtp|pop')
$self->{service} = {};
foreach (@services) { =item B<--unknown-status>
if (defined($self->{option_results}->{filter_service}) && $self->{option_results}->{filter_service} ne '' &&
$_ !~ /$self->{option_results}->{filter_service}/) { Set unknown threshold for status.
$self->{output}->output_add(long_msg => "skipping '" . $_ . "': no matching filter.", debug => 1); Can used special variables like: %{state}, %{display}
next;
} =item B<--warning-status>
$self->{service}->{$_} = { Set warning threshold for status.
display => $_, Can used special variables like: %{state}, %{display}
state => $jsonServiceStates->{$_} ? "running" : "inactive",
}; =item B<--critical-status>
}
} Set critical threshold for status (Default: '%{state} !~ /running/').
Can used special variables like: %{state}, %{display}
1; =back
__END__ =cut
=head1 MODE
Check service states
=over 8
=item B<--filter-service>
Only display some counters (regexp can be used).
(Example: --filter-service='spool|smtp|pop')
=item B<--unknown-status>
unknown status.
default: ''.
=item B<--warning-status>
warning status.
default: ''.
=item B<--critical-status>
critical status.
default: '%{state} !~ /running/'.
=back
=cut

View File

@ -1,190 +0,0 @@
#
# Copyright 2020 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.
# Authors : Roman Morandell - ivertix
#
package apps::smartermail::restapi::mode::spoolmessagecount;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use JSON::XS;
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0, message_separator => ' - ' },
];
$self->{maps_counters}->{global} = [
{ label => 'default', set => {
key_values => [ { name => 'default' } ],
output_template => 'Default count: %d',
perfdatas => [
{ label => 'default', value => 'default_absolute', template => '%d', min => 0 },
],
}
},
{ label => 'waiting', set => {
key_values => [ { name => 'waiting' } ],
output_template => 'Waiting : %d',
perfdatas => [
{ label => 'waiting', value => 'waiting_absolute', template => '%d', min => 0 },
],
}
},
{ label => 'spam', set => {
key_values => [ { name => 'spam' } ],
output_template => 'Spam : %d',
perfdatas => [
{ label => 'spam', value => 'spam_absolute', template => '%d', min => 0 },
],
}
},
{ label => 'virus', set => {
key_values => [ { name => 'virus' } ],
output_template => 'Virus : %d',
perfdatas => [
{ label => 'virus', value => 'virus_absolute', template => '%d', min => 0 },
],
}
},
{ label => 'throttledUsers', set => {
key_values => [ { name => 'throttledUsers' } ],
output_template => 'ThrottledUsers : %d',
perfdatas => [
{ label => 'throttledUsers', value => 'throttledUsers_absolute', template => '%d', min => 0 },
],
}
},
{ label => 'throttledMailingLists', set => {
key_values => [ { name => 'throttledMailingLists' } ],
output_template => 'ThrottledMailingLists : %d',
perfdatas => [
{ label => 'throttledMailingLists', value => 'throttledMailingLists_absolute', template => '%d', min => 0 },
],
}
},
{ label => 'throttledDomains', set => {
key_values => [ { name => 'throttledDomains' } ],
output_template => 'ThrottledDomains : %d',
perfdatas => [
{ label => 'throttledDomains', value => 'throttledDomains_absolute', template => '%d', min => 0 },
],
}
},
{ label => 'spool_limit', set => {
key_values => [ { name => 'spool_limit' } ],
output_template => 'Spool_limit : %d',
perfdatas => [
{ label => 'spool_limit', value => 'spool_limit_absolute', template => '%d', min => 0 },
],
}
},
{ label => 'quarantine_limit', set => {
key_values => [ { name => 'quarantine_limit' } ],
output_template => 'Quarantine_limit : %d',
perfdatas => [
{ label => 'quarantine_limit', value => 'quarantine_limit_absolute', template => '%d', min => 0 },
],
}
}
];
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments =>
{
"filter-counter:s" => { name => 'filter_counters' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
}
sub manage_selection {
my ($self, %options) = @_;
my $jsonResponse = $options{custom}->get_endpoint(api_path => '/settings/sysadmin/spool-message-counts');
my $response;
eval {
$response = decode_json($jsonResponse);
};
# the response was checked on "get_endpoint" if contains 'success=true'
if ($@) {
$self->{output}->output_add(long_msg => $jsonResponse, debug => 1);
$self->{output}->add_option_msg(short_msg => "Cannot decode json response");
$self->{output}->option_exit();
}
my $counts = $response->{counts};
$self->{global} = '';
$self->{global} = {
default => $counts->{default},
waiting => $counts->{waiting},
spam => $counts->{spam},
virus => $counts->{virus},
throttledUsers => $counts->{throttledUsers},
throttledMailingLists => $counts->{throttledMailingLists},
throttledDomains => $counts->{throttledDomains},
spool_limit => $counts->{spool_limit},
quarantine_limit => $counts->{quarantine_limit},
};
}
1;
__END__
=head1 MODE
Check spool message counters.
=over 8
=item B<--filter>
Only display some counters (regexp can be used).
(Example: --filter-counter='active')
=item B<--warning-*>
Threshold warning.
Can be: 'default', 'waiting', 'spam', 'virus', 'throttledUsers', 'throttledMailingLists', 'throttledDomains'.
=item B<--critical-*>
Threshold critical.
Can be: 'default', 'waiting', 'spam', 'virus', 'throttledUsers', 'throttledMailingLists', 'throttledDomains'.
=back
=cut

View File

@ -0,0 +1,108 @@
#
# Copyright 2020 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.
# Authors : Roman Morandell - ivertix
#
package apps::smartermail::restapi::mode::spools;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
sub prefix_spool_output {
my ($self, %options) = @_;
return "Spool '" . $options{instance_value}->{display} ."' ";
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'spools', type => 1, cb_prefix_output => 'prefix_spool_output', message_multiple => 'All spools are ok' }
];
$self->{maps_counters}->{spools} = [
{ label => 'spool-messages', nlabel => 'spool.messages.count', set => {
key_values => [ { name => 'messages' }, { name => 'display' } ],
output_template => 'messages: %d',
perfdatas => [
{ template => '%d', min => 0 }
]
}
}
];
}
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-spool:s' => { name => 'filter_spool' }
});
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
my $results = $options{custom}->request_api(endpoint => '/settings/sysadmin/spool-message-counts');
$self->{spools} = {};
foreach my $name (keys %{$results->{counts}}) {
if (defined($self->{option_results}->{filter_spool}) && $self->{option_results}->{filter_spool} ne '' &&
$name !~ /$self->{option_results}->{filter_spool}/) {
$self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1);
next;
}
$self->{spools}->{$name} = {
display => $name,
messages => $results->{counts}->{$name}
};
}
}
1;
__END__
=head1 MODE
Check spools.
=over 8
=item B<--filter-spool>
Filter spools by name (Can be a regexp).
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'spool-messages'.
=back
=cut

View File

@ -1,57 +1,57 @@
# #
# Copyright 2020 Centreon (http://www.centreon.com/) # Copyright 2020 Centreon (http://www.centreon.com/)
# #
# Centreon is a full-fledged industry-strength solution that meets # Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for # the needs in IT infrastructure and application monitoring for
# service performance. # service performance.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Authors : Roman Morandell - ivertix # Authors : Roman Morandell - ivertix
# #
package apps::smartermail::restapi::plugin; package apps::smartermail::restapi::plugin;
use strict; use strict;
use warnings; use warnings;
use base qw(centreon::plugins::script_custom); use base qw(centreon::plugins::script_custom);
sub new { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options); my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class; bless $self, $class;
$self->{version} = '1.0'; $self->{version} = '1.0';
%{$self->{modes}} = ( $self->{modes} = {
'spoolmessagecount' => 'apps::smartermail::restapi::mode::spoolmessagecount', 'spools' => 'apps::smartermail::restapi::mode::spools',
'licensenotifications' => 'apps::smartermail::restapi::mode::licensenotifications', 'licenses' => 'apps::smartermail::restapi::mode::licenses',
'services' => 'apps::smartermail::restapi::mode::services', 'services' => 'apps::smartermail::restapi::mode::services'
); };
$self->{custom_modes}{api} = 'apps::smartermail::restapi::custom::api';
$self->{custom_modes}->{api} = 'apps::smartermail::restapi::custom::api';
return $self; return $self;
} }
1; 1;
__END__ __END__
=head1 PLUGIN DESCRIPTION =head1 PLUGIN DESCRIPTION
Check Custom mode (an example for creating test with a same line argument). Check SmarterMail software using Rest API.
=over 8 =over 8
=back =back
=cut =cut