Debian Codegraph @

src/hardware/devices/eltek/enexus/snmp/mode/outputs.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::outputs;
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 use centreon::plugins::misc;
29
30 sub custom_status_output {
31 my ($self, %options) = @_;
32
33 return sprintf('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 => 'global', type => 0 },
43 { name => 'cuo', type => 1, cb_prefix_output => 'prefix_cuo_output', message_multiple => 'All outputs for control units are ok', skipped_code => { -10 => 1 } }
44 ];
45
46 $self->{maps_counters}->{global} = [
47 { label => 'outputs-disconnected', nlabel => 'outputs.disconnected.count', display_ok => 0, set => {
48 key_values => [ { name => 'disconnected' }, { name => 'total_contactors' } ],
49 output_template => 'current disconnected outputs: %d',
50 perfdatas => [
51 { value => 'disconnected', template => '%d', min => 0, max => 'total_contactors' }
52 ]
53 }
54 },
55 { label => 'outputs-notenergized', nlabel => 'outputs.notenergized.count', display_ok => 0, set => {
56 key_values => [ { name => 'notenergized' }, { name => 'total_relay' } ],
57 output_template => 'current not energized outputs: %d',
58 perfdatas => [
59 { value => 'notenergized', template => '%d', min => 0, max => 'total_relay' }
60 ]
61 }
62 }
63 ];
64
65 $self->{maps_counters}->{cuo} = [
66 { label => 'status', threshold => 0, set => {
67 key_values => [ { name => 'status' }, { name => 'name' } ],
68 closure_custom_calc => \&catalog_status_calc,
69 closure_custom_output => $self->can('custom_status_output'),
70 closure_custom_perfdata => sub { return 0; },
71 closure_custom_threshold_check => \&catalog_status_threshold
72 }
73 }
74 ];
75 }
76
77 sub prefix_cuo_output {
78 my ($self, %options) = @_;
79
80 return "Control unit output '" . $options{instance_value}->{name} . "' ";
81 }
82
83 sub new {
84 my ($class, %options) = @_;
85 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
86 bless $self, $class;
87
88 $options{options}->add_options(arguments => {
89 'filter-name:s' => { name => 'filter_name' },
90 'unknown-status:s' => { name => 'unknown_status', default => '' },
91 'warning-status:s' => { name => 'warning_status', default => '' },
92 'critical-status:s' => { name => 'critical_status', default => '%{status} =~ /notenergized|disconnected/i' }
93 });
94
95 return $self;
96 }
97
98 sub check_options {
99 my ($self, %options) = @_;
100 $self->SUPER::check_options(%options);
101
102 $self->change_macros(macros => [
103 'warning_status', 'critical_status', 'unknown_status',
104 ]);
105 }
106
107 my $map_status = { 0 => 'notenergized', 1 => 'energized', 2 => 'disconnected', 3 => 'connected' };
108
109 my $mapping = {
110 outputControlUnitOutputStatus => { oid => '.1.3.6.1.4.1.12148.10.12.2.1.2', map => $map_status },
111 outputControlUnitOutputDescription => { oid => '.1.3.6.1.4.1.12148.10.12.2.1.3' }
112 };
113
114 sub manage_selection {
115 my ($self, %options) = @_;
116
117 my $oid_outputControlUnitOutputEntry = '.1.3.6.1.4.1.12148.10.12.2.1';
118 my $snmp_result = $options{snmp}->get_table(
119 oid => $oid_outputControlUnitOutputEntry,
120 start => $mapping->{outputControlUnitOutputStatus}->{oid},
121 nothing_quit => 1
122 );
123
124 $self->{global} = { total_relay => 0, total_contactors => 0, energized => 0, notenergized => 0, connected => 0, disconnected => 0 };
125 $self->{cuo} = {};
126 my $duplicated = {};
127 foreach my $oid (keys %$snmp_result) {
128 next if ($oid !~ /^$mapping->{outputControlUnitOutputStatus}->{oid}\.(.*)$/);
129 my $instance = $1;
130
131 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
132 my $name = centreon::plugins::misc::trim($result->{outputControlUnitOutputDescription});
133 $name = $instance if ($name eq '');
134 $name = $instance if (defined($duplicated->{$name}));
135
136 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
137 $name !~ /$self->{option_results}->{filter_name}/) {
138 $self->{output}->output_add(long_msg => "skipping control unit output '" . $name . "'.", debug => 1);
139 next;
140 }
141
142 if (defined($self->{cuo}->{$name})) {
143 $duplicated->{$name} = 1;
144 my $instance2 = $self->{cuo}->{$name}->{instance};
145 $self->{cuo}->{$instance2} = $self->{cuo}->{$name};
146 $self->{cuo}->{$instance2}->{name} = $instance2;
147 delete $self->{cuo}->{$name};
148 $name = $instance;
149 }
150
151 $self->{cuo}->{$name} = {
152 instance => $instance,
153 name => $name,
154 status => $result->{outputControlUnitOutputStatus}
155 };
156 $self->{global}->{total_relay}++ if ($result->{outputControlUnitOutputStatus} =~ /energized/);
157 $self->{global}->{total_contactors}++ if ($result->{outputControlUnitOutputStatus} =~ /connected/);
158 $self->{global}->{$result->{outputControlUnitOutputStatus}}++;
159 }
160 }
161
162 1;
163
164 __END__
165
166 =head1 MODE
167
168 Check outputs for control units.
169
170 =over 8
171
172 =item B<--filter-name>
173
174 Filter name (can be a regexp).
175
176 =item B<--unknown-status>
177
178 Define the conditions to match for the status to be UNKNOWN.
179 You can use the following variables: %{status}, %{name}
180
181 =item B<--warning-status>
182
183 Define the conditions to match for the status to be WARNING.
184 You can use the following variables: %{status}, %{name}
185
186 =item B<--critical-status>
187
188 Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /notenergized|disconnected/i').
189 You can use the following variables: %{status}, %{name}
190
191 =item B<--warning-*> B<--critical-*>
192
193 Thresholds.
194 Can be: 'outputs-disconnected', 'outputs-notenergized'.
195
196 =back
197
198 =cut