Skip to content

Commit c22ee05

Browse files
(plugin) database::mongodb - add shards option (centreon#4535)
1 parent e365d5e commit c22ee05

8 files changed

Lines changed: 417 additions & 281 deletions

File tree

src/database/mongodb/custom/driver.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ sub check_options {
8181
$self->{password} = (defined($self->{option_results}->{password})) ? $self->{option_results}->{password} : '';
8282
$self->{no_ssl} = (defined($self->{option_results}->{no_ssl})) ? 1 : 0;
8383

84-
if (!defined($self->{hostname}) || $self->{hostname} eq '') {
84+
if ($self->{hostname} eq '') {
8585
$self->{output}->add_option_msg(short_msg => "Need to specify --hostname option.");
8686
$self->{output}->option_exit();
8787
}
@@ -227,7 +227,7 @@ Set timeout in seconds (Default: 10).
227227
228228
=item B<--ssl-opt>
229229
230-
Set SSL Options (--ssl-opt="SSL_version => TLSv1" --ssl-opt="SSL_verify_mode => SSL_VERIFY_NONE").
230+
Set SSL Options (--ssl-opt="SSL_version => 'TLSv1'" --ssl-opt="SSL_verify_mode => SSL_VERIFY_NONE").
231231
232232
=item B<--no-ssl>
233233

src/database/mongodb/mode/collectionstatistics.pm

Lines changed: 120 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,42 @@ use base qw(centreon::plugins::templates::counter);
2525
use strict;
2626
use warnings;
2727

28+
sub custom_shard_perfdata {
29+
my ($self, %options) = @_;
30+
31+
$self->{output}->perfdata_add(
32+
nlabel => $self->{nlabel},
33+
unit => $self->{instance_mode}->{option_results}->{unit},
34+
instances => [$self->{result_values}->{dbName}, $self->{result_values}->{collectionName}, $self->{result_values}->{shardName}],
35+
value => $self->{result_values}->{ $self->{key_values}->[0]->{name} },
36+
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
37+
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
38+
min => 0
39+
);
40+
}
41+
42+
sub long_output {
43+
my ($self, %options) = @_;
44+
45+
return "checking database '" . $options{instance_value}->{display} . "' ";
46+
}
47+
48+
sub prefix_output_collection {
49+
my ($self, %options) = @_;
50+
51+
return "collection '" . $options{instance} . "' ";
52+
}
53+
54+
sub prefix_output_shard {
55+
my ($self, %options) = @_;
56+
57+
return sprintf(
58+
"collection '%s' shard '%s' ",
59+
$options{instance_value}->{collectionName},
60+
$options{instance_value}->{shardName}
61+
);
62+
}
63+
2864
sub set_counters {
2965
my ($self, %options) = @_;
3066

@@ -34,62 +70,77 @@ sub set_counters {
3470
group => [
3571
{ name => 'collections', display_long => 1, cb_prefix_output => 'prefix_output_collection',
3672
message_multiple => 'All collections statistics are ok', type => 1 },
73+
{ name => 'shards', display_long => 1, cb_prefix_output => 'prefix_output_shard',
74+
message_multiple => 'All shards collections statistics are ok', type => 1 }
3775
]
3876
}
3977
];
4078

4179
$self->{maps_counters}->{collections} = [
4280
{ label => 'storage-size', nlabel => 'collection.size.storage.bytes', set => {
43-
key_values => [ { name => 'storageSize' }, { name => 'display' } ],
44-
output_template => 'Storage Size: %s %s',
81+
key_values => [ { name => 'storageSize' } ],
82+
output_template => 'storage size: %s %s',
4583
output_change_bytes => 1,
4684
perfdatas => [
47-
{ value => 'storageSize', template => '%s',
48-
min => 0, unit => 'B', label_extra_instance => 1 },
49-
],
85+
{ template => '%s', min => 0, unit => 'B', label_extra_instance => 1 }
86+
]
5087
}
5188
},
5289
{ label => 'index-size', nlabel => 'collection.size.index.bytes', set => {
53-
key_values => [ { name => 'totalIndexSize' }, { name => 'display' } ],
54-
output_template => 'Index Size: %s %s',
90+
key_values => [ { name => 'totalIndexSize' } ],
91+
output_template => 'index size: %s %s',
5592
output_change_bytes => 1,
5693
perfdatas => [
57-
{ value => 'totalIndexSize', template => '%s',
58-
min => 0, unit => 'B', label_extra_instance => 1 },
59-
],
94+
{ template => '%s', min => 0, unit => 'B', label_extra_instance => 1 }
95+
]
6096
}
6197
},
6298
{ label => 'documents', nlabel => 'collection.documents.count', set => {
63-
key_values => [ { name => 'count' }, { name => 'display' } ],
64-
output_template => 'Documents: %s',
99+
key_values => [ { name => 'count' } ],
100+
output_template => 'documents: %s',
65101
perfdatas => [
66-
{ value => 'count', template => '%s',
67-
min => 0, label_extra_instance => 1 },
68-
],
102+
{ template => '%s', min => 0, label_extra_instance => 1 }
103+
]
69104
}
70105
},
71106
{ label => 'indexes', nlabel => 'collection.indexes.count', set => {
72-
key_values => [ { name => 'nindexes' }, { name => 'display' } ],
73-
output_template => 'Indexes: %s',
107+
key_values => [ { name => 'nindexes' } ],
108+
output_template => 'indexes: %s',
74109
perfdatas => [
75-
{ value => 'nindexes', template => '%s',
76-
min => 0, label_extra_instance => 1 },
77-
],
110+
{ template => '%s', min => 0, label_extra_instance => 1 }
111+
]
78112
}
79-
},
113+
}
80114
];
81-
}
82115

