Debian Codegraph @

src/os/windows/wsman/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 os::windows::wsman::mode::time;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::misc;
28 use DateTime;
29
30 sub custom_usage_output {
31 my ($self, %options) = @_;
32
33 return sprintf(
34 'Time offset %d second(s): %s',
35 $self->{result_values}->{offset},
36 $self->{result_values}->{date}
37 );
38 }
39
40 sub set_counters {
41 my ($self, %options) = @_;
42
43 $self->{maps_counters_type} = [
44 { name => 'offset', type => 0 }
45 ];
46
47 $self->{maps_counters}->{offset} = [
48 { label => 'offset', nlabel => 'time.offset.seconds', set => {
49 key_values => [ { name => 'offset' }, { name => 'date' } ],
50 closure_custom_output => $self->can('custom_usage_output'),
51 perfdatas => [
52 { template => '%d', unit => 's' }
53 ]
54 }
55 }
56 ];
57 }
58
59 sub new {
60 my ($class, %options) = @_;
61 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
62 bless $self, $class;
63
64 $options{options}->add_options(arguments => {
65 'ntp-hostname:s' => { name => 'ntp_hostname' },
66 'ntp-port:s' => { name => 'ntp_port', default => 123 },
67 'timezone:s' => { name => 'timezone' }
68 });
69
70 return $self;
71 }
72
73 sub check_options {
74 my ($self, %options) = @_;
75 $self->SUPER::check_options(%options);
76
77 if (defined($self->{option_results}->{ntp_hostname})) {
78 centreon::plugins::misc::mymodule_load(
79 output => $self->{output}, module => 'Net::NTP',
80 error_msg => "Cannot load module 'Net::NTP'."
81 );
82 }
83 }
84
85 sub get_from_datetime {
86 my ($self, %options) = @_;
87
88 my @remote_date = (
89 $options{year},
90 $options{mounth},
91 $options{day},
92 $options{hour},
93 $options{minute},
94 $options{second},
95 $options{ms}
96 );
97
98 my $dt = DateTime->new(
99 year => $remote_date[0],
100 month => $remote_date[1],
101 day => $remote_date[2],
102 hour => $remote_date[3],
103 minute => $remote_date[4],
104 second => $remote_date[5],
105 time_zone => 'UTC'
106 );
107
108 my $timezone = 'UTC';
109 if (defined($self->{option_results}->{timezone}) && $self->{option_results}->{timezone} ne '') {
110 $timezone = $self->{option_results}->{timezone};
111 my $tz = centreon::plugins::misc::set_timezone(name => $self->{option_results}->{timezone});
112 $dt->set_time_zone($tz->{time_zone});
113 @remote_date = ($dt->year, $dt->month, $dt->day, $dt->hour, $dt->minute, $dt->second);
114 }
115
116 return ($dt->epoch(), \@remote_date, $timezone);
117 }
118
119 sub get_target_time {
120 my ($self, %options) = @_;
121
122 my $results = $options{wsman}->request(
123 uri => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/*',
124 wql_filter => "Select * from Win32_UTCTime",
125 result_type => 'array'
126 );
127
128 #
129 #CLASS: Win32_UTCTime
130 #Day;DayOfWeek;Hour;Milliseconds;Minute;Month;Quarter;Second;WeekInMonth;Year
131 #23;4;18;0;56;12;4;45;4;2021
132 #
133
134 my ($day, $hour, $minute, $month, $second, $year);
135 foreach (@$results) {
136 $day = $_->{Day};
137 $hour = $_->{Hour};
138 $minute = $_->{Minute};
139 $month = $_->{Month};
140 $second = $_->{Second};
141 $year = $_->{Year};
142 }
143
144 return $self->get_from_datetime(
145 year => $year,
146 mounth => $month,
147 day => $day,
148 hour => $hour,
149 minute => $minute,
150 second => $second
151 );
152 }
153
154 sub manage_selection {
155 my ($self, %options) = @_;
156
157 my ($distant_time, $remote_date, $timezone) = $self->get_target_time(%options);
158 if ($distant_time == 0) {
159 $self->{output}->add_option_msg(short_msg => "Couldn't get system date: local time: 0");
160 $self->{output}->option_exit();
161 }
162
163 my $ref_time;
164 if (defined($self->{option_results}->{ntp_hostname}) && $self->{option_results}->{ntp_hostname} ne '') {
165 my %ntp;
166
167 eval {
168 %ntp = Net::NTP::get_ntp_response($self->{option_results}->{ntp_hostname}, $self->{option_results}->{ntp_port});
169 };
170 if ($@) {
171 $self->{output}->add_option_msg(short_msg => "Couldn't connect to ntp server: " . $@);
172 $self->{output}->option_exit();
173 }
174
175 $ref_time = $ntp{'Transmit Timestamp'};
176 } else {
177 $ref_time = time();
178 }
179
180 my $offset = $distant_time - $ref_time;
181 my $remote_date_formated = sprintf(
182 'Local Time: %02d-%02d-%02dT%02d:%02d:%02d (%s)',
183 $remote_date->[0], $remote_date->[1], $remote_date->[2],
184 $remote_date->[3], $remote_date->[4], $remote_date->[5], $timezone
185 );
186
187 $self->{offset} = {
188 offset => sprintf('%d', $offset),
189 date => $remote_date_formated
190 };
191 }
192
193 1;
194
195 __END__
196
197 =head1 MODE
198
199 Check time offset of server with ntp server. Use local time if ntp-host option is not set.
200 SNMP gives a date with second precision (no milliseconds). Time precision is not very accurate.
201 Use threshold with (+-) 2 seconds offset (minimum).
202
203 =over 8
204
205 =item B<--warning-offset>
206
207 Time offset warning threshold (in seconds).
208
209 =item B<--critical-offset>
210
211 Time offset critical Threshold (in seconds).
212
213 =item B<--ntp-hostname>
214
215 Set the ntp hostname (if not set, localtime is used).
216
217 =item B<--ntp-port>
218
219 Set the ntp port (default: 123).
220
221 =item B<--timezone>
222
223 Set the timezone of distant server. For Windows, you need to set it.
224 Can use format: 'Europe/London' or '+0100'.
225
226 =back
227
228 =cut