(plugin) apps::microsoft::activedirectory::wsman - refactoring dcdiag mode

This commit is contained in:
qgarnier 2022-01-26 17:28:26 +01:00 committed by GitHub
parent ec4e69ad2b
commit 24d3c2f6ac
1 changed files with 115 additions and 76 deletions

View File

@ -25,7 +25,7 @@ use base qw(centreon::plugins::mode);
use strict;
use warnings;
use File::Basename;
use XML::LibXML;
use XML::Simple;
sub new {
my ($class, %options) = @_;
@ -33,15 +33,17 @@ sub new {
bless $self, $class;
$options{options}->add_options(arguments => {
"config:s" => { name => 'config' },
"language:s" => { name => 'language', default => 'en' },
"dfsr" => { name => 'dfsr' },
'config:s' => { name => 'config' },
'language:s' => { name => 'language', default => 'en' },
'dfsr' => { name => 'dfsr' },
'nomachineaccount' => { name => 'nomachineaccount' },
"noeventlog" => { name => 'noeventlog' }
});
$self->{os_is2003} = 0;
$self->{os_is2008} = 0;
$self->{os_is2012} = 0;
$self->{os_is2016} = 0;
$self->{msg} = {ok => undef, warning => undef, critical => undef};
return $self;
@ -60,25 +62,32 @@ sub check_options {
sub check_version {
my ($self, %options) = @_;
$self->{result} = $self->{wsman}->execute_winshell_commands(commands => [{label => 'os_version', value => 'ver' }],
keep_open => 1);
$self->{result} = $self->{wsman}->execute_winshell_commands(
commands => [ { label => 'os_version', value => 'ver' } ],
keep_open => 1
);
if ($self->{result}->{os_version}->{exit_code} != 0) {
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => 'Problem execution to get OS version (use verbose for more details)');
$self->{output}->output_add(
severity => 'UNKNOWN',
short_msg => 'Problem execution to get OS version (use verbose for more details)'
);
$self->{output}->output_add(long_msg => 'stderr: ' . $self->{result}->{os_version}->{stderr}) if (defined($self->{result}->{os_version}->{stderr}));
$self->{output}->output_add(long_msg => 'stdout: ' . $self->{result}->{os_version}->{stdout}) if (defined($self->{result}->{os_version}->{stdout}));
return 1;
}
if ($self->{result}->{os_version}->{stdout} !~ /(\d+\.\d+\.\d+)/) {
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => 'Cannot find OS version in output');
$self->{output}->output_add(
severity => 'UNKNOWN',
short_msg => 'Cannot find OS version in output'
);
return 1;
}
# 5.1, 5.2 => XP/2003
# 6.0, 6.1 => Vista/7/2008
# 6.2, 6.3 => 2012
# 10.0 => 2016, 2019
my $os_version = $1;
if ($os_version =~ /^(5\.1\.|5\.2\.)/) {
$self->{os_is2003} = 1;
@ -86,100 +95,126 @@ sub check_version {
$self->{os_is2008} = 1;
} elsif ($os_version =~ /^(6\.2\.|6\.3\.)/) {
$self->{os_is2012} = 1;
} elsif ($os_version =~ /^10\.0\./) {
$self->{os_is2016} = 1;
} else {
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => 'OS version ' . $os_version . ' not managed.');
$self->{output}->output_add(
severity => 'UNKNOWN',
short_msg => 'OS version ' . $os_version . ' not managed.'
);
return 1;
}
return 0;
}
sub load_xml {
sub read_config {
my ($self, %options) = @_;
# Load XML File
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($self->{config_file});
my $nodes = $doc->getElementsByTagName('dcdiag');
if ($nodes->size() == 0) {
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => 'Cannot find a <dcdiag> node in XML Config file');
return 1;
my $content_file = <<'END_FILE';
<?xml version="1.0" encoding="UTF-8"?>
<root>
<dcdiag language="en">
<messages>
<global>Starting test.*?:\s+(.*?)\n.*?(passed|warning|failed)</global>
<ok>passed</ok>
<warning>warning</warning>
<critical>failed</critical>
</messages>
</dcdiag>
<dcdiag language="fr">
<messages>
<global>D.*?marrage du test.*?:\s+(.*?)\n.*?(a r.*?ussi|a .*?chou.|warning)</global>
<ok>a r.*?ussi</ok>
<warning>warning</warning>
<critical>a .*?chou.</critical>
</messages>
</dcdiag>
</root>
END_FILE
if (defined($self->{option_results}->{config}) && $self->{option_results}->{config} ne '') {
$content_file = do {
local $/ = undef;
if (!open my $fh, "<", $self->{option_results}->{config}) {
$self->{output}->add_option_msg(short_msg => "Could not open file $self->{option_results}->{config} : $!");
$self->{output}->option_exit();
}
<$fh>;
};
}
my $language_found = 0;
foreach my $node ($nodes->get_nodelist()) {
if ($node->getAttribute('language') eq $self->{option_results}->{language}) {
$language_found = 1;
my $messages_node = $node->getElementsByTagName('messages');
foreach my $node2 ($messages_node->get_nodelist()) {
foreach my $element_msg ($node2->getChildrenByTagName('*')) {
$self->{msg}->{$element_msg->nodeName} = $element_msg->textContent if (exists($self->{msg}->{$element_msg->nodeName}));
}
}
last;
}
my $content;
eval {
$content = XMLin($content_file, ForceArray => ['dcdiag'], KeyAttr => ['language']);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode xml response: $@");
$self->{output}->option_exit();
}
if ($language_found == 0) {
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => "Cannot found language '" . $self->{option_results}->{language} . "' in XML Config file");
return 1;
if (!defined($content->{dcdiag}->{$self->{option_results}->{language}})) {
$self->{output}->add_option_msg(short_msg => "Cannot find language '$self->{option_results}->{language}' in config file");
$self->{output}->option_exit();
}
foreach my $label (keys %{$self->{msg}}) {
if (!defined($self->{msg}->{$label})) {
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => "Message '" . $label . "' for language '" . $self->{option_results}->{language} . "' not defined in XML Config file");
return 1;
}
}
return 0;
return $content->{dcdiag}->{ $self->{option_results}->{language} };
}
sub dcdiag {
my ($self, %options) = @_;
my $dcdiag_cmd = 'dcdiag /test:services /test:replications /test:advertising /test:fsmocheck /test:ridmanager /test:machineaccount';
my $dcdiag_cmd = 'dcdiag /test:services /test:replications /test:advertising /test:fsmocheck /test:ridmanager';
$dcdiag_cmd .= ' /test:machineaccount' if (!defined($self->{option_results}->{nomachineaccount}));
$dcdiag_cmd .= ' /test:frssysvol' if ($self->{os_is2003} == 1);
$dcdiag_cmd .= ' /test:sysvolcheck' if ($self->{os_is2008} == 1 || $self->{os_is2012} == 1);
$dcdiag_cmd .= ' /test:sysvolcheck' if ($self->{os_is2008} == 1 || $self->{os_is2012} == 1 || $self->{os_is2016} == 1);
if (!defined($self->{option_results}->{noeventlog})) {
$dcdiag_cmd .= ' /test:frsevent /test:kccevent' if ($self->{os_is2003} == 1);
$dcdiag_cmd .= ' /test:frsevent /test:kccevent' if (($self->{os_is2008} == 1 || $self->{os_is2012} == 1) && !defined($self->{option_results}->{dfsr}));
$dcdiag_cmd .= ' /test:dfsrevent /test:kccevent' if (($self->{os_is2008} == 1 || $self->{os_is2012} == 1) && defined($self->{option_results}->{dfsr}));
$dcdiag_cmd .= ' /test:frsevent /test:kccevent' if (($self->{os_is2008} == 1 || $self->{os_is2012} == 1 || $self->{os_is2016} == 1) && !defined($self->{option_results}->{dfsr}));
$dcdiag_cmd .= ' /test:dfsrevent /test:kccevent' if (($self->{os_is2008} == 1 || $self->{os_is2012} == 1 || $self->{os_is2016} == 1) && defined($self->{option_results}->{dfsr}));
}
$self->{result} = $self->{wsman}->execute_winshell_commands(commands => [ { label => 'dcdiag', value => $dcdiag_cmd } ]);
if ($self->{result}->{dcdiag}->{exit_code} != 0) {
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => 'Problem execution of dcdiag (use verbose for more details)');
$self->{output}->output_add(
severity => 'UNKNOWN',
short_msg => 'Problem execution of dcdiag (use verbose for more details)'
);
$self->{output}->output_add(long_msg => 'stderr: ' . $self->{result}->{dcdiag}->{stderr}) if (defined($self->{result}->{dcdiag}->{stderr}));
$self->{output}->output_add(long_msg => 'stdout: ' . $self->{result}->{dcdiag}->{stdout}) if (defined($self->{result}->{dcdiag}->{stdout}));
return 1;
}
my $match = 0;
foreach my $line (split /\n/, $self->{result}->{os_version}->{stdout}) {
if ($line =~ /$self->{msg}->{ok}/) {
while ($self->{result}->{dcdiag}->{stdout} =~ /$options{config}->{messages}->{global}/imsg) {
my ($test_name, $pattern) = ($1, lc($2));
if ($pattern =~ /$options{config}->{messages}->{ok}/i) {
$match = 1;
$self->{output}->output_add(severity => 'OK',
short_msg => $1);
} elsif ($line =~ /$self->{msg}->{critical}/) {
$self->{output}->output_add(
severity => 'OK',
short_msg => $test_name
);
} elsif ($pattern =~ /$options{config}->{messages}->{critical}/i) {
$match = 1;
$self->{output}->output_add(severity => 'CRITICAL',
short_msg => 'test ' . $1);
} elsif ($line =~ /$self->{msg}->{warning}/) {
$self->{output}->output_add(
severity => 'CRITICAL',
short_msg => 'test ' . $test_name
);
} elsif ($pattern =~ /$options{config}->{messages}->{warning}/i) {
$match = 1;
$self->{output}->output_add(severity => 'WARNING',
short_msg => 'test ' . $1);
$self->{output}->output_add(
severity => 'WARNING',
short_msg => 'test ' . $test_name
);
}
}
if ($match == 0) {
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => 'Cannot match output test (maybe you need to set the good language)');
$self->{output}->output_add(
severity => 'UNKNOWN',
short_msg => 'Cannot match output test (maybe you need to set the good language)'
);
return 1;
}
@ -188,11 +223,11 @@ sub dcdiag {
sub run {
my ($self, %options) = @_;
# $options{wsman} = wsman object
$self->{wsman} = $options{wsman};
if ($self->load_xml() == 0 && $self->check_version() == 0) {
$self->dcdiag();
my $config = $self->read_config();
if ($self->check_version() == 0) {
$self->dcdiag(config => $config);
}
$self->{output}->display();
@ -222,6 +257,10 @@ Set the language used in config file (default: 'en').
Specifies that SysVol replication uses DFS instead of FRS (Windows 2008 or later)
=item B<--nomachineaccount>
Don't run the dc tests machineaccount
=item B<--noeventlog>
Don't run the dc tests kccevent, frsevent and dfsrevent