0

我想使用 syslog-ng 来接收 netgear 日志,并使用 python 脚本进程。

但是 syslog-ng 没有运行 python 脚本。

系统日志-ng.config

@version:3.2

options {
    flush_lines (0);
    time_reopen (10);
    log_fifo_size (1000);
    long_hostnames (off);
    use_dns (no);
    use_fqdn (no);
    create_dirs (no);
    keep_hostname (yes);
};

source s_sys {
    udp(ip(0.0.0.0) port(514));
    };

destination d_python{
    program("/usr/local/bin/python /opt/syslog.py");    
    #program("/bin/echo 'haha' >> /tmp/test");
    };

log { source(s_sys); destination(d_python);};

和这样的python脚本

#!/usr/local/bin/python
#coding:utf8

import os
import sys
import datetime

f = open('/var/log/pnet.log', 'a')

f.write('\nstart\n')
f.write('args\n')
f.write('%s\n' % sys.argv)
if not sys.stdin.isatty():
    f.write('stdin\n')
    f.write('%s\n' % date.date.now().isoformat() )
    tty_read = sys.stdin.read()
    f.write("'''\n")
    f.write(tty_read)
    f.write("\n'''\n")
f.write('end\n')
f.close()

脚本已经是 777 即使我将配置更改为使用 'echo' 直接管道到文件中,也没有写一个字......

所以为什么?

4

2 回答 2

0

愚蠢的问题,但你有传入的日志吗?如果您使用简单的文件目标而不是程序,您会收到日志吗?如果不是,则问题不在程序目的地。

另外,尝试更改 flush_lines (0); 选项 1 看看它是否有帮助。

问候,

罗伯特·费克特

于 2014-08-06T07:36:14.927 回答
0

我可以向您展示我的代码以供参考:

我的syslog-ng.conf

source s_test{
    file("/home/test/in.log" follow-freq(1) flags(no-parse));
    };  
destination d_test{
    program ("/home/test/out.py" flush_lines(1) flags(no_multi_line));                                                                                                        
    };  
log {
    source(s_test);
    destination(d_test);
    flags(flow-control);
    };

我的out.py

#!/usr/bin/env python
# encoding: utf-8

import sys 
while True:
    line = sys.stdin.readline()
    file = open("/home/test/out_from_python.log","a")
    file.write(line)
    file.close()

当您键入时echo "something" >> /home/test/in.log,将有一个新的日志位于/home/test/out_from_python.log

于 2016-06-17T09:08:48.697 回答