enhance ntp snmp

This commit is contained in:
garnier-quentin 2019-10-21 09:18:50 +02:00
parent c88f2e2258
commit c2510e70ff
1 changed files with 44 additions and 31 deletions

View File

@ -27,6 +27,16 @@ use warnings;
use centreon::plugins::misc; use centreon::plugins::misc;
use DateTime; use DateTime;
sub custom_usage_output {
my ($self, %options) = @_;
return sprintf(
'Time offset %d second(s): %s',
$self->{result_values}->{offset_absolute},
$self->{result_values}->{date_absolute}
);
}
sub set_counters { sub set_counters {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -36,12 +46,12 @@ sub set_counters {
$self->{maps_counters}->{offset} = [ $self->{maps_counters}->{offset} = [
{ label => 'offset', nlabel => 'time.offset.seconds', set => { { label => 'offset', nlabel => 'time.offset.seconds', set => {
key_values => [ { name => 'offset' } ], key_values => [ { name => 'offset' }, { name => 'date' } ],
output_template => 'Offset: %d second(s)', closure_custom_output => $self->can('custom_usage_output'),
perfdatas => [ perfdatas => [
{ label => 'offset', value => 'offset_absolute', template => '%d', unit => 's' }, { label => 'offset', value => 'offset_absolute', template => '%d', unit => 's' },
], ],
} }
}, },
]; ];
} }
@ -51,33 +61,34 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options); my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => $options{options}->add_options(arguments => {
{ 'ntp-hostname:s' => { name => 'ntp_hostname' },
"ntp-hostname:s" => { name => 'ntp_hostname' }, 'ntp-port:s' => { name => 'ntp_port', default => 123 },
"ntp-port:s" => { name => 'ntp_port', default => 123 }, 'timezone:s' => { name => 'timezone' },
"timezone:s" => { name => 'timezone' }, });
});
return $self; return $self;
} }
sub check_options { sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->SUPER::check_options(%options); $self->SUPER::check_options(%options);
if (defined($self->{option_results}->{ntp_hostname})) { if (defined($self->{option_results}->{ntp_hostname})) {
centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'Net::NTP', centreon::plugins::misc::mymodule_load(
error_msg => "Cannot load module 'Net::NTP'."); output => $self->{output}, module => 'Net::NTP',
error_msg => "Cannot load module 'Net::NTP'."
);
} }
} }
sub manage_selection { sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{snmp} = $options{snmp};
$self->{offset} = {}; $self->{offset} = {};
my ($ref_time, $distant_time); my ($ref_time, $distant_time);
my $oid_hrSystemDate = '.1.3.6.1.2.1.25.1.2.0'; my $oid_hrSystemDate = '.1.3.6.1.2.1.25.1.2.0';
my $result = $self->{snmp}->get_leef(oids => [ $oid_hrSystemDate ], nothing_quit => 1); my $result = $options{snmp}->get_leef(oids => [ $oid_hrSystemDate ], nothing_quit => 1);
if (defined($self->{option_results}->{ntp_hostname}) && $self->{option_results}->{ntp_hostname} ne '') { if (defined($self->{option_results}->{ntp_hostname}) && $self->{option_results}->{ntp_hostname} ne '') {
my %ntp; my %ntp;
@ -85,8 +96,10 @@ sub manage_selection {
%ntp = Net::NTP::get_ntp_response($self->{option_results}->{ntp_hostname}, $self->{option_results}->{ntp_port}); %ntp = Net::NTP::get_ntp_response($self->{option_results}->{ntp_hostname}, $self->{option_results}->{ntp_port});
}; };
if ($@) { if ($@) {
$self->{output}->output_add(severity => 'UNKNOWN', $self->{output}->output_add(
short_msg => "Couldn't connect to ntp server: " . $@); severity => 'UNKNOWN',
short_msg => "Couldn't connect to ntp server: " . $@
);
$self->{output}->display(); $self->{output}->display();
$self->{output}->exit(); $self->{output}->exit();
} }
@ -95,7 +108,7 @@ sub manage_selection {
} else { } else {
$ref_time = time(); $ref_time = time();
} }
my @remote_date = unpack 'n C6 a C2', $result->{$oid_hrSystemDate}; my @remote_date = unpack 'n C6 a C2', $result->{$oid_hrSystemDate};
my $timezone = 'UTC'; my $timezone = 'UTC';
if (defined($self->{option_results}->{timezone}) && $self->{option_results}->{timezone} ne '') { if (defined($self->{option_results}->{timezone}) && $self->{option_results}->{timezone} ne '') {
@ -103,7 +116,7 @@ sub manage_selection {
} elsif (defined($remote_date[9])) { } elsif (defined($remote_date[9])) {
$timezone = sprintf("%s%02d%02d", $remote_date[7], $remote_date[8], $remote_date[9]); # format +0630 $timezone = sprintf("%s%02d%02d", $remote_date[7], $remote_date[8], $remote_date[9]); # format +0630
} }
my $tz = centreon::plugins::misc::set_timezone(name => $timezone); my $tz = centreon::plugins::misc::set_timezone(name => $timezone);
my $dt = DateTime->new( my $dt = DateTime->new(
year => $remote_date[0], year => $remote_date[0],
@ -114,21 +127,21 @@ sub manage_selection {
second => $remote_date[5], second => $remote_date[5],
%$tz %$tz
); );
$distant_time = $dt->epoch; $distant_time = $dt->epoch;
my $offset = $distant_time - $ref_time; my $offset = $distant_time - $ref_time;
my $remote_date_formated = sprintf("Local Time : %02d-%02d-%02dT%02d:%02d:%02d (%s)", $remote_date[0], $remote_date[1], $remote_date[2], my $remote_date_formated = sprintf(
$remote_date[3], $remote_date[4], $remote_date[5], $timezone); 'Local Time : %02d-%02d-%02dT%02d:%02d:%02d (%s)',
$remote_date[0], $remote_date[1], $remote_date[2],
$remote_date[3], $remote_date[4], $remote_date[5], $timezone
);
my $formated_offset = sprintf("%d", $offset); my $formated_offset = sprintf("%d", $offset);
$self->{offset} = { $self->{offset} = {
offset => $formated_offset, offset => $formated_offset,
display => 0 }; date => $remote_date_formated,
};
$self->{output}->add_option_msg(short_msg => $remote_date_formated);
} }
1; 1;