Debian Codegraph @

src/network/cisco/meraki/cloudcontroller/restapi/mode/discovery.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::discovery;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27 use JSON::XS;
28
29 sub new {
30 my ($class, %options) = @_;
31 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
32 bless $self, $class;
33
34 $options{options}->add_options(arguments => {
35 'prettify' => { name => 'prettify' },
36 'resource-type:s' => { name => 'resource_type'},
37 'filter-network-id:s' => { name => 'filter_network_id' },
38 'filter-organization-name:s' => { name => 'filter_organization_name' },
39 'filter-organization-id:s' => { name => 'filter_organization_id' },
40 'filter-model:s' => { name => 'filter_model' },
41 'filter-tags:s' => { name => 'filter_tags' }
42 });
43
44 return $self;
45 }
46
47 sub check_options {
48 my ($self, %options) = @_;
49 $self->SUPER::init(%options);
50
51 if (!defined($self->{option_results}->{resource_type}) || $self->{option_results}->{resource_type} eq '') {
52 $self->{option_results}->{resource_type} = 'device';
53 }
54 if ($self->{option_results}->{resource_type} !~ /^device|network$/) {
55 $self->{output}->add_option_msg(short_msg => 'unknown resource type');
56 $self->{output}->option_exit();
57 }
58 }
59
60 sub discovery_devices {
61 my ($self, %options) = @_;
62
63 my $devices = $options{custom}->get_devices(
64 orgs => [keys %{$options{organizations}}],
65 extended => 1
66 );
67 my $devices_statuses = $options{custom}->get_organization_device_statuses(
68 orgs => [keys %{$options{organizations}}],
69 extended => 1
70 );
71
72 my @results;
73 foreach (values %$devices) {
74 next if ( !centreon::plugins::misc::is_empty($self->{option_results}->{filter_model})
75 && $_->{model} !~ /$self->{option_results}->{filter_model}/);
76 next if ( !centreon::plugins::misc::is_empty($self->{option_results}->{filter_network_id})
77 && $_->{networkId} !~ /$self->{option_results}->{filter_network_id}/);
78 next if ( !centreon::plugins::misc::is_empty($self->{option_results}->{filter_tags})
79 && (!defined($_->{tags}) || $_->{tags} !~ /$self->{option_results}->{filter_tags}/));
80 next if ( !centreon::plugins::misc::is_empty($self->{option_results}->{filter_organization_id})
81 && $_->{orgId} !~ /$self->{option_results}->{filter_organization_id}/);
82 next if ( !centreon::plugins::misc::is_empty($self->{option_results}->{filter_organization_name})
83 && $options{organizations}->{ $_->{orgId} }->{name} !~ /$self->{option_results}->{filter_organization_name}/);
84
85 my $node = {
86 name => $_->{name},
87 status => $devices_statuses->{ $_->{serial} }->{status},
88 address => $_->{address},
89 latitude => $_->{lat},
90 longitude => $_->{lng},
91 mac => $_->{mac},
92 url => $_->{url},
93 notes => $_->{notes},
94 tags => $_->{tags},
95 model => $_->{model},
96 firmware => $_->{firmware},
97 serial => $_->{serial},
98 public_ip => $devices_statuses->{ $_->{serial} }->{publicIp},
99 lan_ip => $_->{lanIp},
100 network_id => $_->{networkId},
101 network_name => $options{networks}->{ $_->{networkId} }->{name},
102 organization_name => $options{organizations}->{ $_->{orgId} }->{name},
103 configuration_updated_at => $_->{configurationUpdatedAt},
104 last_reported_at => $devices_statuses->{ $_->{serial} }->{lastReportedAt}
105 };
106
107 push @results, $node;
108 }
109
110 return @results;
111
112 }
113
114 sub discovery_networks {
115 my ($self, %options) = @_;
116
117 my @results;
118 foreach (values %{$options{networks}}) {
119 next if (defined($self->{option_results}->{filter_tags}) && $self->{option_results}->{filter_tags} ne '' &&
120 (!defined($_->{tags}) || $_->{tags} !~ /$self->{option_results}->{filter_tags}/));
121 next if (defined($self->{option_results}->{filter_organization_id}) && $self->{option_results}->{filter_organization_id} ne '' &&
122 $_->{organizationId} !~ /$self->{option_results}->{filter_organization_id}/);
123 next if (defined($self->{option_results}->{filter_organization_name}) && $self->{option_results}->{filter_organization_name} ne '' &&
124 $options{organizations}->{ $_->{organizationId} }->{name} !~ /$self->{option_results}->{filter_organization_name}/);
125
126 my $node = {
127 name => $_->{name},
128 id => $_->{id},
129 type => $_->{type},
130 timezone => $_->{timeZone},
131 tags => $_->{tags},
132 product_types => $_->{productTypes},
133 organization_id => $_->{organizationId},
134 organization_name => $options{organizations}->{ $_->{organizationId} }->{name},
135 disable_remote_status_page => $_->{disableRemoteStatusPage},
136 disable_my_meraki_com => $_->{disableMyMerakiCom}
137 };
138
139 push @results, $node;
140 }
141
142 return @results;
143 }
144
145 sub run {
146 my ($self, %options) = @_;
147
148 my @disco_data;
149 my $disco_stats;
150
151 $disco_stats->{start_time} = time();
152
153 my $organizations = $options{custom}->get_organizations();
154 my $networks = $options{custom}->get_networks(
155 orgs => [keys %{$organizations}],
156 extended => 1
157 );
158
159 if ($self->{option_results}->{resource_type} eq 'network') {
160 @disco_data = $self->discovery_networks(
161 networks => $networks,
162 organizations => $organizations
163 );
164 } else {
165 @disco_data = $self->discovery_devices(
166 networks => $networks,
167 organizations => $organizations,
168 %options
169 );
170 }
171
172 $disco_stats->{end_time} = time();
173 $disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time};
174 $disco_stats->{discovered_items} = @disco_data;
175 $disco_stats->{results} = \@disco_data;
176
177 my $encoded_data;
178 eval {
179 if (defined($self->{option_results}->{prettify})) {
180 $encoded_data = JSON::XS->new->utf8->pretty->encode($disco_stats);
181 } else {
182 $encoded_data = JSON::XS->new->utf8->encode($disco_stats);
183 }
184 };
185 if ($@) {
186 $encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
187 }
188
189 $self->{output}->output_add(short_msg => $encoded_data);
190 $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
191 $self->{output}->exit();
192 }
193
194 1;
195
196 __END__
197
198 =head1 MODE
199
200 Resources discovery.
201
202 =over 8
203
204 =item B<--prettify>
205
206 Prettify the JSON output.
207
208 =item B<--resource-type>
209
210 Define the type of resources to discover (C<device> or C<network>).
211
212 =item B<--filter-model>
213
214 Define which resources should be discovered based on the resource's model.
215 This option will be treated as a regular expression.
216
217 =item B<--filter-network-id>
218
219 Define which resources should be discovered based on the resource's network ID.
220 This option will be treated as a regular expression.
221
222 =item B<--filter-organization-id>
223
224 Define which resources should be discovered based on the resource's organization ID.
225 This option will be treated as a regular expression.
226
227 =item B<--filter-organization-name>
228
229 Define which resources should be discovered based on the resource's organization name.
230 This option will be treated as a regular expression.
231
232 =item B<--filter-tags>
233
234 Define which resources should be discovered based on the resource's tags.
235 This option will be treated as a regular expression.
236
237 =back
238
239 =cut