Somewhat new to perl, but trying to create an ntp monitor and getting confused by how to exit if the recv() command doesn't get a message within some amount of time (e.g. 5 seconds)
Sorry if anyone was looking for the answer, I re-wrote and the script itself seems to work. This is the script:
#!/usr/bin/perl
# ntpman.pl
# Script is meant to monitor an ntp server. It will query the server for the ntp and recieve the ntp back from the ntp server.
# If the script either fails to send a query or fails to recieve the ntp after 10 seconds, the script will print the "Statistic: 2" down
# code and exit.
#
# Script was partially re-written from Tim Hogard's ntpdate.pl, which is housed in the public domain.
print "Starting ntpman.pl";
use IO::Select;
use Socket;
$HOST="157.142.34.100"; #NTP Server
socket($SOCKET, PF_INET, SOCK_DGRAM, getprotobyname("udp"));
$select = IO::Select->new();
$select->add($SOCKET);
$ipaddr = inet_aton($HOST);
$portaddr = sockaddr_in(123, $ipaddr);
$MSG="\010"."\0"x47;
@ready = $select->can_write(10);
foreach $SOCKET(@ready) {
send($SOCKET, $MSG, 0, $portaddr);
print "NTP query has been sent\n";
}
@ready = $select->can_read(10);
foreach $SOCKET (@ready) {
recv($SOCKET, $MSG, 48, 0);
print "NTP has been recieved\n";
print "\nStatistic: 0";
exit 0;
print "Statistic: 2";
exit 1;