Debian Codegraph @

src/centreon/common/cps/ups/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 centreon::common::cps::ups::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 catalog_status_calc);
28
29 sub custom_status_output {
30 my ($self, %options) = @_;
31
32 return sprintf('battery status is %s', $self->{result_values}->{status});
33 }
34
35 sub custom_load_output {
36 my ($self, %options) = @_;
37
38 return sprintf("charge remaining: %s%% (%s minutes remaining)",
39 $self->{result_values}->{charge_remain},
40 $self->{result_values}->{minute_remain}
41 );
42 }
43
44 sub set_counters {
45 my ($self, %options) = @_;
46
47 $self->{maps_counters_type} = [
48 { name => 'global', type => 0, message_separator => ' - ', skipped_code => { -10 => 1 } },
49 ];
50
51 $self->{maps_counters}->{global} = [
52 { label => 'status', threshold => 0, set => {
53 key_values => [ { name => 'status' } ],
54 closure_custom_calc => \&catalog_status_calc,
55 closure_custom_output => $self->can('custom_status_output'),
56 closure_custom_perfdata => sub { return 0; },
57 closure_custom_threshold_check => \&catalog_status_threshold,
58 }
59 },
60 { label => 'charge-remaining', nlabel => 'battery.charge.remaining.percent', set => {
61 key_values => [ { name => 'charge_remain' }, { name => 'minute_remain' } ],
62 closure_custom_output => $self->can('custom_load_output'),
63 perfdatas => [
64 { value => 'charge_remain', template => '%s', min => 0, max => 100, unit => '%' },
65 ],
66 }
67 },
68 { label => 'voltage', nlabel => 'battery.voltage.volt', display_ok => 0, set => {
69 key_values => [ { name => 'voltage', no_value => 0 } ],
70 output_template => 'voltage: %s V',
71 perfdatas => [
72 { value => 'voltage', template => '%s', unit => 'V' },
73 ],
74 }
75 },
76 { label => 'temperature', nlabel => 'battery.temperature.celsius', display_ok => 0, set => {
77 key_values => [ { name => 'temperature', no_value => 0 } ],
78 output_template => 'temperature: %s C',
79 perfdatas => [
80 { value => 'temperature', template => '%s', unit => 'C' },
81 ],
82 }
83 },
84 ];
85 }
86
87 sub new {
88 my ($class, %options) = @_;
89 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
90 bless $self, $class;
91
92 $options{options}->add_options(arguments => {
93 'unknown-status:s' => { name => 'unknown_status', default => '%{status} =~ /unknown|notPresent/i' },
94 'warning-status:s' => { name => 'warning_status', default => '%{status} =~ /low/i' },
95 'critical-status:s' => { name => 'critical_status', default => '' },
96 });
97
98 return $self;
99 }
100
101 sub check_options {
102 my ($self, %options) = @_;
103 $self->SUPER::check_options(%options);
104
105 $self->change_macros(macros => ['unknown_status', 'warning_status', 'critical_status']);
106 }
107
108 my $map_status = { 1 => 'unknown', 2 => 'normal', 3 => 'low', 4 => 'notPresent' };
109
110 my $mapping = {
111 upsBaseBatteryStatus => { oid => '.1.3.6.1.4.1.3808.1.1.1.2.1.1', map => $map_status },
112 upsAdvanceBatteryCapacity => { oid => '.1.3.6.1.4.1.3808.1.1.1.2.2.1' },
113 upsAdvanceBatteryVoltage => { oid => '.1.3.6.1.4.1.3808.1.1.1.2.2.2' }, # in dV
114 upsAdvanceBatteryTemperature => { oid => '.1.3.6.1.4.1.3808.1.1.1.2.2.3' }, # in degrees Centigrade
115 upsAdvanceBatteryRunTimeRemaining => { oid => '.1.3.6.1.4.1.3808.1.1.1.2.2.4' },
116 };
117
118 sub manage_selection {
119 my ($self, %options) = @_;
120
121 my $oid_upsBattery = '.1.3.6.1.4.1.3808.1.1.1.2';
122 my $snmp_result = $options{snmp}->get_table(oid => $oid_upsBattery, nothing_quit => 1);
123 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => '0');
124
125 $self->{global} = {
126 voltage => (defined($result->{upsAdvanceBatteryVoltage}) && $result->{upsAdvanceBatteryVoltage} =~ /\d/) ? $result->{upsAdvanceBatteryVoltage} * 0.1 : 0,
127 temperature => $result->{upsAdvanceBatteryTemperature},
128 minute_remain => (defined($result->{upsAdvanceBatteryRunTimeRemaining}) && $result->{upsAdvanceBatteryRunTimeRemaining} =~ /\d/) ? ($result->{upsAdvanceBatteryRunTimeRemaining} / 100 / 60) : 'unknown',
129 charge_remain => (defined($result->{upsAdvanceBatteryCapacity}) && $result->{upsAdvanceBatteryCapacity} =~ /\d/) ? $result->{upsAdvanceBatteryCapacity} : undef,
130 status => $result->{upsBaseBatteryStatus},
131 };
132 }
133
134 1;
135
136 __END__
137
138 =head1 MODE
139
140 Check battery status and charge remaining.
141
142 =over 8
143
144 =item B<--unknown-status>
145
146 Define the conditions to match for the status to be UNKNOWN (default: '%{status} =~ /unknown|notPresent/i').
147 You can use the following variables: %{status}
148
149 =item B<--warning-status>
150
151 Define the conditions to match for the status to be WARNING (default: '%{status} =~ /low/i').
152 You can use the following variables: %{status}
153
154 =item B<--critical-status>
155
156 Define the conditions to match for the status to be CRITICAL (default: '').
157 You can use the following variables: %{status}
158
159 =item B<--warning-*> B<--critical-*>
160
161 Thresholds.
162 Can be: 'charge-remaining' (%), 'voltage' (V), 'temperature' (C).
163
164 =back
165
166 =cut