| 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 cloud::prometheus::exporters::nodeexporter::mode::load; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
|
| 28 |
sub set_counters { |
| 29 |
my ($self, %options) = @_; |
| 30 |
|
| 31 |
$self->{maps_counters_type} = [ |
| 32 |
{ name => 'nodes', type => 1, cb_prefix_output => 'prefix_nodes_output', message_multiple => 'All nodes load are ok' }, |
| 33 |
]; |
| 34 |
|
| 35 |
$self->{maps_counters}->{nodes} = [ |
| 36 |
{ label => 'load1', nlabel => 'load.1minute.count', set => { |
| 37 |
key_values => [ { name => 'load1' }, { name => 'display' } ], |
| 38 |
output_template => 'Load 1 minute: %.2f', |
| 39 |
output_change_bytes => 1, |
| 40 |
perfdatas => [ |
| 41 |
{ label => 'load1', value => 'load1', template => '%.2f', |
| 42 |
min => 0, label_extra_instance => 1, instance_use => 'display' }, |
| 43 |
], |
| 44 |
} |
| 45 |
}, |
| 46 |
{ label => 'load5', nlabel => 'load.5minutes.count', set => { |
| 47 |
key_values => [ { name => 'load5' }, { name => 'display' } ], |
| 48 |
output_template => 'Load 5 minutes: %.2f', |
| 49 |
output_change_bytes => 1, |
| 50 |
perfdatas => [ |
| 51 |
{ label => 'load5', value => 'load5', template => '%.2f', |
| 52 |
min => 0, label_extra_instance => 1, instance_use => 'display' }, |
| 53 |
], |
| 54 |
} |
| 55 |
}, |
| 56 |
{ label => 'load15', nlabel => 'load.15minutes.count', set => { |
| 57 |
key_values => [ { name => 'load15' }, { name => 'display' } ], |
| 58 |
output_template => 'Load 15 minutes: %.2f', |
| 59 |
output_change_bytes => 1, |
| 60 |
perfdatas => [ |
| 61 |
{ label => 'load15', value => 'load15', template => '%.2f', |
| 62 |
min => 0, label_extra_instance => 1, instance_use => 'display' }, |
| 63 |
], |
| 64 |
} |
| 65 |
}, |
| 66 |
]; |
| 67 |
} |
| 68 |
|
| 69 |
sub prefix_nodes_output { |
| 70 |
my ($self, %options) = @_; |
| 71 |
|
| 72 |
return "Node '" . $options{instance_value}->{display} . "' "; |
| 73 |
} |
| 74 |
|
| 75 |
sub new { |
| 76 |
my ($class, %options) = @_; |
| 77 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 78 |
bless $self, $class; |
| 79 |
|
| 80 |
$options{options}->add_options(arguments => { |
| 81 |
"instance:s" => { name => 'instance', default => 'instance=~".*"' }, |
| 82 |
"extra-filter:s@" => { name => 'extra_filter' }, |
| 83 |
"metric-overload:s@" => { name => 'metric_overload' }, |
| 84 |
}); |
| 85 |
|
| 86 |
return $self; |
| 87 |
} |
| 88 |
|
| 89 |
sub check_options { |
| 90 |
my ($self, %options) = @_; |
| 91 |
$self->SUPER::check_options(%options); |
| 92 |
|
| 93 |
$self->{metrics} = { |
| 94 |
'load1' => '^node_load1$', |
| 95 |
'load5' => '^node_load5$', |
| 96 |
'load15' => '^node_load15$', |
| 97 |
}; |
| 98 |
foreach my $metric (@{$self->{option_results}->{metric_overload}}) { |
| 99 |
next if ($metric !~ /(.*),(.*)/); |
| 100 |
$self->{metrics}->{$1} = $2 if (defined($self->{metrics}->{$1})); |
| 101 |
} |
| 102 |
|
| 103 |
$self->{labels} = {}; |
| 104 |
foreach my $label (('instance')) { |
| 105 |
if ($self->{option_results}->{$label} !~ /^(\w+)[!~=]+\".*\"$/) { |
| 106 |
$self->{output}->add_option_msg(short_msg => "Need to specify --" . $label . " option as a PromQL filter."); |
| 107 |
$self->{output}->option_exit(); |
| 108 |
} |
| 109 |
$self->{labels}->{$label} = $1; |
| 110 |
} |
| 111 |
|
| 112 |
$self->{extra_filter} = ''; |
| 113 |
foreach my $filter (@{$self->{option_results}->{extra_filter}}) { |
| 114 |
$self->{extra_filter} .= ',' . $filter; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
sub manage_selection { |
| 119 |
my ($self, %options) = @_; |
| 120 |
|
| 121 |
$self->{nodes} = {}; |
| 122 |
|
| 123 |
my $results = $options{custom}->query( |
| 124 |
queries => [ |
| 125 |
'label_replace({__name__=~"' . $self->{metrics}->{load1} . '",' . |
| 126 |
$self->{option_results}->{instance} . |
| 127 |
$self->{extra_filter} . '}, "__name__", "load1", "", "")', |
| 128 |
'label_replace({__name__=~"' . $self->{metrics}->{load5} . '",' . |
| 129 |
$self->{option_results}->{instance} . |
| 130 |
$self->{extra_filter} . '}, "__name__", "load5", "", "")', |
| 131 |
'label_replace({__name__=~"' . $self->{metrics}->{load15} . '",' . |
| 132 |
$self->{option_results}->{instance} . |
| 133 |
$self->{extra_filter} . '}, "__name__", "load15", "", "")' |
| 134 |
] |
| 135 |
); |
| 136 |
|
| 137 |
foreach my $result (@{$results}) { |
| 138 |
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{display} = $result->{metric}->{$self->{labels}->{instance}}; |
| 139 |
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{$result->{metric}->{__name__}} = ${$result->{value}}[1]; |
| 140 |
} |
| 141 |
|
| 142 |
if (scalar(keys %{$self->{nodes}}) <= 0) { |
| 143 |
$self->{output}->add_option_msg(short_msg => "No nodes found."); |
| 144 |
$self->{output}->option_exit(); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
1; |
| 149 |
|
| 150 |
__END__ |
| 151 |
|
| 152 |
=head1 MODE |
| 153 |
|
| 154 |
Check nodes load. |
| 155 |
|
| 156 |
=over 8 |
| 157 |
|
| 158 |
=item B<--instance> |
| 159 |
|
| 160 |
Filter on a specific instance (must be a PromQL filter, Default: 'instance=~".*"') |
| 161 |
|
| 162 |
=item B<--warning-*> |
| 163 |
|
| 164 |
Warning threshold. |
| 165 |
Can be: 'load1', 'load5', 'load15'. |
| 166 |
|
| 167 |
=item B<--critical-*> |
| 168 |
|
| 169 |
Critical threshold. |
| 170 |
Can be: 'load1', 'load5', 'load15'. |
| 171 |
|
| 172 |
=item B<--extra-filter> |
| 173 |
|
| 174 |
Add a PromQL filter (can be defined multiple times) |
| 175 |
|
| 176 |
Example : --extra-filter='name=~".*pretty.*"' |
| 177 |
|
| 178 |
=item B<--metric-overload> |
| 179 |
|
| 180 |
Overload default metrics name (can be defined multiple times) |
| 181 |
|
| 182 |
Example : --metric-overload='metric,^my_metric_name$' |
| 183 |
|
| 184 |
Default : |
| 185 |
|
| 186 |
- load1: ^node_load1$ |
| 187 |
- load5: ^node_load5$ |
| 188 |
- load15: ^node_load15$ |
| 189 |
|
| 190 |
=item B<--filter-counters> |
| 191 |
|
| 192 |
Only display some counters (regexp can be used). |
| 193 |
Example: --filter-counters='load15' |
| 194 |
|
| 195 |
=back |
| 196 |
|
| 197 |
=cut |