Debian Codegraph @

src/hardware/pdu/apc/snmp/mode/outlet.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::outlet;
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);
28
29 sub custom_status_output {
30 my ($self, %options) = @_;
31
32 my $msg = sprintf(
33 "status : '%s' %s%s",
34 $self->{result_values}->{status},
35 $self->{result_values}->{bank} ne '' ? '[bank: ' . $self->{result_values}->{bank} . '] ' : '',
36 '[phase: ' . $self->{result_values}->{phase} . ']',
37 );
38 return $msg;
39 }
40
41 sub custom_status_calc {
42 my ($self, %options) = @_;
43
44 $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'};
45 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
46 $self->{result_values}->{bank} = $options{new_datas}->{$self->{instance} . '_bank'};
47 $self->{result_values}->{phase} = $options{new_datas}->{$self->{instance} . '_phase'};
48 return 0;
49 }
50
51 sub set_counters {
52 my ($self, %options) = @_;
53
54 $self->{maps_counters_type} = [
55 { name => 'outlet', type => 1, cb_prefix_output => 'prefix_outlet_output', message_multiple => 'All outlets are ok', skipped_code => { -10 => 1 } }
56 ];
57
58 $self->{maps_counters}->{outlet} = [
59 { label => 'status', threshold => 0, set => {
60 key_values => [ { name => 'status' }, { name => 'bank' }, { name => 'phase' }, { name => 'display' } ],
61 closure_custom_calc => $self->can('custom_status_calc'),
62 closure_custom_output => $self->can('custom_status_output'),
63 closure_custom_perfdata => sub { return 0; },
64 closure_custom_threshold_check => \&catalog_status_threshold,
65 }
66 },
67 { label => 'current', nlabel => 'outlet.current.ampere', set => {
68 key_values => [ { name => 'current', no_value => 0 }, { name => 'display' } ],
69 output_template => 'current : %s A',
70 perfdatas => [
71 { label => 'current', template => '%s', value => 'current',
72 unit => 'A', min => 0, label_extra_instance => 1, instance_use => 'display' },
73 ],
74 }
75 },
76 ];
77 }
78
79 sub prefix_outlet_output {
80 my ($self, %options) = @_;
81
82 return "Outlet '" . $options{instance_value}->{display} . "' ";
83 }
84
85 sub new {
86 my ($class, %options) = @_;
87 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
88 bless $self, $class;
89
90 $options{options}->add_options(arguments => {
91 'unknown-status:s' => { name => 'unknown_status', default => '' },
92 'warning-status:s' => { name => 'warning_status', default => '' },
93 'critical-status:s' => { name => 'critical_status', default => '%{status} =~ /off/i' },
94 });
95
96 return $self;
97 }
98
99 sub check_options {
100 my ($self, %options) = @_;
101 $self->SUPER::check_options(%options);
102
103 $self->change_macros(macros => [
104 'warning_status', 'critical_status', 'unknown_status',
105 ]);
106 }
107
108 my %map_rpdu_status = (
109 1 => 'on',
110 2 => 'off',
111 );
112 my %map_rpdu_phase = (
113 1 => 'phase1',
114 2 => 'phase2',
115 3 => 'phase3',
116 4 => 'phase1-2',
117 5 => 'phase2-3',
118 6 => 'phase3-1',
119 );
120
121 sub check_rpdu {
122 my ($self, %options) = @_;
123
124 return if (scalar(keys %{$self->{outlet}}) > 0);
125
126 my $mapping = {
127 rPDUOutletStatusOutletName => { oid => '.1.3.6.1.4.1.318.1.1.12.3.5.1.1.2' },
128 rPDUOutletStatusOutletPhase => { oid => '.1.3.6.1.4.1.318.1.1.12.3.5.1.1.3', map => \%map_rpdu_phase },
129 rPDUOutletStatusOutletState => { oid => '.1.3.6.1.4.1.318.1.1.12.3.5.1.1.4', map => \%map_rpdu_status },
130 rPDUOutletStatusOutletBank => { oid => '.1.3.6.1.4.1.318.1.1.12.3.5.1.1.6' },
131 rPDUOutletStatusLoad => { oid => '.1.3.6.1.4.1.318.1.1.12.3.5.1.1.7' },
132 };
133
134 my $oid_rPDUOutletStatusEntry = '.1.3.6.1.4.1.318.1.1.12.3.5.1.1';
135 my $snmp_result = $options{snmp}->get_table(oid => $oid_rPDUOutletStatusEntry, nothing_quit => 1);
136
137 my $duplicated = {};
138 foreach my $oid (keys %{$snmp_result}) {
139 next if ($oid !~ /^$mapping->{rPDUOutletStatusOutletState}->{oid}\.(.*)$/);
140 my $instance = $1;
141 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
142
143 my $name = $result->{rPDUOutletStatusOutletName};
144 $name = $instance if (defined($duplicated->{$name}));
145 if (defined($self->{outlet}->{$name})) {
146 $duplicated->{$name} = 1;
147 my $instance2 = $self->{outlet}->{$name}->{instance};
148 $self->{outlet}->{$instance2} = $self->{outlet}->{$name};
149 $self->{outlet}->{$instance2}->{display} = $instance2;
150 delete $self->{outlet}->{$name};
151 $name = $instance;
152 }
153
154 $self->{outlet}->{$name} = {
155 instance => $instance,
156 display => $name,
157 status => $result->{rPDUOutletStatusOutletState},
158 bank => $result->{rPDUOutletStatusOutletBank},
159 phase => $result->{rPDUOutletStatusOutletPhase},
160 current => $result->{rPDUOutletStatusLoad} / 10,
161 };
162 }
163 }
164
165 sub check_rpdu2 {
166 my ($self, %options) = @_;
167
168 my $map_rpdu2_status = {
169 1 => 'off',
170 2 => 'on',
171 };
172 my $map_rpdu2_phase = {
173 1 => 'seqPhase1ToNeutral', 2 => 'seqPhase2ToNeutral',
174 3 => 'seqPhase3ToNeutral', 4 => 'seqPhase1ToPhase2',
175 5 => 'seqPhase2ToPhase3', 6 => 'seqPhase3ToPhase1',
176 };
177 my $mapping = {
178 rPDU2OutletSwitchedPropertiesPhaseLayout => { oid => '.1.3.6.1.4.1.318.1.1.26.9.2.2.1.5', map => $map_rpdu2_phase },
179 rPDU2OutletSwitchedPropertiesBank => { oid => '.1.3.6.1.4.1.318.1.1.26.9.2.2.1.6' },
180 rPDU2OutletSwitchedStatusName => { oid => '.1.3.6.1.4.1.318.1.1.26.9.2.3.1.3' },
181 rPDU2OutletSwitchedStatusState => { oid => '.1.3.6.1.4.1.318.1.1.26.9.2.3.1.5', map => $map_rpdu2_status },
182 };
183
184 my $oid_rPDU2OutletSwitchedPropertiesEntry = '.1.3.6.1.4.1.318.1.1.26.9.2.2.1';
185 my $oid_rPDU2OutletSwitchedStatusEntry = '.1.3.6.1.4.1.318.1.1.26.9.2.3.1';
186 my $snmp_result = $options{snmp}->get_multiple_table(
187 oids => [
188 { oid => $oid_rPDU2OutletSwitchedPropertiesEntry, start => $mapping->{rPDU2OutletSwitchedPropertiesPhaseLayout}->{oid}, end => $mapping->{rPDU2OutletSwitchedPropertiesBank}->{oid} },
189 { oid => $oid_rPDU2OutletSwitchedStatusEntry, start => $mapping->{rPDU2OutletSwitchedStatusName}->{oid}, end => $mapping->{rPDU2OutletSwitchedStatusState}->{oid} },
190 ],
191 return_type => 1,
192 );
193
194 my $duplicated = {};
195 foreach my $oid (keys %{$snmp_result}) {
196 next if ($oid !~ /^$mapping->{rPDU2OutletSwitchedStatusState}->{oid}\.(.*)$/);
197 my $instance = $1;
198 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
199
200 my $name = $result->{rPDU2OutletSwitchedStatusName} . ' bank ' . $result->{rPDU2OutletSwitchedPropertiesBank};
201 $name = $instance if (defined($duplicated->{$name}));
202 if (defined($self->{outlet}->{$name})) {
203 $duplicated->{$name} = 1;
204 my $instance2 = $self->{outlet}->{$name}->{instance};
205 $self->{outlet}->{$instance2} = $self->{outlet}->{$name};
206 $self->{outlet}->{$instance2}->{display} = $instance2;
207 delete $self->{outlet}->{$name};
208 $name = $instance;
209 }
210
211 $self->{outlet}->{$name} = {
212 instance => $instance,
213 display => $name,
214 status => $result->{rPDU2OutletSwitchedStatusState},
215 bank => '',
216 phase => $result->{rPDU2OutletSwitchedPropertiesPhaseLayout},
217 };
218 }
219 }
220
221 sub manage_selection {
222 my ($self, %options) = @_;
223
224 $self->{outlet} = {};
225
226 $self->check_rpdu2(%options);
227 $self->check_rpdu(%options);
228
229 if (scalar(keys %{$self->{outlet}}) <= 0) {
230 $self->{output}->add_option_msg(short_msg => "No outlet found.");
231 $self->{output}->option_exit();
232 }
233 }
234
235 1;
236
237 __END__
238
239 =head1 MODE
240
241 Check outlet.
242
243 =over 8
244
245 =item B<--unknown-status>
246
247 Define the conditions to match for the status to be UNKNOWN.
248 You can use the following variables: %{status}, %{display}
249
250 =item B<--warning-status>
251
252 Define the conditions to match for the status to be WARNING (default: '').
253 You can use the following variables: %{status}, %{phase}, %{bank}, %{display}
254
255 =item B<--critical-status>
256
257 Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /off/').
258 You can use the following variables: %{status}, %{phase}, %{bank}, %{display}
259
260 =item B<--warning-*> B<--critical-*>
261
262 Thresholds.
263 Can be: 'current'.
264
265 =back
266
267 =cut