@@ -25,6 +25,42 @@ use base qw(centreon::plugins::templates::counter);
2525use strict;
2626use 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+
2864sub 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
95146sub 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
0 commit comments