master_watcher
changeset 8 098af5defd01
parent 7 836e273e9992
equal deleted inserted replaced
7:836e273e9992 8:098af5defd01
     1 #! /usr/bin/perl -w
     1 #! /usr/bin/perl -w
       
     2 # $Id$
     2 my $USAGE = <<'#';
     3 my $USAGE = <<'#';
     3 Usage: $ME [options]
     4 Usage: $ME [options]
     4        -l --logfile=s   Name of the logfile we've to read [$opt_logfile]
     5        -l --logfile=s   Name of the logfile we've to read [$opt_logfile]
     5        -z --zonesdir=s  Where the named.conf's are expected [$opt_zonesdir]
     6        -z --zonesdir=s  Where the named.conf's are expected [$opt_zonesdir]
     6        -u --[no]update  Update the \"masters\"-entries [$opt_update] 
     7        -u --[no]update  Update the \"masters\"-entries [$opt_update] 
     7        -f --[no]follow  Follow the end of the logfile [$opt_follow]
     8        -f --[no]follow  Follow the end of the logfile [$opt_follow]
     8        -d --[no]debug   extra debug output [$opt_debug]
     9        -d --[no]debug   extra debug output [$opt_debug]
     9        -h --help        This text [$opt_help]
    10        -h --help        This text [$opt_help]
    10           --daemon      go into background [$opt_daemon]
    11           --daemon      go into background [$opt_daemon]
    11        -p --pidfile=s   file to store the pid [$opt_pidfile]
    12        -p --pidfile=s   file to store the pid [$opt_pidfile]
       
    13        -v --version     print version [$opt_version]
    12 #
    14 #
    13 # Es wird ein Verzeichnis geben, in diesem Verzeichnis liegt für 
    15 # Es wird ein Verzeichnis geben, in diesem Verzeichnis liegt für 
    14 # *jede* Zone eine eigene Konfigurations-Datei.
    16 # *jede* Zone eine eigene Konfigurations-Datei.
    15 # Diese ganzen Konfigurationsdateien werden dann zusammengefaßt
    17 # Diese ganzen Konfigurationsdateien werden dann zusammengefaßt
    16 # und diese zusammengefaßte wird dem bind per "include" mitgeteilt.
    18 # und diese zusammengefaßte wird dem bind per "include" mitgeteilt.
    60     # "194.162.141.17" => "dalx1.nacamar.de",
    62     # "194.162.141.17" => "dalx1.nacamar.de",
    61 );
    63 );
    62 
    64 
    63 $SIG{__DIE__} = sub { syslog(LOG_ERR, $_[0]); exit -1; };
    65 $SIG{__DIE__} = sub { syslog(LOG_ERR, $_[0]); exit -1; };
    64 $SIG{__WARN__} = sub { syslog(LOG_WARNING, $_[0]); };
    66 $SIG{__WARN__} = sub { syslog(LOG_WARNING, $_[0]); };
       
    67 $SIG{TERM} = $SIG{INT} = sub { exit 0; };
    65 
    68 
    66 my %seen;
    69 my %seen;
    67 
    70 
    68 my $opt_help = 0;
    71 my $opt_help = 0;
    69 my $opt_pidfile = "/var/run/$ME.pid";
    72 my $opt_pidfile = "/var/run/$ME.pid";
    70 my $opt_logfile = "/var/log/syslog";
    73 my $opt_logfile = "/var/log/syslog";
    71 my $opt_zonesdir = "/etc/bind/zones.d";
    74 my $opt_zonesdir = "/etc/bind/zones.d";
    72 my $opt_follow = 0;
    75 my $opt_follow = 0;
    73 my $opt_update = 0;
    76 my $opt_update = 0;
    74 my $opt_debug = 0;
    77 my $opt_debug = 0;
    75 my $opt_daemon = 0;
    78 my $opt_daemon = 1;
       
    79 my $opt_version = 0;
    76 
    80 
    77 my $naptime = 60;
    81 my $naptime = 60;
    78 
    82 
    79 
    83 
    80 sub updateFile($$$);
    84 sub updateFile($$$);
    81 sub debug($;@) { syslog(LOG_DEBUG, "DEBUG " . shift @_, @_) if $opt_debug; }
    85 sub debug($;@) { syslog(LOG_DEBUG, "DEBUG " . shift @_, @_) if $opt_debug; }
       
    86 
       
    87 END {
       
    88     open(PID, $opt_pidfile);
       
    89     my $pid = <PID>;
       
    90     close(PID);
       
    91     unlink $opt_pidfile if $$ == $pid;
       
    92 }
    82 
    93 
    83 MAIN: {
    94 MAIN: {
    84 
    95 
    85 
    96 
    86     openlog($ME, LOG_PID | LOG_PERROR, LOG_DAEMON);
    97     openlog($ME, LOG_PID | LOG_PERROR, LOG_DAEMON);
    92 	"follow!" => \$opt_follow,
   103 	"follow!" => \$opt_follow,
    93 	"update!" => \$opt_update,
   104 	"update!" => \$opt_update,
    94 	"debug!" => \$opt_debug,
   105 	"debug!" => \$opt_debug,
    95 	"daemon!" => \$opt_daemon,
   106 	"daemon!" => \$opt_daemon,
    96 	"pidfile=s" => \$opt_pidfile,
   107 	"pidfile=s" => \$opt_pidfile,
       
   108 	"version!" => \$opt_version,
    97 	"zonesdir=s" => \$opt_zonesdir)
   109 	"zonesdir=s" => \$opt_zonesdir)
    98     or die "$ME: Bad Usage\n";
   110     or die "$ME: Bad Usage\n";
    99 
   111 
   100     if ($opt_help) {
   112     if ($opt_help) {
   101 	print eval "\"$USAGE\"";
   113 	print eval "\"$USAGE\"";
   102 	exit 0;
   114 	exit 0;
   103     }
   115     }
   104 
   116 
   105     open (LOGFILE, $_ = "<$opt_logfile") or die "Can't open $_: $!\n";
   117     if ($opt_version) {
   106 
   118 	print "$ME Version: ", '$Id$', "\n";
   107     # Go Daemon
   119 	exit 0;
   108     #if ($opt_daemon) {
   120     }
   109 #	my $pid = fork();
   121 
   110 #	if 
       
   111 #    }
       
   112 
   122 
   113     # Create the PID-File
   123     # Create the PID-File
   114     {
   124     {
   115 	open(PID, $_ = ">$opt_pidfile.$$") or die "Can't open $opt_pidfile: $!\n";
   125 	open(PID, $_ = ">$opt_pidfile.$$") or die "Can't open $opt_pidfile: $!\n";
   116 	print PID "$$\n";
   126 	print PID "$$\n";
   117 	close(PID);
   127 	close(PID);
   118 
   128 
   119 	if (!rename($_ = "$opt_pidfile.$$", $opt_pidfile)) {
   129 	if (!link($_ = "$opt_pidfile.$$", $opt_pidfile)) {
   120 	    unlink "$opt_pidfile.$$";
   130 	    unlink "$opt_pidfile.$$";
   121 	    die "There's another $ME running.  Bad. Stop.";
   131 	    die "There's another $ME running.  Bad. Stop.";
   122 	}
   132 	}
   123     }
   133 	unlink "$opt_pidfile.$$";
       
   134     }
       
   135 
       
   136     if ($opt_daemon) {
       
   137 	my $pid = fork();
       
   138 
       
   139 	if ($pid < 0) {
       
   140 	    die "Can't fork: $!\n";
       
   141 	} 
       
   142 
       
   143 	if ($pid) {
       
   144 	    open(PID, $_ = ">$opt_pidfile") or die "Can't open $_: $!\n";
       
   145 	    print PID "$pid\n";
       
   146 	    close(PID);
       
   147 	    exit 0;
       
   148 	}
       
   149 
       
   150 	close(STDIN); close(STDOUT); close(STDERR);
       
   151     }
       
   152 
       
   153 
       
   154     open (LOGFILE, $_ = "<$opt_logfile") or die "Can't open $_: $!\n";
   124 
   155 
   125     for (;;) {
   156     for (;;) {
   126 	my (%masters, %missing, %nomasters);
   157 	my (%masters, %missing, %nomasters);
   127 	while (<LOGFILE>) {
   158 	while (<LOGFILE>) {
   128 	    
   159