This commit is contained in:
qgarnier 2021-03-10 09:05:56 +01:00 committed by GitHub
parent 176b959cf6
commit d2bda2f196
4 changed files with 691 additions and 0 deletions

View File

@ -0,0 +1,305 @@
#
# 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.
# Authors : Roman Morandell - ivertix
#
package apps::proxmox::mg::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' },
'realm:s' => { name => 'realm' },
'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} : 8006;
$self->{url_path} = (defined($self->{option_results}->{url_path})) ? $self->{option_results}->{url_path} : '/api2/json';
$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} : 30;
$self->{realm} = (defined($self->{option_results}->{realm})) ? $self->{option_results}->{realm} : 'pmg';
$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} : '';
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, %options) = @_;
$options{content} =~ s/\r//mg;
my $decoded;
eval {
$decoded = JSON::XS->new->utf8->decode($options{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}->set_options(%{$self->{option_results}});
}
sub clean_session {
my ($self, %options) = @_;
my $datas = {};
$options{statefile}->write(data => $datas);
$self->{cookie_id} = undef;
$self->{cookie_user} = undef;
}
sub get_session {
my ($self, %options) = @_;
my $has_cache_file = $options{statefile}->read(statefile => 'proxmox_mg_' . md5_hex($self->{option_results}->{hostname}) . '_' . md5_hex($self->{option_results}->{api_username}));
my $cookie_id = $options{statefile}->get(name => 'cookie_id');
if ($has_cache_file == 0 || !defined($cookie_id)) {
my ($content) = $self->{http}->request(
method => 'POST',
url_path => $self->{url_path} . '/access/ticket',
post_param => [
'username=' . $self->{api_username},
'password=' . $self->{api_password},
'realm=' . $self->{realm}
],
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 => $content);
if (!defined($decoded->{data}->{ticket})) {
$self->{output}->add_option_msg(short_msg => "Cannot get token");
$self->{output}->option_exit();
}
$cookie_id = $decoded->{data}->{ticket};
$options{statefile}->write(data => { cookie_id => $cookie_id });
}
$self->{cookie_id} = $cookie_id;
}
sub request {
my ($self, %options) = @_;
my $endpoint = $options{full_endpoint};
if (!defined($endpoint)) {
$endpoint = $self->{url_path} . $options{endpoint};
}
$self->settings();
if (!defined($self->{access_token})) {
$self->get_session(statefile => $self->{cache});
}
my $content = $self->{http}->request(
method => 'GET',
url_path => $endpoint,
get_param => $options{get_param},
header => [
'Cookie: PMGAuthCookie=' . $self->{cookie_id}
],
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_session(statefile => $self->{cache});
$self->get_session(statefile => $self->{cache});
$content = $self->{http}->request(
url_path => $endpoint,
get_param => $options{get_param},
header => [
'Cookie: PMGAuthCookie=' . $self->{cookie_id}
],
unknown_status => $self->{unknown_http_status},
warning_status => $self->{warning_http_status},
critical_status => $self->{critical_http_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();
}
return $decoded;
}
1;
__END__
=head1 NAME
Proxmog Media Gateway API
=head1 SYNOPSIS
Proxmog Media Gateway api
=head1 REST API OPTIONS
=over 8
=item B<--hostname>
API hostname.
=item B<--url-path>
API url path (Default: '/api2/json')
=item B<--port>
API port (Default: 8806)
=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<--realm>
Set user realm (Default: 'pmg')
=item B<--timeout>
Set HTTP timeout
=back
=head1 DESCRIPTION
B<custom>.
=cut

View File

@ -0,0 +1,237 @@
#
# 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::proxmox::mg::restapi::mode::mail;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
sub mails_long_output {
my ($self, %options) = @_;
return 'checking mail statistics';
}
sub prefix_mail_output {
my ($self, %options) = @_;
return 'number of mails ';
}
sub prefix_virus_output {
my ($self, %options) = @_;
return 'number of virus mails ';
}
sub prefix_spam_output {
my ($self, %options) = @_;
return 'number of spam mails ';
}
sub prefix_traffic_output {
my ($self, %options) = @_;
return 'traffic ';
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'mails', type => 3, cb_long_output => 'mails_long_output', indent_long_output => ' ',
group => [
{ name => 'mail', type => 0, display_short => 0, cb_prefix_output => 'prefix_mail_output', skipped_code => { -10 => 1 } },
{ name => 'traffic', type => 0, display_short => 0, cb_prefix_output => 'prefix_traffic_output', skipped_code => { -10 => 1 } },
{ name => 'spam', type => 0, display_short => 0, cb_prefix_output => 'prefix_spam_output', skipped_code => { -10 => 1 } },
{ name => 'virus', type => 0, display_short => 0, cb_prefix_output => 'prefix_virus_output', skipped_code => { -10 => 1 } }
]
}
];
$self->{maps_counters}->{mail} = [
{ label => 'mails-incoming', nlabel => 'mails.incoming.count', set => {
key_values => [ { name => 'mail_in' } ],
output_template => 'incoming: %s',
perfdatas => [
{ template => '%s', min => 0 }
]
}
},
{ label => 'mails-outgoing', nlabel => 'mails.outgoing.count', set => {
key_values => [ { name => 'mail_out' } ],
output_template => 'outgoing: %s',
perfdatas => [
{ template => '%s', min => 0 }
]
}
}
];
$self->{maps_counters}->{traffic} = [
{ label => 'traffic-in', nlabel => 'mails.traffic.in.bytespersecond', set => {
key_values => [ { name => 'bytes_in_psec' } ],
output_template => 'in: %.2f %s/s',
output_change_bytes => 1,
perfdatas => [
{ template => '%.2f', unit => 'B/s', min => 0 }
]
}
},
{ label => 'traffic-out', nlabel => 'mails.traffic.out.bytespersecond', set => {
key_values => [ { name => 'bytes_out_psec' } ],
output_template => 'out: %.2f %s/s',
output_change_bytes => 1,
perfdatas => [
{ template => '%.2f', unit => 'B/s', min => 0 }
]
}
}
];
$self->{maps_counters}->{spam} = [
{ label => 'spam-incoming', nlabel => 'mails.spam.incoming.count', set => {
key_values => [ { name => 'spam_in' } ],
output_template => 'incoming: %s',
perfdatas => [
{ template => '%s', min => 0 }
]
}
},
{ label => 'spam-outgoing', nlabel => 'mails.spam.outgoing.count', set => {
key_values => [ { name => 'spam_out' } ],
output_template => 'outgoing: %s',
perfdatas => [
{ template => '%s', min => 0 }
]
}
}
];
$self->{maps_counters}->{virus} = [
{ label => 'virus-incoming', nlabel => 'mails.virus.incoming.count', set => {
key_values => [ { name => 'virus_in' } ],
output_template => 'incoming: %s',
perfdatas => [
{ template => '%s', min => 0 }
]
}
},
{ label => 'virus-outgoing', nlabel => 'mails.virus.outgoing.count', set => {
key_values => [ { name => 'virus_out' } ],
output_template => 'outgoing: %s',
perfdatas => [
{ template => '%s', 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 => {
'hours:s' => { name => 'hours', default => 12 },
'timespan:s' => { name => 'timespan', default => 1800 }
});
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
my $datas = $options{custom}->request(
endpoint => '/statistics/recent',
get_param => [
'hours=' . $self->{option_results}->{hours},
'timespan=' . $self->{option_results}->{timespan}
]
);
my $result = { mail_in => 0, mail_out => 0, virus_in => 0, virus_out => 0, spam_in => 0, spam_out => 0, bytes_in => 0, bytes_out => 0 };
foreach (@{$datas->{data}}) {
$result->{mail_in} += $_->{count_in};
$result->{mail_out} += $_->{count_out};
$result->{spam_in} += $_->{spam_in};
$result->{spam_out} += $_->{spam_out};
$result->{virus_in} += $_->{virus_in};
$result->{virus_out} += $_->{virus_out};
$result->{bytes_in} += $_->{bytes_in};
$result->{bytes_out} += $_->{bytes_out};
}
$self->{output}->output_add(short_msg => 'Mail statistics are ok');
$self->{mails} = {
global => {
mail => {
mail_in => $result->{mail_in},
mail_out => $result->{mail_out}
},
spam => {
spam_in => $result->{spam_in},
spam_out => $result->{spam_out}
},
traffic => {
bytes_in_psec => $result->{bytes_in} / ($self->{option_results}->{hours} * 3600),
bytes_out_psec => $result->{bytes_out} / ($self->{option_results}->{hours} * 3600)
},
virus => {
virus_in => $result->{virus_in},
virus_out => $result->{virus_out}
}
}
};
}
1;
__END__
=head1 MODE
Check mail statistics.
=over 8
=item B<--hours>
How many hours of statistics you want (Default: 12)
=item B<--timespan>
Timespan for datapoints in hours timeframe (Default: 1800)
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'virus-incoming', 'virus-outgoing', 'traffic-in', 'traffic-out',
'mails-incoming', 'mails-outgoing', 'spam-incoming', 'spam-outgoing'.
=back
=cut

View File

@ -0,0 +1,97 @@
#
# 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::proxmox::mg::restapi::mode::version;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
sub custom_version_output {
my ($self, %options) = @_;
return sprintf(
'pmg version: %s [release: %s][repoid: %s]',
$self->{result_values}->{version},
$self->{result_values}->{release},
$self->{result_values}->{repoid}
);
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'version', type => 0 }
];
$self->{maps_counters}->{version} = [
{ label => 'version', type => 2, set => {
key_values => [ { name => 'version' }, { name => 'repoid' }, { name => 'release' } ],
closure_custom_output => $self->can('custom_version_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);
bless $self, $class;
$options{options}->add_options(arguments => {
});
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
$self->{version} = $options{custom}->request(endpoint => '/version');
}
1;
__END__
=head1 MODE
Check version.
=over 8
=item B<--warning-version>
Set warning threshold for version.
Can used special variables like: %{version}, %{repoid}, %{release}
=item B<--critical-version>
Set critical threshold for version.
Can used special variables like: %{version}, %{repoid}, %{release}
=back
=cut

View File

@ -0,0 +1,52 @@
#
# 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.
# Authors : Roman Morandell - ivertix
#
package apps::proxmox::mg::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} = {
'mail' => 'apps::proxmox::mg::restapi::mode::mail',
'version' => 'apps::proxmox::mg::restapi::mode::version'
};
$self->{custom_modes}->{api} = 'apps::proxmox::mg::restapi::custom::api';
return $self;
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check Proxmox Mail Gateway using Rest API.
=cut