(plugin) apps::microsoft::iis::wsman - refactoring (#3706)

This commit is contained in:
qgarnier 2022-06-02 14:40:29 +02:00 committed by GitHub
parent 4266841878
commit 197e812814
4 changed files with 204 additions and 274 deletions

View File

@ -0,0 +1,161 @@
#
# Copyright 2022 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 apps::microsoft::iis::wsman::mode::applicationpools;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
sub custom_status_output {
my ($self, %options) = @_;
return sprintf(
'state: %s [auto: %s]',
$self->{result_values}->{state},
$self->{result_values}->{auto_start}
);
}
sub prefix_pool_output {
my ($self, %options) = @_;
return "Application pool '" . $options{instance_value}->{name} . "' ";
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0 },
{ name => 'pools', type => 1, cb_prefix_output => 'prefix_pool_output', message_multiple => 'All application pools are ok' }
];
$self->{maps_counters}->{global} = [
{ label => 'pools-detected', display_ok => 0, nlabel => 'pools.detected.count', set => {
key_values => [ { name => 'detected' } ],
output_template => 'application pools detected: %s',
perfdatas => [
{ template => '%s', min => 0 }
]
}
}
];
$self->{maps_counters}->{pools} = [
{ label => 'status', type => 2, critical_default => '%{auto_start} eq "on" and not %{state} =~ /started|starting/', set => {
key_values => [ { name => 'name' }, { name => 'auto_start' }, { name => 'state' } ],
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
}
];
}
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 => {
'filter-name:s' => { name => 'filter_name' }
});
return $self;
}
my $state_map = {
1 => 'started',
2 => 'starting',
3 => 'stopped',
4 => 'stopping'
};
sub manage_selection {
my ($self, %options) = @_;
my $results = $options{wsman}->request(
uri => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/MicrosoftIISv2/*',
wql_filter => 'Select Name, AppPoolState, AppPoolAutoStart From IIsApplicationPoolSetting',
result_type => 'hash',
hash_key => 'Name'
);
$self->{pools} = {};
foreach my $name (keys %$results) {
if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
$name !~ /$self->{option_results}->{filter_name}/) {
$self->{output}->output_add(long_msg => "skipping pool '" . $name . "': no matching filter.", debug => 1);
next;
}
$self->{pools}->{$name} = {
name => $name,
state => $state_map->{ $results->{$name}->{AppPoolState} },
auto_start => $results->{$name}->{AppPoolAutoStart} =~ /^(?:1|true)$/i ? 'on' : 'off'
};
}
if (scalar(keys %{$self->{pools}}) <= 0) {
$self->{output}->add_option_msg(short_msg => 'No application pool found');
$self->{output}->option_exit();
}
}
1;
__END__
=head1 MODE
Check IIS application pools.
=over 8
=item B<--filter-name>
Filter application pool name (can be a regexp).
=item B<--unknown-status>
Set unknown threshold for status.
Can used special variables like: %{name}, %{state}, %{auto_start}.
=item B<--warning-status>
Set warning threshold for status.
Can used special variables like: %{name}, %{state}, %{auto_start}.
=item B<--critical-status>
Set critical threshold for status (Default: '%{auto_start} eq "on" and not %{state} =~ /started|starting/').
Can used special variables like: %{name}, %{state}, %{auto_start}.
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'pools-detected'.
=back
=cut

View File

@ -1,208 +0,0 @@
#
# Copyright 2022 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 apps::microsoft::iis::wsman::mode::applicationpoolstate;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
my %state_map = (
1 => 'started',
2 => 'starting',
3 => 'stopped',
4 => 'stopping'
);
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments => {
"warning" => { name => 'warning' },
"critical" => { name => 'critical' },
"pools:s" => { name => 'pools' },
"auto" => { name => 'auto' },
"exclude:s" => { name => 'exclude' }
});
$self->{pools_rules} = {};
$self->{wql_filter} = '';
$self->{threshold} = 'CRITICAL';
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
if (!defined($self->{option_results}->{pools}) && !defined($self->{option_results}->{auto})) {
$self->{output}->add_option_msg(short_msg => "Need to specify at least '--pools' or '--auto' option.");
$self->{output}->option_exit();
}
if (defined($self->{option_results}->{pools})) {
my $append = '';
foreach my $rule (split /,/, $self->{option_results}->{pools}) {
if ($rule !~ /^([^\!=]*)(\!=|=){0,1}(.*){0,1}/) {
$self->{output}->add_option_msg(short_msg => "Wrong rule in --pools option: " . $rule);
$self->{output}->option_exit();
}
if (!defined($1) || $1 eq '') {
$self->{output}->add_option_msg(short_msg => "Need pool name for rule: " . $rule);
$self->{output}->option_exit();
}
my $poolname = $1;
my $operator = defined($2) && $2 ne '' ? $2 : '!=';
my $state = defined($3) && $3 ne '' ? lc($3) : 'starting';
if ($operator !~ /^(=|\!=)$/) {
$self->{output}->add_option_msg(short_msg => "Wrong operator for rule: " . $rule . ". Should be '=' or '!='.");
$self->{output}->option_exit();
}
if ($state !~ /^(started|starting|stopped|stopping)$/i) {
$self->{output}->add_option_msg(short_msg => "Wrong state for rule: " . $rule . ". See help for available state.");
$self->{output}->option_exit();
}
$self->{service_rules}->{$poolname} = {operator => $operator, state => $state};
$self->{wql_filter} .= $append . "Name = '" . $poolname . "'";
$append = ' Or ';
}
if ($self->{wql_filter} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify one rule for --pools option.");
$self->{output}->option_exit();
}
}
$self->{threshold} = 'WARNING' if (defined($self->{option_results}->{warning}));
$self->{threshold} = 'CRITICAL' if (defined($self->{option_results}->{critical}));
}
sub check_auto {
my ($self, %options) = @_;
$self->{result} = $self->{wsman}->request(uri => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/MicrosoftIISv2/*',
wql_filter => "Select AppPoolState, Name From IIsApplicationPoolSetting Where AppPoolAutoStart = 'true'",
result_type => 'hash',
hash_key => 'Name');
foreach my $name (sort(keys %{$self->{result}})) {
if (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} ne '' && $self->{result}->{$name}->{Name} =~ /$self->{option_results}->{exclude}/) {
$self->{output}->output_add(long_msg => "Skipping pool '" . $self->{result}->{$name}->{Name} . "'");
next;
}
$self->{output}->output_add(long_msg => "Pool '" . $self->{result}->{$name}->{Name} . "' state: " . $state_map{$self->{result}->{$name}->{AppPoolState}});
if ($state_map{$self->{result}->{$name}->{AppPoolState}} !~ /^starting$/i) {
$self->{output}->output_add(severity => $self->{threshold},
short_msg => "Service '" . $self->{result}->{$name}->{Name} . "' is " . $state_map{$self->{result}->{$name}->{AppPoolState}});
}
}
}
sub check {
my ($self, %options) = @_;
$self->{result} = $self->{wsman}->request(uri => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/MicrosoftIISv2/*',
wql_filter => 'Select AppPoolState, Name From IIsApplicationPoolSetting Where ' . $self->{wql_filter},
result_type => 'hash',
hash_key => 'Name');
foreach my $name (sort(keys %{$self->{service_rules}})) {
if (!defined($self->{result}->{$name})) {
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => "Pool '" . $name . "' not found");
next;
}
$self->{output}->output_add(long_msg => "Pool '" . $name . "' state: " . $state_map{$self->{result}->{$name}->{AppPoolState}});
if ($self->{service_rules}->{$name}->{operator} eq '=' &&
$state_map{$self->{result}->{$name}->{AppPoolState}} eq $self->{service_rules}->{$name}->{state}) {
$self->{output}->output_add(severity => $self->{threshold},
short_msg => "Pool '" . $self->{result}->{$name}->{Name} . "' is " . $state_map{$self->{result}->{$name}->{AppPoolState}});
} elsif ($self->{service_rules}->{$name}->{operator} eq '!=' &&
$state_map{$self->{result}->{$name}->{AppPoolState}} ne $self->{service_rules}->{$name}->{state}) {
$self->{output}->output_add(severity => $self->{threshold},
short_msg => "Service '" . $self->{result}->{$name}->{Name} . "' is " . $state_map{$self->{result}->{$name}->{AppPoolState}});
}
}
}
sub run {
my ($self, %options) = @_;
# $options{wsman} = wsman object
$self->{wsman} = $options{wsman};
$self->{output}->output_add(severity => 'OK',
short_msg => 'All application pools are ok');
if (defined($self->{option_results}->{auto})) {
$self->check_auto();
} else {
$self->check();
}
$self->{output}->display();
$self->{output}->exit();
}
1;
__END__
=head1 MODE
Check IIS Application Pools State.
Need to install IIS WMI provider by installing the IIS Management Scripts and Tools component (compatibility IIS 6.0).
=over 8
=item B<--warning>
Return warning.
=item B<--critical>
Return critical.
=item B<--pools>
Application Pool to monitor.
Syntax: [pool_name[[=|!=]state]],...
Available states are:
- 'started',
- 'starting',
- 'stopped',
- 'stopping'
=item B<--auto>
Return threshold for auto start pools not starting.
=item B<--exclude>
Exclude some pool for --auto option (Can be a regexp).
=back
=cut

View File

@ -25,24 +25,12 @@ use base qw(centreon::plugins::mode);
use strict;
use warnings;
my %state_map = (
1 => 'started',
2 => 'starting',
3 => 'stopped',
4 => 'stopping'
);
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments =>
{
"name:s" => { name => 'name' },
"regexp" => { name => 'use_regexp' },
"filter-state:s" => { name => 'filter_state' },
});
$options{options}->add_options(arguments => {});
return $self;
}
@ -52,47 +40,50 @@ sub check_options {
$self->SUPER::init(%options);
}
my $state_map = {
1 => 'started',
2 => 'starting',
3 => 'stopped',
4 => 'stopping'
};
sub manage_selection {
my ($self, %options) = @_;
$self->{result} = $self->{wsman}->request(uri => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/MicrosoftIISv2/*',
wql_filter => 'Select AppPoolState, AppPoolAutoStart, Name From IIsApplicationPoolSetting',
result_type => 'hash',
hash_key => 'Name');
# AppPoolAutoStart -> true/false
# AppPoolState -> 1=started, 2=starting, 3 = stopped, 4=stopping
foreach my $name (sort(keys %{$self->{result}})) {
if (defined($self->{option_results}->{filter_state}) && $state_map{$self->{result}->{$name}->{AppPoolState}} !~ /$self->{option_results}->{filter_state}/) {
$self->{output}->output_add(long_msg => "Skipping application pool '" . $name . "': no matching filter state");
delete $self->{result}->{$name};
next;
}
my $results = $options{wsman}->request(
uri => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/MicrosoftIISv2/*',
wql_filter => 'Select AppPoolState, AppPoolAutoStart, Name From IIsApplicationPoolSetting',
result_type => 'hash',
hash_key => 'Name'
);
# Get all without a name
next if (!defined($self->{option_results}->{name}));
next if (!defined($self->{option_results}->{use_regexp}) && $name eq $self->{option_results}->{name});
next if (defined($self->{option_results}->{use_regexp}) && $name =~ /$self->{option_results}->{name}/);
$self->{output}->output_add(long_msg => "Skipping application pool '" . $name . "': no matching filter name");
delete $self->{result}->{$name};
foreach my $name (keys %$results) {
$results->{$name}->{AppPoolState} = $state_map->{ $results->{$name}->{AppPoolState} };
$results->{$name}->{AppPoolAutoStart} = $results->{$name}->{AppPoolAutoStart} =~ /^(?:1|true)$/i ? 'on' : 'off';
}
return $results;
}
sub run {
my ($self, %options) = @_;
# $options{wsman} = wsman object
$self->{wsman} = $options{wsman};
$self->manage_selection();
foreach my $name (sort(keys %{$self->{result}})) {
$self->{output}->output_add(long_msg => "'" . $name . "' [AutoStart = " . $self->{result}->{$name}->{AppPoolAutoStart} . '] [' .
'State = ' . $state_map{$self->{result}->{$name}->{AppPoolState}} .
']');
my $results = $self->manage_selection(wsman => $options{wsman});
foreach my $name (sort(keys %$results)) {
$self->{output}->output_add(
long_msg => sprintf(
'[name: %s][auto_start: %s][state: %s]',
$name,
$results->{$name}->{AppPoolAutoStart},
$results->{$name}->{AppPoolState}
)
);
}
$self->{output}->output_add(severity => 'OK',
short_msg => 'List application pools:');
$self->{output}->output_add(
severity => 'OK',
short_msg => 'List application pools:'
);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit();
}
@ -105,15 +96,14 @@ sub disco_format {
sub disco_show {
my ($self, %options) = @_;
# $options{wsman} = wsman object
$self->{wsman} = $options{wsman};
$self->manage_selection();
foreach my $name (sort(keys %{$self->{result}})) {
$self->{output}->add_disco_entry(name => $name,
auto_start => $self->{result}->{$name}->{AppPoolAutoStart},
state => $state_map{$self->{result}->{$name}->{AppPoolState}}
);
my $results = $self->manage_selection(wsman => $options{wsman});
foreach my $name (sort(keys %$results)) {
$self->{output}->add_disco_entry(
name => $name,
auto_start => $results->{$name}->{AppPoolAutoStart},
state => $results->{$name}->{AppPoolState}
);
}
}
@ -128,18 +118,6 @@ Need to install IIS WMI provider by installing the IIS Management Scripts and To
=over 8
=item B<--name>
Set the application pool name.
=item B<--regexp>
Allows to use regexp to filter application pool name (with option --name).
=item B<--filter-state>
Filter application pool state. Regexp can be used.
=back
=cut

View File

@ -29,10 +29,9 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '0.1';
$self->{modes} = {
'applicationpool-state' => 'apps::microsoft::iis::wsman::mode::applicationpoolstate',
'list-applicationpools' => 'apps::microsoft::iis::wsman::mode::listapplicationpools'
'application-pools' => 'apps::microsoft::iis::wsman::mode::applicationpools',
'list-application-pools' => 'apps::microsoft::iis::wsman::mode::listapplicationpools'
};
return $self;