Debian Codegraph @

src/hardware/ups/hp/snmp/mode/time.pm

raw
1 #
2 # Copyright 2024 Centreon (http://www.centreon.com/)
3 #
4 # Centreon is a full-fledged industry-strength solution that meets
5 # the needs in IT infrastructure and application monitoring for
6 # service performance.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20
21 package hardware::ups::hp::snmp::mode::time;
22
23 use base qw(snmp_standard::mode::ntp);
24
25 use strict;
26 use warnings;
27 use DateTime;
28
29 sub get_target_time {
30 my ($self, %options) = @_;
31
32 my $oid_upsConfigDateAndTime = '.1.3.6.1.4.1.232.165.3.9.5.0';
33 my $snmp_result = $options{snmp}->get_leef(oids => [ $oid_upsConfigDateAndTime ], nothing_quit => 1);
34
35 # format: "11/18/2019 20:13:17"
36 if ($snmp_result->{$oid_upsConfigDateAndTime} !~ /^(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+):(\d+)/) {
37 $self->{output}->add_option_msg(short_msg => 'cannot parse date format: ' . $snmp_result->{$oid_upsConfigDateAndTime});
38 $self->{output}->option_exit();
39 }
40
41 my $remote_date = [$3, $1, $2, $4, $5, $6];
42
43 my $timezone = 'UTC';
44 if (defined($self->{option_results}->{timezone}) && $self->{option_results}->{timezone} ne '') {
45 $timezone = $self->{option_results}->{timezone};
46 }
47
48 my $tz = centreon::plugins::misc::set_timezone(name => $timezone);
49 my $dt = DateTime->new(
50 year => $remote_date->[0],
51 month => $remote_date->[1],
52 day => $remote_date->[2],
53 hour => $remote_date->[3],
54 minute => $remote_date->[4],
55 second => $remote_date->[5],
56 %$tz
57 );
58
59 return ($dt->epoch, $remote_date, $timezone);
60 }
61
62 1;
63
64 __END__
65
66 =head1 MODE
67
68 Check time offset of server with ntp server. Use local time if ntp-host option is not set.
69 SNMP gives a date with second precision (no milliseconds). Time precision is not very accurate.
70 Use threshold with (+-) 2 seconds offset (minimum).
71
72 =over 8
73
74 =item B<--warning-offset>
75
76 Time offset warning threshold (in seconds).
77
78 =item B<--critical-offset>
79
80 Time offset critical Threshold (in seconds).
81
82 =item B<--ntp-hostname>
83
84 Set the ntp hostname (if not set, localtime is used).
85
86 =item B<--ntp-port>
87
88 Set the ntp port (default: 123).
89
90 =item B<--timezone>
91
92 Set the timezone of distant server. For Windows, you need to set it.
93 Can use format: 'Europe/London' or '+0100'.
94
95 =back
96
97 =cut