Debian Codegraph @

src/apps/proxmox/ve/restapi/mode/storageusage.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::storageusage;
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_storage_perfdata {
37 my ($self, %options) = @_;
38
39 $self->{output}->perfdata_add(
40 nlabel => 'storage.space.usage.bytes',
41 unit => 'B',
42 instances => $self->{result_values}->{display},
43 value => $self->{result_values}->{used},
44 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
45 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
46 min => 0, max => $self->{result_values}->{total}
47 );
48 }
49
50 sub custom_storage_threshold {
51 my ($self, %options) = @_;
52
53 my $exit = $self->{perfdata}->threshold_check(
54 value => $self->{result_values}->{prct_used},
55 threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]
56 );
57 return $exit;
58 }
59
60 sub custom_storage_output {
61 my ($self, %options) = @_;
62
63 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
64 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
65 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
66
67 return sprintf(
68 "space total: %s used: %s (%.2f%%) free: %s (%.2f%%)",
69 $total_size_value . " " . $total_size_unit,
70 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
71 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
72 );
73 }
74
75 sub custom_storage_calc {
76 my ($self, %options) = @_;
77
78 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
79 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_storage_total'};
80 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_storage_usage'};
81 $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
82
83 return -10 if ($self->{result_values}->{total} <= 0);
84
85 $self->{result_values}->{prct_free} = $self->{result_values}->{free} * 100 / $self->{result_values}->{total};
86 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
87
88 return 0;
89 }
90
91 sub prefix_storages_output {
92 my ($self, %options) = @_;
93
94 return "Storage '" . $options{instance_value}->{display} . "' ";
95 }
96
97 sub set_counters {
98 my ($self, %options) = @_;
99
100 $self->{maps_counters_type} = [
101 { name => 'storages', type => 1, cb_prefix_output => 'prefix_storages_output',
102 message_multiple => 'All storages are ok', skipped_code => { -10 => 1, -11 => 1 } }
103 ];
104
105 $self->{maps_counters}->{storages} = [
106 { label => 'storage-status', type => 2, set => {
107 key_values => [ { name => 'state' }, { name => 'name' } ],
108 closure_custom_output => $self->can('custom_status_output'),
109 closure_custom_perfdata => sub { return 0; },
110 closure_custom_threshold_check => \&catalog_status_threshold_ng
111 }
112 },
113 { label => 'storage', set => {
114 key_values => [ { name => 'storage_usage' }, { name => 'storage_total' }, { name => 'display' } ],
115 closure_custom_calc => $self->can('custom_storage_calc'),
116 closure_custom_output => $self->can('custom_storage_output'),
117 closure_custom_perfdata => $self->can('custom_storage_perfdata'),
118 closure_custom_threshold_check => $self->can('custom_storage_threshold')
119 }
120 }
121 ];
122 }
123
124 sub new {
125 my ($class, %options) = @_;
126 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
127 bless $self, $class;
128
129 $options{options}->add_options(arguments => {
130 'storage-id:s' => { name => 'storage_id' },
131 'storage-name:s' => { name => 'storage_name' },
132 'filter-name:s' => { name => 'filter_name' },
133 'use-name' => { name => 'use_name' },
134 'node-id:s' => { name => 'node_id' },
135 'node-name:s' => { name => 'node_name' }
136 });
137
138 $self->{statefile_cache_storages} = centreon::plugins::statefile->new(%options);
139 return $self;
140 }
141
142 sub check_options {
143 my ($self, %options) = @_;
144 $self->SUPER::check_options(%options);
145
146 $self->{statefile_cache_storages}->check_options(%options);
147 }
148
149 sub manage_selection {
150 my ($self, %options) = @_;
151
152 my $result = $options{custom}->api_get_storages(
153 storage_id => $self->{option_results}->{storage_id},
154 storage_name => $self->{option_results}->{storage_name},
155 node_id => $self->{option_results}->{node_id},
156 node_name => $self->{option_results}->{node_name},
157 statefile => $self->{statefile_cache_storages}
158 );
159
160 $self->{storages} = {};
161 foreach my $storage_id (keys %{$result}) {
162 next if (!defined($result->{$storage_id}->{Stats}));
163
164 my $name = $result->{$storage_id}->{Name};
165 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
166 $name !~ /$self->{option_results}->{filter_name}/) {
167 $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1);
168 next;
169 }
170
171 $self->{storages}->{$storage_id} = {
172 display => defined($self->{option_results}->{use_name}) ? $name : $storage_id,
173 name => $name,
174 state => $result->{$storage_id}->{State},
175 storage_usage => $result->{$storage_id}->{Stats}->{used},
176 storage_total => $result->{$storage_id}->{Stats}->{total}
177 };
178 }
179
180 if (scalar(keys %{$self->{storages}}) <= 0) {
181 $self->{output}->add_option_msg(short_msg => "No storage found.");
182 $self->{output}->option_exit();
183 }
184
185 my $hostnames = $options{custom}->get_hostnames();
186 $self->{cache_name} = 'proxmox_' . $self->{mode} . '_' . $hostnames . '_' . $options{custom}->get_port() . '_' .
187 md5_hex(
188 (defined($self->{option_results}->{filter_counters}) ? $self->{option_results}->{filter_counters} : '') . '_' .
189 (defined($self->{option_results}->{filter_name}) ? $self->{option_results}->{filter_name} : '') . '_' .
190 (defined($self->{option_results}->{storage_id}) ? $self->{option_results}->{storage_id} : '') . '_' .
191 (defined($self->{option_results}->{storage_name}) ? $self->{option_results}->{storage_name} : '') . '_' .
192 (defined($self->{option_results}->{node_id}) ? $self->{option_results}->{node_id} : '') . '_' .
193 (defined($self->{option_results}->{node_name}) ? $self->{option_results}->{node_name} : '')
194 );
195
196 }
197
198 1;
199
200 __END__
201
202 =head1 MODE
203
204 Check storage usage.
205
206 =over 8
207
208 =item B<--filter-counters>
209
210 Only display some counters (regexp can be used).
211 Example: --filter-counters='^storage-status$'
212
213 =item B<--storage-id>
214
215 Exact storage ID.
216
217 =item B<--storage-name>
218
219 Exact storage name (if multiple names: names separated by ':').
220
221 =item B<--use-name>
222
223 Use storage name for perfdata and display.
224
225 =item B<--filter-name>
226
227 Filter by storage name (can be a regexp).
228
229 =item B<--node-id>
230
231 Exact node ID.
232
233 =item B<--node-name>
234
235 Exact node name.
236
237 =item B<--warning-storage-status>
238
239 Define the conditions to match for the status to be WARNING.
240 You can use the following variables: %{name}, %{state}.
241
242 =item B<--critical-storage-status>
243
244 Define the conditions to match for the status to be CRITICAL.
245 You can use the following variables: %{name}, %{state}.
246
247 =item B<--warning-*> B<--critical-*>
248
249 Thresholds.
250 Can be: 'storage' (%).
251
252 =back
253
254 =cut