83-
sub long_output {
84-
my ($self, %options) = @_;
85-
86-
return "Checking database '" . $options{instance_value}->{display} . "' ";
87-
}
88-
89-
sub prefix_output_collection {
90-
my ($self, %options) = @_;
91-
92-
return "Collection '" . $options{instance_value}->{display} . "' ";
116+
$self->{maps_counters}->{shards} = [
117+
{ label => 'shard-storage-size', nlabel => 'collection.size.storage.bytes', set => {
118+
key_values => [ { name => 'storageSize' }, { name => 'dbName' }, { name => 'collectionName' }, { name => 'shardName' } ],
119+
output_template => 'storage size: %s %s',
120+
output_change_bytes => 1,
121+
closure_custom_perfdata => $self->can('custom_shard_perfdata')
122+
}
123+
},
124+
{ label => 'shard-index-size', nlabel => 'collection.size.index.bytes', set => {
125+
key_values => [ { name => 'totalIndexSize' }, { name => 'dbName' }, { name => 'collectionName' }, { name => 'shardName' } ],
126+
output_template => 'index size: %s %s',
127+
output_change_bytes => 1,
128+
closure_custom_perfdata => $self->can('custom_shard_perfdata')
129+
}
130+
},
131+
{ label => 'shard-documents', nlabel => 'collection.documents.count', set => {
132+
key_values => [ { name => 'count' }, { name => 'dbName' }, { name => 'collectionName' }, { name => 'shardName' } ],
133+
output_template => 'documents: %s',
134+
closure_custom_perfdata => $self->can('custom_shard_perfdata')
135+
}
136+
},
137+
{ label => 'shard-indexes', nlabel => 'collection.indexes.count', set => {
138+
key_values => [ { name => 'nindexes' }, { name => 'dbName' }, { name => 'collectionName' }, { name => 'shardName' } ],
139+
output_template => 'indexes: %s',
140+
closure_custom_perfdata => $self->can('custom_shard_perfdata')
141+
}
142+
}
143+
];
93144
}
94145

