Debian Codegraph @

src/network/microsens/g6/snmp/mode/sfp.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 network::microsens::g6::snmp::mode::sfp;
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_ng);
28
29 sub sfp_long_output {
30 my ($self, %options) = @_;
31
32 return sprintf(
33 "checking sfp port '%s'%s",
34 $options{instance},
35 $options{instance_value}->{location} ne '' ? ' [location: ' . $options{instance_value}->{location} . ']' : ''
36 );
37 }
38
39 sub prefix_sfp_output {
40 my ($self, %options) = @_;
41
42 return sprintf(
43 "sfp port '%s'%s ",
44 $options{instance},
45 $options{instance_value}->{location} ne '' ? ' [location: ' . $options{instance_value}->{location} . ']' : ''
46 );
47 }
48
49 sub set_counters {
50 my ($self, %options) = @_;
51
52 $self->{maps_counters_type} = [
53 { name => 'sfp', type => 3, cb_prefix_output => 'prefix_sfp_output', cb_long_output => 'sfp_long_output', indent_long_output => ' ', message_multiple => 'All sfp ports are ok',
54 group => [
55 { name => 'status', type => 0, skipped_code => { -10 => 1 } },
56 { name => 'power', type => 0, skipped_code => { -10 => 1 } },
57 { name => 'temperature', type => 0, skipped_code => { -10 => 1 } }
58 ]
59 }
60 ];
61
62 $self->{maps_counters}->{status} = [
63 { label => 'status', type => 2, critical_default => '%{status} =~ /txFailure|lossOfSignal|readError/', set => {
64 key_values => [ { name => 'status' }, { name => 'location' }, { name => 'port' } ],
65 output_template => 'status: %s',
66 closure_custom_perfdata => sub { return 0; },
67 closure_custom_threshold_check => \&catalog_status_threshold_ng
68 }
69 }
70 ];
71
72 $self->{maps_counters}->{power} = [
73 { label => 'input-power', nlabel => 'port.input.power.dbm', set => {
74 key_values => [ { name => 'input' } ],
75 output_template => 'input power: %s dBm',
76 perfdatas => [
77 { template => '%s', unit => 'dBm', label_extra_instance => 1 }
78 ]
79 }
80 },
81 { label => 'output-power', nlabel => 'port.output.power.dbm', set => {
82 key_values => [ { name => 'output' } ],
83 output_template => 'output power: %s dBm',
84 perfdatas => [
85 { template => '%s', unit => 'dBm', label_extra_instance => 1 }
86 ]
87 }
88 }
89 ];
90
91 $self->{maps_counters}->{temperature} = [
92 { label => 'temperature', nlabel => 'port.temperature.celsius', set => {
93 key_values => [ { name => 'temperature' } ],
94 output_template => 'temperature: %.2f C',
95 perfdatas => [
96 { template => '%s', unit => 'C', label_extra_instance => 1 }
97 ]
98 }
99 }
100 ];
101 }
102
103 sub new {
104 my ($class, %options) = @_;
105 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
106 bless $self, $class;
107
108 $options{options}->add_options(arguments => {
109 'filter-port:s' => { name => 'filter_port' }
110 });
111
112 return $self;
113 }
114
115 my $map_status = {
116 0 => 'ok', 1 => 'laserDisabled', 2 => 'lossOfSignal',
117 3 => 'txFailure', 4 => 'readError'
118 };
119 my $mapping = {
120 location => { oid => '.1.3.6.1.4.1.3181.10.6.1.34.100.1.3' }, # informationLocation
121 status => { oid => '.1.3.6.1.4.1.3181.10.6.1.34.100.1.4', map => $map_status }, # informationStatus
122 output_power => { oid => '.1.3.6.1.4.1.3181.10.6.1.34.100.1.18' }, # informationTxPower
123 input_power => { oid => '.1.3.6.1.4.1.3181.10.6.1.34.100.1.19' }, # informationRxPower
124 temperature => { oid => '.1.3.6.1.4.1.3181.10.6.1.34.100.1.20' } # informationTemperature
125 };
126
127 sub manage_selection {
128 my ($self, %options) = @_;
129
130 my $oid_port = '.1.3.6.1.4.1.3181.10.6.1.34.100.1.2'; # informationPort
131 my $snmp_result = $options{snmp}->get_table(
132 oid => $oid_port,
133 nothing_quit => 1
134 );
135
136 $self->{sfp} = {};
137 foreach my $oid (keys %$snmp_result) {
138 $oid =~ /^$oid_port\.(.*)$/;
139 my $instance = $1;
140
141 my $port = $snmp_result->{$oid};
142 next if ($port == 0);
143
144 if (defined($self->{option_results}->{filter_port}) && $self->{option_results}->{filter_port} ne '' &&
145 $port !~ /$self->{option_results}->{filter_port}/) {
146 $self->{output}->output_add(long_msg => "skipping '" . $port . "': no matching filter.", debug => 1);
147 next;
148 }
149
150 $self->{sfp}->{$port} = {
151 instance => $instance,
152 status => { port => $port },
153 power => {},
154 temperature => {}
155 };
156 }
157
158 if (scalar(keys %{$self->{sfp}}) <= 0) {
159 $self->{output}->add_option_msg(short_msg => 'No sfp port found.');
160 $self->{output}->option_exit();
161 }
162
163 $options{snmp}->load(
164 oids => [
165 map($_->{oid}, values(%$mapping))
166 ],
167 instances => [ map($_->{instance}, values(%{$self->{sfp}})) ],
168 instance_regexp => '^(.*)$'
169 );
170 $snmp_result = $options{snmp}->get_leef(nothing_quit => 1);
171
172 foreach (keys %{$self->{sfp}}) {
173 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $self->{sfp}->{$_}->{instance});
174 $self->{sfp}->{$_}->{status}->{status} = $result->{status};
175 $self->{sfp}->{$_}->{location} = $result->{location};
176 $self->{sfp}->{$_}->{status}->{location} = $result->{location};
177
178 $self->{sfp}->{$_}->{power}->{output} = $1 if ($result->{output_power} =~ /([0-9\.]+)/);
179 $self->{sfp}->{$_}->{power}->{input} = $1 if ($result->{input_power} =~ /([0-9\.]+)/);
180 $self->{sfp}->{$_}->{temperature}->{temperature} = $1 if ($result->{temperature} =~ /([0-9\.]+)\s*C/);
181 }
182 }
183
184 1;
185
186 __END__
187
188 =head1 MODE
189
190 Check sfp ports.
191
192 =over 8
193
194 =item B<--filter-port>
195
196 Filter ports by index (can be a regexp).
197
198 =item B<--warning-status>
199
200 Define the conditions to match for the status to be WARNING.
201 You can use the following variables: %{status}, %{name}, %{location}
202
203 =item B<--critical-status>
204
205 Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /txFailure|lossOfSignal|readError/').
206 You can use the following variables: %{status}, %{port}, %{location}
207
208 =item B<--warning-*> B<--critical-*>
209
210 Thresholds.
211 Can be: 'temperature', 'input-power', 'output-power'.
212
213 =back
214
215 =cut