-
Notifications
You must be signed in to change notification settings - Fork 302
New plugin Azure Graph Explorer #6103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matoy
wants to merge
1
commit into
centreon:develop
Choose a base branch
from
matoy:feat_AzureGraphExplorer
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # | ||
| # Copyright 2024 Centreon (http://www.centreon.com/) | ||
| # | ||
| # Centreon is a full-fledged industry-strength solution that meets | ||
| # the needs in IT infrastructure and application monitoring for | ||
| # service performance. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| package cloud::azure::management::graphexplorer::custom::api; | ||
|
|
||
| use strict; | ||
| use warnings; | ||
| use base qw(cloud::azure::custom::api); | ||
|
|
||
| # Override check_options to make --subscription optional. | ||
| # Azure Resource Graph can query at tenant scope without a subscription. | ||
| sub check_options { | ||
| my ($self, %options) = @_; | ||
|
|
||
| $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10; | ||
| $self->{subscription} = (defined($self->{option_results}->{subscription})) ? $self->{option_results}->{subscription} : undef; | ||
| $self->{tenant} = (defined($self->{option_results}->{tenant})) ? $self->{option_results}->{tenant} : undef; | ||
| $self->{client_id} = (defined($self->{option_results}->{client_id})) ? $self->{option_results}->{client_id} : undef; | ||
| $self->{client_secret} = (defined($self->{option_results}->{client_secret})) ? $self->{option_results}->{client_secret} : undef; | ||
| $self->{login_endpoint} = (defined($self->{option_results}->{login_endpoint})) ? $self->{option_results}->{login_endpoint} : 'https://login.microsoftonline.com'; | ||
| $self->{management_endpoint} = (defined($self->{option_results}->{management_endpoint})) ? $self->{option_results}->{management_endpoint} : 'https://management.azure.com'; | ||
| $self->{api_version} = (defined($self->{option_results}->{api_version})) ? $self->{option_results}->{api_version} : undef; | ||
|
|
||
| # --subscription is optional for Resource Graph (tenant-scope queries are allowed) | ||
| if (!defined($self->{tenant}) || $self->{tenant} eq '') { | ||
| $self->{output}->add_option_msg(short_msg => "Need to specify --tenant option."); | ||
| $self->{output}->option_exit(); | ||
| } | ||
| if (!defined($self->{client_id}) || $self->{client_id} eq '') { | ||
| $self->{output}->add_option_msg(short_msg => "Need to specify --client-id option."); | ||
| $self->{output}->option_exit(); | ||
| } | ||
| if (!defined($self->{client_secret}) || $self->{client_secret} eq '') { | ||
| $self->{output}->add_option_msg(short_msg => "Need to specify --client-secret option."); | ||
| $self->{output}->option_exit(); | ||
| } | ||
| if (!defined($self->{api_version}) || $self->{api_version} eq '') { | ||
| $self->{output}->add_option_msg(short_msg => "Need to specify --api-version option."); | ||
| $self->{output}->option_exit(); | ||
| } | ||
|
|
||
| $self->{cache}->check_options(option_results => $self->{option_results}); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| 1; | ||
|
|
||
| __END__ | ||
|
|
||
| =head1 DESCRIPTION | ||
|
|
||
| B<custom>. Azure Resource Graph custom API mode. | ||
| Extends cloud::azure::custom::api with --subscription made optional, | ||
| allowing tenant-scoped Resource Graph queries. | ||
|
|
||
| =cut |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| # | ||
| # Copyright 2024 Centreon (http://www.centreon.com/) | ||
| # | ||
| # Centreon is a full-fledged industry-strength solution that meets | ||
| # the needs in IT infrastructure and application monitoring for | ||
| # service performance. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| package cloud::azure::management::graphexplorer::mode::query; | ||
|
|
||
| use base qw(centreon::plugins::templates::counter); | ||
|
|
||
| use strict; | ||
| use warnings; | ||
|
|
||
| sub custom_match_output { | ||
| my ($self, %options) = @_; | ||
|
|
||
| my $msg; | ||
| my $message; | ||
|
|
||
| if (defined($self->{instance_mode}->{option_results}->{custom_output}) && $self->{instance_mode}->{option_results}->{custom_output} ne '') { | ||
| eval { | ||
| local $SIG{__WARN__} = sub { $message = $_[0]; }; | ||
| local $SIG{__DIE__} = sub { $message = $_[0]; }; | ||
| $msg = sprintf($self->{instance_mode}->{option_results}->{custom_output}, $self->{result_values}->{match}); | ||
| }; | ||
| } else { | ||
| $msg = sprintf("Resource Graph query returned '%d' result(s)", $self->{result_values}->{match}); | ||
| } | ||
|
|
||
| if (defined($message)) { | ||
| $self->{output}->output_add(long_msg => 'printf substitution issue: ' . $message); | ||
| } | ||
| return $msg; | ||
| } | ||
|
|
||
| sub set_counters { | ||
| my ($self, %options) = @_; | ||
|
|
||
| $self->{maps_counters_type} = [ | ||
| { name => 'global', type => 0 } | ||
| ]; | ||
|
|
||
| $self->{maps_counters}->{global} = [ | ||
| { label => 'match', nlabel => 'resourcegraph.query.match.count', set => { | ||
| key_values => [ { name => 'match' } ], | ||
| closure_custom_output => $self->can('custom_match_output'), | ||
| perfdatas => [ | ||
| { template => '%s', min => 0 } | ||
| ] | ||
| } | ||
| } | ||
| ]; | ||
| } | ||
|
|
||
| sub new { | ||
| my ($class, %options) = @_; | ||
| my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); | ||
| bless $self, $class; | ||
|
|
||
| $options{options}->add_options(arguments => { | ||
| 'query:s' => { name => 'query' }, | ||
| 'custom-output:s' => { name => 'custom_output' }, | ||
| }); | ||
|
|
||
| return $self; | ||
| } | ||
|
|
||
| sub check_options { | ||
| my ($self, %options) = @_; | ||
| $self->SUPER::check_options(%options); | ||
|
|
||
| if (!defined($self->{option_results}->{query}) || $self->{option_results}->{query} eq '') { | ||
| $self->{output}->add_option_msg(short_msg => "Need to specify --query option."); | ||
| $self->{output}->option_exit(); | ||
| } | ||
| } | ||
|
|
||
| sub manage_selection { | ||
| my ($self, %options) = @_; | ||
|
|
||
| my $response = $options{custom}->azure_get_resource_graph( | ||
| query => $self->{option_results}->{query} | ||
| ); | ||
|
|
||
| my $count = 0; | ||
| if (defined($response->{data}) && ref($response->{data}) eq 'ARRAY') { | ||
| $count = scalar(@{$response->{data}}); | ||
| } elsif (defined($response->{count})) { | ||
| $count = $response->{count}; | ||
| } | ||
|
|
||
| if (defined($response->{resultTruncated}) && $response->{resultTruncated} eq 'true') { | ||
| $self->{output}->output_add(long_msg => | ||
| "Warning: result set is truncated (totalRecords: " . $response->{totalRecords} . "). " | ||
| . "Use 'top' in your KQL query to reduce the result set." | ||
| ); | ||
| } | ||
|
|
||
| if (defined($response->{data}) && ref($response->{data}) eq 'ARRAY') { | ||
| foreach my $row (@{$response->{data}}) { | ||
| $self->{output}->output_add(long_msg => join(', ', map { $_ . '=' . (defined($row->{$_}) ? $row->{$_} : '') } keys %{$row})); | ||
| } | ||
| } | ||
|
|
||
| $self->{global} = { match => $count }; | ||
| } | ||
|
|
||
| 1; | ||
|
|
||
| __END__ | ||
|
|
||
| =head1 MODE | ||
|
|
||
| Run a KQL query against Azure Resource Graph and count the number of results. | ||
|
|
||
| Sample command: | ||
| perl centreon_plugins.pl --plugin=cloud::azure::management::graphexplorer::plugin \ | ||
| --custommode api --mode query \ | ||
| --subscription=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX \ | ||
| --tenant=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX \ | ||
| --client-id=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX \ | ||
| --client-secret=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \ | ||
| --query "Resources | where type =~ 'microsoft.compute/virtualmachines' | where properties.powerState.code == 'PowerState/running'" \ | ||
| --custom-output='Running VMs: %d' \ | ||
| --warning-match=10 --critical-match=20 | ||
| OK: Running VMs: 5 | 'resourcegraph.query.match.count'=5;10;20;0; | ||
|
|
||
| =over 8 | ||
|
|
||
| =item B<--query> | ||
|
|
||
| KQL query to run against Azure Resource Graph (required). | ||
| See https://docs.microsoft.com/en-us/azure/governance/resource-graph/concepts/query-language | ||
|
|
||
| =item B<--custom-output> | ||
|
|
||
| Custom output string in printf format. Use %d as placeholder for the result count. | ||
| Example: 'Running VMs: %d' | ||
|
|
||
| =item B<--warning-match> | ||
|
|
||
| Warning threshold on the number of results returned by the query. | ||
|
|
||
| =item B<--critical-match> | ||
|
|
||
| Critical threshold on the number of results returned by the query. | ||
|
|
||
| =back | ||
|
|
||
| =cut | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # | ||
| # Copyright 2024 Centreon (http://www.centreon.com/) | ||
| # | ||
| # Centreon is a full-fledged industry-strength solution that meets | ||
| # the needs in IT infrastructure and application monitoring for | ||
| # service performance. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| package cloud::azure::management::graphexplorer::plugin; | ||
|
|
||
| use strict; | ||
| use warnings; | ||
| use base qw(centreon::plugins::script_custom); | ||
|
|
||
| sub new { | ||
| my ($class, %options) = @_; | ||
| my $self = $class->SUPER::new(package => __PACKAGE__, %options); | ||
| bless $self, $class; | ||
|
|
||
| $self->{version} = '0.1'; | ||
| %{$self->{modes}} = ( | ||
| 'query' => 'cloud::azure::management::graphexplorer::mode::query', | ||
| ); | ||
|
|
||
| $self->{custom_modes}{api} = 'cloud::azure::management::graphexplorer::custom::api'; | ||
| return $self; | ||
| } | ||
|
|
||
| sub init { | ||
| my ($self, %options) = @_; | ||
|
|
||
| $self->{options}->add_options(arguments => { | ||
| 'api-version:s' => { name => 'api_version', default => '2024-04-01' }, | ||
| }); | ||
|
|
||
| $self->SUPER::init(%options); | ||
| } | ||
|
|
||
| 1; | ||
|
|
||
| __END__ | ||
|
|
||
| =head1 PLUGIN DESCRIPTION | ||
|
|
||
| Check Microsoft Azure Resource Graph using KQL queries. | ||
|
|
||
| =cut |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.