95146
sub new {
@@ -98,7 +149,9 @@ sub new {
98149
bless $self, $class;
99150

100151
$options{options}->add_options(arguments => {
101-
"filter-database:s" => { name => 'filter_database' },
152+
'filter-database:s' => { name => 'filter_database' },
153+
'filter-shard:s' => { name => 'filter_shard' },
154+
'add-shards' => { name => 'add_shards' }
102155
});
103156
return $self;
104157
}
@@ -115,21 +168,41 @@ sub manage_selection {
115168

116169
my $collections = $options{custom}->list_collections(database => $database);
117170

118-
$self->{databases}->{$database}->{display} = $database;
171+
$self->{databases}->{$database} = {
172+
display => $database,
173+
collections => {},
174+
shards => {}
175+
};
119176

120177
foreach my $collection (sort @{$collections}) {
121178
my $cl_stats = $options{custom}->run_command(
122179
database => $database,
123-
command => $options{custom}->ordered_hash(collStats => $collection),
180+
command => $options{custom}->ordered_hash(collStats => $collection)
124181
);
125-
182+
126183
$self->{databases}->{$database}->{collections}->{$collection} = {
127-
display => $collection,
128184
storageSize => $cl_stats->{storageSize},
129185
totalIndexSize => $cl_stats->{totalIndexSize},
130186
count => $cl_stats->{count},
131-
nindexes => $cl_stats->{nindexes},
187+
nindexes => $cl_stats->{nindexes}
132188
};
189+
190+
if (defined($self->{option_results}->{add_shards}) && defined($cl_stats->{shards})) {
191+
foreach my $shard_name (keys %{$cl_stats->{shards}}) {
192+
next if (defined($self->{option_results}->{filter_shard}) && $self->{option_results}->{filter_shard} ne ''
193+
&& $shard_name !~ /$self->{option_results}->{filter_shard}/);
194+
195+
$self->{databases}->{$database}->{shards}->{$collection . $shard_name} = {
196+
dbName => $database,
197+
collectionName => $collection,
198+
shardName => $shard_name,
199+
storageSize => $cl_stats->{shards}->{$shard_name}->{storageSize},
200+
totalIndexSize => $cl_stats->{shards}->{$shard_name}->{totalIndexSize},
201+
count => $cl_stats->{shards}->{$shard_name}->{count},
202+
nindexes => $cl_stats->{shards}->{$shard_name}->{nindexes}
203+
};
204+
}
205+
}
133206
}
134207
}
135208

@@ -151,27 +224,21 @@ Check collections statistics per databases
151224
152225
=item B<--filter-database>
153226
154-
Filter database name (Can use regexp).
155-
156-
=item B<--warning-subinstance-collection-size-*-bytes>
157-
158-
Warning threshold.
159-
Can be: 'storage', 'index'.
227+
Filter databases by name (Can use regexp).
160228
161-
=item B<--critical-subinstance-collection-size-*-bytes>
229+
=item B<--filter-shard>
162230
163-
Critical threshold.
164-
Can be: 'storage', 'index'.
231+
Filter shards by name (Can use regexp).
165232
166-
=item B<--warning-subinstance-collection-*-count>
233+
=item B<--add-shards>
167234
168-
Warning threshold.
169-
Can be: 'documents', 'indexes'.
235+
Add collection statistics by shards.
170236
171-
=item B<--critical-subinstance-collection-*-count>
237+
=item B<--warning-*> B<--critical-*>
172238
173-
Critical threshold.
174-
Can be: 'documents', 'indexes'.
239+
Thresholds.
240+
Can be: 'storage-size', 'index-size', 'documents', 'indexes',
241+
'shard-storage-size', 'shard-index-size', 'shard-documents', 'shard-indexes'.
175242
176243
=back
177244

src/database/mongodb/mode/connections.pm

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -26,55 +26,55 @@ use strict;
2626
use warnings;
2727
use Digest::MD5 qw(md5_hex);
2828

29+
sub prefix_output {
30+
my ($self, %options) = @_;
31+
32+
return 'Connections ';
33+
}
34+
2935
sub set_counters {
3036
my ($self, %options) = @_;
3137

3238
$self->{maps_counters_type} = [
33-
{ name => 'global', type => 0, cb_prefix_output => 'prefix_output' },
39+
{ name => 'global', type => 0, cb_prefix_output => 'prefix_output' }
3440
];
3541

3642
$self->{maps_counters}->{global} = [
3743
{ label => 'active', nlabel => 'connections.active.count', set => {
3844
key_values => [ { name => 'active' } ],
39-
output_template => 'Active: %d',
45+
output_template => 'active: %d',
4046
perfdatas => [
41-
{ template => '%d', min => 0, unit => 'conn' },
42-
],
47+
{ template => '%d', min => 0 }
48+
]
4349
}
4450
},
4551
{ label => 'current', nlabel => 'connections.current.count', set => {
4652
key_values => [ { name => 'current' } ],
47-
output_template => 'Current: %d',
53+
output_template => 'current: %d',
4854
perfdatas => [
49-
{ template => '%d', min => 0, unit => 'conn' },
50-
],
55+
{ template => '%d', min => 0 }
56+
]
5157
}
5258
},
5359
{ label => 'usage', nlabel => 'connections.usage.percentage', set => {
5460
key_values => [ { name => 'usage' } ],
55-
output_template => 'Usage: %.2f %%',
61+
output_template => 'usage: %.2f %%',
5662
perfdatas => [
57-
{ template => '%.2f', min => 0, max => 100, unit => '%' },
58-
],
63+
{ template => '%.2f', min => 0, max => 100, unit => '%' }
64+
]
5965
}
6066
},
6167
{ label => 'total-created', nlabel => 'connections.created.persecond', set => {
6268
key_values => [ { name => 'totalCreated', per_second => 1 } ],
63-
output_template => 'Created: %.2f/s',
69+
output_template => 'created: %.2f/s',
6470
perfdatas => [
65-
{ template => '%.2f', min => 0, unit => 'conn/s' },
66-
],
71+
{ template => '%.2f', min => 0 }
72+
]
6773
}
68-
},
74+
}
6975
];
7076
}
7177

