add mongodb plugin

This commit is contained in:
Colin Gagnaire 2019-05-02 17:15:26 +02:00
parent 8e0e3a9f6a
commit ca39ca9d47
8 changed files with 1154 additions and 0 deletions

View File

@ -0,0 +1,217 @@
#
# Copyright 2019 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package database::mongodb::custom::driver;
use strict;
use warnings;
use DateTime;
use MongoDB;
use Hash::Ordered;
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' },
"username:s" => { name => 'username' },
"password:s" => { name => 'password' },
"timeout:s" => { name => 'timeout' },
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'DRIVER OPTIONS', once => 1);
$self->{output} = $options{output};
$self->{mode} = $options{mode};
return $self;
}
sub set_options {
my ($self, %options) = @_;
$self->{option_results} = $options{option_results};
}
sub set_defaults {
my ($self, %options) = @_;
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) = @_;
$self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : 'localhost';
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 27017;
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
$self->{username} = (defined($self->{option_results}->{username})) ? $self->{option_results}->{username} : '';
$self->{password} = (defined($self->{option_results}->{password})) ? $self->{option_results}->{password} : '';
return 0;
}
sub get_hostname {
my ($self, %options) = @_;
return $self->{hostname};
}
sub get_port {
my ($self, %options) = @_;
return $self->{port};
}
sub connect {
my ($self, %options) = @_;
my $uri = 'mongodb://';
$uri .= $self->{username} . ':' . $self->{password} . '@' if ($self->{username} ne '' && $self->{password} ne '');
$uri .= $self->{hostname} if ($self->{hostname} ne '');
$uri .= ':' . $self->{port} if ($self->{port} ne '');
$self->{output}->output_add(long_msg => 'Connection URI: ' . $uri, debug => 1);
$self->{client} = MongoDB::MongoClient->new(host => $uri);
$self->{client}->connect();
eval {
my $conn_status = $self->run_command(
database => 'admin',
command => $self->ordered_hash(ping => 1),
);
};
if ($@) {
$self->{output}->output_add(long_msg => $@, debug => 1);
$self->{output}->add_option_msg(short_msg => "Connection error (add --debug option to display error message)");
$self->{output}->option_exit();
}
}
sub ordered_hash {
my ($self, %options) = @_;
tie my %hash, 'Hash::Ordered';
my $oh = tied %hash;
$oh->push(%options);
return \%hash;
}
sub run_command {
my ($self, %options) = @_;
if (!defined($self->{client})) {
$self->connect();
}
my $db = $self->{client}->get_database($options{database});
return $db->run_command($options{command});
}
sub list_databases {
my ($self, %options) = @_;
if (!defined($self->{client})) {
$self->connect();
}
my @dbs = $self->{client}->database_names;
return \@dbs;
}
sub list_collections {
my ($self, %options) = @_;
if (!defined($self->{client})) {
$self->connect();
}
my $db = $self->{client}->get_database($options{database});
my @cls = $db->collection_names;
return \@cls;
}
1;
__END__
=head1 NAME
MongoDB driver
=head1 DRIVER OPTIONS
MongoDB driver
=over 8
=item B<--hostname>
MongoDB server hostname.
=item B<--port>
Port used (Default: 27017)
=item B<--username>
MongoDB username.
=item B<--password>
MongoDB password.
=item B<--timeout>
Set timeout in seconds (Default: 10).
=back
=head1 DESCRIPTION
B<custom>.
=cut

View File

