Debian Codegraph @

src/network/watchguard/snmp/mode/policyusage.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::watchguard::snmp::mode::policyusage;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use Digest::MD5 qw(md5_hex);
28
29 sub set_counters {
30 my ($self, %options) = @_;
31
32 $self->{maps_counters_type} = [
33 { name => 'policy', type => 1, cb_prefix_output => 'prefix_policy_output', message_multiple => 'All policies are ok' }
34 ];
35
36 $self->{maps_counters}->{policy} = [
37 { label => 'current-connections', nlabel => 'policy.connections.current.count', set => {
38 key_values => [ { name => 'wgPolicyCurrActiveConns' }, { name => 'display' } ],
39 output_template => 'current connections: %s',
40 perfdatas => [
41 { label => 'current_connections', template => '%s', min => 0, label_extra_instance => 1, instance_use => 'display' }
42 ]
43 }
44 },
45 { label => 'total-connections', nlabel => 'policy.connections.total.count', set => {
46 key_values => [ { name => 'wgPolicyActiveStreams', diff => 1 }, { name => 'display' } ],
47 output_template => 'total connections: %s',
48 perfdatas => [
49 { label => 'total_connections', template => '%s', min => 0, label_extra_instance => 1, instance_use => 'display' }
50 ]
51 }
52 },
53 { label => 'l3-traffic', nlabel => 'policy.traffic.l3.bitspersecond', set => {
54 key_values => [ { name => 'wgPolicyL3PackageBytes', per_second => 1 }, { name => 'display' } ],
55 output_template => 'L3 traffic: %s %s/s',
56 output_change_bytes => 2,
57 perfdatas => [
58 { label => 'traffic_l3', template => '%.2f', min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' }
59 ]
60 }
61 },
62 { label => 'l2-traffic', nlabel => 'policy.traffic.l2.bitspersecond', set => {
63 key_values => [ { name => 'wgPolicyL2PackageBytes', per_second => 1 }, { name => 'display' } ],
64 output_template => 'L2 traffic: %s %s/s',
65 output_change_bytes => 2,
66 perfdatas => [
67 { label => 'traffic_l2', template => '%.2f', min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display' }
68 ]
69 }
70 }
71 ];
72 }
73
74 sub new {
75 my ($class, %options) = @_;
76 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
77 bless $self, $class;
78
79 $options{options}->add_options(arguments => {
80 'filter-name:s' => { name => 'filter_name' }
81 });
82
83 return $self;
84 }
85
86 sub prefix_policy_output {
87 my ($self, %options) = @_;
88
89 return "Policy '" . $options{instance_value}->{display} . "' ";
90 }
91
92 my $mapping = {
93 wgPolicyName => { oid => '.1.3.6.1.4.1.3097.4.2.2.1.2' },
94 wgPolicyL3PackageBytes => { oid => '.1.3.6.1.4.1.3097.4.2.2.1.3' },
95 wgPolicyActiveStreams => { oid => '.1.3.6.1.4.1.3097.4.2.2.1.12' },
96 wgPolicyCurrActiveConns => { oid => '.1.3.6.1.4.1.3097.4.2.2.1.18' },
97 wgPolicyL2PackageBytes => { oid => '.1.3.6.1.4.1.3097.4.2.2.1.19' }
98 };
99
100 my $oid_wgPolicyEntry = '.1.3.6.1.4.1.3097.4.2.2.1';
101
102 sub manage_selection {
103 my ($self, %options) = @_;
104
105 if ($options{snmp}->is_snmpv1()) {
106 $self->{output}->add_option_msg(short_msg => "Need to use SNMP v2c or v3.");
107 $self->{output}->option_exit();
108 }
109
110 $self->{policy} = {};
111 my $snmp_result = $options{snmp}->get_table(
112 oid => $oid_wgPolicyEntry,
113 nothing_quit => 1
114 );
115
116 foreach my $oid (keys %{$snmp_result}) {
117 next if ($oid !~ /^$mapping->{wgPolicyName}->{oid}\.(.*)$/);
118 my $instance = $1;
119 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
120
121 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
122 $result->{wgPolicyName} !~ /$self->{option_results}->{filter_name}/) {
123 $self->{output}->output_add(long_msg => "skipping '" . $result->{wgPolicyName} . "': no matching filter.", debug => 1);
124 next;
125 }
126
127 # Must be a Watchguard bug, where wgPolicyCurrActiveConns sporadically returns 2^32−1...
128 if ($result->{wgPolicyCurrActiveConns} == 4294967295) {
129 $result->{wgPolicyCurrActiveConns} = 0;
130 }
131
132 $result->{wgPolicyL3PackageBytes} *= 8;
133 $result->{wgPolicyL2PackageBytes} *= 8;
134
135 $self->{policy}->{$instance} = {
136 display => $result->{wgPolicyName},
137 %$result
138 };
139 }
140
141 if (scalar(keys %{$self->{policy}}) <= 0) {
142 $self->{output}->add_option_msg(short_msg => "No policy found.");
143 $self->{output}->option_exit();
144 }
145
146 $self->{cache_name} = 'watchguard_' . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' .
147 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
148 (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all'));
149 }
150
151 1;
152
153 __END__
154
155 =head1 MODE
156
157 Check policy usage.
158
159 =over 8
160
161 =item B<--filter-counters>
162
163 Only display some counters (regexp can be used).
164 Example: --filter-counters='^total-connections$'
165
166 =item B<--filter-name>
167
168 Filter policy name (can be a regexp).
169
170 =item B<--warning-*>
171
172 Warning threshold.
173 Can be: 'total-connections', 'current-connections'
174 'l3-traffic' (b/s), 'l2-traffic' (b/s).
175
176 =item B<--critical-*>
177
178 Critical threshold.
179 Can be: 'total-connections', 'current-connections'
180 'l3-traffic' (b/s), 'l2-traffic' (b/s).
181
182 =back
183
184 =cut