| 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::nodeusage; |
| 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 = "status: '" . $self->{result_values}->{status} . "'"; |
| 34 |
return $msg; |
| 35 |
} |
| 36 |
|
| 37 |
sub set_counters { |
| 38 |
my ($self, %options) = @_; |
| 39 |
|
| 40 |
$self->{maps_counters_type} = [ |
| 41 |
{ name => 'node', type => 1, cb_prefix_output => 'prefix_node_output', message_multiple => 'All nodes are ok', skipped_code => { -10 => 1 } }, |
| 42 |
]; |
| 43 |
|
| 44 |
$self->{maps_counters}->{node} = [ |
| 45 |
{ label => 'status', threshold => 0, set => { |
| 46 |
key_values => [ { name => 'status' }, { 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 => 'read', nlabel => 'node.io.read.usage.bytespersecond', set => { |
| 54 |
key_values => [ { name => 'io_read_bytes', per_second => 1 }, { name => 'display' } ], |
| 55 |
output_template => 'read i/o : %s %s/s', |
| 56 |
output_change_bytes => 1, |
| 57 |
perfdatas => [ |
| 58 |
{ template => '%d', unit => 'B/s', min => 0, label_extra_instance => 1, instance_use => 'display' } |
| 59 |
] |
| 60 |
} |
| 61 |
}, |
| 62 |
{ label => 'write', nlabel => 'node.io.write.usage.bytespersecond', set => { |
| 63 |
key_values => [ { name => 'io_write_bytes', per_second => 1 }, { name => 'display' } ], |
| 64 |
output_template => 'write i/o : %s %s/s', |
| 65 |
output_change_bytes => 1, |
| 66 |
perfdatas => [ |
| 67 |
{ template => '%d', unit => 'B/s', min => 0, label_extra_instance => 1, instance_use => 'display' } |
| 68 |
] |
| 69 |
} |
| 70 |
} |
| 71 |
]; |
| 72 |
} |
| 73 |
|
| 74 |
sub prefix_node_output { |
| 75 |
my ($self, %options) = @_; |
| 76 |
|
| 77 |
return "Node '" . $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 => '%{status} 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/nodes/?columns=name,running,io_write_bytes,io_read_bytes'); |
| 104 |
|
| 105 |
$self->{node} = {}; |
| 106 |
foreach (@$result) { |
| 107 |
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' |
| 108 |
&& $_->{name} !~ /$self->{option_results}->{filter_name}/); |
| 109 |
|
| 110 |
$self->{node}->{$_->{name}} = { |
| 111 |
display => $_->{name}, |
| 112 |
io_write_bytes => $_->{io_write_bytes}, |
| 113 |
io_read_bytes => $_->{io_read_bytes}, |
| 114 |
status => $_->{running} ? 'running' : 'notrunning', |
| 115 |
}; |
| 116 |
} |
| 117 |
|
| 118 |
if (scalar(keys %{$self->{node}}) <= 0) { |
| 119 |
$self->{output}->add_option_msg(short_msg => 'No node found'); |
| 120 |
$self->{output}->option_exit(); |
| 121 |
} |
| 122 |
|
| 123 |
$self->{cache_name} = "rabbitmq_" . $self->{mode} . '_' . $options{custom}->get_hostname() . '_' . $options{custom}->get_port() . '_' . |
| 124 |
(defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')) . '_' . |
| 125 |
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); |
| 126 |
} |
| 127 |
|
| 128 |
1; |
| 129 |
|
| 130 |
__END__ |
| 131 |
|
| 132 |
=head1 MODE |
| 133 |
|
| 134 |
Check node usage. |
| 135 |
|
| 136 |
=over 8 |
| 137 |
|
| 138 |
=item B<--filter-name> |
| 139 |
|
| 140 |
Filter node 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 "running"'). |
| 150 |
You can use the following variables: %{status}, %{display} |
| 151 |
|
| 152 |
=item B<--warning-*> B<--critical-*> |
| 153 |
|
| 154 |
Thresholds. |
| 155 |
Can be: 'read', 'write'. |
| 156 |
|
| 157 |
=back |
| 158 |
|
| 159 |
=cut |