Debian Codegraph @

src/centreon/common/fortinet/fortigate/snmp/mode/linkmonitor.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::fortinet::fortigate::snmp::mode::linkmonitor;
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 use Digest::MD5 qw(md5_hex);
29
30 sub prefix_output {
31 my ($self, %options) = @_;
32
33 return sprintf(
34 "Link monitor '%s' [vdom: %s] [id: %s] ",
35 $options{instance_value}->{name},
36 $options{instance_value}->{vdom},
37 $options{instance_value}->{id}
38 );
39 }
40
41 sub custom_signal_perfdata {
42 my ($self) = @_;
43
44 my $instances = [];
45 foreach (@{$self->{instance_mode}->{custom_perfdata_instances}}) {
46 push @$instances, $self->{result_values}->{$_};
47 }
48
49 $self->{output}->perfdata_add(
50 nlabel => $self->{nlabel},
51 instances => $instances,
52 value => $self->{result_values}->{ $self->{key_values}->[0]->{name} },
53 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
54 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
55 min => 0
56 );
57 }
58
59 sub set_counters {
60 my ($self, %options) = @_;
61
62 $self->{maps_counters_type} = [
63 { name => 'linkmonitor', type => 1, cb_prefix_output => 'prefix_output', message_multiple => 'All link monitors are ok' }
64 ];
65
66 $self->{maps_counters}->{linkmonitor} = [
67 { label => 'status',
68 type => 2,
69 critical_default => '%{state} eq "dead"',
70 set => {
71 key_values => [ { name => 'state' }, { name => 'vdom' }, { name => 'name' }, { name => 'id' } ],
72 output_template => 'state: %s',
73 closure_custom_perfdata => sub { return 0; },
74 closure_custom_threshold_check => \&catalog_status_threshold_ng
75 }
76 },
77 { label => 'latency', nlabel => 'linkmonitor.latency.milliseconds', set => {
78 key_values => [ { name => 'latency' }, { name => 'vdom' }, { name => 'name' }, { name => 'id' } ],
79 output_template => 'latency: %sms',
80 closure_custom_perfdata => $self->can('custom_signal_perfdata')
81 }
82 },
83 { label => 'jitter', nlabel => 'linkmonitor.jitter.milliseconds', set => {
84 key_values => [ { name => 'jitter' }, { name => 'vdom' }, { name => 'name' }, { name => 'id' } ],
85 output_template => 'jitter: %sms',
86 closure_custom_perfdata => $self->can('custom_signal_perfdata')
87 }
88 },
89 { label => 'packet-loss', nlabel => 'linkmonitor.packet.loss.percentage', set => {
90 key_values => [ { name => 'packet_loss' }, { name => 'vdom' }, { name => 'name' }, { name => 'id' } ],
91 output_template => 'packet loss: %.3f%%',
92 closure_custom_perfdata => $self->can('custom_signal_perfdata')
93 }
94 }
95 ];
96 }
97
98 sub new {
99 my ($class, %options) = @_;
100 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
101 bless $self, $class;
102
103 $options{options}->add_options(arguments => {
104 'filter-id:s' => { name => 'filter_id' },
105 'filter-name:s' => { name => 'filter_name' },
106 'filter-vdom:s' => { name => 'filter_vdom' },
107 'custom-perfdata-instances:s' => { name => 'custom_perfdata_instances' }
108 });
109
110 return $self;
111 }
112
113 sub check_options {
114 my ($self, %options) = @_;
115 $self->SUPER::check_options(%options);
116
117 if (!defined($self->{option_results}->{custom_perfdata_instances}) || $self->{option_results}->{custom_perfdata_instances} eq '') {
118 $self->{option_results}->{custom_perfdata_instances} = '%(name) %(vdom)';
119 }
120
121 $self->{custom_perfdata_instances} = $self->custom_perfdata_instances(
122 option_name => '--custom-perfdata-instances',
123 instances => $self->{option_results}->{custom_perfdata_instances},
124 labels => { name => 1, vdom => 1, id=>1 }
125 );
126 }
127
128 my $mapping_status = { 0 => 'alive', 1 => 'dead' };
129
130 my $mapping = {
131 name => { oid => '.1.3.6.1.4.1.12356.101.4.8.2.1.2' }, # fgLinkMonitorName
132 state => { oid => '.1.3.6.1.4.1.12356.101.4.8.2.1.3', map => $mapping_status }, # fgLinkMonitorState
133 latency => { oid => '.1.3.6.1.4.1.12356.101.4.8.2.1.4' }, # fgLinkMonitorLatency
134 jitter => { oid => '.1.3.6.1.4.1.12356.101.4.8.2.1.5' }, # fgLinkMonitorJitter
135 packet_loss => { oid => '.1.3.6.1.4.1.12356.101.4.8.2.1.8' }, # fgLinkMonitorPacketLoss
136 vdom => { oid => '.1.3.6.1.4.1.12356.101.4.8.2.1.9' }, # fgLinkMonitorVdom
137 };
138
139 my $oid_MappingEntry = '.1.3.6.1.4.1.12356.101.4.8.2.1';
140
141 sub manage_selection {
142 my ($self, %options) = @_;
143
144 if ($options{snmp}->is_snmpv1()) {
145 $self->{output}->add_option_msg(short_msg => "Need to use SNMP v2c or v3.");
146 $self->{output}->option_exit();
147 }
148
149 my $snmp_result = $options{snmp}->get_table(
150 oid => $oid_MappingEntry,
151 nothing_quit => 1
152 );
153
154 $self->{linkmonitor} = {};
155 foreach my $oid (keys %$snmp_result) {
156 next if ($oid !~ /^$mapping->{name}->{oid}\.(.*)$/);
157 my $instance = $1;
158
159 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
160
161 if (defined($self->{option_results}->{filter_id}) && $self->{option_results}->{filter_id} ne '' &&
162 $instance !~ /$self->{option_results}->{filter_id}/) {
163 $self->{output}->output_add(long_msg => "With filter-id: $self->{option_results}->{filter_id} - Skipping link monitor '" . $snmp_result->{$oid} . " with id '" . $instance . "'.", debug => 1);
164 next;
165 }
166 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
167 $snmp_result->{$oid} !~ /$self->{option_results}->{filter_name}/) {
168 $self->{output}->output_add(long_msg => "With filter-name: $self->{option_results}->{filter_name} - Skipping link monitor '" . $snmp_result->{$oid} . " with id '" . $instance . "'.", debug => 1);
169 next;
170 }
171 if (defined($self->{option_results}->{filter_vdom}) && $self->{option_results}->{filter_vdom} ne '' &&
172 $result->{vdom} !~ /$self->{option_results}->{filter_vdom}/) {
173 $self->{output}->output_add(long_msg => "With filter-vdom: $self->{option_results}->{filter_vdom} - Skipping vdom '" . $result->{vdom} . "'.", debug => 1);
174 next;
175 }
176
177 # Remoove the "%" at the end of the result.
178 chop($result->{packet_loss});
179
180 $self->{linkmonitor}->{ $instance } = {
181 id => $instance,
182 name => $result->{name},
183 state => $result->{state},
184 latency => $result->{latency},
185 jitter => $result->{jitter},
186 packet_loss => $result->{packet_loss},
187 vdom => $result->{vdom}
188 };
189
190 }
191
192 return if (scalar(keys %{$self->{linkmonitor}}) <= 0);
193 # Returns : OK: if empty => User needs to set up filters to avoid this mysterious OK result.
194
195 }
196
197 1;
198
199 __END__
200
201 =head1 MODE
202
203 Check link monitor.
204
205 =over 8
206
207 =item B<--filter-id>
208
209 Filter link monitor by ID (can be a regexp).
210
211 =item B<--filter-name>
212
213 Filter link monitor by name (can be a regexp).
214
215 =item B<--filter-vdom>
216
217 Filter link monitor by virtual domain name (can be a regexp).
218
219 =item B<--custom-perfdata-instances>
220
221 Define perfdata instance (default: '%(name) %(vdom)').
222 You can use the following variables: %{name}, %{vdom}, %{id}
223
224 =item B<--unknown-status>
225
226 Define the conditions to match for the status to be UNKNOWN.
227 You can use the following variables: %{state}, %{vdom}, %{id}, %{name}
228
229 =item B<--warning-status>
230
231 Define the conditions to match for the status to be WARNING.
232 You can use the following variables: %{state}, %{vdom}, %{id}, %{name}
233
234 =item B<--critical-status>
235
236 Define the conditions to match for the status to be CRITICAL (Default: '%{state} eq "dead"').
237 You can use the following variables: %{state}, %{vdom}, %{id}, %{name}
238
239 =item B<--warning-*> B<--critical-*>
240
241 Thresholds.
242 Can be: 'latency', 'jitter', 'packet-loss'.
243
244 =back
245
246 =cut