Debian Codegraph @

src/network/zyxel/snmp/mode/vpnstatus.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::zyxel::snmp::mode::vpnstatus;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use Digest::MD5 qw(md5_hex);
28 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
29
30 sub custom_status_output {
31 my ($self, %options) = @_;
32
33 my $msg = 'connection status : ' . $self->{result_values}->{connectstatus} . ' [activation status: ' . $self->{result_values}->{activestatus} . ']';
34 return $msg;
35 }
36
37 sub custom_status_calc {
38 my ($self, %options) = @_;
39
40 $self->{result_values}->{activestatus} = $options{new_datas}->{$self->{instance} . '_activestatus'};
41 $self->{result_values}->{connectstatus} = $options{new_datas}->{$self->{instance} . '_connectstatus'};
42 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
43 return 0;
44 }
45
46 sub set_counters {
47 my ($self, %options) = @_;
48
49 $self->{maps_counters_type} = [
50 { name => 'vpn', type => 1, cb_prefix_output => 'prefix_vpn_output', message_multiple => 'All VPN tunnels are OK' },
51 ];
52
53 $self->{maps_counters}->{vpn} = [
54 { label => 'status', threshold => 0, set => {
55 key_values => [ { name => 'activestatus' }, { name => 'connectstatus' }, { name => 'display' } ],
56 closure_custom_calc => $self->can('custom_status_calc'),
57 closure_custom_output => $self->can('custom_status_output'),
58 closure_custom_perfdata => sub { return 0; },
59 closure_custom_threshold_check => \&catalog_status_threshold,
60 }
61 },
62 { label => 'traffic-in', set => {
63 key_values => [ { name => 'traffic_in', per_second => 1 }, { name => 'display' } ],
64 output_template => 'Traffic In: %s %s/s',
65 perfdatas => [
66 { label => 'traffic_in', template => '%.2f', min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' },
67 ],
68 }
69 },
70 { label => 'traffic-out', set => {
71 key_values => [ { name => 'traffic_out', per_second => 1 }, { name => 'display' } ],
72 output_template => 'Traffic Out: %s %s/s',
73 output_change_bytes => 2,
74 perfdatas => [
75 { label => 'traffic_out', template => '%.2f', min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' },
76 ],
77 }
78 }
79 ];
80 }
81
82 sub prefix_vpn_output {
83 my ($self, %options) = @_;
84
85 return "VPN '" . $options{instance_value}->{display} . "' ";
86 }
87
88 sub new {
89 my ($class, %options) = @_;
90 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
91 bless $self, $class;
92
93 $options{options}->add_options(arguments => {
94 'filter-name:s' => { name => 'filter_name' },
95 'warning-status:s' => { name => 'warning_status', default => '' },
96 'critical-status:s' => { name => 'critical_status', default => '%{connectstatus} eq "disconnected"' }
97 });
98
99 return $self;
100 }
101
102 sub check_options {
103 my ($self, %options) = @_;
104 $self->SUPER::check_options(%options);
105
106 $self->change_macros(macros => ['warning_status', 'critical_status']);
107 }
108
109 my %map_active_status = (0 => 'inactive', 1 => 'active');
110 my %map_connect_status = (0 => 'disconnected', 1 => 'connected');
111
112 my $mapping = {
113 vpnStatusConnectionName => { oid => '.1.3.6.1.4.1.890.1.6.22.2.4.1.2' },
114 vpnStatusActiveStatus => { oid => '.1.3.6.1.4.1.890.1.6.22.2.4.1.5', map => \%map_active_status },
115 vpnStatusConnectStatus => { oid => '.1.3.6.1.4.1.890.1.6.22.2.4.1.6', map => \%map_connect_status },
116 };
117
118 my $mapping2 = {
119 vpnSaMonitorConnectionName => { oid => '.1.3.6.1.4.1.890.1.6.22.2.6.1.2' },
120 vpnSaMonitorInBytes => { oid => '.1.3.6.1.4.1.890.1.6.22.2.6.1.7' },
121 vpnSaMonitorOutBytes => { oid => '.1.3.6.1.4.1.890.1.6.22.2.6.1.9' },
122 };
123
124 my $oid_vpnStatusEntry = '.1.3.6.1.4.1.890.1.6.22.2.4.1';
125 my $oid_vpnSaMonitorEntry = '.1.3.6.1.4.1.890.1.6.22.2.6.1';
126
127 sub manage_selection {
128 my ($self, %options) = @_;
129
130 $self->{cache_name} = "zyxel_" . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . $self->{mode} . '_' .
131 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
132 (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all'));
133
134 $self->{vpn} = {};
135 my $snmp_result = $options{snmp}->get_multiple_table(oids => [
136 { oid => $oid_vpnStatusEntry },
137 { oid => $oid_vpnSaMonitorEntry },
138 ], nothing_quit => 1);
139
140 foreach my $oid (sort keys %{$snmp_result->{$oid_vpnStatusEntry}}) {
141 next if ($oid !~ /^$mapping->{vpnStatusConnectionName}->{oid}\.(.*)$/);
142 my $instance = $1;
143 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result->{$oid_vpnStatusEntry}, instance => $instance);
144 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
145 $result->{vpnStatusConnectionName} !~ /$self->{option_results}->{filter_name}/) {
146 $self->{output}->output_add(long_msg => "skipping '" . $result->{vpnStatusConnectionName} . "': no matching filter.", debug => 1);
147 next;
148 }
149
150 $self->{vpn}->{$result->{vpnStatusConnectionName}} = {
151 display => $result->{vpnStatusConnectionName},
152 activestatus => $result->{vpnStatusActiveStatus},
153 connectstatus => $result->{vpnStatusConnectStatus},
154 };
155 }
156
157 foreach my $oid (sort keys %{$snmp_result->{$oid_vpnSaMonitorEntry}}) {
158 next if ($oid !~ /^$mapping2->{vpnSaMonitorConnectionName}->{oid}\.(.*)$/);
159 my $instance = $1;
160 my $result = $options{snmp}->map_instance(mapping => $mapping2, results => $snmp_result->{$oid_vpnSaMonitorEntry}, instance => $instance);
161 next if (!defined($self->{vpn}->{$result->{vpnSaMonitorConnectionName}}));
162
163 $self->{vpn}->{$result->{vpnSaMonitorConnectionName}}->{traffic_in} = $result->{vpnSaMonitorInBytes} * 8;
164 $self->{vpn}->{$result->{vpnSaMonitorConnectionName}}->{traffic_out} = $result->{vpnSaMonitorOutBytes} * 8;
165 }
166
167 if (scalar(keys %{$self->{vpn}}) <= 0) {
168 $self->{output}->add_option_msg(short_msg => "No vpn found.");
169 $self->{output}->option_exit();
170 }
171 }
172
173 1;
174
175 __END__
176
177 =head1 MODE
178
179 Check VPN state and traffic.
180
181 =over 8
182
183 =item B<--filter-name>
184
185 Filter vpn name with regexp.
186
187 =item B<--warning-*>
188
189 Warning threshold.
190 Can be: 'traffic-in', 'traffic-out'.
191
192 =item B<--critical-*>
193
194 Critical threshold.
195 Can be: 'traffic-in', 'traffic-out'.
196
197 =item B<--warning-status>
198
199 Define the conditions to match for the status to be WARNING.
200 You can use the following variables: %{activestatus}, %{connectstatus}, %{display}
201
202 =item B<--critical-status>
203
204 Define the conditions to match for the status to be CRITICAL (default: '%{connectstatus} eq "disconnected"').
205 You can use the following variables: %{activestatus}, %{connectstatus}, %{display}
206
207 =back
208
209 =cut