我需要使用 Perl 和Linux::Inotify2处理大量(约 100 秒)系统日志消息。
我编写了一个连续生成日志消息的测试脚本。为了处理事件,我的 Perl 脚本如下所示——
#!/usr/bin/perl
use Linux::Inotify2 ;
use Time::HiRes qw(usleep nanosleep);
# create a new object
my $inotify = new Linux::Inotify2
or die "Unable to create new inotify object: $!" ;
# create watch
$inotify->watch ("/var/log/messages", IN_ACCESS|IN_OPEN|IN_CLOSE|IN_MODIFY|IN_Q_OVERFLOW)
or die "watch creation failed" ;
my $count=0;
while () {
my @events = $inotify->read;
unless (@events > 0) {
print "read error: $!";
last ;
}
#printf "mask\t%d\n", $_->mask foreach @events ;
$count++;
print $count."\n";
usleep(100000);
}
如果我取消注释 usleep 函数以模拟处理,我注意到当我停止日志生成器脚本时,inotify 脚本没有赶上它。换句话说, inotify Perl 脚本正在丢失事件。
我也没有看到任何溢出消息。
我如何确保即使我的处理速度很慢,我也不会丢失消息。换句话说,我如何定义可以临时存储消息的“缓冲区”?