| 1 | package Zabbix::Check::Time; | ||||||||||
| 2 | =head1 NAME | ||||||||||
| 3 | |||||||||||
| 4 | Zabbix::Check::Systemd - Zabbix check for system time | ||||||||||
| 5 | |||||||||||
| 6 | =head1 VERSION | ||||||||||
| 7 | |||||||||||
| 8 | version 1.10 | ||||||||||
| 9 | |||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||
| 11 | |||||||||||
| 12 | Zabbix check for system time | ||||||||||
| 13 | |||||||||||
| 14 | UserParameter=cpan.zabbix.check.time.epoch,/usr/bin/perl -MZabbix::Check::Time -e_epoch | ||||||||||
| 15 | UserParameter=cpan.zabbix.check.time.zone,/usr/bin/perl -MZabbix::Check::Time -e_zone | ||||||||||
| 16 | UserParameter=cpan.zabbix.check.time.ntp_offset[*],/usr/bin/perl -MZabbix::Check::Time -e_ntp_offset -- $1 $2 | ||||||||||
| 17 | |||||||||||
| 18 | =head3 epoch | ||||||||||
| 19 | |||||||||||
| 20 | gets system time epoch in seconds | ||||||||||
| 21 | |||||||||||
| 22 | =head3 zone | ||||||||||
| 23 | |||||||||||
| 24 | gets system time zone, eg: +0200 | ||||||||||
| 25 | |||||||||||
| 26 | =head3 ntp_offset $1 $2 | ||||||||||
| 27 | |||||||||||
| 28 | gets system time difference by NTP server | ||||||||||
| 29 | |||||||||||
| 30 | $1: I<server, by defaut pool.ntp.org> | ||||||||||
| 31 | |||||||||||
| 32 | $2: I<port, by default 123> | ||||||||||
| 33 | |||||||||||
| 34 | =cut | ||||||||||
| 35 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 1 | ▶ | no warnings qw(qw utf8); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 1 | ▶ | use v5.14; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 1 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 1 | ▶ | use POSIX; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 1 | ▶ | use Net::NTP; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 1 | ▶ | use Lazy::Utils; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | |||||||||||
| 44 | 1 | ▶ | use Zabbix::Check; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | |||||||||||
| 46 | |||||||||||
| 47 | BEGIN | ||||||||||
| 48 | { | ||||||||||
| 49 | 1 | ▶ | require Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 50 | # set the version for version checking | ||||||||||
| 51 | 1 | our $VERSION = '1.10'; | |||||||||
| 52 | # Inherit from Exporter to export functions and variables | ||||||||||
| 53 | 1 | our @ISA = qw(Exporter); | |||||||||
| 54 | # Functions and variables which are exported by default | ||||||||||
| 55 | 1 | our @EXPORT = qw(_epoch _zone _ntp_offset); | |||||||||
| 56 | # Functions and variables which can be optionally exported | ||||||||||
| 57 | 1 | our @EXPORT_OK = qw(); | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | |||||||||||
| 61 | sub _epoch | ||||||||||
| 62 | { | ||||||||||
| 63 | 0 | ▶ | my $result = time(); | ||||||||
|
Subroutine
_epoch: not called CC=1 SCAR=6.9
| |||||||||||
| 64 | 0 | print $result; | |||||||||
| 65 | 0 | return $result; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | sub _zone | ||||||||||
| 69 | { | ||||||||||
| 70 | 0 | ▶ | my $result = strftime("%z", gmtime()); | ||||||||
|
Subroutine
_zone: not called CC=1 SCAR=6.9
| |||||||||||
| 71 | 0 | print $result; | |||||||||
| 72 | 0 | return $result; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | sub _ntp_offset | ||||||||||
| 76 | { | ||||||||||
| 77 | 0 | ▶ | my ($server, $port) = map(zbxDecode($_), @ARGV); | ||||||||
|
Subroutine
_ntp_offset: not called CC=5 SCAR=34.0
| |||||||||||
| 78 | 0 | ▶ | $server = "pool.ntp.org" unless $server; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 0 | my $result = ""; | |||||||||
| 80 | 0 | my %ntp; | |||||||||
| 81 | 0 | for (1..5) | |||||||||
| 82 | { | ||||||||||
| 83 | 0 | eval { %ntp = get_ntp_response($server, $port) }; | |||||||||
| 84 | 0 | ▶ | $result = sprintf("%.3f", $ntp{Offset}) if defined $ntp{Offset}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 0 | ▶ | last unless $result eq ""; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 0 | sleep(1); | |||||||||
| 87 | } | ||||||||||
| 88 | 0 | print $result; | |||||||||
| 89 | 0 | return $result; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | |||||||||||
| 93 | 1 | 1; | |||||||||
| 94 | __END__ | ||||||||||