NTPD上の時間と、LocalTimeとを比較するスクリプトを作ってみた。
結構色んな要素を含んでいて勉強になったかな。
ネットワークバイト列から時間への変換などをunpackを用いて実装する。
#!/usr/bin/perl use strict; use Socket; # Local Time Lag Alart Threshold Seconds my $threshold_sec = 30; # NTPD Informations my $host = 'ntp1.jst.mfeed.ad.jp'; my $protocol = "ntp"; my $sec_of_70years = 2_208_988_800; # Local Settings my $hostname = `/bin/hostname`; chomp($hostname); my $date = `/bin/date +%Y/%m/%d-%H:%M:%S`; chomp($date); # Variable Settings my @buf; my ($connect_host, $port, $ipaddr, $host_addr); my ($send_data, $send_data_byte, $port_by_byte, $host_by_in_addr_struct, $recv_data); my ($TransmitTime, $ntpd_time, $ntpd_time_sec, $local_time, $local_time_sec, $deley_sec); if (!socket(SOCKET, PF_INET, SOCK_DGRAM, getprotobyname("udp"))) { print "socket: $!"; exit 1; } $port_by_byte = scalar(getservbyname($protocol, "udp")); $host_by_in_addr_struct = inet_aton($host); $connect_host = sockaddr_in($port_by_byte, $host_by_in_addr_struct); $send_data = pack("N12", 0x0B000000); $send_data_byte = send(SOCKET, $send_data, 0, $connect_host); if (!defined($send_data_byte)) { print "send : $!"; exit 2; } $host_addr = recv(SOCKET, $recv_data, 1024, 0); if (!defined($host_addr)) { print "recv : $!"; exit 3; } ($port, $ipaddr) = sockaddr_in($host_addr); $host = gethostbyaddr($ipaddr, AF_INET); print "DEBUG: Host: $host, Port: $port\n"; @buf = unpack("B32 B32 B32 B32 B64 B64 B64 B64 B*", $recv_data); @buf = unpack("N*" , $recv_data); $TransmitTime = $buf[10]; $ntpd_time_sec = $TransmitTime - $sec_of_70years; $local_time_sec = time(); $deley_sec = abs($local_time_sec - $ntpd_time_sec); # DEBUG $ntpd_time = scalar(localtime($ntpd_time_sec)); $local_time = scalar(localtime()); print "DEBUG: ntpd? time: $ntpd_time.???? ntpd? time sec: $ntpd_time_sec\n"; print "DEBUG: local time: $local_time.???? local time sec: $local_time_sec\n"; if ($deley_sec >= $threshold_sec) { print "DEBUG: There is Local time lag ( = $deley_sec seconds ) over $threshold_sec seconds in $date at $hostname.\n"; }
実行結果はこんな感じ。
# perl a DEBUG: Host: ntp1.jst.mfeed.ad.jp, Port: 123 DEBUG: ntpd time: Wed Mar 11 22:43:06 2009. ntpd time sec: 1236778986 DEBUG: local time: Wed Mar 11 22:41:23 2009. local time sec: 1236778883 DEBUG: There is Local time lag ( = 103 seconds ) over 30 seconds in 2009/03/11-22:41:23 at server01.