| 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::cpudetailed; |
| 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-wait', nlabel => 'node.cpu.wait.utilization.percentage', set => { |
| 44 |
key_values => [ { name => 'iowait' } ], |
| 45 |
output_template => 'Wait: %.2f %%', |
| 46 |
perfdatas => [ |
| 47 |
{ label => 'wait', value => 'iowait', template => '%.2f', |
| 48 |
min => 0, max => 100, unit => '%', label_extra_instance => 1 }, |
| 49 |
], |
| 50 |
} |
| 51 |
}, |
| 52 |
{ label => 'node-user', nlabel => 'node.cpu.user.utilization.percentage', set => { |
| 53 |
key_values => [ { name => 'user' } ], |
| 54 |
output_template => 'User: %.2f %%', |
| 55 |
perfdatas => [ |
| 56 |
{ label => 'user', value => 'user', template => '%.2f', |
| 57 |
min => 0, max => 100, unit => '%', label_extra_instance => 1 }, |
| 58 |
], |
| 59 |
} |
| 60 |
}, |
| 61 |
{ label => 'node-softirq', nlabel => 'node.cpu.softirq.utilization.percentage', set => { |
| 62 |
key_values => [ { name => 'softirq' } ], |
| 63 |
output_template => 'Soft Irq: %.2f %%', |
| 64 |
perfdatas => [ |
| 65 |
{ label => 'softirq', value => 'softirq', template => '%.2f', |
| 66 |
min => 0, max => 100, unit => '%', label_extra_instance => 1 }, |
| 67 |
], |
| 68 |
} |
| 69 |
}, |
| 70 |
{ label => 'node-interrupt', nlabel => 'node.cpu.interrupt.utilization.percentage', set => { |
| 71 |
key_values => [ { name => 'irq' } ], |
| 72 |
output_template => 'Interrupt: %.2f %%', |
| 73 |
perfdatas => [ |
| 74 |
{ label => 'interrupt', value => 'irq', template => '%.2f', |
| 75 |
min => 0, max => 100, unit => '%', label_extra_instance => 1 }, |
| 76 |
], |
| 77 |
} |
| 78 |
}, |
| 79 |
{ label => 'node-idle', nlabel => 'node.cpu.idle.utilization.percentage', set => { |
| 80 |
key_values => [ { name => 'idle' } ], |
| 81 |
output_template => 'Idle: %.2f %%', |
| 82 |
perfdatas => [ |
| 83 |
{ label => 'idle', value => 'idle', template => '%.2f', |
| 84 |
min => 0, max => 100, unit => '%', label_extra_instance => 1 }, |
| 85 |
], |
| 86 |
} |
| 87 |
}, |
| 88 |
{ label => 'node-steal', nlabel => 'node.cpu.steal.utilization.percentage', set => { |
| 89 |
key_values => [ { name => 'steal' } ], |
| 90 |
output_template => 'Steal: %.2f %%', |
| 91 |
perfdatas => [ |
| 92 |
{ label => 'steal', value => 'steal', template => '%.2f', |
| 93 |
min => 0, max => 100, unit => '%', label_extra_instance => 1 }, |
| 94 |
], |
| 95 |
} |
| 96 |
}, |
| 97 |
{ label => 'node-system', nlabel => 'node.cpu.system.utilization.percentage', set => { |
| 98 |
key_values => [ { name => 'system' } ], |
| 99 |
output_template => 'System: %.2f %%', |
| 100 |
perfdatas => [ |
| 101 |
{ label => 'system', value => 'system', template => '%.2f', |
| 102 |
min => 0, max => 100, unit => '%', label_extra_instance => 1 }, |
| 103 |
], |
| 104 |
} |
| 105 |
}, |
| 106 |
{ label => 'node-nice', nlabel => 'node.cpu.nice.utilization.percentage', set => { |
| 107 |
key_values => [ { name => 'nice' } ], |
| 108 |
output_template => 'Nice: %.2f %%', |
| 109 |
perfdatas => [ |
| 110 |
{ label => 'nice', value => 'nice', template => '%.2f', |
| 111 |
min => 0, max => 100, unit => '%', label_extra_instance => 1 }, |
| 112 |
], |
| 113 |
} |
| 114 |
}, |
| 115 |
]; |
| 116 |
$self->{maps_counters}->{cpu} = [ |
| 117 |
{ label => 'cpu-wait', nlabel => 'core.cpu.wait.utilization.percentage', set => { |
| 118 |
key_values => [ { name => 'iowait' }, { name => 'display' } ], |
| 119 |
output_template => 'Wait: %.2f %%', |
| 120 |
perfdatas => [ |
| 121 |
{ label => 'wait', value => 'iowait', template => '%.2f', |
| 122 |
min => 0, max => 100, unit => '%', label_extra_instance => 1 }, |
| 123 |
], |
| 124 |
} |
| 125 |
}, |
| 126 |
{ label => 'cpu-user', nlabel => 'core.cpu.user.utilization.percentage', set => { |
| 127 |
key_values => [ { name => 'user' }, { name => 'display' } ], |
| 128 |
output_template => 'User: %.2f %%', |
| 129 |
perfdatas => [ |
| 130 |
{ label => 'user', value => 'user', template => '%.2f', |
| 131 |
min => 0, max => 100, unit => '%', label_extra_instance => 1 }, |
| 132 |
], |
| 133 |
} |
| 134 |
}, |
| 135 |
{ label => 'cpu-softirq', nlabel => 'core.cpu.softirq.utilization.percentage', set => { |
| 136 |
key_values => [ { name => 'softirq' }, { name => 'display' } ], |
| 137 |
output_template => 'Soft Irq: %.2f %%', |
| 138 |
perfdatas => [ |
| 139 |
{ label => 'softirq', value => 'softirq', template => '%.2f', |
| 140 |
min => 0, max => 100, unit => '%', label_extra_instance => 1 }, |
| 141 |
], |
| 142 |
} |
| 143 |
}, |
| 144 |
{ label => 'cpu-interrupt', nlabel => 'core.cpu.interrupt.utilization.percentage', set => { |
| 145 |
key_values => [ { name => 'irq' }, { name => 'display' } ], |
| 146 |
output_template => 'Interrupt: %.2f %%', |
| 147 |
perfdatas => [ |
| 148 |
{ label => 'interrupt', value => 'irq', template => '%.2f', |
| 149 |
min => 0, max => 100, unit => '%', label_extra_instance => 1 }, |
| 150 |
], |
| 151 |
} |
| 152 |
}, |
| 153 |
{ label => 'cpu-idle', nlabel => 'core.cpu.idle.utilization.percentage', set => { |
| 154 |
key_values => [ { name => 'idle' }, { name => 'display' } ], |
| 155 |
output_template => 'Idle: %.2f %%', |
| 156 |
perfdatas => [ |
| 157 |
{ label => 'idle', value => 'idle', template => '%.2f', |
| 158 |
min => 0, max => 100, unit => '%', label_extra_instance => 1 }, |
| 159 |
], |
| 160 |
} |
| 161 |
}, |
| 162 |
{ label => 'cpu-steal', nlabel => 'core.cpu.steal.utilization.percentage', set => { |
| 163 |
key_values => [ { name => 'steal' }, { name => 'display' } ], |
| 164 |
output_template => 'Steal: %.2f %%', |
| 165 |
perfdatas => [ |
| 166 |
{ label => 'steal', value => 'steal', template => '%.2f', |
| 167 |
min => 0, max => 100, unit => '%', label_extra_instance => 1 }, |
| 168 |
], |
| 169 |
} |
| 170 |
}, |
| 171 |
{ label => 'cpu-system', nlabel => 'core.cpu.system.utilization.percentage', set => { |
| 172 |
key_values => [ { name => 'system' }, { name => 'display' } ], |
| 173 |
output_template => 'System: %.2f %%', |
| 174 |
perfdatas => [ |
| 175 |
{ label => 'system', value => 'system', template => '%.2f', |
| 176 |
min => 0, max => 100, unit => '%', label_extra_instance => 1 }, |
| 177 |
], |
| 178 |
} |
| 179 |
}, |
| 180 |
{ label => 'cpu-nice', nlabel => 'core.cpu.nice.utilization.percentage', set => { |
| 181 |
key_values => [ { name => 'nice' }, { name => 'display' } ], |
| 182 |
output_template => 'Nice: %.2f %%', |
| 183 |
perfdatas => [ |
| 184 |
{ label => 'nice', value => 'nice', template => '%.2f', |
| 185 |
min => 0, max => 100, unit => '%', label_extra_instance => 1 }, |
| 186 |
], |
| 187 |
} |
| 188 |
}, |
| 189 |
]; |
| 190 |
} |
| 191 |
|
| 192 |
sub prefix_node_output { |
| 193 |
my ($self, %options) = @_; |
| 194 |
|
| 195 |
return "Node '" . $options{instance_value}->{display} . "' "; |
| 196 |
} |
| 197 |
|
| 198 |
sub node_long_output { |
| 199 |
my ($self, %options) = @_; |
| 200 |
|
| 201 |
return "Checking node '" . $options{instance_value}->{display} . "'"; |
| 202 |
} |
| 203 |
|
| 204 |
sub prefix_global_cpu_output { |
| 205 |
my ($self, %options) = @_; |
| 206 |
|
| 207 |
return "CPU Average "; |
| 208 |
} |
| 209 |
|
| 210 |
sub prefix_cpu_output { |
| 211 |
my ($self, %options) = @_; |
| 212 |
|
| 213 |
return "CPU '" . $options{instance_value}->{display} . "' "; |
| 214 |
} |
| 215 |
|
| 216 |
sub new { |
| 217 |
my ($class, %options) = @_; |
| 218 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 219 |
bless $self, $class; |
| 220 |
|
| 221 |
$options{options}->add_options(arguments => { |
| 222 |
"instance:s" => { name => 'instance', default => 'instance=~".*"' }, |
| 223 |
"cpu:s" => { name => 'cpu', default => 'cpu=~".*"' }, |
| 224 |
"type:s" => { name => 'type', default => 'mode=~".*"' }, |
| 225 |
"extra-filter:s@" => { name => 'extra_filter' }, |
| 226 |
"metric-overload:s@" => { name => 'metric_overload' }, |
| 227 |
}); |
| 228 |
|
| 229 |
return $self; |
| 230 |
} |
| 231 |
|
| 232 |
sub check_options { |
| 233 |
my ($self, %options) = @_; |
| 234 |
$self->SUPER::check_options(%options); |
| 235 |
|
| 236 |
$self->{metrics} = { |
| 237 |
'cpu' => "^node_cpu_seconds_total.*", |
| 238 |
}; |
| 239 |
foreach my $metric (@{$self->{option_results}->{metric_overload}}) { |
| 240 |
next if ($metric !~ /(.*),(.*)/); |
| 241 |
$self->{metrics}->{$1} = $2 if (defined($self->{metrics}->{$1})); |
| 242 |
} |
| 243 |
|
| 244 |
$self->{labels} = {}; |
| 245 |
foreach my $label (('instance', 'cpu', 'type')) { |
| 246 |
if ($self->{option_results}->{$label} !~ /^(\w+)[!~=]+\".*\"$/) { |
| 247 |
$self->{output}->add_option_msg(short_msg => "Need to specify --" . $label . " option as a PromQL filter."); |
| 248 |
$self->{output}->option_exit(); |
| 249 |
} |
| 250 |
$self->{labels}->{$label} = $1; |
| 251 |
} |
| 252 |
|
| 253 |
$self->{extra_filter} = ''; |
| 254 |
foreach my $filter (@{$self->{option_results}->{extra_filter}}) { |
| 255 |
$self->{extra_filter} .= ',' . $filter; |
| 256 |
} |
| 257 |
|
| 258 |
$self->{prom_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900; |
| 259 |
$self->{prom_step} = defined($self->{option_results}->{step}) ? $self->{option_results}->{step} : "5m"; |
| 260 |
} |
| 261 |
|
| 262 |
sub manage_selection { |
| 263 |
my ($self, %options) = @_; |
| 264 |
|
| 265 |
$self->{nodes} = {}; |
| 266 |
|
| 267 |
my $results = $options{custom}->query_range( |
| 268 |
queries => [ |
| 269 |
'(irate({__name__=~"' . $self->{metrics}->{cpu} . '",' . |
| 270 |
$self->{option_results}->{instance} . ',' . |
| 271 |
$self->{option_results}->{cpu} . ',' . |
| 272 |
$self->{option_results}->{type} . |
| 273 |
$self->{extra_filter} . '}[' . $self->{prom_step} . '])) * 100' |
| 274 |
], |
| 275 |
timeframe => $self->{prom_timeframe}, step => $self->{prom_step} |
| 276 |
); |
| 277 |
|
| 278 |
foreach my $result (@{$results}) { |
| 279 |
my $average = $options{custom}->compute(aggregation => 'average', values => $result->{values}); |
| 280 |
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{display} = $result->{metric}->{$self->{labels}->{instance}}; |
| 281 |
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{global_cpu}->{$result->{metric}->{$self->{labels}->{type}}} += $average; |
| 282 |
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{cpu}->{$result->{metric}->{$self->{labels}->{cpu}}}->{display} = $result->{metric}->{$self->{labels}->{cpu}}; |
| 283 |
$self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{cpu}->{$result->{metric}->{$self->{labels}->{cpu}}}->{$result->{metric}->{$self->{labels}->{type}}} = $average; |
| 284 |
} |
| 285 |
|
| 286 |
foreach my $node (keys %{$self->{nodes}}) { |
| 287 |
foreach my $metric (keys %{$self->{nodes}->{$node}->{global_cpu}}) { |
| 288 |
next if ($metric =~ /cpu|display/); |
| 289 |
$self->{nodes}->{$node}->{global_cpu}->{$metric} /= scalar(keys %{$self->{nodes}->{$node}->{cpu}}); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
if (scalar(keys %{$self->{nodes}}) <= 0) { |
| 294 |
$self->{output}->add_option_msg(short_msg => "No nodes found."); |
| 295 |
$self->{output}->option_exit(); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
1; |
| 300 |
|
| 301 |
__END__ |
| 302 |
|
| 303 |
=head1 MODE |
| 304 |
|
| 305 |
Check CPU detailed usage for nodes and each of their cores. |
| 306 |
|
| 307 |
=over 8 |
| 308 |
|
| 309 |
=item B<--instance> |
| 310 |
|
| 311 |
Filter on a specific instance (must be a PromQL filter, Default: 'instance=~".*"') |
| 312 |
|
| 313 |
=item B<--cpu> |
| 314 |
|
| 315 |
Filter on a specific CPU (must be a PromQL filter, Default: 'cpu=~".*"') |
| 316 |
|
| 317 |
=item B<--type> |
| 318 |
|
| 319 |
Filter on a specific type (must be a PromQL filter, Default: 'mode=~".*"') |
| 320 |
|
| 321 |
=item B<--warning-*> |
| 322 |
|
| 323 |
Warning threshold. |
| 324 |
Can be: 'node-idle', 'node-wait', 'node-interrupt', 'node-nice', |
| 325 |
'node-softirq', 'node-steal', 'node-system', 'node-user', |
| 326 |
'cpu-idle', 'cpu-wait', 'cpu-interrupt', 'cpu-nice', 'cpu-softirq', |
| 327 |
'cpu-steal', 'cpu-system', 'cpu-user'. |
| 328 |
|
| 329 |
=item B<--critical-*> |
| 330 |
|
| 331 |
Critical threshold. |
| 332 |
Can be: 'node-idle', 'node-wait', 'node-interrupt', 'node-nice', |
| 333 |
'node-softirq', 'node-steal', 'node-system', 'node-user', |
| 334 |
'cpu-idle', 'cpu-wait', 'cpu-interrupt', 'cpu-nice', 'cpu-softirq', |
| 335 |
'cpu-steal', 'cpu-system', 'cpu-user'. |
| 336 |
|
| 337 |
=item B<--extra-filter> |
| 338 |
|
| 339 |
Add a PromQL filter (can be defined multiple times) |
| 340 |
|
| 341 |
Example : --extra-filter='name=~".*pretty.*"' |
| 342 |
|
| 343 |
=item B<--metric-overload> |
| 344 |
|
| 345 |
Overload default metrics name (can be defined multiple times) |
| 346 |
|
| 347 |
Example : --metric-overload='metric,^my_metric_name$' |
| 348 |
|
| 349 |
Default : |
| 350 |
|
| 351 |
- cpu: ^node_cpu_seconds_total.* |
| 352 |
|
| 353 |
=item B<--filter-counters> |
| 354 |
|
| 355 |
Only display some counters (regexp can be used). |
| 356 |
Example: --filter-counters='wait' |
| 357 |
|
| 358 |
=back |
| 359 |
|
| 360 |
=cut |