#!/usr/bin/perl use strict; use warnings; use 5.010; use POSIX; use Fcntl; use threads; use Tracker; use Domains; use Logs; use constant NAME => 'alert'; use constant VERSION => '1.0'; my %track = ( '/var/log/xenctrlserver/xenctrlserver.log' => \&Logs::base, '/var/log/wwdserver/wwdserver.log' => \&Logs::base ); die 'Usage: '.NAME."\n" if ($#ARGV != -1); # Disable starting multiple instances sysopen RUN, '/var/run/'.NAME.'.pid', O_CREAT | O_WRONLY, 0600; flock(RUN, Fcntl::LOCK_EX | Fcntl::LOCK_NB) or die NAME.": Application already running\n"; # Daemonize exit if fork; POSIX::setsid; chdir '/'; umask 0; # Write PID to a file truncate RUN, 0; print RUN "$$\n"; open STDIN, '<', '/dev/null'; open STDOUT, '>', '/dev/null'; sysopen STDERR, '/var/log/'.NAME.'.log', O_CREAT | O_APPEND | O_WRONLY, 0600; select STDERR; $| = 1; # Disable output buffering # Ignore signals that may terminate the program $SIG{'ALRM'} = 'IGNORE'; # Start a separate thread to track each file my @threads = (); foreach my $filename (keys %track) { my $handler = $track{$filename}; my $tracker = Tracker::new($filename); if (!defined($tracker)) { Report::log(NAME.": Unable to track $filename"); next; } push @threads, threads->new(sub {$tracker->track($handler);}); $threads[$#threads]->detach; } # Watch domain registrations and renewals Domains::watch; # Avoid program termination if the main thread reaches this point # Sleep endlessly so that the other threads can do their job sleep while 1;