Debian Codegraph @

src/cloud/prometheus/exporters/cadvisor/mode/taskstate.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::taskstate;
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 tasks states are ok', skipped_code => { -10 => 1 } },
34 ];
35
36 $self->{maps_counters}->{containers} = [
37 { label => 'sleeping', nlabel => 'tasks.sleeping.count', set => {
38 key_values => [ { name => 'sleeping' }, { name => 'container' }, { name => 'pod' }, { name => 'perf' } ],
39 output_template => 'Sleeping: %d',
40 output_change_bytes => 1,
41 perfdatas => [
42 { label => 'tasks_sleeping', value => 'sleeping', template => '%d',
43 min => 0, label_extra_instance => 1, instance_use => 'perf' },
44 ],
45 }
46 },
47 { label => 'running', nlabel => 'tasks.running.count', set => {
48 key_values => [ { name => 'running' }, { name => 'container' }, { name => 'pod' }, { name => 'perf' } ],
49 output_template => 'Running: %d',
50 output_change_bytes => 1,
51 perfdatas => [
52 { label => 'tasks_running', value => 'running', template => '%d',
53 min => 0, label_extra_instance => 1, instance_use => 'perf' },
54 ],
55 }
56 },
57 { label => 'stopped', nlabel => 'tasks.stopped.count', set => {
58 key_values => [ { name => 'stopped' }, { name => 'container' }, { name => 'pod' }, { name => 'perf' } ],
59 output_template => 'Stopped: %d',
60 output_change_bytes => 1,
61 perfdatas => [
62 { label => 'tasks_stopped', value => 'stopped', template => '%d',
63 min => 0, label_extra_instance => 1, instance_use => 'perf' },
64 ],
65 }
66 },
67 { label => 'uninterruptible', nlabel => 'tasks.uninterruptible.count', set => {
68 key_values => [ { name => 'uninterruptible' }, { name => 'container' }, { name => 'pod' }, { name => 'perf' } ],
69 output_template => 'Uninterruptible: %d',
70 output_change_bytes => 1,
71 perfdatas => [
72 { label => 'tasks_uninterruptible', value => 'uninterruptible', template => '%d',
73 min => 0, label_extra_instance => 1, instance_use => 'perf' },
74 ],
75 }
76 },
77 { label => 'iowaiting', nlabel => 'tasks.iowaiting.count', set => {
78 key_values => [ { name => 'iowaiting' }, { name => 'container' }, { name => 'pod' }, { name => 'perf' } ],
79 output_template => 'Iowaiting: %d',
80 output_change_bytes => 1,
81 perfdatas => [
82 { label => 'tasks_iowaiting', value => 'iowaiting', template => '%d',
83 min => 0, label_extra_instance => 1, instance_use => 'perf' },
84 ],
85 }
86 },
87 ];
88 }
89
90 sub prefix_containers_output {
91 my ($self, %options) = @_;
92
93 return "Container '" . $options{instance_value}->{container} . "' [pod: " . $options{instance_value}->{pod} . "] Tasks ";
94 }
95
96 sub new {
97 my ($class, %options) = @_;
98 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
99 bless $self, $class;
100
101 $options{options}->add_options(arguments => {
102 "container:s" => { name => 'container', default => 'container_name!~".*POD.*"' },
103 "pod:s" => { name => 'pod', default => 'pod_name=~".*"' },
104 "state:s" => { name => 'state', default => 'state=~".*"' },
105 "extra-filter:s@" => { name => 'extra_filter' },
106 "metric-overload:s@" => { name => 'metric_overload' },
107 });
108
109 return $self;
110 }
111
112 sub check_options {
113 my ($self, %options) = @_;
114 $self->SUPER::check_options(%options);
115
116 $self->{metrics} = {
117 'tasks_state' => '^container_tasks_state$',
118 };
119 foreach my $metric (@{$self->{option_results}->{metric_overload}}) {
120 next if ($metric !~ /(.*),(.*)/);
121 $self->{metrics}->{$1} = $2 if (defined($self->{metrics}->{$1}));
122 }
123
124 $self->{labels} = {};
125 foreach my $label (('container', 'pod', 'state')) {
126 if ($self->{option_results}->{$label} !~ /^(\w+)[!~=]+\".*\"$/) {
127 $self->{output}->add_option_msg(short_msg => "Need to specify --" . $label . " option as a PromQL filter.");
128 $self->{output}->option_exit();
129 }
130 $self->{labels}->{$label} = $1;
131 }
132
133 $self->{extra_filter} = '';
134 foreach my $filter (@{$self->{option_results}->{extra_filter}}) {
135 $self->{extra_filter} .= ',' . $filter;
136 }
137 }
138
139 sub manage_selection {
140 my ($self, %options) = @_;
141
142 $self->{containers} = {};
143
144 my $results = $options{custom}->query(
145 queries => [
146 'label_replace({__name__=~"' . $self->{metrics}->{tasks_state} . '",' .
147 $self->{option_results}->{container} . ',' .
148 $self->{option_results}->{pod} . ',' .
149 $self->{option_results}->{state} .
150 $self->{extra_filter} . '}, "__name__", "tasks_state", "", "")'
151 ]
152 );
153
154 foreach my $result (@{$results}) {
155 next if (!defined($result->{metric}->{$self->{labels}->{pod}}) || !defined($result->{metric}->{$self->{labels}->{container}}));
156 $self->{containers}->{$result->{metric}->{$self->{labels}->{pod}} . "_" . $result->{metric}->{$self->{labels}->{container}}}->{container} = $result->{metric}->{$self->{labels}->{container}};
157 $self->{containers}->{$result->{metric}->{$self->{labels}->{pod}} . "_" . $result->{metric}->{$self->{labels}->{container}}}->{pod} = $result->{metric}->{$self->{labels}->{pod}};
158 $self->{containers}->{$result->{metric}->{$self->{labels}->{pod}} . "_" . $result->{metric}->{$self->{labels}->{container}}}->{perf} = $result->{metric}->{$self->{labels}->{pod}} . "_" . $result->{metric}->{$self->{labels}->{container}};
159 $self->{containers}->{$result->{metric}->{$self->{labels}->{pod}} . "_" . $result->{metric}->{$self->{labels}->{container}}}->{$result->{metric}->{$self->{labels}->{state}}} = ${$result->{value}}[1];
160 }
161
162 if (scalar(keys %{$self->{containers}}) <= 0) {
163 $self->{output}->add_option_msg(short_msg => "No containers found.");
164 $self->{output}->option_exit();
165 }
166 }
167
168 1;
169
170 __END__
171
172 =head1 MODE
173
174 Check containers number of tasks in given state.
175
176 =over 8
177
178 =item B<--container>
179
180 Filter on a specific container (must be a PromQL filter, Default: 'container_name!~".*POD.*"')
181
182 =item B<--pod>
183
184 Filter on a specific pod (must be a PromQL filter, Default: 'pod_name=~".*"')
185
186 =item B<--state>
187
188 Filter on a specific state (must be a PromQL filter, Default: 'state=~".*"')
189
190 =item B<--warning-*>
191
192 Warning threshold.
193 Can be: 'sleeping', 'running', 'stopped', 'uninterruptible', 'iowaiting'.
194
195 =item B<--critical-*>
196
197 Critical threshold.
198 Can be: 'sleeping', 'running', 'stopped', 'uninterruptible', 'iowaiting'.
199
200 =item B<--extra-filter>
201
202 Add a PromQL filter (can be defined multiple times)
203
204 Example : --extra-filter='name=~".*pretty.*"'
205
206 =item B<--metric-overload>
207
208 Overload default metrics name (can be defined multiple times)
209
210 Example : --metric-overload='metric,^my_metric_name$'
211
212 Default :
213
214 - tasks_state: ^container_tasks_state$
215
216 =back
217
218 =cut