Debian Codegraph @

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