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