| 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::cisco::meraki::cloudcontroller::snmp::mode::deviceusage; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use centreon::plugins::statefile; |
| 28 |
use Digest::MD5 qw(md5_hex); |
| 29 |
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); |
| 30 |
|
| 31 |
sub custom_status_output { |
| 32 |
my ($self, %options) = @_; |
| 33 |
|
| 34 |
return 'status : ' . $self->{result_values}->{status}; |
| 35 |
} |
| 36 |
|
| 37 |
sub custom_traffic_perfdata { |
| 38 |
my ($self, %options) = @_; |
| 39 |
|
| 40 |
my ($warning, $critical); |
| 41 |
if ($self->{instance_mode}->{option_results}->{units_traffic} eq '%' && defined($self->{result_values}->{speed})) { |
| 42 |
$warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{speed}, cast_int => 1); |
| 43 |
$critical = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{speed}, cast_int => 1); |
| 44 |
} elsif ($self->{instance_mode}->{option_results}->{units_traffic} eq 'b/s') { |
| 45 |
$warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}); |
| 46 |
$critical = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}); |
| 47 |
} |
| 48 |
|
| 49 |
$self->{output}->perfdata_add( |
| 50 |
label => 'traffic_' . $self->{result_values}->{label}, unit => 'b/s', |
| 51 |
instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef, |
| 52 |
value => sprintf("%.2f", $self->{result_values}->{traffic}), |
| 53 |
warning => $warning, |
| 54 |
critical => $critical, |
| 55 |
min => 0, max => $self->{result_values}->{speed} |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
sub custom_traffic_threshold { |
| 60 |
my ($self, %options) = @_; |
| 61 |
|
| 62 |
my $exit = 'ok'; |
| 63 |
if ($self->{instance_mode}->{option_results}->{units_traffic} eq '%' && defined($self->{result_values}->{speed})) { |
| 64 |
$exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic_prct}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]); |
| 65 |
} elsif ($self->{instance_mode}->{option_results}->{units_traffic} eq 'b/s') { |
| 66 |
$exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]); |
| 67 |
} |
| 68 |
return $exit; |
| 69 |
} |
| 70 |
|
| 71 |
sub custom_traffic_output { |
| 72 |
my ($self, %options) = @_; |
| 73 |
|
| 74 |
my ($traffic_value, $traffic_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{traffic}, network => 1); |
| 75 |
my ($total_value, $total_unit); |
| 76 |
if (defined($self->{result_values}->{speed}) && $self->{result_values}->{speed} =~ /[0-9]/) { |
| 77 |
($total_value, $total_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{speed}, network => 1); |
| 78 |
} |
| 79 |
|
| 80 |
return sprintf( |
| 81 |
"Traffic %s: %s/s (%s on %s)", |
| 82 |
ucfirst($self->{result_values}->{label}), $traffic_value . $traffic_unit, |
| 83 |
defined($self->{result_values}->{traffic_prct}) ? sprintf("%.2f%%", $self->{result_values}->{traffic_prct}) : '-', |
| 84 |
defined($total_value) ? $total_value . $total_unit : '-' |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
sub custom_traffic_calc { |
| 89 |
my ($self, %options) = @_; |
| 90 |
|
| 91 |
$self->{result_values}->{label} = $options{extra_options}->{label_ref}; |
| 92 |
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; |
| 93 |
my $diff_traffic = $options{new_datas}->{$self->{instance} . '_' . $self->{result_values}->{label}} - $options{old_datas}->{$self->{instance} . '_' . $self->{result_values}->{label}}; |
| 94 |
$self->{result_values}->{traffic} = $diff_traffic / $options{delta_time}; |
| 95 |
if (defined($self->{instance_mode}->{option_results}->{'speed_' . $self->{result_values}->{label}}) && $self->{instance_mode}->{option_results}->{'speed_' . $self->{result_values}->{label}} =~ /[0-9]/) { |
| 96 |
$self->{result_values}->{traffic_prct} = $self->{result_values}->{traffic} * 100 / ($self->{instance_mode}->{option_results}->{'speed_' . $self->{result_values}->{label}} * 1000 * 1000); |
| 97 |
$self->{result_values}->{speed} = $self->{instance_mode}->{option_results}->{'speed_' . $self->{result_values}->{label}} * 1000 * 1000; |
| 98 |
} |
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
sub prefix_device_output { |
| 103 |
my ($self, %options) = @_; |
| 104 |
|
| 105 |
return "Device '" . $options{instance_value}->{display} . "' [network: " . $options{instance_value}->{network} . "]" . |
| 106 |
" [product: " . $options{instance_value}->{product} . "] "; |
| 107 |
} |
| 108 |
|
| 109 |
sub prefix_interface_output { |
| 110 |
my ($self, %options) = @_; |
| 111 |
|
| 112 |
return "Interface '" . $options{instance_value}->{display} . "' "; |
| 113 |
} |
| 114 |
|
| 115 |
sub set_counters { |
| 116 |
my ($self, %options) = @_; |
| 117 |
|
| 118 |
$self->{maps_counters_type} = [ |
| 119 |
{ name => 'global', type => 0 }, |
| 120 |
{ name => 'device', type => 1, cb_prefix_output => 'prefix_device_output', message_multiple => 'All devices are ok' }, |
| 121 |
{ name => 'interface', type => 1, cb_prefix_output => 'prefix_interface_output', message_multiple => 'All device interfaces are ok', skipped_code => { -10 => 1 } } |
| 122 |
]; |
| 123 |
|
| 124 |
$self->{maps_counters}->{global} = [ |
| 125 |
{ label => 'total-devices', display_ok => 0, set => { |
| 126 |
key_values => [ { name => 'total' } ], |
| 127 |
output_template => 'Total devices: %s', |
| 128 |
perfdatas => [ |
| 129 |
{ label => 'total', template => '%s', min => 0 } |
| 130 |
] |
| 131 |
} |
| 132 |
} |
| 133 |
]; |
| 134 |
|
| 135 |
$self->{maps_counters}->{device} = [ |
| 136 |
{ label => 'status', type => 2, critical_default => '%{status} =~ /offline/', set => { |
| 137 |
key_values => [ { name => 'status' }, { name => 'display' } ], |
| 138 |
closure_custom_output => $self->can('custom_status_output'), |
| 139 |
closure_custom_perfdata => sub { return 0; }, |
| 140 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 141 |
} |
| 142 |
}, |
| 143 |
{ label => 'clients', set => { |
| 144 |
key_values => [ { name => 'clients' }, { name => 'display' } ], |
| 145 |
output_template => 'Clients: %s', |
| 146 |
perfdatas => [ |
| 147 |
{ label => 'clients', template => '%s', |
| 148 |
min => 0, label_extra_instance => 1, instance_use => 'display' } |
| 149 |
] |
| 150 |
} |
| 151 |
} |
| 152 |
]; |
| 153 |
|
| 154 |
$self->{maps_counters}->{interface} = [ |
| 155 |
{ label => 'in', set => { |
| 156 |
key_values => [ { name => 'in', diff => 1 }, { name => 'display' } ], |
| 157 |
closure_custom_calc => $self->can('custom_traffic_calc'), closure_custom_calc_extra_options => { label_ref => 'in' }, |
| 158 |
closure_custom_output => $self->can('custom_traffic_output'), |
| 159 |
closure_custom_perfdata => $self->can('custom_traffic_perfdata'), |
| 160 |
closure_custom_threshold_check => $self->can('custom_traffic_threshold') |
| 161 |
} |
| 162 |
}, |
| 163 |
{ label => 'out', set => { |
| 164 |
key_values => [ { name => 'out', diff => 1 }, { name => 'display' } ], |
| 165 |
closure_custom_calc => $self->can('custom_traffic_calc'), closure_custom_calc_extra_options => { label_ref => 'out' }, |
| 166 |
closure_custom_output => $self->can('custom_traffic_output'), |
| 167 |
closure_custom_perfdata => $self->can('custom_traffic_perfdata'), |
| 168 |
closure_custom_threshold_check => $self->can('custom_traffic_threshold') |
| 169 |
} |
| 170 |
} |
| 171 |
]; |
| 172 |
} |
| 173 |
|
| 174 |
sub new { |
| 175 |
my ($class, %options) = @_; |
| 176 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1); |
| 177 |
bless $self, $class; |
| 178 |
|
| 179 |
$options{options}->add_options(arguments => { |
| 180 |
'filter-name:s' => { name => 'filter_name' }, |
| 181 |
'filter-interface:s' => { name => 'filter_interface' }, |
| 182 |
'filter-network:s' => { name => 'filter_network' }, |
| 183 |
'filter-product:s' => { name => 'filter_product' }, |
| 184 |
'speed-in:s' => { name => 'speed_in' }, |
| 185 |
'speed-out:s' => { name => 'speed_out' }, |
| 186 |
'units-traffic:s' => { name => 'units_traffic', default => '%' }, |
| 187 |
'cache-expires-on:s' => { name => 'cache_expires_on' } |
| 188 |
}); |
| 189 |
|
| 190 |
$self->{cache} = centreon::plugins::statefile->new(%options); |
| 191 |
return $self; |
| 192 |
} |
| 193 |
|
| 194 |
sub check_options { |
| 195 |
my ($self, %options) = @_; |
| 196 |
$self->SUPER::check_options(%options); |
| 197 |
|
| 198 |
$self->{cache}->check_options(option_results => $self->{option_results}); |
| 199 |
if (defined($self->{option_results}->{cache_expires_on}) && $self->{option_results}->{cache_expires_on} =~ /(\d+)/) { |
| 200 |
$self->{cache_expires_on} = $1; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
my %map_status = ( |
| 205 |
0 => 'offline', |
| 206 |
1 => 'online' |
| 207 |
); |
| 208 |
my $mapping = { |
| 209 |
devName => { oid => '.1.3.6.1.4.1.29671.1.1.4.1.2' }, |
| 210 |
devStatus => { oid => '.1.3.6.1.4.1.29671.1.1.4.1.3', map => \%map_status }, |
| 211 |
devClientCount => { oid => '.1.3.6.1.4.1.29671.1.1.4.1.5' }, |
| 212 |
devProductCode => { oid => '.1.3.6.1.4.1.29671.1.1.4.1.9' }, |
| 213 |
devNetworkName => { oid => '.1.3.6.1.4.1.29671.1.1.4.1.11' } |
| 214 |
}; |
| 215 |
my $mapping2 = { |
| 216 |
devInterfaceName => { oid => '.1.3.6.1.4.1.29671.1.1.5.1.3' }, |
| 217 |
devInterfaceSentBytes => { oid => '.1.3.6.1.4.1.29671.1.1.5.1.6' }, |
| 218 |
devInterfaceRecvBytes => { oid => '.1.3.6.1.4.1.29671.1.1.5.1.7' } |
| 219 |
}; |
| 220 |
|
| 221 |
sub get_devices_infos_snmp { |
| 222 |
my ($self, %options) = @_; |
| 223 |
|
| 224 |
my $snmp_result = $options{snmp}->get_multiple_table( |
| 225 |
oids => [ |
| 226 |
{ oid => $mapping->{devName}->{oid} }, |
| 227 |
{ oid => $mapping2->{devInterfaceName}->{oid} }, |
| 228 |
{ oid => $mapping->{devProductCode}->{oid} }, |
| 229 |
{ oid => $mapping->{devNetworkName}->{oid} } |
| 230 |
], |
| 231 |
nothing_quit => 1 |
| 232 |
); |
| 233 |
|
| 234 |
return $snmp_result; |
| 235 |
} |
| 236 |
|
| 237 |
sub get_devices_infos { |
| 238 |
my ($self, %options) = @_; |
| 239 |
|
| 240 |
my $snmp_result; |
| 241 |
if (defined($self->{cache_expires_on})) { |
| 242 |
my $has_cache_file = $self->{cache}->read(statefile => 'meraki_' . $self->{mode} . '_' . md5_hex($options{snmp}->get_hostname())); |
| 243 |
my $timestamp = $self->{cache}->get(name => 'last_timestamp'); |
| 244 |
if ($has_cache_file == 0 || !defined($timestamp) || ((time() - $self->{cache_expires_on}) > $timestamp)) { |
| 245 |
$snmp_result = $self->get_devices_infos_snmp(%options); |
| 246 |
my $datas = { last_timestamp => time(), snmp => $snmp_result }; |
| 247 |
$self->{cache}->write(data => $datas); |
| 248 |
} else { |
| 249 |
$snmp_result = $self->{cache}->get(name => 'snmp'); |
| 250 |
} |
| 251 |
} else { |
| 252 |
$snmp_result = $self->get_devices_infos_snmp(%options); |
| 253 |
} |
| 254 |
|
| 255 |
return $snmp_result; |
| 256 |
} |
| 257 |
|
| 258 |
sub manage_selection { |
| 259 |
my ($self, %options) = @_; |
| 260 |
|
| 261 |
$self->{device} = {}; |
| 262 |
$self->{interface} = {}; |
| 263 |
$self->{global} = { total => 0 }; |
| 264 |
|
| 265 |
my $snmp_result = $self->get_devices_infos(%options); |
| 266 |
foreach my $oid (keys %{$snmp_result->{ $mapping->{devName}->{oid} }}) { |
| 267 |
$oid =~ /^$mapping->{devName}->{oid}\.(.*)$/; |
| 268 |
my $instance = $1; |
| 269 |
my $dev_name = $snmp_result->{$mapping->{devName}->{oid}}->{$oid}; |
| 270 |
my $network = defined($snmp_result->{$mapping->{devNetworkName}->{oid}}->{ $mapping->{devNetworkName}->{oid} . '.' . $instance }) |
| 271 |
? $snmp_result->{$mapping->{devNetworkName}->{oid}}->{ $mapping->{devNetworkName}->{oid} . '.' . $instance } : 'n/a'; |
| 272 |
my $product = $snmp_result->{$mapping->{devProductCode}->{oid}}->{ $mapping->{devProductCode}->{oid} . '.' . $instance }; |
| 273 |
if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && |
| 274 |
$dev_name !~ /$self->{option_results}->{filter_name}/) { |
| 275 |
$self->{output}->output_add(long_msg => "skipping device '" . $dev_name . "': no matching filter.", debug => 1); |
| 276 |
next; |
| 277 |
} |
| 278 |
if (defined($self->{option_results}->{filter_product}) && $self->{option_results}->{filter_product} ne '' && |
| 279 |
$product !~ /$self->{option_results}->{filter_product}/) { |
| 280 |
$self->{output}->output_add(long_msg => "skipping device '" . $dev_name . "': no matching filter.", debug => 1); |
| 281 |
next; |
| 282 |
} |
| 283 |
if (defined($self->{option_results}->{filter_network}) && $self->{option_results}->{filter_network} ne '' && |
| 284 |
$network !~ /$self->{option_results}->{filter_network}/) { |
| 285 |
$self->{output}->output_add(long_msg => "skipping device '" . $dev_name . "': no matching filter.", debug => 1); |
| 286 |
next; |
| 287 |
} |
| 288 |
|
| 289 |
foreach (keys %{$snmp_result->{ $mapping2->{devInterfaceName}->{oid} }}) { |
| 290 |
next if (!/^$mapping2->{devInterfaceName}->{oid}\.$instance\.(.*)/); |
| 291 |
|
| 292 |
my $index = $1; |
| 293 |
my $interface_name = $snmp_result->{ $mapping2->{devInterfaceName}->{oid} }->{$_}; |
| 294 |
if (defined($self->{option_results}->{filter_interface}) && $self->{option_results}->{filter_interface} ne '' && |
| 295 |
$interface_name !~ /$self->{option_results}->{filter_interface}/) { |
| 296 |
$self->{output}->output_add(long_msg => "skipping interface '" . $dev_name . '.' . $interface_name . "': no matching filter.", debug => 1); |
| 297 |
next; |
| 298 |
} |
| 299 |
|
| 300 |
$self->{interface}->{$instance . '.' . $index} = { display => $dev_name . '.' . $interface_name }; |
| 301 |
} |
| 302 |
|
| 303 |
$self->{global}->{total}++; |
| 304 |
$self->{device}->{$instance} = { display => $dev_name, network => $network, product => $product }; |
| 305 |
} |
| 306 |
|
| 307 |
if (scalar(keys %{$self->{interface}}) > 0) { |
| 308 |
$options{snmp}->load(oids => [$mapping2->{devInterfaceSentBytes}->{oid}, $mapping2->{devInterfaceRecvBytes}->{oid}], |
| 309 |
instances => [keys %{$self->{interface}}], instance_regexp => '^(.*)$'); |
| 310 |
$snmp_result = $options{snmp}->get_leef(nothing_quit => 1); |
| 311 |
foreach (keys %{$self->{interface}}) { |
| 312 |
my $result = $options{snmp}->map_instance(mapping => $mapping2, results => $snmp_result, instance => $_); |
| 313 |
|
| 314 |
$self->{interface}->{$_}->{in} = $result->{devInterfaceRecvBytes} * 8; |
| 315 |
$self->{interface}->{$_}->{out} = $result->{devInterfaceSentBytes} * 8; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
if (scalar(keys %{$self->{device}}) > 0) { |
| 320 |
$options{snmp}->load( |
| 321 |
oids => [$mapping->{devStatus}->{oid}, $mapping->{devClientCount}->{oid}], |
| 322 |
instances => [keys %{$self->{device}}], |
| 323 |
instance_regexp => '^(.*)$' |
| 324 |
); |
| 325 |
$snmp_result = $options{snmp}->get_leef(nothing_quit => 1); |
| 326 |
foreach (keys %{$self->{device}}) { |
| 327 |
my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $_); |
| 328 |
|
| 329 |
$self->{device}->{$_}->{status} = $result->{devStatus}; |
| 330 |
$self->{device}->{$_}->{clients} = $result->{devClientCount}; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
$self->{cache_name} = 'cisco_meraki_' . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . |
| 335 |
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' . |
| 336 |
(defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')) . '_' . |
| 337 |
(defined($self->{option_results}->{filter_interface}) ? md5_hex($self->{option_results}->{filter_interface}) : md5_hex('all')) . '_' . |
| 338 |
(defined($self->{option_results}->{filter_product}) ? md5_hex($self->{option_results}->{filter_product}) : md5_hex('all')); |
| 339 |
} |
| 340 |
|
| 341 |
1; |
| 342 |
|
| 343 |
__END__ |
| 344 |
|
| 345 |
=head1 MODE |
| 346 |
|
| 347 |
Check device usages. |
| 348 |
|
| 349 |
=over 8 |
| 350 |
|
| 351 |
=item B<--filter-counters> |
| 352 |
|
| 353 |
Only display some counters (regexp can be used). |
| 354 |
Example: --filter-counters='^clients$' |
| 355 |
|
| 356 |
=item B<--filter-name> |
| 357 |
|
| 358 |
Filter device name (can be a regexp). |
| 359 |
|
| 360 |
=item B<--filter-product> |
| 361 |
|
| 362 |
Filter device product code (can be a regexp). |
| 363 |
|
| 364 |
=item B<--filter-network> |
| 365 |
|
| 366 |
Filter by network name (can be a regexp). |
| 367 |
|
| 368 |
=item B<--filter-interface> |
| 369 |
|
| 370 |
Filter interface name (can be a regexp). |
| 371 |
|
| 372 |
=item B<--cache-expires-on> |
| 373 |
|
| 374 |
Use cache file to speed up mode execution (X seconds before refresh cache file). |
| 375 |
|
| 376 |
=item B<--speed-in> |
| 377 |
|
| 378 |
Set interface speed for incoming traffic (in Mb). |
| 379 |
|
| 380 |
=item B<--speed-out> |
| 381 |
|
| 382 |
Set interface speed for outgoing traffic (in Mb). |
| 383 |
|
| 384 |
=item B<--units-traffic> |
| 385 |
|
| 386 |
Units of thresholds for the traffic (default: '%') ('%', 'b/s'). |
| 387 |
|
| 388 |
=item B<--warning-status> |
| 389 |
|
| 390 |
Define the conditions to match for the status to be WARNING. |
| 391 |
You can use the following variables: %{status}, %{display} |
| 392 |
|
| 393 |
=item B<--critical-status> |
| 394 |
|
| 395 |
Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /offline/'). |
| 396 |
You can use the following variables: %{status}, %{display} |
| 397 |
|
| 398 |
=item B<--warning-*> |
| 399 |
|
| 400 |
Warning threshold. |
| 401 |
Can be: 'total-devices', 'clients', 'in', 'out'. |
| 402 |
|
| 403 |
=item B<--critical-*> |
| 404 |
|
| 405 |
Critical threshold. |
| 406 |
Can be: 'total-devices', 'clients', 'in', 'out'. |
| 407 |
|
| 408 |
=back |
| 409 |
|
| 410 |
=cut |