Debian Codegraph @

src/hardware/ups/alpha/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::alpha::snmp::mode::batterystatus;
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("Battery status is '%s'", $self->{result_values}->{status});
33 return $msg;
34 }
35
36 sub custom_status_calc {
37 my ($self, %options) = @_;
38
39 $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_upsBatteryStatus'};
40 return 0;
41 }
42
43 sub set_counters {
44 my ($self, %options) = @_;
45
46 $self->{maps_counters_type} = [
47 { name => 'global', type => 0 },
48 ];
49
50 $self->{maps_counters}->{global} = [
51 { label => 'status', threshold => 0, set => {
52 key_values => [ { name => 'upsBatteryStatus' } ],
53 closure_custom_calc => $self->can('custom_status_calc'),
54 closure_custom_output => $self->can('custom_status_output'),
55 closure_custom_perfdata => sub { return 0; },
56 closure_custom_threshold_check => \&catalog_status_threshold,
57 }
58 },
59 { label => 'load', set => {
60 key_values => [ { name => 'upsBatteryCapacity' } ],
61 output_template => 'Remaining capacity : %s %%',
62 perfdatas => [
63 { label => 'load', value => 'upsBatteryCapacity', template => '%s',
64 min => 0, max => 100, unit => '%' },
65 ],
66 }
67 },
68 { label => 'current', set => {
69 key_values => [ { name => 'upsBatteryChargingCurrent' } ],
70 output_template => 'Current : %s A',
71 perfdatas => [
72 { label => 'current', value => 'upsBatteryChargingCurrent', template => '%s',
73 min => 0, unit => 'A' },
74 ],
75 }
76 },
77 { label => 'voltage', set => {
78 key_values => [ { name => 'upsBatteryVoltage' } ],
79 output_template => 'Voltage : %s V',
80 perfdatas => [
81 { label => 'voltage', value => 'upsBatteryVoltage', template => '%s',
82 unit => 'V' },
83 ],
84 }
85 },
86 { label => 'temperature', set => {
87 key_values => [ { name => 'upsBatteryTemperature' } ],
88 output_template => 'Temperature : %s C',
89 perfdatas => [
90 { label => 'temperature', value => 'upsBatteryTemperature', template => '%s',
91 unit => 'C'},
92 ],
93 }
94 },
95 ];
96 }
97
98 sub new {
99 my ($class, %options) = @_;
100 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
101 bless $self, $class;
102
103 $options{options}->add_options(arguments => {
104 'unknown-status:s' => { name => 'unknown_status', default => '%{status} =~ /unknown/i' },
105 'warning-status:s' => { name => 'warning_status', default => '%{status} =~ /batteryLow/i' },
106 'critical-status:s' => { name => 'critical_status', default => '%{status} =~ /batteryDepleted/i' },
107 });
108
109 return $self;
110 }
111
112 sub check_options {
113 my ($self, %options) = @_;
114 $self->SUPER::check_options(%options);
115
116 $self->change_macros(macros => ['warning_status', 'critical_status', 'unknown_status']);
117 }
118
119 my %map_battery_status = (
120 1 => 'unknown', 2 => 'batteryNormal', 3 => 'batteryLow', 4 => 'batteryDepleted',
121 );
122
123 my $mapping = {
124 upsBatteryStatus => { oid => '.1.3.6.1.4.1.7309.6.1.2.1', map => \%map_battery_status },
125 upsBatteryVoltage => { oid => '.1.3.6.1.4.1.7309.6.1.2.3' },
126 upsBatteryChargingCurrent => { oid => '.1.3.6.1.4.1.7309.6.1.2.4' },
127 upsBatteryCapacity => { oid => '.1.3.6.1.4.1.7309.6.1.2.5' },
128 upsBatteryTemperature => { oid => '.1.3.6.1.4.1.7309.6.1.2.6' },
129 };
130 my $oid_upsBattery = '.1.3.6.1.4.1.7309.6.1.2';
131
132 sub manage_selection {
133 my ($self, %options) = @_;
134
135 my $snmp_result = $options{snmp}->get_table(
136 oid => $oid_upsBattery,
137 nothing_quit => 1
138 );
139
140 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => '0');
141 $result->{upsBatteryVoltage} *= 0.1;
142 $result->{upsBatteryChargingCurrent} *= 0.1;
143 $self->{global} = { %$result };
144 }
145
146 1;
147
148 __END__
149
150 =head1 MODE
151
152 Check battery status.
153
154 =over 8
155
156 =item B<--filter-counters>
157
158 Only display some counters (regexp can be used).
159 Example: --filter-counters='^status|load$'
160
161 =item B<--unknown-status>
162
163 Define the conditions to match for the status to be UNKNOWN (default: '%{status} =~ /unknown/i').
164 You can use the following variables: %{status}
165
166 =item B<--warning-status>
167
168 Define the conditions to match for the status to be WARNING (default: '%{status} =~ /batteryLow/i').
169 You can use the following variables: %{status}
170
171 =item B<--critical-status>
172
173 Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /batteryDepleted/i').
174 You can use the following variables: %{status}
175
176 =item B<--warning-*>
177
178 Warning threshold.
179 Can be: 'load', 'voltage', 'current', 'temperature'.
180
181 =item B<--critical-*>
182
183 Critical threshold.
184 Can be: 'load', 'voltage', 'current', 'temperature'.
185
186 =back
187
188 =cut