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