Debian Codegraph @

src/hardware/pdu/apc/snmp/mode/load.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 hardware::pdu::apc::snmp::mode::load;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
28
29 sub custom_status_output {
30 my ($self, %options) = @_;
31
32 return 'status: ' . $self->{result_values}->{status};
33 }
34
35 sub prefix_bank_output {
36 my ($self, %options) = @_;
37
38 return "Bank '" . $options{instance_value}->{display} . "' ";
39 }
40
41 sub prefix_phase_output {
42 my ($self, %options) = @_;
43
44 return "Phase '" . $options{instance_value}->{display} . "' ";
45 }
46
47 sub prefix_device_output {
48 my ($self, %options) = @_;
49
50 return "Device '" . $options{instance_value}->{display} . "' ";
51 }
52
53 sub set_counters {
54 my ($self, %options) = @_;
55
56 $self->{maps_counters_type} = [
57 { name => 'device', type => 1, cb_prefix_output => 'prefix_device_output', message_multiple => 'All devices are ok', skipped_code => { -10 => 1 } },
58 { name => 'bank', type => 1, cb_prefix_output => 'prefix_bank_output', message_multiple => 'All banks are ok', skipped_code => { -10 => 1 } },
59 { name => 'phase', type => 1, cb_prefix_output => 'prefix_phase_output', message_multiple => 'All phases are ok', skipped_code => { -10 => 1 } }
60 ];
61
62 $self->{maps_counters}->{bank} = [
63 {
64 label => 'bank-status',
65 type => 2,
66 warning_default => '%{status} =~ /low|nearOverload/i',
67 critical_default => '%{status} =~ /^overload/i',
68 set => {
69 key_values => [ { name => 'status' }, { name => 'display' } ],
70 closure_custom_output => $self->can('custom_status_output'),
71 closure_custom_perfdata => sub { return 0; },
72 closure_custom_threshold_check => \&catalog_status_threshold_ng
73 }
74 },
75 { label => 'current', nlabel => 'bank.current.ampere', set => {
76 key_values => [ { name => 'current' }, { name => 'display' } ],
77 output_template => 'current: %s A',
78 perfdatas => [
79 { template => '%s', unit => 'A', min => 0, label_extra_instance => 1, instance_use => 'display' }
80 ]
81 }
82 }
83 ];
84
85 $self->{maps_counters}->{phase} = [
86 {
87 label => 'phase-status',
88 type => 2,
89 warning_default => '%{status} =~ /low|nearOverload/i',
90 critical_default => '%{status} =~ /^overload/i',
91 set => {
92 key_values => [ { name => 'status' }, { name => 'display' } ],
93 closure_custom_output => $self->can('custom_status_output'),
94 closure_custom_perfdata => sub { return 0; },
95 closure_custom_threshold_check => \&catalog_status_threshold_ng
96 }
97 },
98 { label => 'current', nlabel => 'phase.current.ampere', set => {
99 key_values => [ { name => 'current' }, { name => 'display' } ],
100 output_template => 'current: %s A',
101 perfdatas => [
102 { template => '%s', unit => 'A', min => 0, label_extra_instance => 1, instance_use => 'display' }
103 ]
104 }
105 },
106 { label => 'power', nlabel => 'phase.power.watt', set => {
107 key_values => [ { name => 'power' }, { name => 'display' } ],
108 output_template => 'power: %s W',
109 perfdatas => [
110 { template => '%s', unit => 'W', min => 0, label_extra_instance => 1, instance_use => 'display' }
111 ]
112 }
113 }
114 ];
115
116 $self->{maps_counters}->{device} = [
117 { label => 'power', nlabel => 'device.power.watt', set => {
118 key_values => [ { name => 'power' }, { name => 'display' } ],
119 output_template => 'power: %s W',
120 perfdatas => [
121 { template => '%s', unit => 'W', min => 0, label_extra_instance => 1, instance_use => 'display' }
122 ]
123 }
124 }
125 ];
126 }
127
128 sub new {
129 my ($class, %options) = @_;
130 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
131 bless $self, $class;
132
133 $options{options}->add_options(arguments => {});
134
135 return $self;
136 }
137
138 my $map_rpdu_status = {
139 1 => 'normal',
140 2 => 'low',
141 3 => 'nearOverload',
142 4 => 'overload',
143 };
144
145 sub check_rpdu {
146 my ($self, %options) = @_;
147
148 return if (scalar(keys %{$self->{phase}}) > 0);
149
150 my $mapping = {
151 rPDULoadStatusLoad => { oid => '.1.3.6.1.4.1.318.1.1.12.2.3.1.1.2' },
152 rPDULoadStatusLoadState => { oid => '.1.3.6.1.4.1.318.1.1.12.2.3.1.1.3', map => $map_rpdu_status },
153 rPDULoadStatusPhaseNumber => { oid => '.1.3.6.1.4.1.318.1.1.12.2.3.1.1.4' },
154 rPDULoadStatusBankNumber => { oid => '.1.3.6.1.4.1.318.1.1.12.2.3.1.1.5' } # Bank 0 = no bank (phase total)
155 };
156
157 my $oid_rPDULoadStatusEntry = '.1.3.6.1.4.1.318.1.1.12.2.3.1.1';
158 my $snmp_result = $options{snmp}->get_table(oid => $oid_rPDULoadStatusEntry, nothing_quit => 1);
159
160 foreach my $oid (keys %{$snmp_result}) {
161 next if ($oid !~ /^$mapping->{rPDULoadStatusLoadState}->{oid}\.(.*)$/);
162 my $instance = $1;
163 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
164
165 if ($result->{rPDULoadStatusBankNumber} == 0) {
166 $self->{phase}->{$result->{rPDULoadStatusPhaseNumber}} = {
167 display => $result->{rPDULoadStatusPhaseNumber},
168 status => $result->{rPDULoadStatusLoadState},
169 current => $result->{rPDULoadStatusLoad} / 10
170 };
171 } else {
172 $self->{bank}->{$result->{rPDULoadStatusBankNumber}} = { display => $result->{rPDULoadStatusBankNumber}, current => 0 }
173 if (!defined($self->{bank}->{$result->{rPDULoadStatusBankNumber}}));
174 $self->{bank}->{$result->{rPDULoadStatusBankNumber}}->{status} = $result->{rPDULoadStatusLoadState};
175 $self->{bank}->{$result->{rPDULoadStatusBankNumber}}->{current} += $result->{rPDULoadStatusLoad} / 10;
176 }
177 }
178
179 my $oid_rPDUIdentName = '.1.3.6.1.4.1.318.1.1.12.1.1.0';
180 my $oid_rPDUIdentDevicePowerWatts = '.1.3.6.1.4.1.318.1.1.12.1.16.0';
181 $snmp_result = $options{snmp}->get_leef(oids => [$oid_rPDUIdentName, $oid_rPDUIdentDevicePowerWatts]);
182 $self->{device}->{0} = {
183 display => $snmp_result->{$oid_rPDUIdentName},
184 power => $snmp_result->{$oid_rPDUIdentDevicePowerWatts}
185 };
186 }
187
188 my $map_rpdu2_status = {
189 1 => 'low',
190 2 => 'normal',
191 3 => 'nearOverload',
192 4 => 'overload',
193 };
194
195 sub check_rpdu2 {
196 my ($self, %options) = @_;
197
198 my $mapping_phase = {
199 rPDU2PhaseStatusModule => { oid => '.1.3.6.1.4.1.318.1.1.26.6.3.1.2' },
200 rPDU2PhaseStatusNumber => { oid => '.1.3.6.1.4.1.318.1.1.26.6.3.1.3' },
201 rPDU2PhaseStatusLoadState => { oid => '.1.3.6.1.4.1.318.1.1.26.6.3.1.4', map => $map_rpdu2_status },
202 rPDU2PhaseStatusCurrent => { oid => '.1.3.6.1.4.1.318.1.1.26.6.3.1.5' },
203 rPDU2PhaseStatusPower => { oid => '.1.3.6.1.4.1.318.1.1.26.6.3.1.7' }
204 };
205 my $mapping_bank = {
206 rPDU2BankStatusModule => { oid => '.1.3.6.1.4.1.318.1.1.26.8.3.1.2' },
207 rPDU2BankStatusNumber => { oid => '.1.3.6.1.4.1.318.1.1.26.8.3.1.3' },
208 rPDU2BankStatusLoadState => { oid => '.1.3.6.1.4.1.318.1.1.26.8.3.1.4', map => $map_rpdu2_status },
209 rPDU2BankStatusCurrent => { oid => '.1.3.6.1.4.1.318.1.1.26.8.3.1.5' }
210 };
211 my $mapping_device = {
212 rPDU2DeviceStatusName => { oid => '.1.3.6.1.4.1.318.1.1.26.4.3.1.3' },
213 rPDU2DeviceStatusPower => { oid => '.1.3.6.1.4.1.318.1.1.26.4.3.1.5' }
214 };
215
216 my $oid_rPDU2DeviceStatusEntry = '.1.3.6.1.4.1.318.1.1.26.4.3.1';
217 my $oid_rPDU2PhaseStatusEntry = '.1.3.6.1.4.1.318.1.1.26.6.3.1';
218 my $oid_rPDU2BankStatusEntry = '.1.3.6.1.4.1.318.1.1.26.8.3.1';
219 my $snmp_result = $options{snmp}->get_multiple_table(oids => [
220 { oid => $oid_rPDU2PhaseStatusEntry, end => $mapping_phase->{rPDU2PhaseStatusPower}->{oid} },
221 { oid => $oid_rPDU2BankStatusEntry, end => $mapping_bank->{rPDU2BankStatusCurrent}->{oid} },
222 { oid => $oid_rPDU2DeviceStatusEntry, end => $mapping_device->{rPDU2DeviceStatusPower}->{oid} }
223 ]);
224
225 foreach my $oid (keys %{$snmp_result->{$oid_rPDU2PhaseStatusEntry}}) {
226 next if ($oid !~ /^$mapping_phase->{rPDU2PhaseStatusLoadState}->{oid}\.(.*)$/);
227 my $instance = $1;
228 my $result = $options{snmp}->map_instance(mapping => $mapping_phase, results => $snmp_result->{$oid_rPDU2PhaseStatusEntry}, instance => $instance);
229
230 my $name = 'mod' . $result->{rPDU2PhaseStatusModule} . 'phase' . $result->{rPDU2PhaseStatusNumber};
231 $self->{phase}->{$name} = {
232 display => $name,
233 status => $result->{rPDU2PhaseStatusLoadState},
234 current => $result->{rPDU2PhaseStatusCurrent} / 10,
235 power => defined($result->{rPDU2PhaseStatusPower}) ? $result->{rPDU2PhaseStatusPower} * 10 : undef # hundreth of kW. So * 10 for watt
236 }
237 }
238
239 foreach my $oid (keys %{$snmp_result->{$oid_rPDU2BankStatusEntry}}) {
240 next if ($oid !~ /^$mapping_bank->{rPDU2BankStatusLoadState}->{oid}\.(.*)$/);
241 my $instance = $1;
242 my $result = $options{snmp}->map_instance(mapping => $mapping_bank, results => $snmp_result->{$oid_rPDU2BankStatusEntry}, instance => $instance);
243
244 my $name = 'mod' . $result->{rPDU2BankStatusModule} . 'bank' . $result->{rPDU2BankStatusNumber};
245 $self->{bank}->{$name} = {
246 display => $name,
247 status => $result->{rPDU2BankStatusLoadState},
248 current => $result->{rPDU2BankStatusCurrent} / 10
249 }
250 }
251
252 foreach my $oid (keys %{$snmp_result->{$oid_rPDU2DeviceStatusEntry}}) {
253 next if ($oid !~ /^$mapping_device->{rPDU2DeviceStatusName}->{oid}\.(.*)$/);
254 my $instance = $1;
255 my $result = $options{snmp}->map_instance(mapping => $mapping_device, results => $snmp_result->{$oid_rPDU2DeviceStatusEntry}, instance => $instance);
256
257 $self->{device}->{$result->{rPDU2DeviceStatusName}} = {
258 display => $result->{rPDU2DeviceStatusName},
259 power => defined($result->{rPDU2DeviceStatusPower}) ? $result->{rPDU2DeviceStatusPower} * 10 : undef
260 }
261 }
262 }
263
264 sub manage_selection {
265 my ($self, %options) = @_;
266
267 $self->{bank} = {};
268 $self->{phase} = {};
269 $self->{device} = {};
270
271 $self->check_rpdu2(%options);
272 $self->check_rpdu(%options);
273
274 if (scalar(keys %{$self->{device}}) <= 0) {
275 $self->{output}->add_option_msg(short_msg => "No device found.");
276 $self->{output}->option_exit();
277 }
278 }
279
280 1;
281
282 __END__
283
284 =head1 MODE
285
286 Check phase/bank load.
287
288 =over 8
289
290 =item B<--unknown-bank-status>
291
292 Define the conditions to match for the status to be WARNING.
293 You can use the following variables: %{type}, %{status}, %{display}
294
295 =item B<--warning-bank-status>
296
297 Define the conditions to match for the status to be WARNING (default: '%{status} =~ /low|nearOverload/i').
298 You can use the following variables: %{type}, %{status}, %{display}
299
300 =item B<--critical-bank-status>
301
302 Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /^overload/').
303 You can use the following variables: %{type}, %{status}, %{display}
304
305 =item B<--unknown-phase-status>
306
307 Define the conditions to match for the status to be WARNING.
308 You can use the following variables: %{status}, %{display}
309
310 =item B<--warning-phase-status>
311
312 Define the conditions to match for the status to be WARNING (default: '%{status} =~ /low|nearOverload/i').
313 You can use the following variables: %{status}, %{display}
314
315 =item B<--critical-phase-status>
316
317 Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /^overload/i').
318 You can use the following variables: %{status}, %{display}
319
320 =item B<--warning-*> B<--critical-*>
321
322 Thresholds.
323 Can be: 'current', 'power'.
324
325 =back
326
327 =cut