From 33ff112c4a6075619282138246801adfdd4dc596 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Wed, 5 Jun 2019 09:09:34 +0200 Subject: [PATCH] Fix #1536 --- os/windows/local/mode/ntp.pm | 72 +++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 25 deletions(-) diff --git a/os/windows/local/mode/ntp.pm b/os/windows/local/mode/ntp.pm index 07446ed19..dae5b5453 100644 --- a/os/windows/local/mode/ntp.pm +++ b/os/windows/local/mode/ntp.pm @@ -37,14 +37,14 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "ntp-hostname:s" => { name => 'ntp_hostname' }, - "ntp-port:s" => { name => 'ntp_port', default => 123 }, - "warning:s" => { name => 'warning' }, - "critical:s" => { name => 'critical' }, - "timeout:s" => { name => 'timeout', default => 30 }, - }); + $options{options}->add_options(arguments => { + 'ntp-hostname:s' => { name => 'ntp_hostname' }, + 'ntp-port:s' => { name => 'ntp_port', default => 123 }, + 'warning:s' => { name => 'warning' }, + 'critical:s' => { name => 'critical' }, + 'timeout:s' => { name => 'timeout', default => 30 }, + }); + return $self; } @@ -53,12 +53,28 @@ sub check_options { $self->SUPER::init(%options); if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); - $self->{output}->option_exit(); + $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); + $self->{output}->option_exit(); } if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); - $self->{output}->option_exit(); + $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); + $self->{output}->option_exit(); + } +} + +sub check_ntp_query { + my ($self, %options) = @_; + + my ($stdout) = centreon::plugins::misc::windows_execute( + output => $self->{output}, + timeout => $self->{option_results}->{timeout}, + command => 'w32tm /query /status', + command_path => undef, + command_options => undef, + no_quit => 1 + ); + if ($stdout =~ /^Source:\s+(\S+)/mi) { + return $1; } } @@ -67,11 +83,13 @@ sub run { my $ntp_hostname = $self->{option_results}->{ntp_hostname}; if (!defined($ntp_hostname)) { - my ($stdout) = centreon::plugins::misc::windows_execute(output => $self->{output}, - timeout => $self->{option_results}->{timeout}, - command => 'w32tm /dumpreg /subkey:parameters', - command_path => undef, - command_options => undef); + my ($stdout) = centreon::plugins::misc::windows_execute( + output => $self->{output}, + timeout => $self->{option_results}->{timeout}, + command => 'w32tm /dumpreg /subkey:parameters', + command_path => undef, + command_options => undef + ); my ($type, $ntp_server); $stdout =~ /^Type\s+\S+\s+(\S+)/mi; $type = $1; @@ -85,16 +103,19 @@ sub run { # AllSync: The client synchronizes time from any available time source, including domain hierarchy and external time sources if ($type =~ /NoSync/i) { $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => sprintf("No ntp configuration set. Please use --ntp-hostname or set windows ntp configuration.")); + short_msg => sprintf('No ntp configuration set. Please use --ntp-hostname or set windows ntp configuration.')); $self->{output}->display(); $self->{output}->exit(); + } elsif ($type =~ /NT5DS/i) { + $ntp_server = $self->check_ntp_query(); } if (!defined($ntp_server)) { $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => sprintf("Cannot get ntp source configuration (it uses AD). Please use --ntp-hostname.")); + short_msg => sprintf('Cannot get ntp source configuration. Please use --ntp-hostname.')); $self->{output}->display(); $self->{output}->exit(); } + $ntp_hostname = $ntp_server; } @@ -117,13 +138,14 @@ sub run { my $exit = $self->{perfdata}->threshold_check(value => $diff, threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); $self->{output}->output_add(severity => $exit, - short_msg => sprintf("Time offset %.3f second(s)", $diff)); + short_msg => sprintf('Time offset %.3f second(s)', $diff)); - $self->{output}->perfdata_add(label => 'offset', unit => 's', - value => sprintf("%.3f", $diff), - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), - ); + $self->{output}->perfdata_add( + label => 'offset', unit => 's', + value => sprintf("%.3f", $diff), + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), + ); $self->{output}->display(); $self->{output}->exit();