AlexRNeos
Member | Редактировать | Профиль | Сообщение | ICQ | Цитировать | Сообщить модератору Есть скрипт, делает следующее при создании зоны на мастере автоматически добавляется эта зона на слейве. Все работает, но возникла задача так же удалять зоны на слейве, которые удалили на мастере. Помогите в дописании... Код: use strict; use POSIX 'setsid'; use English; use Data::Dumper; my (%cfg); open CFG,'/etc/zoneadd.conf' or die 'File: /etc/zoneadd.conf '.$!; my $cfg=join "\n", <CFG>; close CFG; eval "$cfg"; die $@ if $@; $cfg{'debug'} ||= 0; #use Data::Dumper;print Dumper(\%cfg);exit; #Daemon part if ( $cfg{'daemon'} and ! int $cfg{'debug'} ) { open(STDIN,'</dev/null'); open(STDOUT,'>/dev/null'); open(STDERR, '>&STDOUT'); defined( my $pid = fork) or die('Error: Can\'t fork '.$!); exit if $pid; setsid or die('Error: Can\'t start new session!'); umask 0; chdir('/'); $PROGRAM_NAME='zoneadd'; } scalar keys %cfg < 5 and die "Not found config data in config file! (May be you specify 'my %cfg = {' against '%cfg = {'?)\n"; $cfg{'debug'} and print STDERR "DEBUG: dump config from config file /etc/zoneadd.conf:\n"; $cfg{'debug'} and print STDERR Dumper(\%cfg); open ZONES, $cfg{'named.conf'} or die $cfg{'named.conf'}." ".$!; $cfg{'zones'} = []; while (<ZONES>) { if ( /^zone\s+"([\w\.]+)"\s+{/ ) { push @{$cfg{'zones'}}, $1; } } close ZONES; if ( $cfg{'debug'} ) { print STDERR "DEBUG: found these ".scalar(@{$cfg{'zones'}})." zones in named.conf:\n"; print STDERR "\t".$_."\n" foreach @{$cfg{'zones'}}; } #Main cycle my $exit; $cfg{'notifylog_size'}= -s $cfg{'notifylog'} or die "Empty or nonexistent notify log!\n"; while ( ! defined($exit) ) { $cfg{'debug'} and print STDERR "DEBUG: sleeping ".$cfg{'interval'}." seconds...\n"; sleep int($cfg{'interval'}); if ( -s $cfg{'notifylog'} > $cfg{'notifylog_size'}) { processing_log() or $exit=1; } } ########################## #Functions ########################## sub processing_log { my ($log,$server,$domain,$keyname); my @notifies; #Parse log open LOG, $cfg{'notifylog'} or die $cfg{'notifylog'}." ".$!; while (<LOG>) { $log.=$_; if ( /notify: notice: client ([\d\:\.]+)#\d+: received notify for zone '([\w\.]+)': TSIG '([\w\.-]+)': not authoritative/ ) { $cfg{'debug'} and print STDERR "DEBUG: found string by pattern:\n"; $cfg{'debug'} and print STDERR $_; ($server,$domain,$keyname)=($1,$2,$3); $cfg{'debug'} and print STDERR "DEBUG: search server ".$server." with key ".$keyname." in cfg...\n"; my $found=0; foreach ( @{$cfg{'allowed'}}) { if ( $_ eq $server and $cfg{'keynames'}->{$server} eq $keyname ) { $cfg{'debug'} and print STDERR "DEBUG: found!\n"; $found=1; push @notifies, {server=>$server,domain=>$domain}; } } $cfg{'debug'} and ! $found and print STDERR "DEBUG: not found!\n"; } } if ( $cfg{'debug'} ) { @notifies or print STDERR "DEBUG: Not found new notifies\n"; @notifies and print STDERR "DEBUG: Found these new notifies:\n"; print STDERR "server ".$_->{'server'}.", domain ".$_->{'domain'}."\n" foreach @notifies; } #Clean log and move processed data to other log close LOG; $cfg{'debug'} and print STDERR "DEBUG: named log ".$cfg{'notifylog'}." cleaning...\n"; open LOG, '>',$cfg{'notifylog'} or die $cfg{'notifylog'}." ".$!; print LOG "cleaned by ".$PROGRAM_NAME."\n"; close LOG; $cfg{'debug'} and print STDERR "DEBUG: done, named log backup ".$cfg{'notifylog'}.".zoneadd saving...\n"; open LOG, '>>',$cfg{'notifylog'}.'.zoneadd' or die $cfg{'notifylog'}.".zoneadd ".$!; print LOG $log; close LOG; $cfg{'debug'} and print STDERR "DEBUG: done\n"; $cfg{'notifylog_size'}= -s $cfg{'notifylog'} or die "Empty or nonexistent notify log!\n"; #Create backup if ( scalar(@notifies) ) { $cfg{'debug'} and print STDERR "DEBUG: Save backup of named.conf to ".$cfg{'named.conf'}.'.'.time."\n"; my $config; open ZONES, $cfg{'named.conf'} or die $cfg{'named.conf'}." ".$!; while (<ZONES>) { $config.=$_;} close ZONES; open ZONES, '>',$cfg{'named.conf'}.'.'.time or die $cfg{'named.conf'}.'.'.time." ".$!; print ZONES $config; close ZONES; $cfg{'debug'} and print STDERR "DEBUG: done\n"; } #Parse notifies my $restart=0; foreach my $notify ( @notifies ) { #Check for already registered my $found=0; foreach ( @{$cfg{'zones'}}) { if ( lc($_) eq lc($notify->{'domain'}) ) { $found++;last;} } next if $found; #Create addons/mail my $time=scalar(localtime(time)); my ($zone,$mail)=($cfg{'pattern'},$cfg{'mail_cmd'}); $zone=~s/\%server/$notify->{'server'}/g; $zone=~s/\%zone/$notify->{'domain'}/g; $zone=~s/\%time/$time/g; $mail=~s/\%server/$notify->{'server'}/g; $mail=~s/\%zone/$notify->{'domain'}/g; $mail=~s/\%time/$time/g; $cfg{'debug'} and print STDERR "DEBUG: saving zone to named.conf...\n"; #Append to main conf open CONF, '>>'.$cfg{'named.conf'} or die $cfg{'named.conf'}." ".$!; print CONF $zone; close CONF; #add to self array push @{$cfg{'zones'}}, $notify->{'domain'}; $cfg{'debug'} and print $zone."\n"; $cfg{'debug'} and print STDERR "DEBUG: done\n"; #Admin notify system($mail); $restart++; $cfg{'debug'} and print STDERR "DEBUG: mail to admin sended\n"; $cfg{'debug'} and print STDERR "DEBUG: ".$mail."\n"; } if ( $restart ) { $cfg{'debug'} and print STDERR "DEBUG: restart named server:\n"; $cfg{'debug'} and print STDERR $cfg{'restart_cmd'}."\n"; if ( system($cfg{'restart_cmd'}) ) { $cfg{'debug'} and print STDERR "DEBUG: restart cmd failed, run next command:\n"; $cfg{'debug'} and print STDERR $cfg{'failed_mail_cmd'}."\n"; system($cfg{'failed_mail_cmd'}); } $cfg{'debug'} and print STDERR "DEBUG: done, sleeping 5 seconds...\n"; sleep 5; $cfg{'notifylog_size'}= -s $cfg{'notifylog'} or die "Empty or nonexistent notify log!\n"; } return 1; } | |