72-
sub prefix_output {
73-
my ($self, %options) = @_;
74-
75-
return "Connections ";
76-
}
77-
7878
sub new {
7979
my ($class, %options) = @_;
8080
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1, statefile => 1);
@@ -99,8 +99,8 @@ sub manage_selection {
9999
$self->{global}->{current} = $server_stats->{connections}->{current};
100100
$self->{global}->{usage} = $server_stats->{connections}->{current} / ($server_stats->{connections}->{current} + $server_stats->{connections}->{available});
101101
$self->{global}->{totalCreated} = $server_stats->{connections}->{totalCreated};
102-
103-
$self->{cache_name} = "mongodb_" . $self->{mode} . '_' . $options{custom}->get_hostname() . '_' . $options{custom}->get_port() . '_' .
102+
103+
$self->{cache_name} = 'mongodb_' . $self->{mode} . '_' . $options{custom}->get_hostname() . '_' . $options{custom}->get_port() . '_' .
104104
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all'));
105105
}
106106

@@ -110,35 +110,14 @@ __END__
110110
111111
=head1 MODE
112112
113-
Check connections statistics
113+
Check connections statistics.
114114
115115
=over 8
116116
117-
=item B<--warning-connections-*-count>
118-
119-
Warning threshold.
120-
Can be: 'active', 'current'.
121-
122-
=item B<--critical-connections-*-count>
123-
124-
Critical threshold.
125-
Can be: 'active', 'current'.
126-
127-
=item B<--warning-connections-usage-percentage>
128-
129-
Warning threshold for connections usage (current over available)
130-
131-
=item B<--critical-connections-usage-percentage>
132-
133-
Critical threshold for connections usage (current over available)
134-
135-
=item B<--warning-connections-created-persecond>
136-
137-
Warning threshold for connections created per second.
138-
139-
=item B<--critical-connections-created-persecond>
117+
=item B<--warning-*> B<--critical-*>
140118
141-
Critical threshold for connections created per second.
119+
Thresholds.
120+
Can be: 'active', 'current', 'usage', 'total-created'.
142121
143122
=back
144123

0 commit comments

Comments
 (0)