Debian Codegraph @

src/network/cisco/meraki/cloudcontroller/restapi/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::restapi::mode::listdevices;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27
28 sub new {
29 my ($class, %options) = @_;
30 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
31 bless $self, $class;
32
33 $options{options}->add_options(arguments => {
34 'filter-network-id:s' => { name => 'filter_network_id' },
35 'filter-organization-name:s' => { name => 'filter_organization_name' },
36 'filter-organization-id:s' => { name => 'filter_organization_id' },
37 'filter-tags:s' => { name => 'filter_tags' }
38 });
39
40 return $self;
41 }
42
43 sub check_options {
44 my ($self, %options) = @_;
45 $self->SUPER::init(%options);
46 }
47
48 sub manage_selection {
49 my ($self, %options) = @_;
50
51 my $organizations = $options{custom}->get_organizations();
52 my $networks = $options{custom}->get_networks(
53 orgs => [keys %$organizations],
54 extended => 1
55 );
56 my $devices = $options{custom}->get_devices(
57 orgs => [keys %$organizations],
58 extended => 1
59 );
60 my $devices_statuses = $options{custom}->get_organization_device_statuses(
61 orgs => [keys %$organizations],
62 extended => 1
63 );
64
65 my $results = {};
66 foreach (keys %$devices) {
67 next if (defined($self->{option_results}->{filter_network_id}) && $self->{option_results}->{filter_network_id} ne '' &&
68 $devices->{$_}->{networkId} !~ /$self->{option_results}->{filter_network_id}/);
69 if (defined($self->{option_results}->{filter_tags}) && $self->{option_results}->{filter_tags} ne '') {
70 my $tags;
71 $tags = join(' ', @{$devices->{$_}->{tags}}) if (defined($devices->{$_}->{tags}));
72 next if (!defined($tags) || $tags !~ /$self->{option_results}->{filter_tags}/);
73 }
74 next if (defined($self->{option_results}->{filter_organization_id}) && $self->{option_results}->{filter_organization_id} ne '' &&
75 $devices->{$_}->{orgId} !~ /$self->{option_results}->{filter_organization_id}/);
76
77 my $organization_name = $organizations->{ $devices->{$_}->{orgId} }->{name};
78 next if (defined($self->{option_results}->{filter_organization_name}) && $self->{option_results}->{filter_organization_name} ne '' &&
79 $organization_name !~ /$self->{option_results}->{filter_organization_name}/);
80
81 $results->{$_} = {
82 name => $devices->{$_}->{name},
83 tags => $devices->{$_}->{tags},
84 status => $devices_statuses->{ $devices->{$_}->{serial} }->{status},
85 public_ip => $devices_statuses->{ $devices->{$_}->{serial} }->{publicIp},
86 network_name => $networks->{ $devices->{$_}->{networkId} }->{name},
87 organization_name => $organization_name,
88 model => $devices->{$_}->{model},
89 serial => $_
90 };
91 }
92
93 return $results;
94 }
95
96 sub run {
97 my ($self, %options) = @_;
98
99 my $devices = $self->manage_selection(%options);
100 foreach (values %$devices) {
101 $self->{output}->output_add(long_msg => sprintf(
102 '[name: %s][status: %s][network name: %s][organization name: %s][model: %s][serial: %s]',
103 $_->{name},
104 $_->{status},
105 $_->{network_name},
106 $_->{organization_name},
107 $_->{model},
108 $_->{serial}
109 )
110 );
111 }
112
113 $self->{output}->output_add(
114 severity => 'OK',
115 short_msg => 'List devices:'
116 );
117 $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
118 $self->{output}->exit();
119 }
120
121 sub disco_format {
122 my ($self, %options) = @_;
123
124 $self->{output}->add_disco_format(elements => [
125 'name', 'status', 'tags', 'organization_name', 'network_id', 'network_name', 'model', 'serial'
126 ]);
127 }
128
129 sub disco_show {
130 my ($self, %options) = @_;
131
132 my $devices = $self->manage_selection(%options);
133 foreach (values %$devices) {
134 $self->{output}->add_disco_entry(
135 name => $_->{name},
136 status => $_->{status},
137 network_name => $_->{network_name},
138 network_id => $_->{networkId},
139 organization_name => $_->{organization_name},
140 tags => defined($_->{tags}) ? join(',', @{$_->{tags}}) : '',
141 model => $_->{model},
142 serial => $_->{serial}
143 );
144 }
145 }
146
147 1;
148
149 __END__
150
151 =head1 MODE
152
153 List devices.
154
155 =over 8
156
157 =item B<--filter-network-id>
158
159 Filter devices by network ID (can be a regexp).
160
161 =item B<--filter-organization-id>
162
163 Filter devices by organization ID (can be a regexp).
164
165 =item B<--filter-organization-name>
166
167 Filter devices by organization name (can be a regexp).
168
169 =item B<--filter-tags>
170
171 Filter devices by tags (can be a regexp).
172
173 =back
174
175 =cut