Debian Codegraph @

src/storage/hp/primera/restapi/mode/volumeusage.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 storage::hp::primera::restapi::mode::volumeusage;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27
28 sub custom_usage_output {
29 my ($self, %options) = @_;
30
31 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
32 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
33 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
34 return sprintf(
35 "Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
36 $total_size_value . " " . $total_size_unit,
37 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
38 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
39 );
40 }
41
42 sub set_counters {
43 my ($self, %options) = @_;
44
45 $self->{maps_counters_type} = [
46 { name => 'volume', type => 1, cb_prefix_output => 'prefix_volume_output', message_multiple => 'All volumes are ok' },
47 ];
48
49 $self->{maps_counters}->{volume} = [
50 { label => 'usage', nlabel => 'volume.space.usage.bytes', set => {
51 key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'name' }, { name => 'id' } ],
52 closure_custom_output => $self->can('custom_usage_output'),
53 perfdatas => [
54 { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'name' }
55 ]
56 }
57 },
58 { label => 'usage-free', display_ok => 0, nlabel => 'volume.space.free.bytes', set => {
59 key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, , { name => 'name' }, { name => 'id' } ],
60 closure_custom_output => $self->can('custom_usage_output'),
61 perfdatas => [
62 { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'name' }
63 ]
64 }
65 },
66 { label => 'usage-prct', display_ok => 0, nlabel => 'volume.space.usage.percentage', set => {
67 key_values => [ { name => 'prct_used' }, { name => 'name' }, { name => 'id' } ],
68 output_template => 'Used : %.2f %%',
69 perfdatas => [
70 { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'name' }
71 ]
72 }
73 }
74 ];
75 }
76
77 sub new {
78 my ($class, %options) = @_;
79 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
80 bless $self, $class;
81
82 $options{options}->add_options(arguments => {
83 'filter-id:s' => { name => 'filter_id' },
84 'filter-name:s' => { name => 'filter_name' }
85 });
86
87 return $self;
88 }
89
90 sub prefix_volume_output {
91 my ($self, %options) = @_;
92
93 return "Volume '" . $options{instance_value}->{name} . "' (#" . $options{instance_value}->{id} . ") ";
94 }
95
96 sub manage_selection {
97 my ($self, %options) = @_;
98
99 my $response = $options{custom}->request_api(endpoint => '/api/v1/volumes');
100 my $volumes = $response->{members};
101 $self->{volume} = {};
102
103 for my $volume (@{$volumes}) {
104 my $name = $volume->{name};
105 my $id = $volume->{id};
106
107 if (defined($self->{option_results}->{filter_name}) and $self->{option_results}->{filter_name} ne '' and
108 $name !~ /$self->{option_results}->{filter_name}/) {
109 $self->{output}->output_add(long_msg => "Skipping volume named '" . $name . "': not matching filter /" . $self->{option_results}->{filter_name} . "/.", debug => 1);
110 next;
111 }
112 if (defined($self->{option_results}->{filter_id}) and $self->{option_results}->{filter_id} ne '' and
113 $id !~ /$self->{option_results}->{filter_id}/) {
114 $self->{output}->output_add(long_msg => "Skipping volume #" . $id . ": not matching filter /" . $self->{option_results}->{filter_id} . "/.", debug => 1);
115 next;
116 }
117
118 my $total = $volume->{sizeMiB} * 1024 * 1024;
119 my $snap_used = $volume->{snapshotSpace}->{usedMiB};
120 my $adm_used = $volume->{adminSpace}->{usedMiB};
121 my $usr_used = $volume->{userSpace}->{usedMiB};
122
123 my $used = ($snap_used + $adm_used + $usr_used) * 1024 * 1024;
124 $self->{volume}->{$name} = {
125 id => $id,
126 name => $name,
127 total => $total,
128 used => $used,
129 free => ($total - $used) >= 0 ? ($total - $used) : 0,
130 prct_used => $used * 100 / $total,
131 prct_free => (100 - ($used * 100 / $total) >= 0) ? (100 - ($used * 100 / $total)) : 0
132 };
133 }
134
135 if (scalar(keys %{$self->{volume}}) <= 0) {
136 $self->{output}->add_option_msg(short_msg => "No volume found.");
137 $self->{output}->option_exit();
138 }
139 }
140
141 1;
142
143 __END__
144
145 =head1 MODE
146
147 Check volume usage.
148
149 =over 8
150
151 =item B<--filter-counters>
152
153 Define which counters (filtered by regular expression) should be monitored.
154 Example: --filter-counters='^usage$'
155
156 =item B<--filter-id>
157
158 Define which volumes should be monitored based on their IDs.
159 This option will be treated as a regular expression.
160
161 =item B<--filter-name>
162
163 Define which volumes should be monitored based on the volume names.
164 This option will be treated as a regular expression.
165
166 =item B<--warning-*> B<--critical-*>
167
168 Thresholds for volume usage metrics.
169 * may be replaced with:
170 'usage' (B), 'usage-free' (B), 'usage-prct' (%).
171
172 =back
173
174 =cut