Debian Codegraph @

src/cloud/azure/network/publicip/mode/status.pm

raw
1 #
2 # Copyright 2020 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::network::publicip::mode::status;
22
23 use base qw(centreon::plugins::templates::counter);
24 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
25
26 use strict;
27 use warnings;
28
29
30 sub new {
31 my ($class, %options) = @_;
32 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
33 bless $self, $class;
34
35 $options{options}->add_options(arguments => {
36 'resource:s' => { name => 'resource' },
37 'resource-group:s' => { name => 'resource_group' }
38 });
39
40 $options{options}->add_help(package => __PACKAGE__, sections => 'CUSTOM MODE OPTIONS', once => 1);
41
42 return $self;
43 }
44
45
46 sub prefix_status_output {
47 my ($self, %options) = @_;
48
49 return sprintf("Public IP instance '%s', IP: %s (%s) ", $options{instance_value}->{display}, $options{instance_value}->{ipaddress}, $options{instance_value}->{ipversion});
50 }
51
52 sub custom_ddos_status_output {
53 my ($self, %options) = @_;
54
55 return sprintf('current DDOS status: "%s"', $self->{result_values}->{status});
56 }
57
58 sub custom_provisioning_state_output {
59 my ($self, %options) = @_;
60
61 return sprintf('current provisioning state: "%s"', $self->{result_values}->{state});
62 }
63
64 sub set_counters {
65 my ($self, %options) = @_;
66
67 $self->{maps_counters_type} = [
68 { name => 'global', type => 0, cb_prefix_output => 'prefix_status_output' }
69 ];
70
71 $self->{maps_counters}->{global} = [
72 { label => 'ddos-status', type => 2, critical_default => '%{status} =~ /DDOS Attack ongoing/i', set => {
73 key_values => [ { name => 'status' } ],
74 closure_custom_output => $self->can('custom_ddos_status_output'),
75 closure_custom_perfdata => sub { return 0; },
76 closure_custom_threshold_check => \&catalog_status_threshold_ng,
77 }
78 },
79 { label => 'provisioning-state', type => 2, critical_default => '%{state} =~ /Failed/i', set => {
80 key_values => [ { name => 'state' } ],
81 closure_custom_output => $self->can('custom_provisioning_state_output'),
82 closure_custom_perfdata => sub { return 0; },
83 closure_custom_threshold_check => \&catalog_status_threshold_ng,
84 }
85 }
86 ];
87 }
88
89 sub check_options {
90 my ($self, %options) = @_;
91 $self->SUPER::check_options(%options);
92
93 if (!defined($self->{option_results}->{resource}) || $self->{option_results}->{resource} eq '') {
94 $self->{output}->add_option_msg(short_msg => 'Need to specify either --resource <name> with --resource-group option or --resource <id>.');
95 $self->{output}->option_exit();
96 }
97 my $resource = $self->{option_results}->{resource};
98 my $resource_group = defined($self->{option_results}->{resource_group}) ? $self->{option_results}->{resource_group} : '';
99 if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Network\/publicIPAddresses\/(.*)$/) {
100 $resource_group = $1;
101 $resource = $2;
102 }
103
104 $self->{az_resource} = $resource;
105 $self->{az_resource_group} = $resource_group;
106 $self->{az_resource_type} = 'publicIPAddresses';
107 $self->{az_resource_namespace} = 'Microsoft.Network';
108 $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900;
109 $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : 'PT5M';
110 $self->{az_aggregations} = ['Maximum'];
111
112 }
113
114 sub manage_selection {
115 my ($self, %options) = @_;
116
117 my %metric_results;
118 my $raw_results;
119
120 my $publicip_properties = $options{custom}->azure_get_publicip(resource => $self->{az_resource}, resource_group => $self->{az_resource_group});
121 $self->{global} = {
122 ipaddress => $publicip_properties->{properties}->{ipAddress},
123 ipversion => $publicip_properties->{properties}->{publicIPAddressVersion},
124 state => $publicip_properties->{properties}->{provisioningState}
125 };
126
127 ($metric_results{$self->{az_resource}}, $raw_results) = $options{custom}->azure_get_metrics(
128 aggregations => $self->{az_aggregations},
129 interval => $self->{az_interval},
130 metrics => ['IfUnderDDoSAttack'],
131 resource => $self->{az_resource},
132 resource_group => $self->{az_resource_group},
133 resource_namespace => $self->{az_resource_namespace},
134 resource_type => $self->{az_resource_type},
135 timeframe => $self->{az_timeframe}
136 );
137
138 $self->{global}->{display} = $self->{az_resource};
139 $self->{global}->{numeric_status} =
140 defined($metric_results{$self->{az_resource}}->{IfUnderDDoSAttack}->{maximum}) ?
141 $metric_results{$self->{az_resource}}->{IfUnderDDoSAttack}->{maximum} : 0;
142
143
144 $self->{global}->{status} = $self->{global}->{numeric_status} > 0 ? 'DDOS Attack ongoing' : 'OK';
145
146 }
147
148 1;
149
150 __END__
151
152 =head1 MODE
153
154 Check Azure Public IP status.
155
156 Example:
157
158 Using resource name:
159
160 perl centreon_plugins.pl --plugin=cloud::azure::network::publicip::plugin --mode=status --custommode=api
161 --resource=<publicip_id> --resource-group=<resourcegroup_id>
162 --critical-provisioning-state='%{state} =~ /Failed/i'
163
164 Using resource ID:
165
166 perl centreon_plugins.pl --plugin=cloud::azure::network::publicip::plugin --mode=status --custommode=api
167 --resource='/subscriptions/<subscription_id>/resourceGroups/<resourcegroup_id>/providers/Microsoft.Network/publicIPAddresses/<publicip_id>'
168 --critical-provisioning-state='%{state} =~ /Failed/i'
169
170 Default aggregation: 'maximum' / 'average', 'total', 'minimum' and 'maximum' are valid.
171
172 =head1 CUSTOM MODE OPTIONS
173
174 =over 8
175
176 =item B<--resource>
177
178 Set resource name or ID (required).
179
180 =item B<--resource-group>
181
182 Set resource group (required if resource's name is used).
183
184 =item B<--warning-ddos-status>
185
186 Warning threshold for DDOS attack status (default: '').
187
188 =item B<--critical-ddos-status>
189
190 Critical threshold for DDOS attack status (default: '%{status} =~ /DDOS Attack ongoing/i').
191
192 =item B<--warning-provisioning-state>
193
194 Warning threshold for provisioning state (default: '').
195
196 =item B<--critical-provisioning-state>
197
198 Critical threshold for provisioning state (default: '%{state} =~ /Failed/i').
199
200 =back
201
202 =cut