Debian Codegraph @

src/cloud/azure/management/resource/mode/items.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 cloud::azure::management::resource::mode::items;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27
28 sub prefix_group_output {
29 my ($self, %options) = @_;
30
31 return "Resource group '" . $options{instance_value}->{display} . "' ";
32 }
33
34 sub set_counters {
35 my ($self, %options) = @_;
36
37 $self->{maps_counters_type} = [
38 { name => 'groups', type => 1, cb_prefix_output => 'prefix_group_output', message_multiple => 'All groups are ok' },
39 ];
40
41 $self->{maps_counters}->{groups} = [
42 { label => 'total', set => {
43 key_values => [ { name => 'total' } , { name => 'display' } ],
44 output_template => "Total number of items : %s",
45 perfdatas => [
46 { label => 'total', value => 'total', template => '%d',
47 label_extra_instance => 1, instance_use => 'display', min => 0 },
48 ],
49 }
50 },
51 { label => 'compute', set => {
52 key_values => [ { name => 'compute' } , { name => 'display' } ],
53 output_template => "Compute: %s",
54 perfdatas => [
55 { label => 'compute', value => 'compute', template => '%d',
56 label_extra_instance => 1, instance_use => 'display', min => 0 },
57 ],
58 }
59 },
60 { label => 'storage', set => {
61 key_values => [ { name => 'storage' } , { name => 'display' } ],
62 output_template => "Storage: %s",
63 perfdatas => [
64 { label => 'storage', value => 'storage', template => '%d',
65 label_extra_instance => 1, instance_use => 'display', min => 0 },
66 ],
67 }
68 },
69 { label => 'network', set => {
70 key_values => [ { name => 'network' } , { name => 'display' } ],
71 output_template => "Network: %s",
72 perfdatas => [
73 { label => 'network', value => 'network', template => '%d',
74 label_extra_instance => 1, instance_use => 'display', min => 0 },
75 ],
76 }
77 },
78 ];
79 }
80
81 sub new {
82 my ($class, %options) = @_;
83 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
84 bless $self, $class;
85
86 $options{options}->add_options(arguments =>
87 {
88 "resource-group:s" => { name => 'resource_group' },
89 "filter-name:s" => { name => 'filter_name' },
90 "hidden" => { name => 'hidden' },
91 });
92
93 return $self;
94 }
95
96 sub check_options {
97 my ($self, %options) = @_;
98 $self->SUPER::check_options(%options);
99 }
100
101 sub manage_selection {
102 my ($self, %options) = @_;
103
104 my $resources = $options{custom}->azure_list_resources(
105 namespace => '',
106 resource_type => '',
107 location => $self->{option_results}->{location},
108 resource_group => $self->{option_results}->{resource_group}
109 );
110
111 my $groups;
112 if (defined($self->{option_results}->{resource_group}) && $self->{option_results}->{resource_group} ne '') {
113 push @{$groups}, { name => $self->{option_results}->{resource_group} };
114 } else {
115 $groups = $options{custom}->azure_list_groups();
116 }
117
118 foreach my $group (@{$groups}) {
119 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
120 $group->{name} !~ /$self->{option_results}->{filter_name}/) {
121 $self->{output}->output_add(long_msg => "skipping '" . $group->{name} . "': no matching filter.", debug => 1);
122 next;
123 }
124
125 $self->{groups}->{$group->{name}} = {
126 display => $group->{name},
127 total => 0,
128 compute => 0,
129 storage => 0,
130 network => 0,
131 };
132
133 foreach my $item (@{$resources}) {
134 my $resource_group = '';
135 $resource_group = $item->{resourceGroup} if (defined($item->{resourceGroup}));
136 $resource_group = $1 if (defined($item->{id}) && $item->{id} =~ /resourceGroups\/(.*)\/providers/);
137 next if (defined($resource_group) && $resource_group !~ /$group->{name}/);
138 next if (!defined($self->{option_results}->{hidden}) && $item->{type} =~ /^Microsoft\..*\/.*\/.*/);
139 $self->{groups}->{$group->{name}}->{total}++;
140 $self->{groups}->{$group->{name}}->{compute}++ if ($item->{type} =~ /^Microsoft\.Compute\//);
141 $self->{groups}->{$group->{name}}->{storage}++ if ($item->{type} =~ /^Microsoft\.Storage\//);
142 $self->{groups}->{$group->{name}}->{network}++ if ($item->{type} =~ /^Microsoft\.Network\//);
143 }
144 }
145
146 if (scalar(keys %{$self->{groups}}) <= 0) {
147 $self->{output}->add_option_msg(short_msg => "No groups found.");
148 $self->{output}->option_exit();
149 }
150 }
151
152 1;
153
154 __END__
155
156 =head1 MODE
157
158 Check number of items in resource groups.
159
160 Example:
161 perl centreon_plugins.pl --plugin=cloud::azure::management::resource::plugin --custommode=azcli --mode=items
162 --filter-name='.*' --critical-items='10' --verbose
163
164 =over 8
165
166 =item B<--resource-group>
167
168 Set resource group (optional).
169
170 =item B<--filter-name>
171
172 Filter resource name (can be a regexp).
173
174 =item B<--warning-*>
175
176 Warning threshold.
177 Can be: 'total', 'compute', 'storage', 'network'.
178
179 =item B<--critical-*>
180
181 Critical threshold.
182 Can be: 'total', 'compute', 'storage', 'network'.
183
184 =back
185
186 =cut