Debian Codegraph @

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