Debian Codegraph @

src/apps/mq/activemq/jmx/mode/brokers.pm

raw
1 #
2 # Copyright 2024 Centreon (http://www.centreon.com/)
3 #
4 # Centreon is a full-fledged industry-strength solution that meets
5 # the needs in IT infrastructure and application monitoring for
6 # service performance.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20
21 package apps::mq::activemq::jmx::mode::brokers;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use Digest::MD5 qw(md5_hex);
28 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
29
30 sub custom_status_calc {
31 my ($self, %options) = @_;
32
33 $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_CurrentStatus'};
34 $self->{result_values}->{name} = $options{new_datas}->{$self->{instance} . '_name'};
35
36 return 0;
37 }
38
39 sub broker_long_output {
40 my ($self, %options) = @_;
41
42 return "checking broker '" . $options{instance} . "'";
43 }
44
45 sub prefix_broker_output {
46 my ($self, %options) = @_;
47
48 return "Broker '" . $options{instance} . "' ";
49 }
50
51 sub prefix_queue_output {
52 my ($self, %options) = @_;
53
54 return "queue '" . $options{instance} . "' ";
55 }
56
57 sub prefix_topic_output {
58 my ($self, %options) = @_;
59
60 return "topic '" . $options{instance} . "' ";
61 }
62
63 sub set_counters {
64 my ($self, %options) = @_;
65
66 $self->{maps_counters_type} = [
67 { name => 'brokers', type => 3, cb_prefix_output => 'prefix_broker_output', cb_long_output => 'broker_long_output', indent_long_output => ' ', message_multiple => 'All brokers are ok',
68 group => [
69 { name => 'global', type => 0, skipped_code => { -10 => 1 } },
70 { name => 'queue', display_long => 1, cb_prefix_output => 'prefix_queue_output', message_multiple => 'All queues are ok', type => 1, skipped_code => { -10 => 1 } },
71 { name => 'topic', display_long => 1, cb_prefix_output => 'prefix_topic_output', message_multiple => 'All topics are ok', type => 1, skipped_code => { -10 => 1 } }
72 ]
73 }
74 ];
75
76 $self->{maps_counters}->{global} = [
77 { label => 'status', type => 2, critical_default => '%{status} !~ /Good/i', set => {
78 key_values => [ { name => 'CurrentStatus' }, { name => 'name' } ],
79 closure_custom_calc => $self->can('custom_status_calc'),
80 output_template => 'status: %s',
81 output_use => 'status',
82 closure_custom_perfdata => sub { return 0; },
83 closure_custom_threshold_check => \&catalog_status_threshold_ng
84 }
85 },
86 { label => 'store-usage', nlabel => 'broker.store.usage.percentage', set => {
87 key_values => [ { name => 'StorePercentUsage' } ],
88 output_template => 'store usage: %.2f %%',
89 perfdatas => [
90 { template => '%.2f', unit => '%', min => 0, max => 100, label_extra_instance => 1 }
91 ]
92 }
93 },
94 { label => 'temporary-usage', nlabel => 'broker.temporary.usage.percentage', set => {
95 key_values => [ { name => 'TempPercentUsage' } ],
96 output_template => 'temporary usage: %.2f %%',
97 perfdatas => [
98 { template => '%.2f', unit => '%', min => 0, max => 100, label_extra_instance => 1 }
99 ]
100 }
101 },
102 { label => 'memory-usage', nlabel => 'broker.memory.usage.percentage', set => {
103 key_values => [ { name => 'MemoryPercentUsage' } ],
104 output_template => 'memory usage: %.2f %%',
105 perfdatas => [
106 { template => '%.2f', unit => '%', min => 0, max => 100, label_extra_instance => 1 }
107 ]
108 }
109 }
110 ];
111
112 foreach (('queue', 'topic')) {
113 $self->{maps_counters}->{$_} = [
114 { label => $_ . '-average-enqueue-time', nlabel => 'broker.' . $_ . '.average.enqueue.time.milliseconds', set => {
115 key_values => [ { name => 'AverageEnqueueTime' } ],
116 output_template => 'average time messages remained enqueued: %.3f ms',
117 perfdatas => [
118 { template => '%.3f', unit => 'ms', min => 0, label_extra_instance => 1 }
119 ]
120 }
121 },
122 { label => $_ . '-consumers-connected', nlabel => 'broker.' . $_ . '.consumers.connected.count', set => {
123 key_values => [ { name => 'ConsumerCount' } ],
124 output_template => 'consumers connected: %s',
125 perfdatas => [
126 { template => '%s', min => 0, label_extra_instance => 1 }
127 ]
128 }
129 },
130 { label => $_ . '-producers-connected', nlabel => 'broker.' . $_ . '.producers.connected.count', display_ok => 0, set => {
131 key_values => [ { name => 'ProducerCount' } ],
132 output_template => 'producers connected: %s',
133 perfdatas => [
134 { template => '%s', min => 0, label_extra_instance => 1 }
135 ]
136 }
137 },
138 { label => $_ . '-memory-usage', nlabel => 'broker.' . $_ . '.memory.usage.percentage', display_ok => 0, set => {
139 key_values => [ { name => 'MemoryPercentUsage' } ],
140 output_template => 'memory usage: %.2f %%',
141 perfdatas => [
142 { template => '%.2f', unit => '%', min => 0, max => 100, label_extra_instance => 1 }
143 ]
144 }
145 },
146 { label => $_ . '-size', nlabel => 'broker.' . $_ . '.size.count', set => {
147 key_values => [ { name => 'QueueSize' } ],
148 output_template => 'queue size: %s',
149 perfdatas => [
150 { template => '%s', min => 0, label_extra_instance => 1 }
151 ]
152 }
153 },
154 { label => $_ . '-messages-enqueued', nlabel => 'broker.' . $_ . '.messages.enqueued.count', display_ok => 0, set => {
155 key_values => [ { name => 'EnqueueCount', diff => 1 } ],
156 output_template => 'messages enqueued: %s',
157 perfdatas => [
158 { template => '%s', min => 0, label_extra_instance => 1 }
159 ]
160 }
161 },
162 { label => $_ . '-messages-dequeued', nlabel => 'broker.' . $_ . '.messages.dequeue.count', display_ok => 0, set => {
163 key_values => [ { name => 'DequeueCount', diff => 1 } ],
164 output_template => 'messages dequeued: %s',
165 perfdatas => [
166 { template => '%s', min => 0, label_extra_instance => 1 }
167 ]
168 }
169 },
170 { label => $_ . '-messages-expired', nlabel => 'broker.' . $_ . '.messages.expired.count', display_ok => 0, set => {
171 key_values => [ { name => 'ExpiredCount', diff => 1 } ],
172 output_template => 'messages expired: %s',
173 perfdatas => [
174 { template => '%s', min => 0, label_extra_instance => 1 }
175 ]
176 }
177 },
178 { label => $_ . '-messages-inflighted', nlabel => 'broker.' . $_ . '.messages.inflighted.count', display_ok => 0, set => {
179 key_values => [ { name => 'InFlightCount', diff => 1 } ],
180 output_template => 'messages in-flighted: %s',
181 perfdatas => [
182 { template => '%s', min => 0, label_extra_instance => 1 }
183 ]
184 }
185 },
186 { label => $_ . '-messages-size-average', nlabel => 'broker.' . $_ . '.messages.size.average.bytes', display_ok => 0, set => {
187 key_values => [ { name => 'AverageMessageSize' } ],
188 output_template => 'average messages size: %s %s',
189 output_change_bytes => 1,
190 perfdatas => [
191 { template => '%s', unit => 'B', min => 0, label_extra_instance => 1 }
192 ]
193 }
194 }
195 ];
196 }
197 }
198
199 sub new {
200 my ($class, %options) = @_;
201 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
202 bless $self, $class;
203
204 $options{options}->add_options(arguments => {
205 'filter-broker-name:s' => { name => 'filter_broker_name' },
206 'filter-destination-name:s' => { name => 'filter_destination_name' },
207 'filter-destination-type:s' => { name => 'filter_destination_type' },
208 'request:s@' => { name => 'request' }
209 });
210
211 return $self;
212 }
213
214 sub manage_selection {
215 my ($self, %options) = @_;
216
217 my $request = [
218 {
219 mbean => 'org.apache.activemq:brokerName=*,destinationName=*,destinationType=*,type=Broker',
220 attributes => [
221 { name => 'AverageEnqueueTime' }, { name => 'ConsumerCount' },
222 { name => 'ProducerCount' }, { name => 'MemoryPercentUsage' },
223 { name => 'QueueSize' }, { name => 'EnqueueCount' },
224 { name => 'DequeueCount' }, { name => 'ExpiredCount' },
225 { name => 'InFlightCount' }, { name => 'AverageMessageSize' }
226 ]
227 },
228 {
229 mbean => 'org.apache.activemq:brokerName=*,type=Broker',
230 attributes => [
231 { name => 'StorePercentUsage' }, { name => 'TempPercentUsage' },
232 { name => 'MemoryPercentUsage' }
233 ]
234 },
235 {
236 mbean => 'org.apache.activemq:brokerName=*,type=Broker,service=Health',
237 attributes => [
238 { name => 'CurrentStatus' }
239 ]
240 }
241 ];
242
243 if (defined($self->{option_results}->{request}) && $self->{option_results}->{request} ne '') {
244 centreon::plugins::misc::mymodule_load(
245 output => $self->{output}, module => 'JSON::XS',
246 error_msg => "Cannot load module 'JSON::XS'."
247 );
248 $request = undef;
249 foreach (@{$self->{option_results}->{request}}) {
250 eval {
251 push @$request, JSON::XS->new->utf8->decode($_);
252 };
253 if ($@) {
254 $self->{output}->add_option_msg(short_msg => "Cannot use request as it is a malformed JSON: " . $@);
255 $self->{output}->option_exit();
256 }
257 }
258 }
259
260 my $result = $options{custom}->get_attributes(request => $request, nothing_quit => 1);
261
262 $self->{cache_name} = 'activemq_' . $self->{mode} . '_' . md5_hex($options{custom}->get_connection_info()) . '_' .
263 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
264 (defined($self->{option_results}->{filter_broker_name}) ? md5_hex($self->{option_results}->{filter_broker_name}) : md5_hex('all')) . '_' .
265 (defined($self->{option_results}->{filter_destination_name}) ? md5_hex($self->{option_results}->{filter_destination_name}) : md5_hex('all')) . '_' .
266 (defined($self->{option_results}->{filter_destination_type}) ? md5_hex($self->{option_results}->{filter_destination_type}) : md5_hex('all'));
267
268 $self->{brokers} = {};
269 foreach my $mbean (keys %$result) {
270 my ($broker_name, $destination_name, $destination_type);
271
272 $broker_name = $1 if ($mbean =~ /brokerName=(.*?)(?:,|$)/);
273 $destination_name = $1 if ($mbean =~ /destinationName=(.*?)(?:,|$)/);
274 $destination_type = $1 if ($mbean =~ /destinationType=(.*?)(?:,|$)/);
275
276 if (defined($self->{option_results}->{filter_broker_name}) && $self->{option_results}->{filter_broker_name} ne '' &&
277 $broker_name !~ /$self->{option_results}->{filter_broker_name}/) {
278 $self->{output}->output_add(long_msg => "skipping '" . $broker_name . "': no matching filter.", debug => 1);
279 next;
280 }
281
282 if (!defined($self->{brokers}->{$broker_name})) {
283 $self->{brokers}->{$broker_name} = {
284 global => { name => $broker_name },
285 queue => {},
286 topic => {}
287 };
288 }
289
290 if (defined($destination_name)) {
291 my $type = lc($destination_type);
292 next if ($type ne 'topic' && $type ne 'queue');
293
294 if (defined($self->{option_results}->{filter_destination_name}) && $self->{option_results}->{filter_destination_name} ne '' &&
295 $destination_name !~ /$self->{option_results}->{filter_destination_name}/) {
296 $self->{output}->output_add(long_msg => "skipping '" . $destination_name . "': no matching filter.", debug => 1);
297 next;
298 }
299 if (defined($self->{option_results}->{filter_destination_type}) && $self->{option_results}->{filter_destination_type} ne '' &&
300 $destination_type !~ /$self->{option_results}->{filter_destination_type}/) {
301 $self->{output}->output_add(long_msg => "skipping '" . $broker_name . "': no matching filter.", debug => 1);
302 next;
303 }
304
305 $self->{brokers}->{$broker_name}->{$type}->{$destination_name} = $result->{$mbean};
306 } else {
307 foreach (keys %{$result->{$mbean}}) {
308 $self->{brokers}->{$broker_name}->{global}->{$_} = $result->{$mbean}->{$_};
309 }
310 }
311 }
312
313 if (scalar(keys %{$self->{brokers}}) <= 0) {
314 $self->{output}->output_add(short_msg => 'no brokers found');
315 }
316 }
317
318 1;
319
320 __END__
321
322 =head1 MODE
323
324 Check brokers.
325
326 =over 8
327
328 =item B<--filter-broker-name>
329
330 Filter broker name (can be a regexp).
331
332 =item B<--filter-destination-name>
333
334 Filter destination name (can be a regexp).
335
336 =item B<--filter-destination-type>
337
338 Filter destination type (can be a regexp).
339
340 =item B<--request>
341
342 EXPERIMENTAL Option : Community-supported only (no support from Centreon at this time)
343
344 Set the MBean and attributes to request (will replace defaults)
345 in a JSON-formatted fashion.
346
347 This is useful to reduce the size of returned data by providing destination
348 type and name or broker name instead of filtering afterwards, and grabbing
349 exactly the wanted attributes.
350
351 This can be set multiple times.
352
353 Example:
354
355 --request='{"mbean":"org.apache.activemq:brokerName=*,destinationName=MyQueue,destinationType=Queue,type=Broker","attributes":[{"name":"QueueSize"}]}'
356 --request='{"mbean":"org.apache.activemq:brokerName=*,type=Broker,service=Health","attributes":[{"name":"CurrentStatus"}]}'
357
358 =item B<--warning-status>
359
360 Define the conditions to match for the status to be WARNING.
361 You can use the following variables: %{status}, %{name}
362
363 =item B<--critical-status>
364
365 Define the conditions to match for the status to be CRITICAL (default: '%{status} !~ /Good/i').
366 You can use the following variables: %{status}, %{name}
367
368 =item B<--warning-*> B<--critical-*>
369
370 Thresholds.
371 Can be: 'store-usage' (%), 'temporary-usage' (%), 'memory-usage' (%),
372 'queue-average-enqueue-time' (ms), 'queue-consumers-connected',
373 'queue-producers-connected', 'queue-memory-usage' (%), 'queue-size',
374 'queue-messages-enqueued', 'queue-messages-dequeued', 'queue-messages-expired',
375 'queue-messages-inflighted',
376 'topic-average-enqueue-time' (ms), 'topic-consumers-connected',
377 'topic-producers-connected', 'topic-memory-usage' (%), 'topic-size',
378 'topic-messages-enqueued', 'topic-messages-dequeued', 'topic-messages-expired',
379 'topic-messages-inflighted'.
380
381 =back
382
383 =cut