Created
April 2, 2010 21:03
-
-
Save anonymous/353707 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
use warnings; | |
use Data::Dumper; | |
use File::Find::Object; | |
use Linux::Inotify2; | |
use POE; | |
$|++; | |
my $watchers; | |
my $tree = File::Find::Object->new({}, "/etc/znc"); | |
POE::Session->create( | |
inline_states => { | |
_start => sub { | |
$_[KERNEL]->alias_set('notify'); | |
while (my $file = $tree->next()) { | |
#print "watching $file\n"; | |
push(@{ $_[HEAP]{inotify} },new Linux::Inotify2) | |
or warn "Unable to create new inotify object: $!"; | |
my $list = $_[HEAP]{inotify}; | |
my $idx = $#{ $list }; | |
$_[HEAP]{inotify}->[$idx]->watch( | |
$file, | |
IN_ALL_EVENTS, | |
$_[SESSION]->postback("watch_hdlr") | |
) or warn "Unable to watch dir: $!"; | |
my $inotify_FH; | |
open $inotify_FH, "<&=" . $_[HEAP]{inotify}->[$idx]->fileno or die "Can't fdopen: $!\n"; | |
$_[KERNEL]->select_read( $inotify_FH, "inotify_poll", $idx ); | |
} | |
}, | |
inotify_poll => sub { | |
$_[HEAP]{inotify}->[ $_[$#_] ]->poll; | |
}, | |
watch_hdlr => \&watch_hdlr, | |
}, | |
); | |
sub watch_hdlr { | |
my $event = $_[ARG1][0]; | |
my $name = $event->fullname; | |
my $command=<<' ...'; | |
/bin/bash -c "sleep 10; for h in odin freyr thor; do | |
if [ \"\$(hostname -s)\" != \"\${h}\" ];then | |
unison -auto -batch -group -times \ | |
-ignore='Name znc.pem' \ | |
-ignore='Name configs/znc.conf.seed' \ | |
-ignore='Name moddata/webadmin/.registry' \ | |
/etc/znc/ \ | |
ssh://znc\@\${h}.websages.com//etc/znc/; | |
fi; | |
done > \${HOME}/unison.log 2>\&1" | |
... | |
if( ($event->IN_DELETE) || ($event->IN_CLOSE_WRITE )){ system($command); } | |
#print "$name was accessed\n" if $event->IN_ACCESS; | |
#print "$name is no longer mounted\n" if $event->IN_UNMOUNT; | |
#print "$name is gone\n" if $event->IN_IGNORED; | |
#print "$name is gone\n" if $event->IN_DELETE; | |
#print "$name is new\n" if $event->IN_CLOSE_WRITE; | |
#print "events for $name have been lost\n" if $event->IN_Q_OVERFLOW; | |
} | |
POE::Kernel->run(); | |
exit 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment