Debian Codegraph @

src/hardware/devices/eltek/enexus/snmp/mode/battery.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::devices::eltek::enexus::snmp::mode::battery;
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 catalog_status_calc);
28 use centreon::plugins::misc;
29
30 sub custom_status_output {
31 my ($self, %options) = @_;
32
33 return sprintf('status: %s',
34 $self->{result_values}->{status}
35 );
36 }
37
38 sub custom_temperature_output {
39 my ($self, %options) = @_;
40
41 return sprintf('temperature: %s %s',
42 $self->{result_values}->{temperature},
43 $self->{result_values}->{temperature_unit}
44 );
45 }
46
47 sub custom_temperature_perfdata {
48 my ($self, %options) = @_;
49
50 $self->{output}->perfdata_add(
51 nlabel => 'battery.temperature.' . ($self->{result_values}->{temperature_unit} eq 'C' ? 'celsius' : 'fahrenheit'),
52 unit => $self->{result_values}->{temperature_unit},
53 value => $self->{result_values}->{temperature},
54 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
55 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
56 );
57 }
58
59 sub custom_charge_remaining_output {
60 my ($self, %options) = @_;
61
62 return sprintf('remaining capacity: %s %s',
63 $self->{result_values}->{charge_remaining},
64 $self->{result_values}->{charge_remaining_unit}
65 );
66 }
67
68 sub custom_charge_remaining_perfdata {
69 my ($self, %options) = @_;
70
71 $self->{output}->perfdata_add(
72 nlabel => 'battery.charge.remaining.' . ($self->{result_values}->{charge_remaining_unit} eq '%' ? 'percentage' : 'amperehour'),
73 unit => $self->{result_values}->{charge_remaining_unit},
74 value => $self->{result_values}->{charge_remaining},
75 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
76 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
77 min => 0,
78 max => $self->{result_values}->{charge_remaining_unit} eq '%' ? 100 : undef
79 );
80 }
81
82 sub custom_charge_time_output {
83 my ($self, %options) = @_;
84
85 return sprintf(
86 'remaining time: %s',
87 centreon::plugins::misc::change_seconds(value => $self->{result_values}->{charge_remaining_time})
88 );
89 }
90
91 sub set_counters {
92 my ($self, %options) = @_;
93
94 $self->{maps_counters_type} = [
95 { name => 'battery', type => 0, cb_prefix_output => 'prefix_battery_output', skipped_code => { -10 => 1 } }
96 ];
97
98 $self->{maps_counters}->{battery} = [
99 { label => 'status', threshold => 0, set => {
100 key_values => [ { name => 'status' } ],
101 closure_custom_calc => \&catalog_status_calc,
102 closure_custom_output => $self->can('custom_status_output'),
103 closure_custom_perfdata => sub { return 0; },
104 closure_custom_threshold_check => \&catalog_status_threshold
105 }
106 },
107 { label => 'temperature', display_ok => 0, set => {
108 key_values => [ { name => 'temperature' }, { name => 'temperature_unit' } ],
109 closure_custom_output => $self->can('custom_temperature_output'),
110 closure_custom_perfdata => $self->can('custom_temperature_perfdata')
111 }
112 },
113 { label => 'charge-remaining', set => {
114 key_values => [ { name => 'charge_remaining' }, { name => 'charge_remaining_unit' } ],
115 closure_custom_output => $self->can('custom_charge_remaining_output'),
116 closure_custom_perfdata => $self->can('custom_charge_remaining_perfdata')
117 }
118 },
119 { label => 'charge-remaining-time', nlabel => 'battery.charge.remaining.time.seconds', set => {
120 key_values => [ { name => 'charge_remaining_time' } ],
121 closure_custom_output => $self->can('custom_charge_time_output'),
122 perfdatas => [
123 { value => 'charge_remaining_time', template => '%s', min => 0, unit => 's' },
124 ],
125 }
126 },
127 { label => 'voltage', nlabel => 'battery.voltage.volt', display_ok => 0, set => {
128 key_values => [ { name => 'voltage' } ],
129 output_template => 'voltage: %.2f V',
130 perfdatas => [
131 { value => 'voltage', template => '%.2f', unit => 'V' }
132 ]
133 }
134 },
135 { label => 'current', nlabel => 'battery.current.ampere', display_ok => 0, set => {
136 key_values => [ { name => 'current' } ],
137 output_template => 'current: %.2f A',
138 perfdatas => [
139 { value => 'current', template => '%.2f', min => 0, unit => 'A' }
140 ]
141 }
142 }
143 ];
144 }
145
146 sub prefix_battery_output {
147 my ($self, %options) = @_;
148
149 return 'Battery ';
150 }
151
152 sub new {
153 my ($class, %options) = @_;
154 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
155 bless $self, $class;
156
157 $options{options}->add_options(arguments => {
158 'unknown-status:s' => { name => 'unknown_status', default => '' },
159 'warning-status:s' => { name => 'warning_status', default => '%{status} =~ /minor|warning/i' },
160 'critical-status:s' => { name => 'critical_status', default => '%{status} =~ /error|major|critical/i' }
161 });
162
163 return $self;
164 }
165
166 sub check_options {
167 my ($self, %options) = @_;
168 $self->SUPER::check_options(%options);
169
170 $self->change_macros(macros => [
171 'warning_status', 'critical_status', 'unknown_status',
172 ]);
173 }
174
175 my $map_status = {
176 0 => 'error', 1 => 'normal', 2 => 'minorAlarm', 3 => 'majorAlarm',
177 4 => 'disabled', 5 => 'disconnected', 6 => 'notPresent',
178 7 => 'minorAndMajor', 8 => 'majorLow', 9 => 'minorLow',
179 10 => 'majorHigh', 11 => 'minorHigh', 12 => 'event',
180 13 => 'valueVolt', 14 => 'valueAmp', 15 => 'valueTemp',
181 16 => 'valueUnit', 17 => 'valuePerCent', 18 => 'critical',
182 19 => 'warning'
183 };
184 my $map_decimal_setting = { 0 => 'ampere', 1 => 'deciAmpere' };
185 my $map_temp_setting = { 0 => 'celsius', 1 => 'fahrenheit' };
186 my $map_capacity = { 0 => 'ah', 1 => 'percent' };
187
188 my $mapping = {
189 powerSystemCurrentDecimalSetting => { oid => '.1.3.6.1.4.1.12148.10.2.15', map => $map_decimal_setting },
190 powerSystemTemperatureScale => { oid => '.1.3.6.1.4.1.12148.10.2.16', map => $map_temp_setting },
191 powerSystemCapacityScale => { oid => '.1.3.6.1.4.1.12148.10.2.17', map => $map_capacity },
192 batteryStatus => { oid => '.1.3.6.1.4.1.12148.10.10.1', map => $map_status },
193 batteryVoltageValue => { oid => '.1.3.6.1.4.1.12148.10.10.5.5' },
194 batteryVoltageMajorHighLevel => { oid => '.1.3.6.1.4.1.12148.10.10.5.6' },
195 batteryVoltageMinorHighLevel => { oid => '.1.3.6.1.4.1.12148.10.10.5.7' }, # 0.01 for vdc
196 batteryVoltageMinorLowLevel => { oid => '.1.3.6.1.4.1.12148.10.10.5.8' },
197 batteryVoltageMajorLowLevel => { oid => '.1.3.6.1.4.1.12148.10.10.5.9' },
198 batteryCurrentsValue => { oid => '.1.3.6.1.4.1.12148.10.10.6.5' }, # A or dA
199 batteryCurrentsMajorHighLevel => { oid => '.1.3.6.1.4.1.12148.10.10.6.6' },
200 batteryCurrentsMinorHighLevel => { oid => '.1.3.6.1.4.1.12148.10.10.6.7' },
201 batteryCurrentsMinorLowLevel => { oid => '.1.3.6.1.4.1.12148.10.10.6.8' },
202 batteryCurrentsMajorLowLevel => { oid => '.1.3.6.1.4.1.12148.10.10.6.9' },
203 batteryTemperaturesValue => { oid => '.1.3.6.1.4.1.12148.10.10.7.5' }, # C or F
204 batteryTemperaturesMajorHighLevel => { oid => '.1.3.6.1.4.1.12148.10.10.7.6' },
205 batteryTemperaturesMinorHighLevel => { oid => '.1.3.6.1.4.1.12148.10.10.7.7' },
206 batteryTemperaturesMinorLowLevel => { oid => '.1.3.6.1.4.1.12148.10.10.7.8' },
207 batteryTemperaturesMajorLowLevel => { oid => '.1.3.6.1.4.1.12148.10.10.7.9' },
208 batteryRemainingCapacityValue => { oid => '.1.3.6.1.4.1.12148.10.10.9.5' }, # ah or %
209 batteryRemainingCapacityMinorLowLevel => { oid => '.1.3.6.1.4.1.12148.10.10.9.6' },
210 batteryRemainingCapacityMajorLowLevel => { oid => '.1.3.6.1.4.1.12148.10.10.9.7' },
211 loadCurrentValue => { oid => '.1.3.6.1.4.1.12148.10.9.2.5' }, # A or dA
212 };
213
214 sub threshold_eltek_configured {
215 my ($self, %options) = @_;
216
217 if ((!defined($self->{option_results}->{'critical-' . $options{label}}) || $self->{option_results}->{'critical-' . $options{label}} eq '') &&
218 (!defined($self->{option_results}->{'warning-' . $options{label}}) || $self->{option_results}->{'warning-' . $options{label}} eq '')) {
219 my ($crit, $warn) = ('', '');
220 $crit = $options{low_crit} . ':' if (defined($options{low_crit}) && $options{low_crit} ne '');
221 $crit .= $options{high_crit} if (defined($options{high_crit}) && $options{high_crit} ne '');
222 $warn = $options{low_warn} . ':' if (defined($options{low_warn}) && $options{low_warn} ne '');
223 $warn .= $options{high_warn} if (defined($options{high_warn}) && $options{high_warn} ne '');
224 $self->{perfdata}->threshold_validate(label => 'critical-' . $options{label}, value => $crit);
225 $self->{perfdata}->threshold_validate(label => 'warning-' . $options{label}, value => $warn);
226 }
227 }
228
229 sub manage_selection {
230 my ($self, %options) = @_;
231
232 my $snmp_result = $options{snmp}->get_leef(
233 oids => [ map($_->{oid} . '.0', values(%$mapping)) ],
234 nothing_quit => 1
235 );
236 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => '0');
237
238 my $scale_current = 1;
239 $scale_current = 0.1 if ($result->{powerSystemCurrentDecimalSetting} eq 'deciAmpere');
240 $self->{battery} = {
241 status => $result->{batteryStatus},
242 temperature => $result->{batteryTemperaturesValue},
243 temperature_unit => $result->{powerSystemTemperatureScale} eq 'celsius' ? 'C' : 'F',
244 voltage => $result->{batteryVoltageValue} * 0.01,
245 current => $result->{batteryCurrentsValue} * $scale_current,
246 charge_remaining => $result->{batteryRemainingCapacityValue},
247 charge_remaining_unit => $result->{powerSystemCapacityScale}
248 };
249 # we can calculate the time remaining if unit is ah (amperehour) and current battery is discharging (negative value)
250 my $current;
251 if ($result->{batteryCurrentsValue} < 0) {
252 $current = $result->{batteryCurrentsValue} * -1
253 } elsif ($result->{loadCurrentValue} > 0) {
254 $current = $result->{loadCurrentValue};
255 }
256
257
258 if ($result->{powerSystemCapacityScale} eq 'ah' && defined($current)) {
259 $self->{battery}->{charge_remaining_time} =
260 int(($result->{batteryRemainingCapacityValue} * 3600) / ($current * $scale_current));
261 }
262
263 $self->threshold_eltek_configured(
264 label => 'temperature',
265 high_crit => $result->{batteryTemperaturesMajorHighLevel},
266 low_crit => $result->{batteryTemperaturesMajorLowLevel},
267 high_warn => $result->{batteryTemperaturesMinorHighLevel},
268 low_warn => $result->{batteryTemperaturesMinorLowLevel}
269 );
270 $self->threshold_eltek_configured(
271 label => 'battery-voltage-volt',
272 high_crit => $result->{batteryVoltageMajorHighLevel} * 0.01,
273 low_crit => $result->{batteryVoltageMajorLowLevel} * 0.01,
274 high_warn => $result->{batteryVoltageMinorHighLevel} * 0.01,
275 low_warn => $result->{batteryVoltageMinorLowLevel} * 0.01
276 );
277 $self->threshold_eltek_configured(
278 label => 'battery-current-ampere',
279 high_crit => $result->{batteryCurrentsMajorHighLevel} * $scale_current,
280 low_crit => $result->{batteryCurrentsMajorLowLevel} * $scale_current,
281 high_warn => $result->{batteryCurrentsMinorHighLevel} * $scale_current,
282 low_warn => $result->{batteryCurrentsMinorLowLevel} * $scale_current
283 );
284 $self->threshold_eltek_configured(
285 label => 'charge-remaining',
286 low_crit => $result->{batteryRemainingCapacityMajorLowLevel},
287 low_warn => $result->{batteryRemainingCapacityMinorLowLevel}
288 );
289 }
290
291 1;
292
293 __END__
294
295 =head1 MODE
296
297 Check battery.
298
299 =over 8
300
301 =item B<--unknown-status>
302
303 Define the conditions to match for the status to be UNKNOWN.
304 You can use the following variables: %{status}
305
306 =item B<--warning-status>
307
308 Define the conditions to match for the status to be WARNING (default: '%{status} =~ /minor|warning/i').
309 You can use the following variables: %{status}
310
311 =item B<--critical-status>
312
313 Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /error|major|critical/i').
314 You can use the following variables: %{status}
315
316 =item B<--warning-*> B<--critical-*>
317
318 Thresholds.
319 Can be: 'temperature', 'voltage', 'current',
320 'charge-remaining', 'charge-remaining-time'.
321
322 =back
323
324 =cut