Debian Codegraph @

src/hardware/devices/eltek/enexus/snmp/mode/load.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::load;
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(
33 'status: %s',
34 $self->{result_values}->{status}
35 );
36 }
37
38 sub set_counters {
39 my ($self, %options) = @_;
40
41 $self->{maps_counters_type} = [
42 { name => 'load', type => 0, cb_prefix_output => 'prefix_load_output', skipped_code => { -10 => 1 } },
43 { name => 'phase', type => 1, cb_prefix_output => 'prefix_phase_output', message_multiple => 'All phases are ok', skipped_code => { -10 => 1 } }
44 ];
45
46 $self->{maps_counters}->{load} = [
47 { label => 'status', threshold => 0, set => {
48 key_values => [ { name => 'status' } ],
49 closure_custom_calc => \&catalog_status_calc,
50 closure_custom_output => $self->can('custom_status_output'),
51 closure_custom_perfdata => sub { return 0; },
52 closure_custom_threshold_check => \&catalog_status_threshold
53 }
54 },
55 { label => 'current', nlabel => 'load.current.ampere', set => {
56 key_values => [ { name => 'current' } ],
57 output_template => 'current: %s A',
58 perfdatas => [
59 { value => 'current', template => '%s', unit => 'A', min => 0 }
60 ]
61 }
62 },
63 { label => 'power', nlabel => 'load.power.watt', display_ok => 0, set => {
64 key_values => [ { name => 'power' } ],
65 output_template => 'power: %s W',
66 perfdatas => [
67 { value => 'power', template => '%s', unit => 'W', min => 0 }
68 ]
69 }
70 }
71 ];
72
73 $self->{maps_counters}->{phase} = [
74 { label => 'voltage', nlabel => 'phase.voltage.volt', set => {
75 key_values => [ { name => 'voltage' } ],
76 output_template => 'voltage: %.2f V',
77 perfdatas => [
78 { value => 'voltage', template => '%.2f', unit => 'V', label_extra_instance => 1 }
79 ]
80 }
81 }
82 ];
83 }
84
85 sub prefix_load_output {
86 my ($self, %options) = @_;
87
88 return 'Load ';
89 }
90
91 sub prefix_phase_output {
92 my ($self, %options) = @_;
93
94 return "Phase '" . $options{instance_value}->{display} . "' ";
95 }
96
97 sub new {
98 my ($class, %options) = @_;
99 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
100 bless $self, $class;
101
102 $options{options}->add_options(arguments => {
103 'unknown-status:s' => { name => 'unknown_status', default => '' },
104 'warning-status:s' => { name => 'warning_status', default => '%{status} =~ /minor|warning/i' },
105 'critical-status:s' => { name => 'critical_status', default => '%{status} =~ /error|major|critical/i' }
106 });
107
108 return $self;
109 }
110
111 sub check_options {
112 my ($self, %options) = @_;
113 $self->SUPER::check_options(%options);
114
115 $self->change_macros(macros => [
116 'warning_status', 'critical_status', 'unknown_status',
117 ]);
118 }
119
120 my $map_status = {
121 0 => 'error', 1 => 'normal', 2 => 'minorAlarm', 3 => 'majorAlarm',
122 4 => 'disabled', 5 => 'disconnected', 6 => 'notPresent',
123 7 => 'minorAndMajor', 8 => 'majorLow', 9 => 'minorLow',
124 10 => 'majorHigh', 11 => 'minorHigh', 12 => 'event',
125 13 => 'valueVolt', 14 => 'valueAmp', 15 => 'valueTemp',
126 16 => 'valueUnit', 17 => 'valuePerCent', 18 => 'critical',
127 19 => 'warning'
128 };
129 my $map_decimal_setting = { 0 => 'ampere', 1 => 'deciAmpere' };
130
131 my $mapping = {
132 powerSystemCurrentDecimalSetting => { oid => '.1.3.6.1.4.1.12148.10.2.15', map => $map_decimal_setting },
133 loadStatus => { oid => '.1.3.6.1.4.1.12148.10.9.1', map => $map_status },
134 loadCurrentValue => { oid => '.1.3.6.1.4.1.12148.10.9.2.5' }, # A or dA
135 loadCurrentMajorHighLevel => { oid => '.1.3.6.1.4.1.12148.10.9.2.6' },
136 loadCurrentMinorHighLevel => { oid => '.1.3.6.1.4.1.12148.10.9.2.7' },
137 loadEnergyLogAccumulated => { oid => '.1.3.6.1.4.1.12148.10.9.8.1' }, # Watt
138 batteryVoltageValue => { oid => '.1.3.6.1.4.1.12148.10.10.5.5' } # not sure we should use that value
139 };
140
141 sub threshold_eltek_configured {
142 my ($self, %options) = @_;
143
144 if ((!defined($self->{option_results}->{'critical-' . $options{label}}) || $self->{option_results}->{'critical-' . $options{label}} eq '') &&
145 (!defined($self->{option_results}->{'warning-' . $options{label}}) || $self->{option_results}->{'warning-' . $options{label}} eq '')) {
146 my ($crit, $warn) = ('', '');
147 $crit = $options{low_crit} . ':' if (defined($options{low_crit}) && $options{low_crit} ne '');
148 $crit .= $options{high_crit} if (defined($options{high_crit}) && $options{high_crit} ne '');
149 $warn = $options{low_warn} . ':' if (defined($options{low_warn}) && $options{low_warn} ne '');
150 $warn .= $options{high_warn} if (defined($options{high_warn}) && $options{high_warn} ne '');
151 $self->{perfdata}->threshold_validate(label => 'critical-' . $options{label}, value => $crit);
152 $self->{perfdata}->threshold_validate(label => 'warning-' . $options{label}, value => $warn);
153 }
154 }
155
156 sub manage_selection {
157 my ($self, %options) = @_;
158
159 my $snmp_result = $options{snmp}->get_leef(
160 oids => [ map($_->{oid} . '.0', values(%$mapping)) ],
161 nothing_quit => 1
162 );
163 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => '0');
164
165 my $scale_current = 1;
166 $scale_current = 0.1 if ($result->{powerSystemCurrentDecimalSetting} eq 'deciAmpere');
167 $self->{load} = {
168 status => $result->{loadStatus},
169 power => $result->{loadCurrentValue} * $scale_current * ($result->{batteryVoltageValue} * 0.01),
170 current => $result->{loadCurrentValue} * $scale_current
171 };
172
173 $self->threshold_eltek_configured(
174 label => 'load-current-ampere',
175 high_crit => $result->{loadCurrentMajorHighLevel} * $scale_current,
176 high_warn => $result->{loadCurrentMinorHighLevel} * $scale_current
177 );
178
179 my $oid_loadVoltageValue = '.1.3.6.1.4.1.12148.10.9.9.1.6';
180 $snmp_result = $options{snmp}->get_table(oid => $oid_loadVoltageValue);
181 $self->{phase} = {};
182 foreach (keys %$snmp_result) {
183 /\.(\d+)$/;
184 $self->{phase}->{$1} = { display => $1, voltage => $snmp_result->{$_} * 0.01 };
185 }
186 }
187
188 1;
189
190 __END__
191
192 =head1 MODE
193
194 Check load.
195
196 =over 8
197
198 =item B<--unknown-status>
199
200 Define the conditions to match for the status to be UNKNOWN.
201 You can use the following variables: %{status}
202
203 =item B<--warning-status>
204
205 Define the conditions to match for the status to be WARNING (default: '%{status} =~ /minor|warning/i').
206 You can use the following variables: %{status}
207
208 =item B<--critical-status>
209
210 Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /error|major|critical/i').
211 You can use the following variables: %{status}
212
213 =item B<--warning-*> B<--critical-*>
214
215 Thresholds.
216 Can be: 'current', 'power'.
217
218 =back
219
220 =cut