Debian Codegraph @

src/hardware/ups/apc/snmp/mode/inputlines.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::ups::apc::snmp::mode::inputlines;
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 sprintf("last input line fail cause is '%s'", $self->{result_values}->{last_cause});
33 }
34
35 sub input_long_output {
36 my ($self, %options) = @_;
37
38 return sprintf(
39 "checking input line '%s' [type: %s]",
40 $options{instance_value}->{inputNum},
41 $options{instance_value}->{inputType}
42 );
43 }
44
45 sub input_bpack_output {
46 my ($self, %options) = @_;
47
48 return sprintf(
49 "input line '%s' [type: %s] ",
50 $options{instance_value}->{inputNum},
51 $options{instance_value}->{inputType}
52 );
53 }
54
55 sub prefix_phase_output {
56 my ($self, %options) = @_;
57
58 return "phase '" . $options{instance_value}->{phaseNum} . "' ";
59 }
60
61 sub set_counters {
62 my ($self, %options) = @_;
63
64 $self->{maps_counters_type} = [
65 { name => 'global', type => 0, skipped_code => { -10 => 1 } },
66 { name => 'inputs', type => 3, cb_prefix_output => 'prefix_input_output', cb_long_output => 'input_long_output', indent_long_output => ' ', message_multiple => 'All inputs are ok',
67 group => [
68 { name => 'input_global', type => 0, skipped_code => { -10 => 1 } },
69 { name => 'phases', display_long => 1, cb_prefix_output => 'prefix_phase_output', message_multiple => 'phases are ok', type => 1, skipped_code => { -10 => 1 } }
70 ]
71 }
72 ];
73
74 $self->{maps_counters}->{global} = [
75 { label => 'voltage', nlabel => 'lines.input.voltage.volt', set => {
76 key_values => [ { name => 'voltage' } ],
77 output_template => 'voltage: %s V',
78 perfdatas => [
79 { template => '%s', unit => 'V' }
80 ]
81 }
82 },
83 { label => 'frequence', nlabel => 'lines.input.frequence.hertz', set => {
84 key_values => [ { name => 'frequency' } ],
85 output_template => 'frequence: %s Hz',
86 perfdatas => [
87 { template => '%s', unit => 'Hz' }
88 ]
89 }
90 },
91 { label => 'status', type => 2, set => {
92 key_values => [ { name => 'last_cause' } ],
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 ];
99
100 $self->{maps_counters}->{input_global} = [
101 { label => 'line-frequence', nlabel => 'line.input.frequence.hertz', set => {
102 key_values => [ { name => 'frequency' }, { name => 'inputNum' }, { name => 'inputType' } ],
103 output_template => 'frequence: %s Hz',
104 closure_custom_perfdata => sub {
105 my ($self, %options) = @_;
106
107 $self->{output}->perfdata_add(
108 nlabel => $self->{nlabel},
109 unit => 'Hz',
110 instances => [$self->{result_values}->{inputType} . '.' . $self->{result_values}->{inputNum}],
111 value => sprintf('%s', $self->{result_values}->{frequency}),
112 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
113 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel})
114 );
115 }
116 }
117 }
118 ];
119
120 $self->{maps_counters}->{phases} = [
121 { label => 'line-phase-voltage', nlabel => 'line.input.voltage.volt', set => {
122 key_values => [
123 { name => 'voltage' }, { name => 'inputNum' },
124 { name => 'inputType' }, { name => 'phaseNum' }
125 ],
126 output_template => 'voltage: %s V',
127 closure_custom_perfdata => sub {
128 my ($self, %options) = @_;
129
130 $self->{output}->perfdata_add(
131 nlabel => $self->{nlabel},
132 unit => 'V',
133 instances => [$self->{result_values}->{inputType} . '.' . $self->{result_values}->{inputNum}, $self->{result_values}->{phaseNum}],
134 value => sprintf('%s', $self->{result_values}->{voltage}),
135 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
136 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel})
137 );
138 }
139 }
140 },
141 { label => 'line-phase-current', nlabel => 'line.input.current.ampere', set => {
142 key_values => [
143 { name => 'current' }, { name => 'inputNum' },
144 { name => 'inputType' }, { name => 'phaseNum' }
145 ],
146 output_template => 'current: %s A',
147 closure_custom_perfdata => sub {
148 my ($self, %options) = @_;
149
150 $self->{output}->perfdata_add(
151 nlabel => $self->{nlabel},
152 unit => 'A',
153 instances => [$self->{result_values}->{inputType} . '.' . $self->{result_values}->{inputNum}, $self->{result_values}->{phaseNum}],
154 value => sprintf('%s', $self->{result_values}->{current}),
155 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
156 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel})
157 );
158 }
159 }
160 },
161 { label => 'line-phase-power', nlabel => 'line.input.power.watt', set => {
162 key_values => [
163 { name => 'power' }, { name => 'inputNum' },
164 { name => 'inputType' }, { name => 'phaseNum' }
165 ],
166 output_template => 'power: %.2f W',
167 closure_custom_perfdata => sub {
168 my ($self, %options) = @_;
169
170 $self->{output}->perfdata_add(
171 nlabel => $self->{nlabel},
172 unit => 'W',
173 instances => [$self->{result_values}->{inputType} . '.' . $self->{result_values}->{inputNum}, $self->{result_values}->{phaseNum}],
174 value => sprintf('%.2f', $self->{result_values}->{power}),
175 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
176 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel})
177 );
178 }
179 }
180 }
181 ];
182 }
183
184 sub new {
185 my ($class, %options) = @_;
186 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
187 bless $self, $class;
188
189 $options{options}->add_options(arguments => {});
190
191 return $self;
192 }
193
194 my $map_status = {
195 1 => 'noTransfer', 2 => 'highLineVoltage', 3 => 'brownout', 4 => 'blackout',
196 5 => 'smallMomentarySag', 6 => 'deepMomentarySag', 7 => 'smallMomentarySpike',
197 8 => 'largeMomentarySpike', 9 => 'selfTest', 10 => 'rateOfVoltageChange',
198 };
199 my $map_input_type = { 1 => 'unknown', 2 => 'main', 3 => 'bypass' };
200
201 my $mapping = {
202 upsAdvInputLineVoltage => { oid => '.1.3.6.1.4.1.318.1.1.1.3.2.1' },
203 upsAdvInputFrequency => { oid => '.1.3.6.1.4.1.318.1.1.1.3.2.4' },
204 upsAdvInputLineFailCause => { oid => '.1.3.6.1.4.1.318.1.1.1.3.2.5', map => $map_status },
205 upsAdvConfigHighTransferVolt => { oid => '.1.3.6.1.4.1.318.1.1.1.5.2.2' },
206 upsAdvConfigLowTransferVolt => { oid => '.1.3.6.1.4.1.318.1.1.1.5.2.3' },
207 upsHighPrecInputLineVoltage => { oid => '.1.3.6.1.4.1.318.1.1.1.3.3.1' },
208 upsHighPrecInputFrequency => { oid => '.1.3.6.1.4.1.318.1.1.1.3.3.4' }
209 };
210 my $mapping_input = {
211 frequency => { oid => '.1.3.6.1.4.1.318.1.1.1.9.2.2.1.4' }, # upsPhaseInputFrequency
212 type => { oid => '.1.3.6.1.4.1.318.1.1.1.9.2.2.1.5', map => $map_input_type } # upsPhaseInputType
213 };
214 my $mapping_input_phase = {
215 voltage => { oid => '.1.3.6.1.4.1.318.1.1.1.9.2.3.1.3' }, # upsPhaseInputVoltage
216 current => { oid => '.1.3.6.1.4.1.318.1.1.1.9.2.3.1.6' }, # upsPhaseInputCurrent
217 power => { oid => '.1.3.6.1.4.1.318.1.1.1.9.2.3.1.9' } # upsPhaseInputPower
218 };
219
220 sub manage_selection {
221 my ($self, %options) = @_;
222
223 my $snmp_result = $options{snmp}->get_leef(
224 oids => [ map($_->{oid} . '.0', values(%$mapping)) ],
225 nothing_quit => 1
226 );
227
228 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => '0');
229 if ((!defined($self->{option_results}->{'warning-voltage'}) || $self->{option_results}->{'warning-voltage'} eq '') &&
230 (!defined($self->{option_results}->{'critical-voltage'}) || $self->{option_results}->{'critical-voltage'} eq '')
231 ) {
232 my $th = '';
233 $th .= $result->{upsAdvConfigHighTransferVolt} if (defined($result->{upsAdvConfigHighTransferVolt}) && $result->{upsAdvConfigHighTransferVolt} =~ /\d+/);
234 $th = $result->{upsAdvConfigLowTransferVolt} . ':' . $th if (defined($result->{upsAdvConfigLowTransferVolt}) && $result->{upsAdvConfigLowTransferVolt} =~ /\d+/);
235 $self->{perfdata}->threshold_validate(label => 'critical-voltage', value => $th) if ($th ne '');
236 }
237
238 $self->{global} = {
239 last_cause => $result->{upsAdvInputLineFailCause},
240 voltage => defined($result->{upsHighPrecInputLineVoltage}) && $result->{upsHighPrecInputLineVoltage} =~ /\d/ ?
241 $result->{upsHighPrecInputLineVoltage} * 0.1 : $result->{upsAdvInputLineVoltage},
242 frequency => defined($result->{upsHighPrecInputFrequency}) && $result->{upsHighPrecInputFrequency} =~ /\d/ ?
243 $result->{upsHighPrecInputFrequency} * 0.1 : $result->{upsAdvInputFrequency},
244 };
245
246 my $oid_inputTable = '.1.3.6.1.4.1.318.1.1.1.9.2.2.1';
247 $snmp_result = $options{snmp}->get_table(
248 oid => $oid_inputTable,
249 start => $mapping_input->{frequency}->{oid},
250 end => $mapping_input->{type}->{oid}
251 );
252
253 $self->{inputs} = {};
254 foreach my $oid (keys %$snmp_result) {
255 next if ($oid !~ /^$mapping_input->{type}->{oid}\.(.*)$/);
256 my $instance = $1;
257
258 my $result = $options{snmp}->map_instance(mapping => $mapping_input, results => $snmp_result, instance => $instance);
259 $self->{inputs}->{$instance} = {
260 inputNum => $instance,
261 inputType => $result->{type},
262 input_global => {
263 inputNum => $instance,
264 inputType => $result->{type},
265 frequency => defined($result->{frequency}) && $result->{frequency} != -1 ? $result->{frequency} * 0.1 : undef
266 },
267 phases => {}
268 };
269 }
270
271 my $oid_inputPhaseTable = '.1.3.6.1.4.1.318.1.1.1.9.2.3.1';
272 $snmp_result = $options{snmp}->get_table(
273 oid => $oid_inputPhaseTable,
274 start => $mapping_input_phase->{voltage}->{oid},
275 end => $mapping_input_phase->{power}->{oid}
276 );
277
278 foreach my $oid (keys %$snmp_result) {
279 next if ($oid !~ /^$mapping_input_phase->{voltage}->{oid}\.(\d+)\.(\d+)$/);
280 my ($input_num, $phase_num) = ($1, $2);
281
282 my $result = $options{snmp}->map_instance(mapping => $mapping_input_phase, results => $snmp_result, instance => $input_num . '.' . $phase_num);
283 $self->{inputs}->{$input_num}->{phases}->{$phase_num} = {
284 inputNum => $input_num,
285 inputType => $self->{inputs}->{$input_num}->{inputType},
286 phaseNum => $phase_num,
287 voltage => defined($result->{voltage}) && $result->{voltage} != -1 ? $result->{voltage} : undef,
288 current => defined($result->{current}) && $result->{current} != -1 ? $result->{current} * 0.1 : undef,
289 power => defined($result->{power}) && $result->{power} != -1 ? $result->{power} : undef
290 };
291 }
292 }
293
294 1;
295
296 __END__
297
298 =head1 MODE
299
300 Check input lines.
301
302 =over 8
303
304 =item B<--filter-counters>
305
306 Only display some counters (regexp can be used).
307 Example: --filter-counters='^frequence|voltage$'
308
309 =item B<--warning-status>
310
311 Define the conditions to match for the status to be WARNING.
312 You can use the following variables: %{last_cause}
313
314 =item B<--critical-status>
315
316 Define the conditions to match for the status to be CRITICAL.
317 You can use the following variables: %{last_cause}
318
319 =item B<--warning-*> B<--critical-*>
320
321 Thresholds.
322 Can be: 'voltage', 'frequence', 'line-frequence',
323 'line-phase-voltage', 'line-phase-current', 'line-phase-power'.
324
325 =back
326
327 =cut