Debian Codegraph @

src/network/cisco/meraki/cloudcontroller/snmp/mode/listdevices.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::cisco::meraki::cloudcontroller::snmp::mode::listdevices;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27
28 my %map_status = (
29 0 => 'offline',
30 1 => 'online',
31 );
32 my $mapping = {
33 devName => { oid => '.1.3.6.1.4.1.29671.1.1.4.1.2' },
34 devStatus => { oid => '.1.3.6.1.4.1.29671.1.1.4.1.3', map => \%map_status },
35 devProductCode => { oid => '.1.3.6.1.4.1.29671.1.1.4.1.9' },
36 devNetworkName => { oid => '.1.3.6.1.4.1.29671.1.1.4.1.11' },
37 };
38
39 sub new {
40 my ($class, %options) = @_;
41 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
42 bless $self, $class;
43
44 $options{options}->add_options(arguments => {
45 "filter-name:s" => { name => 'filter_name' },
46 });
47 return $self;
48 }
49
50 sub check_options {
51 my ($self, %options) = @_;
52 $self->SUPER::init(%options);
53 }
54
55 sub manage_selection {
56 my ($self, %options) = @_;
57
58 my $snmp_result = $options{snmp}->get_multiple_table(oids => [
59 { oid => $mapping->{devName}->{oid} },
60 { oid => $mapping->{devStatus}->{oid} },
61 { oid => $mapping->{devProductCode}->{oid} },
62 { oid => $mapping->{devNetworkName}->{oid} }
63 ], return_type => 1, nothing_quit => 1);
64
65 $self->{devices} = {};
66 foreach my $oid (keys %{$snmp_result}) {
67 next if ($oid !~ /^$mapping->{devName}->{oid}\.(.*)$/);
68 my $instance = $1;
69 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
70
71 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
72 $result->{devName} !~ /$self->{option_results}->{filter_name}/) {
73 $self->{output}->output_add(long_msg => "skipping device '" . $result->{devName} . "': no matching filter.", debug => 1);
74 next;
75 }
76
77 $self->{devices}->{$instance} = {
78 name => $result->{devName},
79 status => $result->{devStatus},
80 network => $result->{devNetworkName},
81 product => $result->{devProductCode},
82 };
83 }
84 }
85
86 sub run {
87 my ($self, %options) = @_;
88
89 $self->manage_selection(%options);
90 foreach my $instance (sort keys %{$self->{devices}}) {
91 $self->{output}->output_add(long_msg => "[name = '" . $self->{devices}->{$instance}->{name} . "']" .
92 " [status = '" . $self->{devices}->{$instance}->{status} . "']" .
93 " [network = '" . $self->{devices}->{$instance}->{network} . "']" .
94 " [product = '" . $self->{devices}->{$instance}->{product} . "']");
95 }
96
97 $self->{output}->output_add(severity => 'OK',
98 short_msg => 'List devices:');
99 $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
100 $self->{output}->exit();
101 }
102
103 sub disco_format {
104 my ($self, %options) = @_;
105
106 $self->{output}->add_disco_format(elements => ['name', 'status', 'network', 'product']);
107 }
108
109 sub disco_show {
110 my ($self, %options) = @_;
111
112 $self->manage_selection(%options);
113 foreach my $instance (sort keys %{$self->{devices}}) {
114 $self->{output}->add_disco_entry(
115 name => $self->{devices}->{$instance}->{name},
116 status => $self->{devices}->{$instance}->{status},
117 network => $self->{devices}->{$instance}->{network},
118 product => $self->{devices}->{$instance}->{product},
119 );
120 }
121 }
122
123 1;
124
125 __END__
126
127 =head1 MODE
128
129 List devices.
130
131 =over 8
132
133 =item B<--filter-name>
134
135 Filter by device name (can be a regexp).
136
137 =back
138
139 =cut