Debian Codegraph @

src/hardware/devices/hikvision/nvr/isapi/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::devices::hikvision::nvr::isapi::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_target_time {
86 my ($self, %options) = @_;
87
88 my $result = $options{custom}->request_api(endpoint => '/ISAPI/System/time/capabilities');
89 if (!defined($result->{localTime})) {
90 $self->{output}->add_option_msg(short_msg => "Cannot find time informations");
91 $self->{output}->option_exit();
92 }
93
94 if ($result->{localTime} !~ /^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)(.*)$/) {
95 $self->{output}->add_option_msg(short_msg => "unsupported date format");
96 $self->{output}->option_exit();
97 }
98
99 my $tz = { time_zone => $7 };
100 if (defined($self->{option_results}->{timezone}) && $self->{option_results}->{timezone} ne '') {
101 $tz = centreon::plugins::misc::set_timezone(name => $self->{option_results}->{timezone});
102 }
103
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 return ($dt->epoch(), $result->{localTime});
115 }
116
117 sub manage_selection {
118 my ($self, %options) = @_;
119
120 my ($distant_time, $remote_date) = $self->get_target_time(%options);
121
122 my $ref_time;
123 if (defined($self->{option_results}->{ntp_hostname}) && $self->{option_results}->{ntp_hostname} ne '') {
124 my %ntp;
125
126 eval {
127 %ntp = Net::NTP::get_ntp_response($self->{option_results}->{ntp_hostname}, $self->{option_results}->{ntp_port});
128 };
129 if ($@) {
130 $self->{output}->add_option_msg(short_msg => "Couldn't connect to ntp server: " . $@);
131 $self->{output}->option_exit();
132 }
133
134 $ref_time = $ntp{'Transmit Timestamp'};
135 } else {
136 $ref_time = time();
137 }
138
139 my $offset = $distant_time - $ref_time;
140 $self->{offset} = {
141 offset => sprintf('%d', $offset),
142 date => $remote_date
143 };
144 }
145
146 1;
147
148 __END__
149
150 =head1 MODE
151
152 Check time offset of server with ntp server. Use local time if ntp-host option is not set.
153
154 =over 8
155
156 =item B<--warning-offset>
157
158 Time offset warning threshold (in seconds).
159
160 =item B<--critical-offset>
161
162 Time offset critical Threshold (in seconds).
163
164 =item B<--ntp-hostname>
165
166 Set the ntp hostname (if not set, localtime is used).
167
168 =item B<--ntp-port>
169
170 Set the ntp port (default: 123).
171
172 =item B<--timezone>
173
174 Override the timezone of distant equipment.
175 Can use format: 'Europe/London' or '+0100'.
176
177 =back
178
179 =cut