Debian Codegraph @

src/storage/hp/3par/ssh/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 storage::hp::3par::ssh::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 prefix_node_output {
41 my ($self, %options) = @_;
42
43 return "Node '" . $options{instance_value}->{id} . "' ";
44 }
45
46 sub set_counters {
47 my ($self, %options) = @_;
48
49 $self->{maps_counters_type} = [
50 { name => 'nodes', type => 1, cb_prefix_output => 'prefix_node_output', message_multiple => 'All nodes time are ok' }
51 ];
52
53 $self->{maps_counters}->{nodes} = [
54 { label => 'offset', nlabel => 'node.time.offset.seconds', set => {
55 key_values => [ { name => 'offset' }, { name => 'date' } ],
56 closure_custom_output => $self->can('custom_usage_output'),
57 perfdatas => [
58 { template => '%d', unit => 's', , label_extra_instance => 1 }
59 ]
60 }
61 }
62 ];
63 }
64
65 sub new {
66 my ($class, %options) = @_;
67 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
68 bless $self, $class;
69
70 $options{options}->add_options(arguments => {
71 'filter-node-id:s' => { name => 'filter_node_id' },
72 'ntp-hostname:s' => { name => 'ntp_hostname' },
73 'ntp-port:s' => { name => 'ntp_port', default => 123 },
74 'timezone:s' => { name => 'timezone' }
75 });
76
77 return $self;
78 }
79
80 sub check_options {
81 my ($self, %options) = @_;
82 $self->SUPER::check_options(%options);
83
84 if (defined($self->{option_results}->{ntp_hostname})) {
85 centreon::plugins::misc::mymodule_load(
86 output => $self->{output}, module => 'Net::NTP',
87 error_msg => "Cannot load module 'Net::NTP'."
88 );
89 }
90 }
91
92 sub get_target_time {
93 my ($self, %options) = @_;
94
95 # 2022-01-25 15:55:37 MSK (Europe/Moscow)
96 return undef if ($options{date} !~ /^\s*(\d{4})-(\d{2})-(\d{2})\s+(\d+):(\d+):(\d+)\s+\S+\s+\(\S+\)/);
97
98 my $timezone = defined($7) ? $7 : 'UTC';
99 if (defined($self->{option_results}->{timezone}) && $self->{option_results}->{timezone} ne '') {
100 $timezone = $self->{option_results}->{timezone};
101 }
102
103 my $tz = centreon::plugins::misc::set_timezone(name => $timezone);
104 my $dt = DateTime->new(
105 year => $1,
106 month => $2,
107 day => $3,
108 hour => $4,
109 minute => $5,
110 second => $6,
111 %$tz
112 );
113
114 my @remote_date = ($dt->year, $dt->month, $dt->day, $dt->hour, $dt->minute, $dt->second);
115 return ($dt->epoch(), \@remote_date, $timezone);
116 }
117
118 sub manage_selection {
119 my ($self, %options) = @_;
120
121 my ($content) = $options{custom}->execute_command(commands => ['showdate']);
122 #Node Date
123 #0 2022-01-25 15:55:37 MSK (Europe/Moscow)
124 #1 2022-01-25 15:55:36 MSK (Europe/Moscow)
125
126 my $ref_time;
127 if (defined($self->{option_results}->{ntp_hostname}) && $self->{option_results}->{ntp_hostname} ne '') {
128 my %ntp;
129
130 eval {
131 %ntp = Net::NTP::get_ntp_response($self->{option_results}->{ntp_hostname}, $self->{option_results}->{ntp_port});
132 };
133 if ($@) {
134 $self->{output}->add_option_msg(short_msg => "Couldn't connect to ntp server: " . $@);
135 $self->{output}->option_exit();
136 }
137
138 $ref_time = $ntp{'Transmit Timestamp'};
139 } else {
140 $ref_time = time();
141 }
142
143 $self->{nodes} = {};
144 foreach my $line (split(/\n/, $content)) {
145 next if ($line !~ /^\s*(\d+)\s+(.*)\s*$/);
146 my ($node_id, $date) = ($1, $2);
147
148 next if (defined($self->{option_results}->{filter_node_id}) && $self->{option_results}->{filter_node_id} ne '' &&
149 $node_id !~ /$self->{option_results}->{filter_node_id}/);
150
151 my ($distant_time, $remote_date, $timezone) = $self->get_target_time(date => $date);
152 next if (!defined($distant_time));
153
154 my $remote_date_formated = sprintf(
155 'local time: %02d-%02d-%02dT%02d:%02d:%02d (%s)',
156 $remote_date->[0], $remote_date->[1], $remote_date->[2],
157 $remote_date->[3], $remote_date->[4], $remote_date->[5], $timezone
158 );
159 my $offset = $distant_time - $ref_time;
160
161 $self->{nodes}->{'node' . $node_id} = {
162 id => $node_id,
163 offset => sprintf('%d', $offset),
164 date => $remote_date_formated
165 };
166 }
167
168 if (scalar(keys %{$self->{nodes}}) <= 0) {
169 $self->{output}->add_option_msg(short_msg => "Couldn't get nodes date");
170 $self->{output}->option_exit();
171 }
172 }
173
174 1;
175
176 __END__
177
178 =head1 MODE
179
180 Check nodes time offset (use local time if ntp-host option is not set).
181
182 =over 8
183
184 =item B<--filter-node-id>
185
186 Filter nodes by ID (can be a regexp).
187
188 =item B<--ntp-hostname>
189
190 Set the ntp hostname (if not set, localtime is used).
191
192 =item B<--ntp-port>
193
194 Set the ntp port (default: 123).
195
196 =item B<--timezone>
197
198 Set the timezone for displaying the date (default: UTC).
199
200 =item B<--warning-offset>
201
202 Time offset warning threshold (in seconds).
203
204 =item B<--critical-offset>
205
206 Time offset critical Threshold (in seconds).
207
208 =back
209
210 =cut