Debian Codegraph @

src/apps/mq/rabbitmq/restapi/mode/queueusage.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::rabbitmq::restapi::mode::queueusage;
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 catalog_status_calc);
29
30 sub custom_status_output {
31 my ($self, %options) = @_;
32
33 my $msg = "state: '" . $self->{result_values}->{state} . "'";
34 return $msg;
35 }
36
37 sub set_counters {
38 my ($self, %options) = @_;
39
40 $self->{maps_counters_type} = [
41 { name => 'queue', type => 1, cb_prefix_output => 'prefix_queue_output', message_multiple => 'All queues are ok', skipped_code => { -10 => 1 } },
42 ];
43
44 $self->{maps_counters}->{queue} = [
45 { label => 'status', threshold => 0, set => {
46 key_values => [ { name => 'state' }, { name => 'display' } ],
47 closure_custom_calc => \&catalog_status_calc,
48 closure_custom_output => $self->can('custom_status_output'),
49 closure_custom_perfdata => sub { return 0; },
50 closure_custom_threshold_check => \&catalog_status_threshold,
51 }
52 },
53 { label => 'queue-msg', nlabel => 'queue.messages.count', set => {
54 key_values => [ { name => 'queue_messages' }, { name => 'display' } ],
55 output_template => 'current queue messages : %s',
56 perfdatas => [
57 { label => 'queue_msg', value => 'queue_messages', template => '%d',
58 min => 0, label_extra_instance => 1, instance_use => 'display' },
59 ],
60 }
61 },
62 { label => 'queue-msg-ready', nlabel => 'queue.messages.ready.count', set => {
63 key_values => [ { name => 'queue_messages_ready' }, { name => 'display' } ],
64 output_template => 'current queue messages ready : %s',
65 perfdatas => [
66 { label => 'queue_msg_ready', value => 'queue_messages_ready', template => '%d',
67 min => 0, label_extra_instance => 1, instance_use => 'display' },
68 ],
69 }
70 },
71 ];
72 }
73
74 sub prefix_queue_output {
75 my ($self, %options) = @_;
76
77 return "Queue '" . $options{instance_value}->{display} . "' ";
78 }
79
80 sub new {
81 my ($class, %options) = @_;
82 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
83 bless $self, $class;
84
85 $options{options}->add_options(arguments => {
86 'filter-name:s' => { name => 'filter_name' },
87 'warning-status:s' => { name => 'warning_status', default => '' },
88 'critical-status:s' => { name => 'critical_status', default => '%{state} ne "running"' },
89 });
90 return $self;
91 }
92
93 sub check_options {
94 my ($self, %options) = @_;
95 $self->SUPER::check_options(%options);
96
97 $self->change_macros(macros => ['warning_status', 'critical_status']);
98 }
99
100 sub manage_selection {
101 my ($self, %options) = @_;
102
103 my $result = $options{custom}->query(url_path => '/api/queues/?columns=vhost,name,state,messages,messages_ready');
104
105 $self->{queue} = {};
106 foreach (@$result) {
107 my $name = $_->{vhost} . ':' . $_->{name};
108 next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
109 && $_->{name} !~ /$self->{option_results}->{filter_name}/);
110
111 $self->{queue}->{$name} = {
112 display => $name,
113 queue_messages_ready => $_->{messages_ready},
114 queue_messages => $_->{messages},
115 state => $_->{state},
116 };
117 }
118
119 if (scalar(keys %{$self->{queue}}) <= 0) {
120 $self->{output}->add_option_msg(short_msg => 'No queue found');
121 $self->{output}->option_exit();
122 }
123
124 $self->{cache_name} = "rabbitmq_" . $self->{mode} . '_' . $options{custom}->get_hostname() . '_' . $options{custom}->get_port() . '_' .
125 (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')) . '_' .
126 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all'));
127 }
128
129 1;
130
131 __END__
132
133 =head1 MODE
134
135 Check queue usage.
136
137 =over 8
138
139 =item B<--filter-name>
140
141 Filter queue name (can use regexp).
142
143 =item B<--warning-status>
144
145 Define the conditions to match for the status to be WARNING (default: '').
146 You can use the following variables: %{state}, %{display}
147
148 =item B<--critical-status>
149
150 Define the conditions to match for the status to be CRITICAL (default: '%{state} ne "running"').
151 You can use the following variables: %{state}, %{display}
152
153 =item B<--warning-*> B<--critical-*>
154
155 Thresholds.
156 Can be: 'queue-msg', 'queue-msg-ready'.
157
158 =back
159
160 =cut