@ -0,0 +1,190 @@
#
# Copyright 2019 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package database::mongodb::mode::collectionstatistics;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'databases', type => 3, cb_long_output => 'long_output',
message_multiple => 'All databases statistics are ok', indent_long_output => ' ',
group => [
{ name => 'collections', display_long => 1, cb_prefix_output => 'prefix_output_collection',
message_multiple => 'All collections statistics are ok', type => 1 },
]
}
];
$self->{maps_counters}->{collections} = [
{ label => 'storage-size', nlabel => 'collection.size.storage.bytes', set => {
key_values => [ { name => 'storageSize' }, { name => 'display' } ],
output_template => 'Storage Size: %s %s',
output_change_bytes => 1,
perfdatas => [
{ value => 'storageSize_absolute', template => '%s',
min => 0, unit => 'B', label_extra_instance => 1 },
],
}
},
{ label => 'index-size', nlabel => 'collection.size.index.bytes', set => {
key_values => [ { name => 'totalIndexSize' }, { name => 'display' } ],
output_template => 'Index Size: %s %s',
output_change_bytes => 1,
perfdatas => [
{ value => 'totalIndexSize_absolute', template => '%s',
min => 0, unit => 'B', label_extra_instance => 1 },
],
}
},
{ label => 'documents', nlabel => 'collection.documents.count', set => {
key_values => [ { name => 'count' }, { name => 'display' } ],
output_template => 'Documents: %s',
perfdatas => [
{ value => 'count_absolute', template => '%s',
min => 0, label_extra_instance => 1 },
],
}
},
{ label => 'indexes', nlabel => 'collection.indexes.count', set => {
key_values => [ { name => 'nindexes' }, { name => 'display' } ],
output_template => 'Indexes: %s',
perfdatas => [
{ value => 'nindexes_absolute', template => '%s',
min => 0, label_extra_instance => 1 },
],
}
},
];
}
sub long_output {
my ($self, %options) = @_;
return "Checking database '" . $options{instance_value}->{display} . "' ";
}
sub prefix_output_collection {
my ($self, %options) = @_;
return "Collection '" . $options{instance_value}->{display} . "' ";
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments => {
"filter-database:s" => { name => 'filter_database' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
}
sub manage_selection {
my ($self, %options) = @_;
$self->{custom} = $options{custom};
$self->{databases} = {};
my $databases = $self->{custom}->list_databases();
foreach my $database (sort @{$databases}) {
next if (defined($self->{option_results}->{filter_database}) && $self->{option_results}->{filter_database} ne ''
&& $database !~ /$self->{option_results}->{filter_database}/);
my $collections = $self->{custom}->list_collections(database => $database);
$self->{databases}->{$database}->{display} = $database;
foreach my $collection (sort @{$collections}) {
my $cl_stats = $self->{custom}->run_command(
database => $database,
command => $self->{custom}->ordered_hash(collStats => $collection),
);
$self->{databases}->{$database}->{collections}->{$collection} = {
display => $collection,
storageSize => $cl_stats->{storageSize},
totalIndexSize => $cl_stats->{totalIndexSize},
count => $cl_stats->{count},
nindexes => $cl_stats->{nindexes},
};
}
}
if (scalar(keys %{$self->{databases}}) <= 0) {
$self->{output}->add_option_msg(short_msg => 'No databases found');
$self->{output}->option_exit();
}
use Data::Dumper;
print Dumper $self;
}
1;
__END__
=head1 MODE
Check collections statistics per databases
=over 8
=item B<--filter-database>
Filter database name (Can use regexp).
=item B<--warning-subinstance-collection-size-*-bytes>
Threshold warning.
Can be: 'storage', 'index'.
=item B<--critical-subinstance-collection-size-*-bytes>
Threshold critical.
Can be: 'storage', 'index'.
=item B<--warning-subinstance-collection-*-count>
Threshold warning.
Can be: 'documents', 'indexes'.
=item B<--critical-subinstance-collection-*-count>
Threshold critical.
Can be: 'documents', 'indexes'.
=back
=cut

View File

@ -0,0 +1,154 @@
#
# Copyright 2019 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package database::mongodb::mode::connections;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use Digest::MD5 qw(md5_hex);
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0, cb_prefix_output => 'prefix_output' },
];
$self->{maps_counters}->{global} = [
{ label => 'active', nlabel => 'connections.active.count', set => {
key_values => [ { name => 'active' } ],
output_template => 'Active: %d',
perfdatas => [
{ value => 'active_absolute', template => '%d', min => 0, unit => 'conn' },
],
}
},
{ label => 'current', nlabel => 'connections.current.count', set => {
key_values => [ { name => 'current' } ],
output_template => 'Current: %d',
perfdatas => [
{ value => 'current_absolute', template => '%d', min => 0, unit => 'conn' },
],
}
},
{ label => 'usage', nlabel => 'connections.usage.percentage', set => {
key_values => [ { name => 'usage' } ],
output_template => 'Usage: %.2f %%',
perfdatas => [
{ value => 'usage_absolute', template => '%.2f', min => 0, max => 100, unit => '%' },
],
}
},
{ label => 'total-created', nlabel => 'connections.persecond', set => {
key_values => [ { name => 'totalCreated', diff => 1 } ],
output_template => 'Created: %.2f/s',
per_second => 1,
perfdatas => [
{ value => 'totalCreated_per_second', template => '%.2f', min => 0, unit => 'conn/s' },
],
}
},
];
}
sub prefix_output {
my ($self, %options) = @_;
return "Connections ";
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1, statefile => 1);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments => {});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
}
sub manage_selection {
my ($self, %options) = @_;
$self->{custom} = $options{custom};
$self->{global} = {};
my $server_stats = $self->{custom}->run_command(
database => 'admin',
command => $self->{custom}->ordered_hash(serverStatus => 1),
);
$self->{global}->{active} = $server_stats->{connections}->{active};
$self->{global}->{current} = $server_stats->{connections}->{current};
$self->{global}->{usage} = $server_stats->{connections}->{current} / ($server_stats->{connections}->{current} + $server_stats->{connections}->{available});
$self->{global}->{totalCreated} = $server_stats->{connections}->{totalCreated};
$self->{cache_name} = "mongodb_" . $self->{mode} . '_' . $self->{custom}->get_hostname() . '_' . $self->{custom}->get_port() . '_' .
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all'));
}
1;
__END__
=head1 MODE
Check connections statistics
=over 8
=item B<--warning-connections-*-count>
Threshold warning.
Can be: 'active', 'current'.
=item B<--critical-connections-*-count>
Threshold critical.
Can be: 'active', 'current'.
=item B<--warning-connections-usage-percentage>
Threshold warning for connections usage (current over available)
=item B<--critical-connections-usage-percentage>
Threshold critical for connections usage (current over available)
=item B<--warning-connections-persecond>
Threshold warning for connections created per second.
=item B<--critical-connections-persecond>
Threshold critical for connections created per second.
=back
=cut

