Debian Codegraph @

src/hardware/ups/mge/snmp/mode/batterystatus.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::mge::snmp::mode::batterystatus;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27
28 my %map_battery_status = (
29 '.1.3.6.1.4.1.705.1.5.9.0' => 'BatteryFaultBattery',
30 '.1.3.6.1.4.1.705.1.5.10.0' => 'BatteryNoBattery',
31 '.1.3.6.1.4.1.705.1.5.11.0' => 'BatteryReplacement',
32 '.1.3.6.1.4.1.705.1.5.12.0' => 'BatteryUnavailableBattery',
33 '.1.3.6.1.4.1.705.1.5.13.0' => 'BatteryNotHighCharge',
34 '.1.3.6.1.4.1.705.1.5.14.0' => 'BatteryLowBattery',
35 '.1.3.6.1.4.1.705.1.5.15.0' => 'BatteryChargerFault',
36 '.1.3.6.1.4.1.705.1.5.16.0' => 'BatteryLowCondition',
37 '.1.3.6.1.4.1.705.1.5.17.0' => 'BatteryLowRecharge',
38 );
39
40 sub new {
41 my ($class, %options) = @_;
42 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
43 bless $self, $class;
44
45 $options{options}->add_options(arguments =>
46 {
47 "warning:s" => { name => 'warning', },
48 "critical:s" => { name => 'critical', },
49 "filter-status:s" => { name => 'filter_status', },
50 });
51
52 return $self;
53 }
54
55 sub check_options {
56 my ($self, %options) = @_;
57 $self->SUPER::init(%options);
58
59 if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) {
60 $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'.");
61 $self->{output}->option_exit();
62 }
63 if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) {
64 $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'.");
65 $self->{output}->option_exit();
66 }
67 }
68
69 sub run {
70 my ($self, %options) = @_;
71 $self->{snmp} = $options{snmp};
72
73 my $oid_upsmgBattery = '.1.3.6.1.4.1.705.1.5';
74 my $oid_upsmgBatteryRemainingTime = '.1.3.6.1.4.1.705.1.5.1.0'; # in seconds
75 my $oid_upsmgBatteryLevel = '.1.3.6.1.4.1.705.1.5.2.0';
76 my $oid_upsmgBatteryVoltage = '.1.3.6.1.4.1.705.1.5.5.0'; # in dV
77 my $oid_upsmgBatteryCurrent = '.1.3.6.1.4.1.705.1.5.6.0'; # in dA
78 my $oid_upsmgBatteryTemperature = '.1.3.6.1.4.1.705.1.5.7.0'; # in degrees Centigrade
79
80 my $result = $self->{snmp}->get_table(oid => $oid_upsmgBattery, nothing_quit => 1);
81
82 my $current = defined($result->{$oid_upsmgBatteryCurrent}) ? $result->{$oid_upsmgBatteryCurrent} * 0.1 : 0;
83 my $voltage = defined($result->{$oid_upsmgBatteryVoltage}) ? $result->{$oid_upsmgBatteryVoltage} * 0.1 : 0;
84 my $min_remain = defined($result->{$oid_upsmgBatteryRemainingTime}) ? int($result->{$oid_upsmgBatteryRemainingTime} / 60) : 'unknown';
85 my $charge_remain = defined($result->{$oid_upsmgBatteryLevel}) ? $result->{$oid_upsmgBatteryLevel} : 'unknown';
86 my $temp = defined($result->{$oid_upsmgBatteryTemperature}) ? $result->{$oid_upsmgBatteryTemperature} : 0;
87
88 $self->{output}->output_add(severity => 'OK',
89 short_msg => sprintf("Battery status is ok"));
90 foreach my $oid ($self->{snmp}->oid_lex_sort(keys %map_battery_status)) {
91 next if (defined($self->{option_results}->{filter_status}) && $self->{option_results}->{filter_status} ne '' && $map_battery_status{$oid} =~ /$self->{option_results}->{filter_status}/);
92 if (defined($result->{$oid}) && $result->{$oid} == 1) {
93 $self->{output}->output_add(severity => 'CRITICAL',
94 short_msg => sprintf("Battery status is '%s'", $map_battery_status{$oid}));
95 }
96 }
97
98 my $exit_code = 'ok';
99 if ($charge_remain ne 'unknown') {
100 $exit_code = $self->{perfdata}->threshold_check(value => $charge_remain,
101 threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]);
102 $self->{output}->perfdata_add(label => 'load', unit => '%',
103 value => $charge_remain,
104 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'),
105 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'),
106 min => 0, max => 100);
107 }
108 $self->{output}->output_add(severity => $exit_code,
109 short_msg => sprintf("Charge remaining: %s%% (%s minutes remaining)", $charge_remain, $min_remain));
110
111 if ($current != 0) {
112 $self->{output}->perfdata_add(label => 'current', unit => 'A',
113 value => $current,
114 );
115 }
116 if ($voltage != 0) {
117 $self->{output}->perfdata_add(label => 'voltage', unit => 'V',
118 value => $voltage,
119 );
120 }
121 if ($temp != 0) {
122 $self->{output}->perfdata_add(label => 'temp', unit => 'C',
123 value => $temp,
124 );
125 }
126
127 $self->{output}->display();
128 $self->{output}->exit();
129 }
130
131 1;
132
133 __END__
134
135 =head1 MODE
136
137 Check Battery Status and battery charge remaining.
138
139 =over 8
140
141 =item B<--warning>
142
143 Warning threshold in percent of charge remaining.
144
145 =item B<--critical>
146
147 Critical threshold in percent of charge remaining.
148
149 =item B<--filter-status>
150
151 Filter on status. (can be a regexp)
152
153 =back
154
155 =cut