Debian Codegraph @

src/cloud/azure/common/storageaccount/tableentitycount.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::common::storageaccount::tableentitycount;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27
28 sub prefix_metric_output {
29 my ($self, %options) = @_;
30
31 return "Resource '" . $options{instance_value}->{display} . "' " . $options{instance_value}->{stat} . " ";
32 }
33
34 sub set_counters {
35 my ($self, %options) = @_;
36
37 $self->{maps_counters_type} = [
38 { name => 'metric', type => 1, cb_prefix_output => 'prefix_metric_output', message_multiple => "All count metrics are ok", skipped_code => { -10 => 1 } },
39 ];
40
41 foreach my $aggregation ('average', 'total') {
42 foreach my $metric ('TableEntityCount') {
43 my $metric_label = lc($metric);
44 my $entry = { label => $metric_label . '-' . $aggregation, set => {
45 key_values => [ { name => $metric_label . '_' . $aggregation }, { name => 'display' }, { name => 'stat' } ],
46 output_template => $metric . ': %s',
47 perfdatas => [
48 { label => $metric_label . '_' . $aggregation, value => $metric_label . '_' . $aggregation ,
49 template => '%s', label_extra_instance => 1, instance_use => 'display',
50 min => 0 },
51 ],
52 }
53 };
54 push @{$self->{maps_counters}->{metric}}, $entry;
55 }
56 }
57 }
58
59 sub new {
60 my ($class, %options) = @_;
61 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
62 bless $self, $class;
63
64 $options{options}->add_options(arguments =>
65 {
66 "resource:s@" => { name => 'resource' },
67 "resource-group:s" => { name => 'resource_group' },
68 "resource-namespace:s" => { name => 'resource_namespace' }
69 });
70
71 return $self;
72 }
73
74 sub check_options {
75 my ($self, %options) = @_;
76 $self->SUPER::check_options(%options);
77
78 if (!defined($self->{option_results}->{resource})) {
79 $self->{output}->add_option_msg(short_msg => "Need to specify either --resource <name> with --resource-group option or --resource <id>.");
80 $self->{output}->option_exit();
81 }
82
83 $self->{az_resource} = $self->{option_results}->{resource};
84 $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group}));
85 $self->{az_resource_type} = 'storageAccounts';
86 $self->{az_resource_namespace} = defined($self->{option_results}->{resource_namespace}) ? $self->{option_results}->{resource_namespace} : 'Microsoft.Storage';
87 $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 3600;
88 $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT1H";
89 $self->{az_aggregations} = ['Average'];
90 if (defined($self->{option_results}->{aggregation})) {
91 $self->{az_aggregations} = [];
92 foreach my $stat (@{$self->{option_results}->{aggregation}}) {
93 if ($stat ne '') {
94 push @{$self->{az_aggregations}}, ucfirst(lc($stat));
95 }
96 }
97 }
98
99 foreach my $metric ('TableEntityCount') {
100 push @{$self->{az_metrics}}, $metric;
101 }
102 }
103
104 sub manage_selection {
105 my ($self, %options) = @_;
106
107 my %metric_results;
108 foreach my $resource (@{$self->{az_resource}}) {
109 my $resource_group = $self->{az_resource_group};
110 my $resource_name = $resource;
111 my $tableservice_name = '/tableServices/default';
112 if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/(.*)\/storageAccounts\/(.*)$/) {
113 $resource_group = $1;
114 $self->{az_resource_namespace} = $2,
115 $resource_name = $3;
116 }
117
118 ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics(
119 resource => $resource_name . $tableservice_name,
120 resource_group => $resource_group,
121 resource_type => $self->{az_resource_type},
122 resource_namespace => $self->{az_resource_namespace},
123 metrics => $self->{az_metrics},
124 aggregations => $self->{az_aggregations},
125 timeframe => $self->{az_timeframe},
126 interval => $self->{az_interval},
127 );
128
129 foreach my $metric (@{$self->{az_metrics}}) {
130 my $metric_name = lc($metric);
131 $metric_name =~ s/ /_/g;
132 foreach my $aggregation (@{$self->{az_aggregations}}) {
133 next if (!defined($metric_results{$resource_name}->{$metric_name}->{lc($aggregation)}) && !defined($self->{option_results}->{zeroed}));
134
135 $self->{metric}->{$resource_name . "_" . lc($aggregation)}->{display} = $resource_name;
136 $self->{metric}->{$resource_name . "_" . lc($aggregation)}->{stat} = lc($aggregation);
137 $self->{metric}->{$resource_name . "_" . lc($aggregation)}->{$metric_name . "_" . lc($aggregation)} = defined($metric_results{$resource_name}->{$metric_name}->{lc($aggregation)}) ? $metric_results{$resource_name}->{$metric_name}->{lc($aggregation)} : 0;
138 }
139 }
140 }
141
142 if (scalar(keys %{$self->{metric}}) <= 0) {
143 $self->{output}->add_option_msg(short_msg => 'No metrics. Check your options or use --zeroed option to set 0 on undefined values');
144 $self->{output}->option_exit();
145 }
146 }
147
148 1;
149
150 __END__
151
152 =head1 MODE
153
154 Check storage account resources table entity count metric.
155
156 Example:
157
158 Using resource name:
159
160 perl centreon_plugins.pl --plugin=cloud::azure::storage::storageaccount::plugin --custommode=azcli --mode=table-entity-count
161 --resource=MYFILER --resource-group=MYHOSTGROUP --aggregation='average' --critical-tableentitycount-average='10' --verbose
162
163 Using resource ID:
164
165 perl centreon_plugins.pl --plugin=cloud::azure::storage::storageaccount::plugin --custommode=azcli --mode=table-entity-count
166 --resource='/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Storage/storageAccounts/xxx/tableServices/default'
167 --aggregation='average' --critical-tableentitycount-average='10' --verbose
168
169 Default aggregation: 'average' / Total and average are valid.
170
171 =over 8
172
173 =item B<--resource>
174
175 Set resource name or ID (required).
176
177 =item B<--resource-group>
178
179 Set resource group (required if resource's name is used).
180
181 =item B<--resource-namespace>
182
183 Specify resource namespace. Can be: 'Microsoft.Storage' or 'Microsoft.ClassicStorage'.
184 Default: 'Microsoft.Storage'.
185
186 =item B<--warning-tableentitycount-*>
187
188 Warning thresholds (* can be: 'average', 'total').
189
190 =item B<--critical-tableentitycount-*>
191
192 Critical thresholds (* can be: 'average', 'total').
193
194 =back
195
196 =cut