| 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::cpu; |
| 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 => 3, cb_prefix_output => 'prefix_node_output', cb_long_output => 'node_long_output', |
| 33 |
message_multiple => 'All nodes usage are ok', indent_long_output => ' ', |
| 34 |
group => [ |
| 35 |
{ name => 'global_cpu', cb_prefix_output => 'prefix_global_cpu_output', type => 0, skipped_code => { -10 => 1 } }, |
| 36 |
{ name => 'cpu', display_long => 1, cb_prefix_output => 'prefix_cpu_output', |
| 37 |
message_multiple => 'All CPUs usage are ok', type => 1, skipped_code => { -10 => 1 } }, |
| 38 |
] |
| 39 |
} |
| 40 |
]; |
| 41 |
|
| 42 |
$self->{maps_counters}->{global_cpu} = [ |
| 43 |
{ label => 'node-usage', nlabel => 'node.cpu.utilization.percentage', set => { |
| 44 |
key_values => [ { name => 'node_average' } ], |
| 45 |
output_template => '%.2f %%', |
| 46 |
perfdatas => [ |
| 47 |
{ label => 'node', value => 'node_average', template => '%.2f', unit => '%', |
| 48 |
min => 0, max => 100, label_extra_instance => 1 }, |
| 49 |
], |
| 50 |
} |
| 51 |
}, |
| 52 |
]; |
| 53 |
$self->{maps_counters}->{cpu} = [ |
| 54 |
{ label => 'cpu-usage', nlabel => 'core.cpu.utilization.percentage', set => { |
| 55 |
key_values => [ { name => 'cpu_usage' }, { name => 'display' } ], |
| 56 |
output_template => 'Usage: %.2f %%', |
| 57 |
perfdatas => [ |
| 58 |
{ label => 'cpu', value => 'cpu_usage', template => '%.2f', unit => '%', |
| 59 |
min => 0, max => 100, label_extra_instance => 1 }, |
| 60 |
], |
| 61 |
} |
| 62 |
}, |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
sub prefix_node_output { |
| 67 |
my ($self, %options) = @_; |
| 68 |
|
| 69 |
return "Node '" . $options{instance_value}->{display} . "' "; |
| 70 |
} |
| 71 |
|
| 72 |
sub node_long_output { |
| 73 |
my ($self, %options) = @_; |
| 74 |
|
| 75 |
return "Checking node '" . $options{instance_value}->{display} . "'"; |
| 76 |
} |
| 77 |
|
| 78 |
sub prefix_global_cpu_output { |
| 79 |
my ($self, %options) = @_; |
| 80 |
|
| 81 |
return "CPU Average Usage: "; |
| 82 |
} |
| 83 |
|
| 84 |
sub prefix_cpu_output { |
| 85 |
my ($self, %options) = @_; |
| 86 |
|
| 87 |
return "CPU '" . $options{instance_value}->{display} . "' "; |
| 88 |
} |
| 89 |
|
| 90 |
sub new { |
| 91 |
my ($class, %options) = @_; |
| 92 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 93 |
bless $self, $class; |
| 94 |
|
| 95 |
$options{options}->add_options(arguments => { |
| 96 |
"instance:s" => { name => 'instance', default => 'instance=~".*"' }, |
| 97 |
"cpu:s" => { name => 'cpu', default => 'cpu=~".*"' }, |
| 98 |
"extra-filter:s@" => { name => 'extra_filter' }, |
| 99 |
"metric-overload:s@" => { name => 'metric_overload' }, |
| 100 |
}); |
| 101 |
|
| 102 |
return $self; |
| 103 |
} |
| 104 |
|
| 105 |
sub check_options { |
| 106 |
my ($self, %options) = @_; |
| 107 |
$self->SUPER::check_options(%options); |
| 108 |
|
| 109 |
$self->{metrics} = { |
| 110 |
'cpu' => "^node_cpu.*", |
| 111 |
}; |
| 112 |
foreach my $metric (@{$self->{option_results}->{metric_overload}}) { |
| 113 |
next if ($metric !~ /(.*),(.*)/); |
| 114 |
$self->{metrics}->{$1} = $2 if (defined($self->{metrics}->{$1})); |
| 115 |
} |
| 116 |
|
| 117 |
$self->{labels} = {}; |
| 118 |
foreach my $label (('instance', 'cpu')) { |
| 119 |
if ($self->{option_results}->{$label} !~ /^(\w+)[!~=]+\".*\"$/) { |
| 120 |
$self->{output}->add_option_msg(short_msg => "Need to specify --" . $label . " option as a PromQL filter."); |
| 121 |
$self->{output}->option_exit(); |
| 122 |
} |
| 123 |
$self->{labels}->{$label} = $1; |
| 124 |
} |
| 125 |
|
| 126 |
$self->{extra_filter} = ''; |
| 127 |
foreach my $filter (@{$self->{option_results}->{extra_filter}}) { |
| 128 |
$self->{extra_filter} .= ',' . $filter; |
| 129 |
} |
| 130 |
|
| 131 |
$self->{prom_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900; |
| 132 |
$self->{prom_step} = defined($self->{option_results}->{step}) ? $self->{option_results}->{step} : "5m"; |
| 133 |
} |
| 134 |
|
| 135 |
sub manage_selection { |
| 136 |
my ($self, %options) = @_; |
| 137 |
|
| 138 |
$self->{nodes} = {}; |
| 139 |
|
| 140 |
my $results = $options{custom}->query_range( |
| 141 |
queries => [ |
| 142 |
'(1 - irate({__name__=~"' . $self->{metrics}->{cpu} . '",' . |
| 143 |
'mode="idle",' . |
| 144 |
$self->{option_results}->{instance} . ',' . |
| 145 |
$self->{option_results}->{cpu} . |
| 146 |
$self->{extra_filter} . '}[' . $self->{prom_step} . '])) * 100' |
| 147 |
], |
| 148 |
timeframe => $self->{prom_timeframe}, step => $self->{prom_step} |
| 149 |
); |
| 150 |
|
| 151 |
foreach my $result (@{$results}) { |
| 152 |
my $average = $options{custom}->compute(aggregation => 'average', values => $result->{values}); |
| 153 |
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{display} = $result->{metric}->{$self->{labels}->{instance}}, |
| 154 |
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{global_cpu}->{node_average} += $average; |
| 155 |
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{cpu}->{$result->{metric}->{$self->{labels}->{cpu}}}->{display} = $result->{metric}->{$self->{labels}->{cpu}}; |
| 156 |
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{cpu}->{$result->{metric}->{$self->{labels}->{cpu}}}->{cpu_usage} = $average; |
| 157 |
} |
| 158 |
|
| 159 |
foreach my $node (keys %{$self->{nodes}}) { |
| 160 |
$self->{nodes}->{$node}->{global_cpu}->{node_average} /= scalar(keys %{$self->{nodes}->{$node}->{cpu}}); |
| 161 |
} |
| 162 |
|
| 163 |
if (scalar(keys %{$self->{nodes}}) <= 0) { |
| 164 |
$self->{output}->add_option_msg(short_msg => "No nodes found."); |
| 165 |
$self->{output}->option_exit(); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
1; |
| 170 |
|
| 171 |
__END__ |
| 172 |
|
| 173 |
=head1 MODE |
| 174 |
|
| 175 |
Check CPU usage for nodes and each of their cores. |
| 176 |
|
| 177 |
=over 8 |
| 178 |
|
| 179 |
=item B<--instance> |
| 180 |
|
| 181 |
Filter on a specific instance (must be a PromQL filter, Default: 'instance=~".*"') |
| 182 |
|
| 183 |
=item B<--cpu> |
| 184 |
|
| 185 |
Filter on a specific CPU (must be a PromQL filter, Default: 'cpu=~".*"') |
| 186 |
|
| 187 |
=item B<--warning-*> |
| 188 |
|
| 189 |
Warning threshold. |
| 190 |
Can be: 'node-usage', 'cpu-usage'. |
| 191 |
|
| 192 |
=item B<--critical-*> |
| 193 |
|
| 194 |
Critical threshold. |
| 195 |
Can be: 'node-usage', 'cpu-usage'. |
| 196 |
|
| 197 |
=item B<--extra-filter> |
| 198 |
|
| 199 |
Add a PromQL filter (can be defined multiple times) |
| 200 |
|
| 201 |
Example : --extra-filter='name=~".*pretty.*"' |
| 202 |
|
| 203 |
=item B<--metric-overload> |
| 204 |
|
| 205 |
Overload default metrics name (can be defined multiple times) |
| 206 |
|
| 207 |
Example : --metric-overload='metric,^my_metric_name$' |
| 208 |
|
| 209 |
Default : |
| 210 |
|
| 211 |
- cpu: ^node_cpu.* |
| 212 |
|
| 213 |
=item B<--filter-counters> |
| 214 |
|
| 215 |
Only display some counters (regexp can be used). |
| 216 |
Example: --filter-counters='node' |
| 217 |
|
| 218 |
=back |
| 219 |
|
| 220 |
=cut |