| 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::batterystatus; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use DateTime::Format::Strptime; |
| 28 |
use DateTime; |
| 29 |
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); |
| 30 |
|
| 31 |
sub custom_battery_status_output { |
| 32 |
my ($self, %options) = @_; |
| 33 |
|
| 34 |
return sprintf( |
| 35 |
"battery status is '%s' [battery needs replace: %s] [last replace date: %s]", |
| 36 |
$self->{result_values}->{status}, |
| 37 |
$self->{result_values}->{replace}, |
| 38 |
$self->{result_values}->{last_replace_date} |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
sub custom_status_calc { |
| 43 |
my ($self, %options) = @_; |
| 44 |
|
| 45 |
$self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_upsBasicBatteryStatus'}; |
| 46 |
$self->{result_values}->{replace} = $options{new_datas}->{$self->{instance} . '_upsAdvBatteryReplaceIndicator'}; |
| 47 |
$self->{result_values}->{last_replace_date} = $options{new_datas}->{$self->{instance} . '_upsBasicBatteryLastReplaceDate'}; |
| 48 |
return 0; |
| 49 |
} |
| 50 |
|
| 51 |
sub custom_status_output { |
| 52 |
my ($self, %options) = @_; |
| 53 |
|
| 54 |
return sprintf( |
| 55 |
"status is '%s'", |
| 56 |
$self->{result_values}->{status} |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
sub bpack_long_output { |
| 61 |
my ($self, %options) = @_; |
| 62 |
|
| 63 |
return "checking battery pack '" . $options{instance_value}->{display} . "'"; |
| 64 |
} |
| 65 |
|
| 66 |
sub prefix_bpack_output { |
| 67 |
my ($self, %options) = @_; |
| 68 |
|
| 69 |
return "battery pack '" . $options{instance_value}->{display} . "' "; |
| 70 |
} |
| 71 |
|
| 72 |
sub prefix_cartridge_output { |
| 73 |
my ($self, %options) = @_; |
| 74 |
|
| 75 |
return "cartridge '" . $options{instance_value}->{display} . "' "; |
| 76 |
} |
| 77 |
|
| 78 |
sub set_counters { |
| 79 |
my ($self, %options) = @_; |
| 80 |
|
| 81 |
$self->{maps_counters_type} = [ |
| 82 |
{ name => 'global', type => 0, skipped_code => { -10 => 1 } }, |
| 83 |
{ name => 'bpacks', type => 3, cb_prefix_output => 'prefix_bpack_output', cb_long_output => 'bpack_long_output', indent_long_output => ' ', message_multiple => 'All battery packs are ok', |
| 84 |
group => [ |
| 85 |
{ name => 'bpack_global', type => 0, skipped_code => { -10 => 1 } }, |
| 86 |
{ name => 'cartridges', display_long => 1, cb_prefix_output => 'prefix_cartridge_output', message_multiple => 'cartridges are ok', type => 1, skipped_code => { -10 => 1 } } |
| 87 |
] |
| 88 |
} |
| 89 |
]; |
| 90 |
|
| 91 |
$self->{maps_counters}->{global} = [ |
| 92 |
{ |
| 93 |
label => 'status', |
| 94 |
type => 2, |
| 95 |
unknown_default => '%{status} =~ /unknown/i', |
| 96 |
warning_default => '%{status} =~ /batteryLow/i', |
| 97 |
critical_default => '%{replace} =~ /yes/i', |
| 98 |
set => { |
| 99 |
key_values => [ |
| 100 |
{ name => 'upsBasicBatteryStatus' }, |
| 101 |
{ name => 'upsAdvBatteryReplaceIndicator' }, |
| 102 |
{ name => 'upsBasicBatteryLastReplaceDate' } |
| 103 |
], |
| 104 |
closure_custom_calc => $self->can('custom_status_calc'), |
| 105 |
closure_custom_output => $self->can('custom_battery_status_output'), |
| 106 |
closure_custom_perfdata => sub { return 0; }, |
| 107 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 108 |
} |
| 109 |
}, |
| 110 |
{ label => 'load', nlabel => 'battery.charge.remaining.percent', set => { |
| 111 |
key_values => [ { name => 'upsAdvBatteryCapacity' } ], |
| 112 |
output_template => 'remaining capacity: %s %%', |
| 113 |
perfdatas => [ |
| 114 |
{ label => 'load', template => '%s', min => 0, max => 100, unit => '%' } |
| 115 |
] |
| 116 |
} |
| 117 |
}, |
| 118 |
{ label => 'time', nlabel => 'battery.charge.remaining.minutes', set => { |
| 119 |
key_values => [ { name => 'upsAdvBatteryRunTimeRemaining' } ], |
| 120 |
output_template => 'remaining time: %.2f minutes', |
| 121 |
perfdatas => [ |
| 122 |
{ label => 'load_time', template => '%.2f', min => 0, unit => 'm' } |
| 123 |
] |
| 124 |
} |
| 125 |
}, |
| 126 |
{ label => 'timeon', nlabel => 'battery.timeon.minutes', set => { |
| 127 |
key_values => [ { name => 'upsBasicBatteryTimeOnBattery' } ], |
| 128 |
output_template => 'time on battery: %.2f minutes', |
| 129 |
perfdatas => [ |
| 130 |
{ label => 'timeon', template => '%.2f', min => 0, unit => 'm' } |
| 131 |
] |
| 132 |
} |
| 133 |
}, |
| 134 |
{ label => 'current', nlabel => 'battery.current.ampere', set => { |
| 135 |
key_values => [ { name => 'upsAdvBatteryCurrent' } ], |
| 136 |
output_template => 'current: %s A', |
| 137 |
perfdatas => [ |
| 138 |
{ label => 'current', template => '%s', min => 0, unit => 'A' } |
| 139 |
] |
| 140 |
} |
| 141 |
}, |
| 142 |
{ label => 'voltage', nlabel => 'battery.voltage.volt', set => { |
| 143 |
key_values => [ { name => 'upsAdvBatteryActualVoltage' } ], |
| 144 |
output_template => 'voltage: %s V', |
| 145 |
perfdatas => [ |
| 146 |
{ label => 'voltage', template => '%s', unit => 'V' } |
| 147 |
] |
| 148 |
} |
| 149 |
}, |
| 150 |
{ label => 'temperature', nlabel => 'battery.temperature.celsius', set => { |
| 151 |
key_values => [ { name => 'upsAdvBatteryTemperature' } ], |
| 152 |
output_template => 'temperature: %s C', |
| 153 |
perfdatas => [ |
| 154 |
{ label => 'temperature', template => '%s', unit => 'C'} |
| 155 |
] |
| 156 |
} |
| 157 |
}, |
| 158 |
{ label => 'replace-lasttime', nlabel => 'battery.replace.lasttime.seconds', display_ok => 0, set => { |
| 159 |
key_values => [ { name => 'last_replace_time' } ], |
| 160 |
output_template => 'replace last time: %s s', |
| 161 |
perfdatas => [ |
| 162 |
{ label => 'replace_last_time', template => '%s', unit => 's'} |
| 163 |
] |
| 164 |
} |
| 165 |
} |
| 166 |
]; |
| 167 |
|
| 168 |
$self->{maps_counters}->{bpack_global} = [ |
| 169 |
{ label => 'battery-pack-status', type => 2, critical_default => '%{status} ne "OK"', set => { |
| 170 |
key_values => [ { name => 'status' }, { name => 'display' } ], |
| 171 |
closure_custom_output => $self->can('custom_status_output'), |
| 172 |
closure_custom_perfdata => sub { return 0; }, |
| 173 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 174 |
} |
| 175 |
} |
| 176 |
]; |
| 177 |
|
| 178 |
$self->{maps_counters}->{cartridges} = [ |
| 179 |
{ label => 'cartridge-status', type => 2, critical_default => '%{status} ne "OK"', set => { |
| 180 |
key_values => [ { name => 'status' }, { name => 'display' } ], |
| 181 |
closure_custom_output => $self->can('custom_status_output'), |
| 182 |
closure_custom_perfdata => sub { return 0; }, |
| 183 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 184 |
} |
| 185 |
} |
| 186 |
]; |
| 187 |
} |
| 188 |
|
| 189 |
sub new { |
| 190 |
my ($class, %options) = @_; |
| 191 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 192 |
bless $self, $class; |
| 193 |
|
| 194 |
$options{options}->add_options(arguments => { |
| 195 |
'replace-lasttime-format:s' => { name => 'replace_lasttime_format' } |
| 196 |
}); |
| 197 |
|
| 198 |
return $self; |
| 199 |
} |
| 200 |
|
| 201 |
sub check_options { |
| 202 |
my ($self, %options) = @_; |
| 203 |
$self->SUPER::check_options(%options); |
| 204 |
|
| 205 |
my $pattern = '%m/%d/%Y'; |
| 206 |
if (defined($self->{option_results}->{replace_lasttime_format}) && $self->{option_results}->{replace_lasttime_format} ne '') { |
| 207 |
$pattern = $self->{option_results}->{replace_lasttime_format}; |
| 208 |
} |
| 209 |
|
| 210 |
$self->{strp} = DateTime::Format::Strptime->new( |
| 211 |
pattern => $pattern, |
| 212 |
on_error => 'undef' |
| 213 |
); |
| 214 |
} |
| 215 |
|
| 216 |
my $map_battery_status = { |
| 217 |
1 => 'unknown', 2 => 'batteryNormal', 3 => 'batteryLow' |
| 218 |
}; |
| 219 |
my $map_replace_status = { |
| 220 |
1 => 'no', 2 => 'yes' |
| 221 |
}; |
| 222 |
|
| 223 |
my $mapping = { |
| 224 |
upsBasicBatteryStatus => { oid => '.1.3.6.1.4.1.318.1.1.1.2.1.1', map => $map_battery_status }, |
| 225 |
upsBasicBatteryTimeOnBattery => { oid => '.1.3.6.1.4.1.318.1.1.1.2.1.2' }, |
| 226 |
upsBasicBatteryLastReplaceDate => { oid => '.1.3.6.1.4.1.318.1.1.1.2.1.3' } |
| 227 |
}; |
| 228 |
my $mapping2 = { |
| 229 |
upsAdvBatteryCapacity => { oid => '.1.3.6.1.4.1.318.1.1.1.2.2.1' }, |
| 230 |
upsAdvBatteryTemperature => { oid => '.1.3.6.1.4.1.318.1.1.1.2.2.2' }, |
| 231 |
upsAdvBatteryRunTimeRemaining => { oid => '.1.3.6.1.4.1.318.1.1.1.2.2.3' }, |
| 232 |
upsAdvBatteryReplaceIndicator => { oid => '.1.3.6.1.4.1.318.1.1.1.2.2.4', map => $map_replace_status }, |
| 233 |
upsAdvBatteryActualVoltage => { oid => '.1.3.6.1.4.1.318.1.1.1.2.2.8' }, |
| 234 |
upsAdvBatteryCurrent => { oid => '.1.3.6.1.4.1.318.1.1.1.2.2.9' } |
| 235 |
}; |
| 236 |
my $oid_upsBasicBattery = '.1.3.6.1.4.1.318.1.1.1.2.1'; |
| 237 |
my $oid_upsAdvBattery = '.1.3.6.1.4.1.318.1.1.1.2.2'; |
| 238 |
my $oid_upsHighPrecBatteryPackOnlyStatus = '.1.3.6.1.4.1.318.1.1.1.2.3.10.4.1.5'; |
| 239 |
my $oid_upsHighPrecBatteryPackCartridgeStatus = '.1.3.6.1.4.1.318.1.1.1.2.3.10.2.1.10'; |
| 240 |
|
| 241 |
my $map_battery_pack_status = { |
| 242 |
0 => 'disconnected', 1 => 'overvoltage', |
| 243 |
2 => 'needsReplacement', 3 => 'overtemperatureCritical', |
| 244 |
4 => 'charger', 5 => 'temperatureSensor', |
| 245 |
6 => 'busSoftStart', 7 => 'overtemperatureWarning', |
| 246 |
8 => 'generalError', 9 => 'communication', |
| 247 |
10 => 'disconnectedFrame', 11 => 'firmwareMismatch' |
| 248 |
}; |
| 249 |
|
| 250 |
sub add_battery_pack { |
| 251 |
my ($self, %options) = @_; |
| 252 |
|
| 253 |
$self->{bpacks} = {}; |
| 254 |
foreach my $oid (keys %{$options{snmp_result}->{$oid_upsHighPrecBatteryPackOnlyStatus}}) { |
| 255 |
next if ($options{snmp_result}->{$oid_upsHighPrecBatteryPackOnlyStatus}->{$oid} eq ''); |
| 256 |
|
| 257 |
$oid =~ /^$oid_upsHighPrecBatteryPackOnlyStatus\.(\d+)/; |
| 258 |
my $pack_index = $1; |
| 259 |
$self->{bpacks}->{$pack_index} = { |
| 260 |
display => $pack_index, |
| 261 |
bpack_global => { |
| 262 |
display => $pack_index, |
| 263 |
status => 'OK' |
| 264 |
}, |
| 265 |
cartridges => {} |
| 266 |
}; |
| 267 |
|
| 268 |
my $status = ''; |
| 269 |
my ($i, $append) = (0, ''); |
| 270 |
foreach my $bit (split //, $options{snmp_result}->{$oid_upsHighPrecBatteryPackOnlyStatus}->{$oid}) { |
| 271 |
if ($bit) { |
| 272 |
$status .= $append . $map_battery_pack_status->{$i}; |
| 273 |
$append = ','; |
| 274 |
} |
| 275 |
$i++; |
| 276 |
} |
| 277 |
|
| 278 |
$self->{bpacks}->{$pack_index}->{bpack_global}->{status} = $status if ($status ne ''); |
| 279 |
} |
| 280 |
|
| 281 |
foreach my $oid (keys %{$options{snmp_result}->{$oid_upsHighPrecBatteryPackCartridgeStatus}}) { |
| 282 |
next if ($options{snmp_result}->{$oid_upsHighPrecBatteryPackCartridgeStatus}->{$oid} eq ''); |
| 283 |
|
| 284 |
$oid =~ /^$oid_upsHighPrecBatteryPackCartridgeStatus\.(\d+)\.\d+\.(\d+)/; |
| 285 |
my ($pack_index, $cartridge_index) = ($1, $2); |
| 286 |
if (!defined($self->{bpacks}->{$pack_index})) { |
| 287 |
$self->{bpacks}->{$pack_index} = { |
| 288 |
display => $pack_index, |
| 289 |
cartridges => {} |
| 290 |
}; |
| 291 |
} |
| 292 |
|
| 293 |
$self->{bpacks}->{$pack_index}->{cartridges}->{$cartridge_index} = { |
| 294 |
display => $cartridge_index, |
| 295 |
status => 'OK' |
| 296 |
}; |
| 297 |
|
| 298 |
my $status = ''; |
| 299 |
my ($i, $append) = (0, ''); |
| 300 |
foreach my $bit (split //, $options{snmp_result}->{$oid_upsHighPrecBatteryPackCartridgeStatus}->{$oid}) { |
| 301 |
if ($bit) { |
| 302 |
$status .= $append . $map_battery_pack_status->{$i}; |
| 303 |
$append = ','; |
| 304 |
} |
| 305 |
$i++; |
| 306 |
} |
| 307 |
|
| 308 |
$self->{bpacks}->{$pack_index}->{cartridges}->{$cartridge_index}->{status} = $status if ($status ne ''); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
sub manage_selection { |
| 313 |
my ($self, %options) = @_; |
| 314 |
|
| 315 |
my $snmp_result = $options{snmp}->get_multiple_table( |
| 316 |
oids => [ |
| 317 |
{ oid => $oid_upsBasicBattery }, |
| 318 |
{ oid => $oid_upsAdvBattery, end => $mapping2->{upsAdvBatteryCurrent}->{oid} }, |
| 319 |
{ oid => $oid_upsHighPrecBatteryPackCartridgeStatus }, |
| 320 |
{ oid => $oid_upsHighPrecBatteryPackOnlyStatus } |
| 321 |
], |
| 322 |
nothing_quit => 1 |
| 323 |
); |
| 324 |
|
| 325 |
$self->{global} = {}; |
| 326 |
my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result->{$oid_upsBasicBattery}, instance => '0'); |
| 327 |
my $result2 = $options{snmp}->map_instance(mapping => $mapping2, results => $snmp_result->{$oid_upsAdvBattery}, instance => '0'); |
| 328 |
|
| 329 |
$result->{upsBasicBatteryTimeOnBattery} = sprintf('%.0f', $result->{upsBasicBatteryTimeOnBattery} / 100 / 60) |
| 330 |
if (defined($result->{upsBasicBatteryTimeOnBattery})); |
| 331 |
$result2->{upsAdvBatteryRunTimeRemaining} = sprintf("%.0f", $result2->{upsAdvBatteryRunTimeRemaining} / 100 / 60) |
| 332 |
if (defined($result2->{upsAdvBatteryRunTimeRemaining})); |
| 333 |
$result2->{upsAdvBatteryReplaceIndicator} = 'n/a' |
| 334 |
if (!defined($result2->{upsAdvBatteryReplaceIndicator})); |
| 335 |
$self->{global} = { %$result, %$result2 }; |
| 336 |
|
| 337 |
if (defined($result->{upsBasicBatteryLastReplaceDate}) && $result->{upsBasicBatteryLastReplaceDate} ne '') { |
| 338 |
my $dt = $self->{strp}->parse_datetime($result->{upsBasicBatteryLastReplaceDate}); |
| 339 |
if (defined($dt)) { |
| 340 |
$self->{global}->{last_replace_time} = time() - $dt->epoch(); |
| 341 |
} else { |
| 342 |
$self->{output}->output_add(long_msg => "cannot parse date: " . $result->{upsBasicBatteryLastReplaceDate} . ' (please use option --replace-lasttime-format)'); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
$self->add_battery_pack(snmp_result => $snmp_result); |
| 347 |
|
| 348 |
} |
| 349 |
|
| 350 |
1; |
| 351 |
|
| 352 |
__END__ |
| 353 |
|
| 354 |
=head1 MODE |
| 355 |
|
| 356 |
Check battery status and battery charge remaining. |
| 357 |
|
| 358 |
=over 8 |
| 359 |
|
| 360 |
=item B<--filter-counters> |
| 361 |
|
| 362 |
Only display some counters (regexp can be used). |
| 363 |
Example: --filter-counters='^status|load$' |
| 364 |
|
| 365 |
=item B<--replace-lasttime-format> |
| 366 |
|
| 367 |
Define the date format (default: '%m/%d/%Y'). |
| 368 |
|
| 369 |
=item B<--unknown-status> |
| 370 |
|
| 371 |
Define the conditions to match for the status to be UNKNOWN (default: '%{status} =~ /unknown/i'). |
| 372 |
You can use the following variables: %{status}, %{replace} |
| 373 |
|
| 374 |
=item B<--warning-status> |
| 375 |
|
| 376 |
Define the conditions to match for the status to be WARNING (default: '%{status} =~ /batteryLow/i'). |
| 377 |
You can use the following variables: %{status}, %{replace} |
| 378 |
|
| 379 |
=item B<--critical-status> |
| 380 |
|
| 381 |
Define the conditions to match for the status to be CRITICAL (default: '%{replace} =~ /yes/i'). |
| 382 |
You can use the following variables: %{status}, %{replace} |
| 383 |
|
| 384 |
=item B<--unknown-battery-pack-status> |
| 385 |
|
| 386 |
Define the conditions to match for the status to be UNKNOWN. |
| 387 |
You can use the following variables: %{status} |
| 388 |
|
| 389 |
=item B<--warning-battery-pack-status> |
| 390 |
|
| 391 |
Define the conditions to match for the status to be WARNING. |
| 392 |
You can use the following variables: %{status} |
| 393 |
|
| 394 |
=item B<--critical-battery-pack-status> |
| 395 |
|
| 396 |
Define the conditions to match for the status to be CRITICAL (default: '%{status} ne "OK"'). |
| 397 |
You can use the following variables: %{status} |
| 398 |
|
| 399 |
=item B<--unknown-cartridge-status> |
| 400 |
|
| 401 |
Define the conditions to match for the status to be UNKNOWN. |
| 402 |
You can use the following variables: %{status} |
| 403 |
|
| 404 |
=item B<--warning-cartridge-status> |
| 405 |
|
| 406 |
Define the conditions to match for the status to be WARNING. |
| 407 |
You can use the following variables: %{status} |
| 408 |
|
| 409 |
=item B<--critical-cartridge-status> |
| 410 |
|
| 411 |
Define the conditions to match for the status to be CRITICAL (default: '%{status} ne "OK"'). |
| 412 |
You can use the following variables: %{status} |
| 413 |
|
| 414 |
=item B<--warning-*> B<--critical-*> |
| 415 |
|
| 416 |
Thresholds. |
| 417 |
Can be: 'load', 'voltage', 'current', |
| 418 |
'temperature', 'time', 'replace-lasttime', 'timeon'. |
| 419 |
|
| 420 |
=back |
| 421 |
|
| 422 |
=cut |