Debian Codegraph @

src/network/peplink/pepwave/snmp/mode/wanusage.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::peplink::pepwave::snmp::mode::wanusage;
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 = sprintf('health status : %s', $self->{result_values}->{health_status});
34 return $msg;
35 }
36
37 sub custom_status_calc {
38 my ($self, %options) = @_;
39
40 $self->{result_values}->{health_status} = $options{new_datas}->{$self->{instance} . '_health_status'};
41 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
42 return 0;
43 }
44
45 sub set_counters {
46 my ($self, %options) = @_;
47
48 $self->{maps_counters_type} = [
49 { name => 'wan', type => 1, cb_prefix_output => 'prefix_wan_output', message_multiple => 'All WANs are ok', skipped_code => { -10 => 1 } }
50 ];
51
52 $self->{maps_counters}->{wan} = [
53 { label => 'health-status', threshold => 0, set => {
54 key_values => [ { name => 'health_status' }, { name => 'display' } ],
55 closure_custom_calc => $self->can('custom_status_calc'),
56 closure_custom_output => $self->can('custom_status_output'),
57 closure_custom_perfdata => sub { return 0; },
58 closure_custom_threshold_check => \&catalog_status_threshold,
59 }
60 },
61 { label => 'signal', set => {
62 key_values => [ { name => 'signal' }, { name => 'display' } ],
63 output_template => 'Signal Strength : %s dBm',
64 perfdatas => [
65 { label => 'signal', template => '%s', unit => 'dBm', min => 0, label_extra_instance => 1, instance_use => 'display' },
66 ],
67 }
68 },
69 { label => 'traffic-in', set => {
70 key_values => [ { name => 'traffic_in', per_second => 1 }, { name => 'display' } ],
71 output_template => 'Traffic In : %s %s/s',
72 output_change_bytes => 2,
73 perfdatas => [
74 { label => 'traffic_in', template => '%d', min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' },
75 ],
76 }
77 },
78 { label => 'traffic-out', set => {
79 key_values => [ { name => 'traffic_out', per_second => 1 }, { name => 'display' } ],
80 output_template => 'Traffic Out : %s %s/s',
81 output_change_bytes => 2,
82 perfdatas => [
83 { label => 'traffic_out', template => '%d', min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' },
84 ],
85 }
86 }
87 ];
88 }
89
90 sub prefix_wan_output {
91 my ($self, %options) = @_;
92
93 return "WAN '" . $options{instance_value}->{display} . "' ";
94 }
95
96 sub new {
97 my ($class, %options) = @_;
98 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
99 bless $self, $class;
100
101 $options{options}->add_options(arguments => {
102 'filter-name:s' => { name => 'filter_name' },
103 'warning-health-status:s' => { name => 'warning_health_status', default => '' },
104 'critical-health-status:s' => { name => 'critical_health_status', default => '%{health_status} =~ /fail/' }
105 });
106
107 return $self;
108 }
109
110 sub check_options {
111 my ($self, %options) = @_;
112 $self->SUPER::check_options(%options);
113
114 $self->change_macros(macros => ['warning_health_status', 'critical_health_status']);
115 }
116
117 my %mapping_health = (
118 0 => 'fail',
119 1 => 'success',
120 );
121
122 my $mapping = {
123 wanName => { oid => '.1.3.6.1.4.1.27662.2.1.2.1.2' },
124 wanHealthCheckState => { oid => '.1.3.6.1.4.1.27662.2.1.2.1.4', map => \%mapping_health },
125 wanSignal => { oid => '.1.3.6.1.4.1.27662.2.1.2.1.5' },
126 };
127 my $mapping2 = {
128 wanDataUsageTxByte => { oid => '.1.3.6.1.4.1.27662.2.1.4.1.2' },
129 wanDataUsageRxByte => { oid => '.1.3.6.1.4.1.27662.2.1.4.1.3' },
130 };
131 my $oid_wanEntry = '.1.3.6.1.4.1.27662.2.1.2.1';
132 my $oid_wanDataUsageEntry = '.1.3.6.1.4.1.27662.2.1.4.1';
133
134 sub manage_selection {
135 my ($self, %options) = @_;
136
137 if ($options{snmp}->is_snmpv1()) {
138 $self->{output}->add_option_msg(short_msg => "Need to use SNMP v2c or v3.");
139 $self->{output}->option_exit();
140 }
141
142 $self->{wan} = {};
143 my $snmp_result = $options{snmp}->get_multiple_table(oids => [
144 { oid => $oid_wanEntry, start => $mapping->{wanName}->{oid}, end => $mapping->{wanSignal}->{oid} },
145 { oid => $oid_wanDataUsageEntry },
146 ], nothing_quit => 1);
147 foreach my $oid (keys %{$snmp_result->{$oid_wanEntry}}) {
148 next if ($oid !~ /^$mapping->{wanName}->{oid}\.(.*)$/);
149 my $instance = $1;
150 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result->{$oid_wanEntry}, instance => $instance);
151 my $result2 = $options{snmp}->map_instance(mapping => $mapping2, results => $snmp_result->{$oid_wanDataUsageEntry}, instance => $instance . '.3');
152
153 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
154 $result->{wanName} !~ /$self->{option_results}->{filter_name}/) {
155 $self->{output}->output_add(long_msg => "skipping '" . $result->{wanName} . "': no matching filter.", debug => 1);
156 next;
157 }
158
159 $self->{wan}->{$instance} = { display => $result->{wanName},
160 health_status => $result->{wanHealthCheckState},
161 signal => $result->{wanSignal} != -9999 ? $result->{wanSignal} : undef,
162 traffic_in => $result2->{wanDataUsageRxByte} * 1024 * 1024 * 8,
163 traffic_out => $result2->{wanDataUsageTxByte} * 1024 * 1024 * 8,
164 };
165 }
166
167 if (scalar(keys %{$self->{wan}}) <= 0) {
168 $self->{output}->add_option_msg(short_msg => "No wan found.");
169 $self->{output}->option_exit();
170 }
171
172 $self->{cache_name} = "peplink_" . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' .
173 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
174 (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all'));
175 }
176
177 1;
178
179 __END__
180
181 =head1 MODE
182
183 Check wan usage.
184
185 =over 8
186
187 =item B<--filter-counters>
188
189 Only display some counters (regexp can be used).
190 Example: --filter-counters='^traffic-in$'
191
192 =item B<--filter-name>
193
194 Filter wan name (can be a regexp).
195
196 =item B<--warning-health-status>
197
198 Define the conditions to match for the status to be WARNING (default: '').
199 You can use the following variables: %{health_status}, %{display}
200
201 =item B<--critical-health-status>
202
203 Define the conditions to match for the status to be CRITICAL (default: '%{health_status} =~ /fail/').
204 You can use the following variables: %{health_status}, %{display}
205
206 =item B<--warning-*>
207
208 Warning threshold.
209 Can be: 'traffic-in', 'traffic-out'.
210
211 =item B<--critical-*>
212
213 Critical threshold.
214 Can be: Can be: 'traffic-in', 'traffic-out'.
215
216 =back
217
218 =cut