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/)
#
# 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::custom::api;
use strict;
use warnings;
use centreon::plugins::http;
use centreon::plugins::statefile;
use JSON::XS;
use Digest::MD5 qw(md5_hex);
sub new {
my ($class, %options) = @_;
my $self = {};
bless $self, $class;
# $options{options} = options object
# $options{output} = output object
# $options{exit_value} = integer
# $options{noptions} = integer
if (!defined($options{output})) {
print "Class Custom: Need to specify 'output' argument.\n";
exit 3;
}
if (!defined($options{options})) {
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
$options{output}->option_exit();
}
if (!defined($options{noptions})) {
$options{options}->add_options(arguments => {
"hostname:s" => { name => 'hostname' },
"port:s" => { name => 'port' },
"proto:s" => { name => 'proto' },
"url_path:s" => { name => 'url_path' },
"username:s" => { name => 'username' },
"password:s" => { name => 'password' },
"timeout:s" => { name => 'timeout' },
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
$self->{output} = $options{output};
$self->{mode} = $options{mode};
$self->{http} = centreon::plugins::http->new(%options);
$self->{cache} = centreon::plugins::statefile->new(%options);
return $self;
}
# Method to manage multiples
sub set_options {
my ($self, %options) = @_;
# options{options_result}
$self->{option_results} = $options{option_results};
}
# Method to manage multiples
sub set_defaults {
my ($self, %options) = @_;
# options{default}
# Manage default value
foreach (keys %{$options{default}}) {
if ($_ eq $self->{mode}) {
for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) {
foreach my $opt (keys %{$options{default}->{$_}[$i]}) {
if (!defined($self->{option_results}->{$opt}[$i])) {
$self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt};
}
}
}
}
}
}
sub check_options {
my ($self, %options) = @_;
# return 1 = ok still customarg
# return 0 = no customarg left
$self->{hostname} = $self->{option_results}->{hostname};
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
$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;
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
if (!defined($self->{hostname})) {
$self->{output}->add_option_msg(short_msg => "Need to specify hostname option.");
$self->{output}->option_exit();
}
$self->{cache}->check_options(option_results => $self->{option_results});
return 0;
}
sub get_connection_infos {
my ($self, %options) = @_;
return $self->{hostname} . '_' . $self->{http}->get_port();
}
sub get_hostname {
my ($self, %options) = @_;
return $self->{hostname};
}
sub get_port {
my ($self, %options) = @_;
return $self->{port};
}
sub get_endpoint {
my ($self, %options) = @_;
my $result = $self->request_api(%options);
return $result;
}
sub json_decode {
my ($self, $content) = @_;
my $decoded;
eval {
$decoded = JSON::XS->new->utf8->decode($content);
};
if ($@) {
$self->{output}->output_add(long_msg => $content, debug => 1);
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
}
return $decoded;
}
sub build_options_for_httplib {
my ($self, %options) = @_;
$self->{option_results}->{hostname} = $self->{hostname};
$self->{option_results}->{port} = $self->{port};
$self->{option_results}->{proto} = $self->{proto};
$self->{option_results}->{url_path} = $self->{url_path};
$self->{option_results}->{username} = $self->{username};
$self->{option_results}->{password} = $self->{password};
$self->{option_results}->{warning_status} = '';
$self->{option_results}->{critical_status} = '';
}
sub settings {
my ($self, %options) = @_;
$self->build_options_for_httplib();
$self->{http}->add_header(key => 'Accept', value => 'application/json');
if (defined($self->{api_token})) {
$self->{http}->add_header(key => 'Authorization', value => 'Bearer ' . $self->{api_token});
$self->{http}->add_header(key => 'Content-Type', value => 'application/json');
}
$self->{http}->set_options(%{$self->{option_results}});
}
sub get_auth_token {
my ($self, %options) = @_;
my $has_cache_file = $options{statefile}->read(statefile => 'smatermail_api_' . md5_hex($self->{option_results}->{hostname}) .
'_' . md5_hex($self->{option_results}->{username}));
my $expires_on = $options{statefile}->get(name => 'expires_on');
my $accessToken = $options{statefile}->get(name => 'accessToken');
if ($has_cache_file == 0 || !defined($accessToken) || (($expires_on - time()) < 60)) {
my $post_param = [ 'username=' . $self->{username}, 'password=' . $self->{password} ];
$self->settings();
my $url = $self->{url_path} . '/auth/authenticate-user';
my $content = $self->{http}->request(method => 'POST', url_path => $url, post_param => $post_param);
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->{output}->option_exit();
}
my $jsonResponse = $self->json_decode($content);
if (!defined($jsonResponse->{"resultCode"}) || $jsonResponse->{"resultCode"} ne "200" || !defined($jsonResponse->{accessToken})) {
$self->{output}->output_add(long_msg => $content, debug => 1);
$self->{output}->add_option_msg(short_msg => "Cannot get token");
$self->{output}->option_exit();
}
$accessToken = $jsonResponse->{accessToken};
my $datas = { last_timestamp => time(), accessToken => $jsonResponse->{accessToken}, expires_on => time() + 900 };
$options{statefile}->write(data => $datas);
}
return $accessToken;
}
sub request_api {
my ($self, %options) = @_;
if (!defined($self->{api_token})) {
$self->{api_token} = $self->get_auth_token(statefile => $self->{cache});
}
$self->settings();
my $content = $self->{http}->request(
method => $options{method},
url_path => $self->{url_path} . $options{api_path},
query_form_post => $options{query_form_post},
critical_status => '', warning_status => '', unknown_status => '');
my $jsonResponse = $self->json_decode($content);
if (!$jsonResponse->{success}) {
$self->{output}->output_add(long_msg => $content, debug => 1);
$self->{output}->add_option_msg(short_msg => "Request could not be processed: $jsonResponse->{message}");
$self->{output}->option_exit();
}
return ($jsonResponse, JSON::XS->new->utf8->pretty->encode($jsonResponse));
}
1;
__END__
=head1 NAME
SmarterMail API
=head1 SYNOPSIS
smartermail api
=head1 API OPTIONS
=over 8
=item B<--hostname>
API hostname.
=item B<--url-path>
API url path (Default: '/api/v1')
=item B<--port>
API port (Default: 443)
=item B<--proto>
Specify https if needed (Default: 'https')
=item B<--username>
Set API username
=item B<--password>
Set API password
=item B<--timeout>
Set HTTP timeout
=back
=head1 DESCRIPTION
B<custom>.
=cut
#
# 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::custom::api;
use strict;
use warnings;
use centreon::plugins::http;
use centreon::plugins::statefile;
use JSON::XS;
use Digest::MD5 qw(md5_hex);
sub new {
my ($class, %options) = @_;
my $self = {};
bless $self, $class;
if (!defined($options{output})) {
print "Class Custom: Need to specify 'output' argument.\n";
exit 3;
}
if (!defined($options{options})) {
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
$options{output}->option_exit();
}
if (!defined($options{noptions})) {
$options{options}->add_options(arguments => {
'hostname:s' => { name => 'hostname' },
'port:s' => { name => 'port' },
'proto:s' => { name => 'proto' },
'url-path:s' => { name => 'url_path' },
'api-username:s' => { name => 'api_username' },
'api-password:s' => { name => 'api_password' },
'timeout:s' => { name => 'timeout' },
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
$self->{output} = $options{output};
$self->{http} = centreon::plugins::http->new(%options);
$self->{cache} = centreon::plugins::statefile->new(%options);
return $self;
}
sub set_options {
my ($self, %options) = @_;
$self->{option_results} = $options{option_results};
}
sub set_defaults {}
sub check_options {
my ($self, %options) = @_;
$self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : '';
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
$self->{url_path} = (defined($self->{option_results}->{url_path})) ? $self->{option_results}->{url_path} : '/api/v1';
$self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : '';
$self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : '';
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
if ($self->{hostname} eq '') {
$self->{output}->add_option_msg(short_msg => 'Need to specify hostname option.');
$self->{output}->option_exit();
}
if ($self->{api_username} eq '') {
$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();
}
$self->{cache}->check_options(option_results => $self->{option_results});
return 0;
}
sub get_connection_infos {
my ($self, %options) = @_;
return $self->{hostname} . '_' . $self->{http}->get_port();
}
sub get_hostname {
my ($self, %options) = @_;
return $self->{hostname};
}
sub get_port {
my ($self, %options) = @_;
return $self->{port};
}
sub json_decode {
my ($self, $content) = @_;
my $decoded;
eval {
$decoded = JSON::XS->new->utf8->decode($content);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
}
return $decoded;
}
sub build_options_for_httplib {
my ($self, %options) = @_;
$self->{option_results}->{hostname} = $self->{hostname};
$self->{option_results}->{port} = $self->{port};
$self->{option_results}->{proto} = $self->{proto};
}
sub settings {
my ($self, %options) = @_;
$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}});
}
sub clean_token {
my ($self, %options) = @_;
my $datas = {};
$options{statefile}->write(data => $datas);
$self->{access_token} = undef;
$self->{http}->add_header(key => 'Authorization', value => undef);
}
sub get_auth_token {
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');
my $access_token = $options{statefile}->get(name => 'access_token');
# Token expires every 15 minutes
if ($has_cache_file == 0 || !defined($access_token) || (time() > $expires_on)) {
my $json_request = { username => $self->{api_username}, password => $self->{api_password} };
my $encoded;
eval {
$encoded = encode_json($json_request);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => 'cannot encode json request');
$self->{output}->option_exit();
}
my ($content) = $self->{http}->request(
method => 'POST',
url_path => $self->{url_path} . '/auth/authenticate-user',
query_form_post => $encoded,
warning_status => '', unknown_status => '', critical_status => ''
);
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->{output}->option_exit();
}
my $decoded = $self->json_decode($content);
if (!defined($decoded->{accessToken})) {
$self->{output}->add_option_msg(short_msg => "Cannot get token");
$self->{output}->option_exit();
}
$access_token = $decoded->{accessToken};
my $datas = {
access_token => $access_token,
expires_on => time() + 900
};
$options{statefile}->write(data => $datas);
}
$self->{access_token} = $access_token;
$self->{http}->add_header(key => 'Authorization', value => 'Bearer ' . $self->{acess_token});
}
sub request_api {
my ($self, %options) = @_;
$self->settings();
if (!defined($self->{access_token})) {
$self->get_auth_token(statefile => $self->{cache});
}
my $content = $self->{http}->request(
method => 'GET',
url_path => $self->{url_path} . $options{endpoint},
warning_status => '', unknown_status => '', critical_status => ''
);
# Maybe there is an issue with the token. So we retry.
if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) {
$self->clean_token(statefile => $self->{cache});
$self->get_auth_token(statefile => $self->{cache});
$content = $self->{http}->request(
url_path => $self->{url_path} . $options{endpoint},
warning_status => '', unknown_status => '', critical_status => ''
);
}
my $decoded = $self->json_decode(content => $content);
if (!defined($decoded)) {
$self->{output}->add_option_msg(short_msg => 'Error while retrieving data (add --debug option for detailed message)');
$self->{output}->option_exit();
}
if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) {
my $message = 'api request error';
if (defined($decoded->{message})) {
$message .= ': ' . $decoded->{message};
}
$self->{output}->add_option_msg(short_msg => $message);
$self->{output}->option_exit();
}
return $decoded;
}
1;
__END__
=head1 NAME
SmarterMail API
=head1 SYNOPSIS
smartermail api
=head1 REST API OPTIONS
=over 8
=item B<--hostname>
API hostname.
=item B<--url-path>
API url path (Default: '/api/v1')
=item B<--port>
API port (Default: 443)
=item B<--proto>
Specify https if needed (Default: 'https')
=item B<--api-username>
Set API username
=item B<--api-password>
Set API password
=item B<--timeout>
Set HTTP timeout
=back
=head1 DESCRIPTION
B<custom>.
=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/)
#
# 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::services;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use JSON::XS;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_calc);
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'service', type => 1, cb_prefix_output => 'prefix_service_output',
message_multiple => 'All services are ok' },
];
$self->{maps_counters}->{service} = [
{ label => 'status', threshold => 0, set => {
key_values => [ { name => 'state' }, { name => 'display' }],
closure_custom_perfdata => sub { return 0; },
closure_custom_output => $self->can('custom_status_output'),
closure_custom_threshold_check => \&catalog_status_threshold,
}
},
];
}
sub custom_status_output {
my ($self, %options) = @_;
my $msg .= 'state is ' . $self->{result_values}->{state};
return $msg;
}
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);
bless $self, $class;
$options{options}->add_options(arguments =>
{
"filter-service:s" => { name => 'filter_service' },
'unknown-status:s' => { name => 'unknown_status', default => '' },
'warning-status:s' => { name => 'warning_status', default => '' },
'critical-status:s' => { name => 'critical_status', default => '%{state} !~ /running/' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
$self->change_macros(macros => [
'warning_status', 'critical_status', 'unknown_status',
]);
}
sub manage_selection {
my ($self, %options) = @_;
my $jsonResponse = $options{custom}->get_endpoint(api_path => '/settings/sysadmin/services');
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 $jsonServiceStates = $response->{services};
my @services = ('spool', 'smtp', 'pop', 'xmpp', 'imap', 'ldap', 'popretrieval', 'imapretrieval', 'indexing');
$self->{service} = {};
foreach (@services) {
if (defined($self->{option_results}->{filter_service}) && $self->{option_results}->{filter_service} ne '' &&
$_ !~ /$self->{option_results}->{filter_service}/) {
$self->{output}->output_add(long_msg => "skipping '" . $_ . "': no matching filter.", debug => 1);
next;
}
$self->{service}->{$_} = {
display => $_,
state => $jsonServiceStates->{$_} ? "running" : "inactive",
};
}
}
1;
__END__
=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
#
# 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::services;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
sub custom_status_output {
my ($self, %options) = @_;
return 'state is ' . $self->{result_values}->{state};
}
sub prefix_service_output {
my ($self, %options) = @_;
return "Service '" . $options{instance_value}->{display} ."' ";
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'services', type => 1, cb_prefix_output => 'prefix_service_output', message_multiple => 'All services are ok' }
];
$self->{maps_counters}->{services} = [
{ label => 'status', type => 2, critical_default => '%{state} !~ /running/', set => {
key_values => [ { name => 'state' }, { name => 'display' } ],
closure_custom_perfdata => sub { return 0; },
closure_custom_output => $self->can('custom_status_output'),
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
}
];
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments => {
'filter-service:s' => { name => 'filter_service' }
});
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
my $results = $options{custom}->request_api(endpoint => '/settings/sysadmin/services');
$self->{services} = {};
foreach my $svc_name (keys %{$results->{services}}) {
if (defined($self->{option_results}->{filter_service}) && $self->{option_results}->{filter_service} ne '' &&
$svc_name !~ /$self->{option_results}->{filter_service}/) {
$self->{output}->output_add(long_msg => "skipping '" . $svc_name . "': no matching filter.", debug => 1);
next;
}
$self->{services}->{$svc_name} = {
display => $svc_name,
state => $results->{services}->{$svc_name} =~ /True|1/i ? 'running' : 'inactive'
};
}
}
1;
__END__
=head1 MODE
Check services.
=over 8
=item B<--filter-service>
Only display some counters (regexp can be used).
(Example: --filter-service='spool|smtp|pop')
=item B<--unknown-status>
Set unknown threshold for status.
Can used special variables like: %{state}, %{display}
=item B<--warning-status>
Set warning threshold for status.
Can used special variables like: %{state}, %{display}
=item B<--critical-status>
Set critical threshold for status (Default: '%{state} !~ /running/').
Can used special variables like: %{state}, %{display}
=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/)
#
# 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::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} = '1.0';
%{$self->{modes}} = (
'spoolmessagecount' => 'apps::smartermail::restapi::mode::spoolmessagecount',
'licensenotifications' => 'apps::smartermail::restapi::mode::licensenotifications',
'services' => 'apps::smartermail::restapi::mode::services',
);
$self->{custom_modes}{api} = 'apps::smartermail::restapi::custom::api';
return $self;
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check Custom mode (an example for creating test with a same line argument).
=over 8
=back
=cut
#
# 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::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} = '1.0';
$self->{modes} = {
'spools' => 'apps::smartermail::restapi::mode::spools',
'licenses' => 'apps::smartermail::restapi::mode::licenses',
'services' => 'apps::smartermail::restapi::mode::services'
};
$self->{custom_modes}->{api} = 'apps::smartermail::restapi::custom::api';
return $self;
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check SmarterMail software using Rest API.
=over 8
=back
=cut