Debian Codegraph @

src/storage/hitachi/hcp/snmp/mode/tenants.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::hitachi::hcp::snmp::mode::tenants;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27
28 sub custom_space_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 "space 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 custom_space_usage_prct_threshold {
43 my ($self, %options) = @_;
44
45 my $threshold = $self->{instance_mode}->{option_results}->{'warning-instance-tenant-space-usage-percentage'};
46 $threshold = $self->{result_values}->{soft_quota} if (!defined($threshold) || $threshold eq '');
47 $self->{perfdata}->threshold_validate(
48 label => 'warning-' . $self->{thlabel} . '-' . $self->{result_values}->{instance},
49 value => $threshold
50 );
51
52 return $self->{perfdata}->threshold_check(
53 value => $self->{result_values}->{prct_used},
54 threshold => [
55 { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
56 { label => 'warning-'. $self->{thlabel} . '-' . $self->{result_values}->{instance}, exit_litteral => 'warning' }
57 ]
58 );
59 }
60
61 sub custom_space_usage_prct_perfdata {
62 my ($self, %options) = @_;
63
64 $self->{output}->perfdata_add(
65 nlabel => $self->{nlabel},
66 instances => $self->{result_values}->{name},
67 value => sprintf('%.2f', $self->{result_values}->{prct_used}),
68 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel} . '-' . $self->{result_values}->{instance}),
69 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
70 unit => '%',
71 min => 0,
72 max => 100
73 );
74 }
75
76 sub tenant_long_output {
77 my ($self, %options) = @_;
78
79 return sprintf(
80 "checking tenant '%s'",
81 $options{instance_value}->{name}
82 );
83 }
84
85 sub prefix_tenant_output {
86 my ($self, %options) = @_;
87
88 return sprintf(
89 "tenant '%s' ",
90 $options{instance_value}->{name}
91 );
92 }
93
94 sub set_counters {
95 my ($self, %options) = @_;
96
97 $self->{maps_counters_type} = [
98 { name => 'tenants', type => 3, cb_prefix_output => 'prefix_tenant_output', cb_long_output => 'tenant_long_output',
99 indent_long_output => ' ', message_multiple => 'All tenants are ok',
100 group => [
101 { name => 'space', type => 0, skipped_code => { -10 => 1 } }
102 ]
103 }
104 ];
105
106 $self->{maps_counters}->{space} = [
107 { label => 'space-usage', nlabel => 'tenant.space.usage.bytes', set => {
108 key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
109 closure_custom_output => $self->can('custom_space_usage_output'),
110 perfdatas => [
111 { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1 }
112 ]
113 }
114 },
115 { label => 'space-usage-free', display_ok => 0, nlabel => 'tenant.space.free.bytes', set => {
116 key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
117 closure_custom_output => $self->can('custom_space_usage_output'),
118 perfdatas => [
119 { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1 }
120 ]
121 }
122 },
123 { label => 'space-usage-prct', display_ok => 0, nlabel => 'tenant.space.usage.percentage', set => {
124 key_values => [
125 { name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'prct_free' }, { name => 'total' },
126 { name => 'soft_quota' }, { name => 'instance' }, { name => 'name' }
127 ],
128 closure_custom_output => $self->can('custom_space_usage_output'),
129 closure_custom_threshold_check => $self->can('custom_space_usage_prct_threshold'),
130 closure_custom_perfdata => $self->can('custom_space_usage_prct_perfdata')
131 }
132 }
133 ];
134 }
135
136 sub new {
137 my ($class, %options) = @_;
138 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
139 bless $self, $class;
140
141 $options{options}->add_options(arguments => {
142 'filter-tenant-name:s' => { name => 'filter_tenant_name' }
143 });
144
145 return $self;
146 }
147
148 my $mapping = {
149 hard_quota => { oid => '.1.3.6.1.4.1.116.5.46.4.1.1.21' }, # tenantHardQuota (This is the total amount of storage available to the tenant)
150 soft_quota => { oid => '.1.3.6.1.4.1.116.5.46.4.1.1.22' }, # tenantSoftQuota (percentage)
151 used => { oid => '.1.3.6.1.4.1.116.5.46.4.1.1.24' } # tenantPhysicalBytesUsed
152 };
153
154 sub manage_selection {
155 my ($self, %options) = @_;
156
157 my $oid_tenantName = '.1.3.6.1.4.1.116.5.46.4.1.1.2';
158 my $snmp_result = $options{snmp}->get_table(
159 oid => $oid_tenantName,
160 nothing_quit => 1
161 );
162
163 $self->{tenants} = {};
164 foreach (keys %$snmp_result) {
165 /\.(\d+)$/;
166 my $instance = $1;
167
168 if (defined($self->{option_results}->{filter_tenant_name}) && $self->{option_results}->{filter_tenant_name} ne '' &&
169 $snmp_result->{$_} !~ /$self->{option_results}->{filter_tenant_name}/) {
170 $self->{output}->output_add(long_msg => "skipping tenant '" . $snmp_result->{$_} . "'.", debug => 1);
171 next;
172 }
173
174 $self->{tenants}->{ $snmp_result->{$_} } = {
175 name => $snmp_result->{$_},
176 instance => $instance,
177 space => {
178 name => $snmp_result->{$_},
179 instance => $instance
180 }
181 };
182 }
183
184 return if (scalar(keys %{$self->{tenants}}) <= 0);
185
186 $options{snmp}->load(
187 oids => [ map($_->{oid}, values(%$mapping)) ],
188 instances => [ map($_->{instance}, values(%{$self->{tenants}})) ],
189 instance_regexp => '^(.*)$'
190 );
191 $snmp_result = $options{snmp}->get_leef();
192 foreach (keys %{$self->{tenants}}) {
193 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $self->{tenants}->{$_}->{instance});
194
195 next if (!defined($result->{hard_quota}));
196
197 $self->{tenants}->{$_}->{space}->{soft_quota} = $result->{soft_quota};
198 $self->{tenants}->{$_}->{space}->{total} = $result->{hard_quota};
199 $self->{tenants}->{$_}->{space}->{free} = $result->{hard_quota} - $result->{used};
200 $self->{tenants}->{$_}->{space}->{used} = $result->{used};
201 $self->{tenants}->{$_}->{space}->{prct_used} = $result->{used} * 100 / $result->{hard_quota};
202 $self->{tenants}->{$_}->{space}->{prct_free} = 100 - $self->{tenants}->{$_}->{space}->{prct_used};
203 }
204 }
205
206 1;
207
208 __END__
209
210 =head1 MODE
211
212 Check tenants.
213
214 =over 8
215
216 =item B<--filter-tenant-name>
217
218 Filter tenants by name (can be a regexp).
219
220 =item B<--warning-*> B<--critical-*>
221
222 Thresholds.
223 Can be: 'space-usage-prct', 'space-usage', 'space-usage-free'.
224
225 =back
226
227 =cut