Debian Codegraph @

src/apps/proxmox/ve/restapi/mode/nodeusage.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 apps::proxmox::ve::restapi::mode::nodeusage;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use Digest::MD5 qw(md5_hex);
28 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
29
30 sub custom_status_output {
31 my ($self, %options) = @_;
32
33 return 'state: ' . $self->{result_values}->{state};
34 }
35
36 sub custom_cpu_calc {
37 my ($self, %options) = @_;
38
39 my $delta_cpu_total = $options{new_datas}->{$self->{instance} . '_cpu_total_usage'} - $options{old_datas}->{$self->{instance} . '_cpu_total_usage'};
40 $self->{result_values}->{prct_cpu} = (($delta_cpu_total) * 100);
41 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
42
43 return 0;
44 }
45
46 sub custom_memory_perfdata {
47 my ($self, %options) = @_;
48
49 $self->{output}->perfdata_add(
50 nlabel => 'node.memory.usage.bytes',
51 unit => 'B',
52 instances => $self->{result_values}->{display},
53 value => $self->{result_values}->{used},
54 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
55 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
56 min => 0, max => $self->{result_values}->{total}
57 );
58 }
59
60 sub custom_memory_threshold {
61 my ($self, %options) = @_;
62
63 my $exit = $self->{perfdata}->threshold_check(
64 value => $self->{result_values}->{prct_used},
65 threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]
66 );
67 return $exit;
68 }
69
70 sub custom_memory_output {
71 my ($self, %options) = @_;
72
73 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
74 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
75 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
76
77 return sprintf(
78 'memory total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
79 $total_size_value . " " . $total_size_unit,
80 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
81 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
82 );
83 }
84
85 sub custom_memory_calc {
86 my ($self, %options) = @_;
87
88 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
89 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_memory_total'};
90 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_memory_usage'};
91 $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
92 $self->{result_values}->{prct_free} = $self->{result_values}->{free} * 100 / $self->{result_values}->{total};
93 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
94 return 0;
95 }
96
97 sub custom_swap_perfdata {
98 my ($self, %options) = @_;
99
100 $self->{output}->perfdata_add(
101 nlabel => 'node.swap.usage.bytes',
102 unit => 'B',
103 instances => $self->{result_values}->{display},
104 value => $self->{result_values}->{used},
105 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
106 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
107 min => 0, max => $self->{result_values}->{total}
108 );
109 }
110
111 sub custom_swap_threshold {
112 my ($self, %options) = @_;
113
114 my $exit = $self->{perfdata}->threshold_check(
115 value => $self->{result_values}->{prct_used},
116 threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]
117 );
118 return $exit;
119 }
120
121 sub custom_swap_output {
122 my ($self, %options) = @_;
123
124 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
125 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
126 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
127
128 return sprintf(
129 'swap total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
130 $total_size_value . " " . $total_size_unit,
131 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
132 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
133 );
134 }
135
136 sub custom_swap_calc {
137 my ($self, %options) = @_;
138
139 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
140 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_swap_total'};
141 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_swap_usage'};
142 $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
143 $self->{result_values}->{prct_free} = $self->{result_values}->{free} * 100 / $self->{result_values}->{total};
144 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
145 return 0;
146 }
147
148 sub custom_fs_perfdata {
149 my ($self, %options) = @_;
150
151 $self->{output}->perfdata_add(
152 nlabel => 'node.filesystem.usage.bytes',
153 unit => 'B',
154 instances => $self->{result_values}->{display},
155 value => $self->{result_values}->{used},
156 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
157 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
158 min => 0, max => $self->{result_values}->{total}
159 );
160 }
161
162 sub custom_fs_threshold {
163 my ($self, %options) = @_;
164
165 my $exit = $self->{perfdata}->threshold_check(
166 value => $self->{result_values}->{prct_used},
167 threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]
168 );
169 return $exit;
170 }
171
172 sub custom_fs_output {
173 my ($self, %options) = @_;
174
175 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
176 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
177 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
178
179 return sprintf(
180 'root filesystem total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
181 $total_size_value . " " . $total_size_unit,
182 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
183 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
184 );
185 }
186
187 sub custom_fs_calc {
188 my ($self, %options) = @_;
189
190 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
191 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_fs_total'};
192 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_fs_usage'};
193 $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
194 $self->{result_values}->{prct_free} = $self->{result_values}->{free} * 100 / $self->{result_values}->{total};
195 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
196 return 0;
197 }
198
199 sub prefix_nodes_output {
200 my ($self, %options) = @_;
201
202 return "Node '" . $options{instance_value}->{display} . "' ";
203 }
204
205 sub set_counters {
206 my ($self, %options) = @_;
207
208 $self->{maps_counters_type} = [
209 { name => 'nodes', type => 1, cb_prefix_output => 'prefix_nodes_output', message_multiple => 'All nodes are ok', skipped_code => { -10 => 1, -11 => 1 } }
210 ];
211
212 $self->{maps_counters}->{nodes} = [
213 { label => 'node-status', type => 2, set => {
214 key_values => [ { name => 'state' }, { name => 'name' } ],
215 closure_custom_output => $self->can('custom_status_output'),
216 closure_custom_perfdata => sub { return 0; },
217 closure_custom_threshold_check => \&catalog_status_threshold_ng
218 }
219 },
220 { label => 'cpu', nlabel => 'node.cpu.utilization.percentage', set => {
221 key_values => [ { name => 'cpu_total_usage', diff => 1 }, { name => 'cpu_number' }, { name => 'display' } ],
222 output_template => 'cpu usage: %.2f %%',
223 closure_custom_calc => $self->can('custom_cpu_calc'),
224 output_use => 'prct_cpu', threshold_use => 'prct_cpu',
225 perfdatas => [
226 { value => 'prct_cpu', template => '%.2f',
227 unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' }
228 ]
229 }
230 },
231 { label => 'memory', set => {
232 key_values => [ { name => 'memory_usage' }, { name => 'memory_total' }, { name => 'display' } ],
233 closure_custom_calc => $self->can('custom_memory_calc'),
234 closure_custom_output => $self->can('custom_memory_output'),
235 closure_custom_perfdata => $self->can('custom_memory_perfdata'),
236 closure_custom_threshold_check => $self->can('custom_memory_threshold')
237 }
238 },
239 { label => 'fs', set => {
240 key_values => [ { name => 'fs_usage' }, { name => 'fs_total' }, { name => 'display' } ],
241 closure_custom_calc => $self->can('custom_fs_calc'),
242 closure_custom_output => $self->can('custom_fs_output'),
243 closure_custom_perfdata => $self->can('custom_fs_perfdata'),
244 closure_custom_threshold_check => $self->can('custom_fs_threshold')
245 }
246 },
247 { label => 'swap', set => {
248 key_values => [ { name => 'swap_usage' }, { name => 'swap_total' }, { name => 'display' } ],
249 closure_custom_calc => $self->can('custom_swap_calc'),
250 closure_custom_output => $self->can('custom_swap_output'),
251 closure_custom_perfdata => $self->can('custom_swap_perfdata'),
252 closure_custom_threshold_check => $self->can('custom_swap_threshold')
253 }
254 }
255 ];
256 }
257
258 sub new {
259 my ($class, %options) = @_;
260 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
261 bless $self, $class;
262
263 $options{options}->add_options(arguments => {
264 'node-id:s' => { name => 'node_id' },
265 'node-name:s' => { name => 'node_name' },
266 'filter-name:s' => { name => 'filter_name' },
267 'use-name' => { name => 'use_name' }
268 });
269
270 $self->{statefile_cache_nodes} = centreon::plugins::statefile->new(%options);
271 return $self;
272 }
273
274 sub check_options {
275 my ($self, %options) = @_;
276 $self->SUPER::check_options(%options);
277
278 $self->{statefile_cache_nodes}->check_options(%options);
279 }
280
281 sub manage_selection {
282 my ($self, %options) = @_;
283
284 $self->{nodes} = {};
285
286 my $result = $options{custom}->api_get_nodes(
287 node_id => $self->{option_results}->{node_id},
288 node_name => $self->{option_results}->{node_name},
289 statefile => $self->{statefile_cache_nodes}
290 );
291
292 foreach my $node_id (keys %{$result}) {
293 next if (!defined($result->{$node_id}->{Stats}));
294
295 my $name = $result->{$node_id}->{Name};
296 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
297 $name !~ /$self->{option_results}->{filter_name}/) {
298 $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1);
299 next;
300 }
301
302 $self->{nodes}->{$node_id} = {
303 display => defined($self->{option_results}->{use_name}) ? $name : $node_id,
304 name => $name,
305 state => $result->{$node_id}->{State},
306 cpu_total_usage => $result->{$node_id}->{Stats}->{cpu},
307 cpu_number => $result->{$node_id}->{Stats}->{cpuinfo}->{cpus},
308 memory_usage => $result->{$node_id}->{Stats}->{memory}->{used},
309 memory_total => $result->{$node_id}->{Stats}->{memory}->{total},
310 swap_usage => $result->{$node_id}->{Stats}->{swap}->{used},
311 swap_total =>
312 defined($result->{$node_id}->{Stats}->{swap}->{total}) && $result->{$node_id}->{Stats}->{swap}->{total} > 0 ? $result->{$node_id}->{Stats}->{swap}->{total} : undef,
313 fs_usage => $result->{$node_id}->{Stats}->{rootfs}->{used},
314 fs_total => $result->{$node_id}->{Stats}->{rootfs}->{total}
315 };
316 }
317
318 if (scalar(keys %{$self->{nodes}}) <= 0) {
319 $self->{output}->add_option_msg(short_msg => "No node found.");
320 $self->{output}->option_exit();
321 }
322
323 my $hostnames = $options{custom}->get_hostnames();
324 $self->{cache_name} = 'proxmox_' . $self->{mode} . '_' .$hostnames . '_' . $options{custom}->get_port() . '_' .
325 md5_hex(
326 (defined($self->{option_results}->{filter_counters}) ? $self->{option_results}->{filter_counters} : '') . '_' .
327 (defined($self->{option_results}->{filter_name}) ? $self->{option_results}->{filter_name} : '') . '_' .
328 (defined($self->{option_results}->{node_id}) ? $self->{option_results}->{node_id} : '') . '_' .
329 (defined($self->{option_results}->{node_name}) ? $self->{option_results}->{node_name} : '')
330 );
331 }
332
333 1;
334
335 __END__
336
337 =head1 MODE
338
339 Check node usage.
340
341 =over 8
342
343 =item B<--node-id>
344
345 Exact node ID.
346
347 =item B<--node-name>
348
349 Exact node name (if multiple names: names separated by ':').
350
351 =item B<--use-name>
352
353 Use node name for perfdata and display.
354
355 =item B<--filter-name>
356
357 Filter by node name (can be a regexp).
358
359 =item B<--filter-counters>
360
361 Only display some counters (regexp can be used).
362 Example: --filter-counters='^node-status$'
363
364 =item B<--warning-*> B<--critical-*>
365
366 Thresholds.
367 Can be: 'cpu' (%), 'memory' (%), 'swap' (%), 'fs' (%).
368
369 =item B<--warning-node-status>
370
371 Define the conditions to match for the status to be WARNING.
372 You can use the following variables: %{name}, %{state}.
373
374 =item B<--critical-node-status>
375
376 Define the conditions to match for the status to be CRITICAL.
377 You can use the following variables: %{name}, %{state}.
378
379 =back
380
381 =cut