Debian Codegraph @

src/cloud/prometheus/exporters/cadvisor/mode/cpu.pm

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