View File

@ -0,0 +1,97 @@
#
# Copyright 2019 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package database::mongodb::mode::connectiontime;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use Time::HiRes;
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0 },
];
$self->{maps_counters}->{global} = [
{ label => 'connection-time', nlabel => 'database.connection.time.milliseconds', set => {
key_values => [ { name => 'connection_time' } ],
output_template => 'Connection established in %d ms',
perfdatas => [
{ value => 'connection_time_absolute', template => '%d', unit => 'ms',
min => 0 },
],
}
},
];
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments => {});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
sub manage_selection {
my ($self, %options) = @_;
$self->{custom} = $options{custom};
my $start = Time::HiRes::time();
$self->{custom}->connect();
my $end = Time::HiRes::time();
$self->{global}->{connection_time} = ($end - $start) * 1000;
}
1;
__END__
=head1 MODE
Check database connection time.
=over 8
=item B<--warning-database-connection-time-milliseconds>
Threshold warning in milliseconds.
=item B<--critical-database-connection-time-milliseconds>>
Threshold critical in milliseconds.
=back
=cut

View File

@ -0,0 +1,203 @@
#
# Copyright 2019 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package database::mongodb::mode::databasestatistics;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'databases', type => 1, cb_prefix_output => 'prefix_database_output',
message_multiple => 'All databases statistics are ok' },
];
$self->{maps_counters}->{databases} = [
{ label => 'storage-size', nlabel => 'database.size.storage.bytes', set => {
key_values => [ { name => 'storageSize' }, { name => 'display' } ],
output_template => 'Storage Size: %s %s',
output_change_bytes => 1,
perfdatas => [
{ value => 'storageSize_absolute', template => '%s',
min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'data-size', nlabel => 'database.size.data.bytes', set => {
key_values => [ { name => 'dataSize' }, { name => 'display' } ],
output_template => 'Data Size: %s %s',
output_change_bytes => 1,
perfdatas => [
{ value => 'dataSize_absolute', template => '%s',
min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'index-size', nlabel => 'database.size.index.bytes', set => {
key_values => [ { name => 'indexSize' }, { name => 'display' } ],
output_template => 'Index Size: %s %s',
output_change_bytes => 1,
perfdatas => [
{ value => 'indexSize_absolute', template => '%s',
min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'collections', nlabel => 'database.collections.count', set => {
key_values => [ { name => 'collections' }, { name => 'display' } ],
output_template => 'Collections: %s',
perfdatas => [
{ value => 'collections_absolute', template => '%s',
min => 0, label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'views', nlabel => 'database.views.count', set => {
key_values => [ { name => 'views' }, { name => 'display' } ],
output_template => 'Views: %s',
perfdatas => [
{ value => 'views_absolute', template => '%s',
min => 0, label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'objects', nlabel => 'database.objects.count', set => {
key_values => [ { name => 'objects' }, { name => 'display' } ],
output_template => 'Objects: %s',
perfdatas => [
{ value => 'objects_absolute', template => '%s',
min => 0, label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'indexes', nlabel => 'database.indexes.count', set => {
key_values => [ { name => 'indexes' }, { name => 'display' } ],
output_template => 'Indexes: %s',
perfdatas => [
{ value => 'indexes_absolute', template => '%s',
min => 0, label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
];
}
sub prefix_database_output {
my ($self, %options) = @_;
return "Database '" . $options{instance_value}->{display} . "' ";
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments => {
"filter-database:s" => { name => 'filter_database' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
}
sub manage_selection {
my ($self, %options) = @_;
$self->{custom} = $options{custom};
$self->{databases} = {};
my $databases = $self->{custom}->list_databases();
foreach my $database (sort @{$databases}) {
next if (defined($self->{option_results}->{filter_database}) && $self->{option_results}->{filter_database} ne ''
&& $database !~ /$self->{option_results}->{filter_database}/);
my $db_stats = $self->{custom}->run_command(
database => $database,
command => $self->{custom}->ordered_hash('dbStats' => 1),
);
$self->{databases}->{$db_stats->{db}} = {
display => $db_stats->{db},
collections => $db_stats->{collections},
views => $db_stats->{views},
objects => $db_stats->{objects},
storageSize => $db_stats->{storageSize},
indexSize => $db_stats->{indexSize},
dataSize => $db_stats->{dataSize},
indexes => $db_stats->{indexes},
}
}
if (scalar(keys %{$self->{databases}}) <= 0) {
$self->{output}->add_option_msg(short_msg => 'No databases found');
$self->{output}->option_exit();
}
}
1;
__END__
=head1 MODE
Check databases statistics
=over 8
=item B<--filter-database>
Filter database name (Can use regexp).
=item B<--warning-instance-database-size-*-bytes>
Threshold warning.
Can be: 'storage', 'data', 'index'.
=item B<--critical-instance-database-size-*-bytes>
Threshold critical.
Can be: 'storage', 'data', 'index'.
=item B<--warning-instance-database-*-count>
Threshold warning.
Can be: 'collections', 'views', 'objects',
'indexes'.
=item B<--critical-instance-database-*-count>
Threshold critical.
Can be: 'collections', 'views', 'objects',
'indexes'.
=back
=cut

View File

@ -0,0 +1,94 @@
#
# Copyright 2019 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package database::mongodb::mode::listdatabases;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments => {});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
sub manage_selection {
my ($self, %options) = @_;
$self->{custom} = $options{custom};
$self->{databases} = $self->{custom}->list_databases();
}
sub run {
my ($self, %options) = @_;
$self->manage_selection(%options);
foreach my $database (sort @{$self->{databases}}) {
$self->{output}->output_add(long_msg => sprintf("[name = %s]",
$database));
}
$self->{output}->output_add(severity => 'OK',
short_msg => "List databases:");
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit();
}
sub disco_format {
my ($self, %options) = @_;
$self->{output}->add_disco_format(elements => ['name']);
}
sub disco_show {
my ($self, %options) = @_;
$self->manage_selection(%options);
foreach my $database (sort @{$self->{databases}}) {
$self->{output}->add_disco_entry(name => $database);
}
}
1;
__END__
=head1 MODE
List databases.
=over 8
=back
=cut

View File

@ -0,0 +1,145 @@
#
# Copyright 2019 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package database::mongodb::mode::queries;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use Digest::MD5 qw(md5_hex);
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0, cb_prefix_output => 'prefix_output' },
];
$self->{maps_counters}->{global} = [
{ label => 'total', nlabel => 'queries.persecond', set => {
key_values => [ { name => 'total', diff => 1 } ],
per_second => 1,
output_template => 'Total : %d',
perfdatas => [
{ value => 'total_per_second', template => '%d', unit => '/s', min => 0 },
],
}
},
];
foreach ('insert', 'query', 'update', 'delete', 'getmore', 'command') {
push @{$self->{maps_counters}->{global}}, {
label => $_, nlabel => 'queries.' . $_ . '.persecond', display_ok => 0, set => {
key_values => [ { name => $_, diff => 1 } ],
per_second => 1,
output_template => $_ . ' : %.2f',
perfdatas => [
{ value => $_ . '_per_second',template => '%.2f', unit => '/s', min => 0 },
],
}
};
push @{$self->{maps_counters}->{global}}, {
label => $_ . '-count', , nlabel => 'queries.' . $_ . '.count', display_ok => 0, set => {
key_values => [ { name => $_, diff => 1 } ],
output_template => $_ . ' count : %d',
perfdatas => [
{ value => $_ . '_absolute', template => '%d', min => 0 },
],
}
};
}
}
sub prefix_output {
my ($self, %options) = @_;
return "Requests ";
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1, statefile => 1);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments => {});
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
$self->{custom} = $options{custom};
$self->{global} = {};
my $server_stats = $self->{custom}->run_command(
database => 'admin',
command => $self->{custom}->ordered_hash(serverStatus => 1),
);
foreach my $querie (keys %{$server_stats->{opcounters}}) {
$self->{global}->{$querie} = $server_stats->{opcounters}->{$querie};
$self->{global}->{total} += $server_stats->{opcounters}->{$querie};
}
$self->{cache_name} = "mongodb_" . $self->{mode} . '_' . $self->{custom}->get_hostname() . '_' . $self->{custom}->get_port() . '_' .
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all'));
}
1;
__END__
=head1 MODE
Check number of queries executed (absolute and per second).
=over 8
=item B<--warning-queries-*-persecond>
Threshold warning.
Can be: 'insert', 'query', 'update',
'delete', 'getmore', 'command'
=item B<--critical-queries-*-persecond>
Threshold critical.
Can be: 'insert', 'query', 'update',
'delete', 'getmore', 'command'
=item B<--warning-queries-*-count>
Threshold warning.
Can be: 'insert', 'query', 'update',
'delete', 'getmore', 'command'
=item B<--critical-queries-*-count>
Threshold critical.
Can be: 'insert', 'query', 'update',
'delete', 'getmore', 'command'
=back
=cut

View File

@ -0,0 +1,54 @@
#
# Copyright 2019 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package database::mongodb::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}} = (
'collection-statistics' => 'database::mongodb::mode::collectionstatistics',
'connections' => 'database::mongodb::mode::connections',
'connection-time' => 'database::mongodb::mode::connectiontime',
'database-statistics' => 'database::mongodb::mode::databasestatistics',
'queries' => 'database::mongodb::mode::queries',
'list-databases' => 'database::mongodb::mode::listdatabases',
);
$self->{custom_modes}{driver} = 'database::mongodb::custom::driver';
return $self;
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check MongoDB server.
=cut