| 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::cadvisor::mode::memory; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
|
| 28 |
sub custom_perfdata { |
| 29 |
my ($self) = @_; |
| 30 |
|
| 31 |
$self->{output}->perfdata_add( |
| 32 |
nlabel => $self->{nlabel}, |
| 33 |
unit => 'B', |
| 34 |
instances => [ $self->{result_values}->{pod}, $self->{result_values}->{container} ], |
| 35 |
value => sprintf('%s', $self->{result_values}->{ $self->{key_values}->[0]->{name} }), |
| 36 |
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}), |
| 37 |
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}), |
| 38 |
min => 0 |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
sub custom_usage_perfdata { |
| 43 |
my ($self, %options) = @_; |
| 44 |
|
| 45 |
my $label = $self->{result_values}->{perfdata}; |
| 46 |
my $value_perf = $self->{result_values}->{used}; |
| 47 |
|
| 48 |
my %total_options = (); |
| 49 |
if ($self->{result_values}->{total} > 0 && $self->{instance_mode}->{option_results}->{units} eq '%') { |
| 50 |
$total_options{total} = $self->{result_values}->{total}; |
| 51 |
$total_options{cast_int} = 1; |
| 52 |
} |
| 53 |
|
| 54 |
$self->{output}->perfdata_add( |
| 55 |
nlabel => 'memory.' . $label . '.bytes', |
| 56 |
unit => 'B', |
| 57 |
instances => [ $self->{result_values}->{pod}, $self->{result_values}->{container} ], |
| 58 |
value => $value_perf, |
| 59 |
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options), |
| 60 |
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options), |
| 61 |
min => 0, max => $self->{result_values}->{total} |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
sub custom_usage_threshold { |
| 66 |
my ($self, %options) = @_; |
| 67 |
|
| 68 |
return 'ok' if ($self->{result_values}->{total} <= 0 && $self->{instance_mode}->{option_results}->{units} eq '%'); |
| 69 |
my ($exit, $threshold_value); |
| 70 |
$threshold_value = $self->{result_values}->{used}; |
| 71 |
if ($self->{instance_mode}->{option_results}->{units} eq '%') { |
| 72 |
$threshold_value = $self->{result_values}->{prct_used}; |
| 73 |
} |
| 74 |
$exit = $self->{perfdata}->threshold_check( |
| 75 |
value => $threshold_value, |
| 76 |
threshold => [ |
| 77 |
{ label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, |
| 78 |
{ label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } |
| 79 |
] |
| 80 |
); |
| 81 |
return $exit; |
| 82 |
} |
| 83 |
|
| 84 |
sub custom_usage_output { |
| 85 |
my ($self, %options) = @_; |
| 86 |
|
| 87 |
my $msg; |
| 88 |
my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}); |
| 89 |
if ($self->{result_values}->{total} <= 0) { |
| 90 |
$msg = sprintf("%s: %s (unlimited)", ucfirst($self->{result_values}->{label}), $total_used_value . " " . $total_used_unit); |
| 91 |
} else { |
| 92 |
my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}); |
| 93 |
$msg = sprintf( |
| 94 |
"%s: %s (%.2f%% of %s)", |
| 95 |
ucfirst($self->{result_values}->{label}), $total_used_value . " " . $total_used_unit, |
| 96 |
$self->{result_values}->{prct_used}, |
| 97 |
$total_size_value . " " . $total_size_unit |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
return $msg; |
| 102 |
} |
| 103 |
|
| 104 |
sub custom_usage_calc { |
| 105 |
my ($self, %options) = @_; |
| 106 |
|
| 107 |
$self->{result_values}->{label} = $options{extra_options}->{label_ref}; |
| 108 |
$self->{result_values}->{perfdata} = $options{extra_options}->{perfdata_ref}; |
| 109 |
$self->{result_values}->{container} = $options{new_datas}->{$self->{instance} . '_container'}; |
| 110 |
$self->{result_values}->{pod} = $options{new_datas}->{$self->{instance} . '_pod'}; |
| 111 |
$self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_limits'}; |
| 112 |
$self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_' . $self->{result_values}->{label}}; |
| 113 |
return 0 if ($self->{result_values}->{total} == 0); |
| 114 |
|
| 115 |
$self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total}; |
| 116 |
|
| 117 |
return 0; |
| 118 |
} |
| 119 |
|
| 120 |
sub prefix_containers_output { |
| 121 |
my ($self, %options) = @_; |
| 122 |
|
| 123 |
return "Container '" . $options{instance_value}->{container} . "' [pod: " . $options{instance_value}->{pod} . "] Memory "; |
| 124 |
} |
| 125 |
|
| 126 |
sub set_counters { |
| 127 |
my ($self, %options) = @_; |
| 128 |
|
| 129 |
$self->{maps_counters_type} = [ |
| 130 |
{ name => 'containers', type => 1, cb_prefix_output => 'prefix_containers_output', |
| 131 |
message_multiple => 'All containers memory usage are ok' } |
| 132 |
]; |
| 133 |
|
| 134 |
$self->{maps_counters}->{containers} = [ |
| 135 |
{ label => 'usage', set => { |
| 136 |
key_values => [ { name => 'limits' }, { name => 'usage' }, { name => 'container' }, { name => 'pod' } ], |
| 137 |
closure_custom_calc => $self->can('custom_usage_calc'), |
| 138 |
closure_custom_calc_extra_options => { label_ref => 'usage', perfdata_ref => 'used' }, |
| 139 |
closure_custom_output => $self->can('custom_usage_output'), |
| 140 |
closure_custom_perfdata => $self->can('custom_usage_perfdata'), |
| 141 |
closure_custom_threshold_check => $self->can('custom_usage_threshold') |
| 142 |
} |
| 143 |
}, |
| 144 |
{ label => 'working', set => { |
| 145 |
key_values => [ { name => 'limits' }, { name => 'working' }, { name => 'container' }, { name => 'pod' } ], |
| 146 |
closure_custom_calc => $self->can('custom_usage_calc'), |
| 147 |
closure_custom_calc_extra_options => { label_ref => 'working', perfdata_ref => 'working' }, |
| 148 |
closure_custom_output => $self->can('custom_usage_output'), |
| 149 |
closure_custom_perfdata => $self->can('custom_usage_perfdata'), |
| 150 |
closure_custom_threshold_check => $self->can('custom_usage_threshold') |
| 151 |
} |
| 152 |
}, |
| 153 |
{ label => 'cache', nlabel => 'cache.usage.bytes', set => { |
| 154 |
key_values => [ { name => 'cache' }, { name => 'container' }, { name => 'pod' } ], |
| 155 |
output_template => 'Cache: %.2f %s', |
| 156 |
output_change_bytes => 1, |
| 157 |
closure_custom_perfdata => $self->can('custom_perfdata') |
| 158 |
} |
| 159 |
}, |
| 160 |
{ label => 'rss', nlabel => 'rss.usage.bytes', set => { |
| 161 |
key_values => [ { name => 'rss' }, { name => 'container' }, { name => 'pod' } ], |
| 162 |
output_template => 'Rss: %.2f %s', |
| 163 |
output_change_bytes => 1, |
| 164 |
closure_custom_perfdata => $self->can('custom_perfdata') |
| 165 |
} |
| 166 |
}, |
| 167 |
{ label => 'swap', nlabel => 'swap.usage.bytes', set => { |
| 168 |
key_values => [ { name => 'swap' }, { name => 'container' }, { name => 'pod' } ], |
| 169 |
output_template => 'Swap: %.2f %s', |
| 170 |
output_change_bytes => 1, |
| 171 |
closure_custom_perfdata => $self->can('custom_perfdata') |
| 172 |
} |
| 173 |
} |
| 174 |
]; |
| 175 |
} |
| 176 |
|
| 177 |
sub new { |
| 178 |
my ($class, %options) = @_; |
| 179 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 180 |
bless $self, $class; |
| 181 |
|
| 182 |
$options{options}->add_options(arguments => { |
| 183 |
"container:s" => { name => 'container', default => 'container_name!~".*POD.*"' }, |
| 184 |
"pod:s" => { name => 'pod', default => 'pod_name=~".*"' }, |
| 185 |
"extra-filter:s@" => { name => 'extra_filter' }, |
| 186 |
"units:s" => { name => 'units', default => '%' }, |
| 187 |
"metric-overload:s@" => { name => 'metric_overload' } |
| 188 |
}); |
| 189 |
|
| 190 |
return $self; |
| 191 |
} |
| 192 |
|
| 193 |
sub check_options { |
| 194 |
my ($self, %options) = @_; |
| 195 |
$self->SUPER::check_options(%options); |
| 196 |
|
| 197 |
$self->{metrics} = { |
| 198 |
'limits' => "^container_spec_memory_limit_bytes.*", |
| 199 |
'usage' => "^container_memory_usage_bytes.*", |
| 200 |
'working' => "^container_memory_working_set_bytes.*", |
| 201 |
'cache' => "^container_memory_cache.*", |
| 202 |
'rss' => "^container_memory_rss.*", |
| 203 |
'swap' => "^container_memory_swap.*", |
| 204 |
}; |
| 205 |
foreach my $metric (@{$self->{option_results}->{metric_overload}}) { |
| 206 |
next if ($metric !~ /(.*),(.*)/); |
| 207 |
$self->{metrics}->{$1} = $2 if (defined($self->{metrics}->{$1})); |
| 208 |
} |
| 209 |
|
| 210 |
$self->{labels} = {}; |
| 211 |
foreach my $label (('container', 'pod')) { |
| 212 |
if ($self->{option_results}->{$label} !~ /^(\w+)[!~=]+\".*\"$/) { |
| 213 |
$self->{output}->add_option_msg(short_msg => "Need to specify --" . $label . " option as a PromQL filter."); |
| 214 |
$self->{output}->option_exit(); |
| 215 |
} |
| 216 |
$self->{labels}->{$label} = $1; |
| 217 |
} |
| 218 |
|
| 219 |
$self->{extra_filter} = ''; |
| 220 |
foreach my $filter (@{$self->{option_results}->{extra_filter}}) { |
| 221 |
$self->{extra_filter} .= ',' . $filter; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
sub manage_selection { |
| 226 |
my ($self, %options) = @_; |
| 227 |
|
| 228 |
my $results = $options{custom}->query( |
| 229 |
queries => [ |
| 230 |
'label_replace({__name__=~"' . $self->{metrics}->{usage} . '",' . |
| 231 |
$self->{option_results}->{container} . ',' . |
| 232 |
$self->{option_results}->{pod} . |
| 233 |
$self->{extra_filter} . '}, "__name__", "usage", "", "")', |
| 234 |
'label_replace({__name__=~"' . $self->{metrics}->{limits} . '",' . |
| 235 |
$self->{option_results}->{container} . ',' . |
| 236 |
$self->{option_results}->{pod} . |
| 237 |
$self->{extra_filter} . '}, "__name__", "limits", "", "")', |
| 238 |
'label_replace({__name__=~"' . $self->{metrics}->{working} . '",' . |
| 239 |
$self->{option_results}->{container} . ',' . |
| 240 |
$self->{option_results}->{pod} . |
| 241 |
$self->{extra_filter} . '}, "__name__", "working", "", "")', |
| 242 |
'label_replace({__name__=~"' . $self->{metrics}->{cache} . '",' . |
| 243 |
$self->{option_results}->{container} . ',' . |
| 244 |
$self->{option_results}->{pod} . |
| 245 |
$self->{extra_filter} . '}, "__name__", "cache", "", "")', |
| 246 |
'label_replace({__name__=~"' . $self->{metrics}->{rss} . '",' . |
| 247 |
$self->{option_results}->{container} . ',' . |
| 248 |
$self->{option_results}->{pod} . |
| 249 |
$self->{extra_filter} . '}, "__name__", "rss", "", "")', |
| 250 |
'label_replace({__name__=~"' . $self->{metrics}->{swap} . '",' . |
| 251 |
$self->{option_results}->{container} . ',' . |
| 252 |
$self->{option_results}->{pod} . |
| 253 |
$self->{extra_filter} . '}, "__name__", "swap", "", "")' |
| 254 |
] |
| 255 |
); |
| 256 |
|
| 257 |
$self->{containers} = {}; |
| 258 |
foreach my $result (@{$results}) { |
| 259 |
next if (!defined($result->{metric}->{$self->{labels}->{pod}}) || !defined($result->{metric}->{$self->{labels}->{container}})); |
| 260 |
$self->{containers}->{$result->{metric}->{$self->{labels}->{pod}} . '_' . $result->{metric}->{$self->{labels}->{container}}}->{container} = $result->{metric}->{$self->{labels}->{container}}; |
| 261 |
$self->{containers}->{$result->{metric}->{$self->{labels}->{pod}} . '_' . $result->{metric}->{$self->{labels}->{container}}}->{pod} = $result->{metric}->{$self->{labels}->{pod}}; |
| 262 |
$self->{containers}->{$result->{metric}->{$self->{labels}->{pod}} . '_' . $result->{metric}->{$self->{labels}->{container}}}->{$result->{metric}->{__name__}} = ${$result->{value}}[1]; |
| 263 |
} |
| 264 |
|
| 265 |
if (scalar(keys %{$self->{containers}}) <= 0) { |
| 266 |
$self->{output}->add_option_msg(short_msg => "No containers found."); |
| 267 |
$self->{output}->option_exit(); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
1; |
| 272 |
|
| 273 |
__END__ |
| 274 |
|
| 275 |
=head1 MODE |
| 276 |
|
| 277 |
Check containers memory usage. |
| 278 |
|
| 279 |
=over 8 |
| 280 |
|
| 281 |
=item B<--container> |
| 282 |
|
| 283 |
Filter on a specific container (must be a PromQL filter, Default: 'container_name!~".*POD.*"') |
| 284 |
|
| 285 |
=item B<--pod> |
| 286 |
|
| 287 |
Filter on a specific pod (must be a PromQL filter, Default: 'pod_name=~".*"') |
| 288 |
|
| 289 |
=item B<--warning-*> |
| 290 |
|
| 291 |
Warning threshold. |
| 292 |
Can be: 'usage', 'working', 'cache' (B), 'rss' (B), 'swap' (B). |
| 293 |
|
| 294 |
=item B<--critical-*> |
| 295 |
|
| 296 |
Critical threshold. |
| 297 |
Can be: 'usage', 'working', 'cache' (B), 'rss' (B), 'swap' (B). |
| 298 |
|
| 299 |
=item B<--units> |
| 300 |
|
| 301 |
Units of thresholds (default: '%') ('%', 'B'). |
| 302 |
|
| 303 |
=item B<--extra-filter> |
| 304 |
|
| 305 |
Add a PromQL filter (can be defined multiple times) |
| 306 |
|
| 307 |
Example : --extra-filter='name=~".*pretty.*"' |
| 308 |
|
| 309 |
=item B<--metric-overload> |
| 310 |
|
| 311 |
Overload default metrics name (can be defined multiple times) |
| 312 |
|
| 313 |
Example : --metric-overload='metric,^my_metric_name$' |
| 314 |
|
| 315 |
Default : |
| 316 |
|
| 317 |
- limits: ^container_spec_memory_limit_bytes.* |
| 318 |
- usage: ^container_memory_usage_bytes.* |
| 319 |
- working: ^container_memory_working_set_bytes.* |
| 320 |
- cache: ^container_memory_cache.* |
| 321 |
- rss: ^container_memory_rss.* |
| 322 |
- swap: ^container_memory_swap.* |
| 323 |
|
| 324 |
=item B<--filter-counters> |
| 325 |
|
| 326 |
Only display some counters (regexp can be used). |
| 327 |
Example: --filter-counters='usage' |
| 328 |
|
| 329 |
=back |
| 330 |
|
| 331 |
=cut |