perl管道掉落事件



我试图通过syslog-ng将syslog日志管道到perl脚本,但并非所有的syslog日志条目都能通过-实际上可能是三分之一。

我找遍了所有地方,都找不到任何一个人有我所遇到的问题。这似乎很简单,但我就是找不到答案!

我的syslog-ng设置如下:

source s_1 { tcp(port(514)); };
destination d_zen { program("/tmp/zen.pl"); }; 
log { source(s_1); destination(d_zen); }; 
下面是我的perl脚本:
#!/usr/bin/perl
use strict;
use warnings;
$|=1
my $filename = "/tmp/zen.log";
open(my $fh, '>>', $filename) or die "could not open file '$filename' $!";
while ( <STDIN> ) {
    print $fh <STDIN>."n";
};

任何想法吗?

您的Perl行缓冲区禁用了吗?

根据syslog-ng手册,它可能会导致一些问题:

"某些外部应用程序缓冲日志消息,这可能会导致意外的延迟和其他问题。例如,如果将日志消息发送到外部Perl脚本,Perl将对终端输出使用行缓冲区,否则使用块缓冲区。您可能希望在外部应用程序中禁用缓冲。"

另外,我不知道您的脚本如何读取传入消息(我现在不perl),但我认为它应该使用循环来继续读取传入消息。所以syslog-ng应该在启动时启动脚本,它应该继续运行并处理消息。

HTH,

问候,罗伯特Fekete

我找到了问题所在。我的while循环没有正确构建:

#!/usr/bin/perl
$|=1;
use strict;
use warnings;
my $filename = "/tmp/zen.log";
open(my $fh, '>', $filename) or die "could not open file '$filename' $!";
my $my_string;
while( <> ) {
    $my_string .= $_;
    print $fh "$my_stringn";
};

相关内容

最新更新