Debian Codegraph @

src/apps/microsoft/dhcp/snmp/mode/subnets.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 apps::microsoft::dhcp::snmp::mode::subnets;
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 custom_status_output {
30 my ($self, %options) = @_;
31
32 return sprintf(
33 'status: %s',
34 $self->{result_values}->{status}
35 );
36 }
37
38 sub custom_usage_output {
39 my ($self, %options) = @_;
40
41 return sprintf(
42 'addresses usage total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
43 $self->{result_values}->{total},
44 $self->{result_values}->{used}, $self->{result_values}->{prct_used},
45 $self->{result_values}->{free}, $self->{result_values}->{prct_free}
46 );
47 }
48
49 sub prefix_subnet_output {
50 my ($self, %options) = @_;
51
52 return sprintf(
53 "Subnet '%s' ",
54 $options{instance_value}->{name}
55 );
56 }
57
58 sub set_counters {
59 my ($self, %options) = @_;
60
61 $self->{maps_counters_type} = [
62 { name => 'subnets', type => 1, cb_prefix_output => 'prefix_subnet_output', message_multiple => 'All subnets are ok', skipped_code => { -10 => 1 } }
63 ];
64
65 $self->{maps_counters}->{subnets} = [
66 {
67 label => 'status', type => 2, set => {
68 key_values => [
69 { name => 'status' }, { name => 'name' }
70 ],
71 closure_custom_output => $self->can('custom_status_output'),
72 closure_custom_perfdata => sub { return 0; },
73 closure_custom_threshold_check => \&catalog_status_threshold_ng
74 }
75 },
76 { label => 'addresses-usage', nlabel => 'subnet.addresses.usage.count', set => {
77 key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
78 closure_custom_output => $self->can('custom_usage_output'),
79 perfdatas => [
80 { template => '%d', min => 0, max => 'total', cast_int => 1, label_extra_instance => 1 }
81 ]
82 }
83 },
84 { label => 'addresses-usage-free', nlabel => 'subnet.addresses.free.count', display_ok => 0, set => {
85 key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
86 closure_custom_output => $self->can('custom_usage_output'),
87 perfdatas => [
88 { template => '%d', min => 0, max => 'total', cast_int => 1, label_extra_instance => 1 }
89 ]
90 }
91 },
92 { label => 'addresses-usage-prct', nlabel => 'subnet.addresses.usage.percentage', display_ok => 0, set => {
93 key_values => [ { name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'prct_free' }, { name => 'total' } ],
94 closure_custom_output => $self->can('custom_usage_output'),
95 perfdatas => [
96 { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 }
97 ]
98 }
99 },
100 { label => 'pending-offers', nlabel => 'subnet.pending.offers.count', set => {
101 key_values => [ { name => 'pending_offers' } ],
102 output_template => 'pending offers: %s',
103 perfdatas => [
104 { template => '%s', min => 0, label_extra_instance => 1 }
105 ]
106 }
107 }
108 ];
109 }
110
111 sub new {
112 my ($class, %options) = @_;
113 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
114 bless $self, $class;
115
116 $options{options}->add_options(arguments => {
117 'filter-subnet-address:s' => { name => 'filter_subnet_address' }
118 });
119
120 return $self;
121 }
122
123 my $mapping = {
124 used => { oid => '.1.3.6.1.4.1.311.1.3.2.1.1.2' }, # noAddInUse
125 free => { oid => '.1.3.6.1.4.1.311.1.3.2.1.1.3' }, # noAddFree
126 pending_offers => { oid => '.1.3.6.1.4.1.311.1.3.2.1.1.4' } # noPendingOffers
127 };
128 my $oid_subnetAdd = '.1.3.6.1.4.1.311.1.3.2.1.1.1';
129
130 sub manage_selection {
131 my ($self, %options) = @_;
132
133 my $snmp_result = $options{snmp}->get_table(
134 oid => $oid_subnetAdd,
135 nothing_quit => 1
136 );
137
138 $self->{subnets} = {};
139 foreach (keys %$snmp_result) {
140 /^$oid_subnetAdd\.(.*)$/;
141 my $instance = $1;
142
143 if (defined($self->{option_results}->{filter_subnet_address}) && $self->{option_results}->{filter_subnet_address} ne '' &&
144 $snmp_result->{$_} !~ /$self->{option_results}->{filter_subnet_address}/) {
145 $self->{output}->output_add(long_msg => "skipping '" . $snmp_result->{$_} . "': no matching 'org' filter.", debug => 1);
146 next;
147 }
148
149 $self->{subnets}->{$instance} = { name => $snmp_result->{$_} };
150 }
151
152 if (scalar(keys %{$self->{subnets}}) <= 0) {
153 $self->{output}->add_option_msg(short_msg => "No subnets found.");
154 $self->{output}->option_exit();
155 }
156
157 $options{snmp}->load(
158 oids => [
159 map($_->{oid}, values(%$mapping))
160 ],
161 instances => [keys %{$self->{subnets}}],
162 instance_regexp => '^(.*)$'
163 );
164 $snmp_result = $options{snmp}->get_leef(nothing_quit => 1);
165
166 foreach (keys %{$self->{subnets}}) {
167 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $_);
168
169 $self->{subnets}->{$_}->{status} = 'enabled';
170 if ($result->{free} == 0 && $result->{used} == 0 && $result->{pending_offers} == 0) {
171 $self->{subnets}->{$_}->{status} = 'disabled';
172 next;
173 }
174 $self->{subnets}->{$_}->{pending_offers} = $result->{pending_offers};
175 $self->{subnets}->{$_}->{free} = $result->{free};
176 $self->{subnets}->{$_}->{used} = $result->{used};
177 $self->{subnets}->{$_}->{total} = $result->{used} + $result->{free};
178 $self->{subnets}->{$_}->{prct_used} = $result->{used} * 100 / ($result->{used} + $result->{free});
179 $self->{subnets}->{$_}->{prct_free} = $result->{free} * 100 / ($result->{used} + $result->{free});
180 }
181 }
182
183 1;
184
185 __END__
186
187 =head1 MODE
188
189 Check dhcp subnets.
190
191 =over 8
192
193 =item B<--filter-counters>
194
195 Only display some counters (regexp can be used).
196 Example: --filter-counters='pending'
197
198 =item B<--filter-subnet-address>
199
200 Filter subnets by address (can be a regexp).
201
202 =item B<--unknown-status>
203
204 Define the conditions to match for the status to be UNKNOWN.
205 You can use the following variables: %{status}, %{name}
206
207 =item B<--warning-status>
208
209 Define the conditions to match for the status to be WARNING.
210 You can use the following variables: %{status}, %{name}
211
212 =item B<--critical-status>
213
214 Define the conditions to match for the status to be CRITICAL.
215 You can use the following variables: %{status}, %{name}
216
217 =item B<--warning-*> B<--critical-*>
218
219 Thresholds.
220 Can be: 'addresses-usage', 'addresses-usage-free', 'addresses-usage-prct', 'pending-offers'.
221
222 =back
223
224 =cut