syslog-ng 在 syslog 模式上执行脚本

syslog-ng 在 syslog 模式上执行脚本

我有一个简单的要求。我想接收来自用户设施的所有系统日志消息并将它们存储在一个文件中。如果系统日志消息包含特定模式,我想执行一个脚本。

我有以下配置,

destination d_logfile { file("/var/log/logile.log"); };
destination d_start_script { program("/home/ubuntu/start-script.sh"); };
destination d_stop_script { program("/home/ubuntu/stop-script.sh"); };

filter f_logfile { facility(user) and not filter(f_debug); };
filter f_filter_start { facility(user) and message("start"); };
filter f_filter_stop { facility(user) and message("stop"); };

log { source(s_network_tcp); filter(f_logfile); destination(d_logfile); };
log { source(s_network_tcp); filter(f_filter_start); destination(d_start_script; };
log { source(s_network_tcp); filter(f_filter_stop; destination(d_stop_script); };

当我启动 syslog-ng 时,它似乎循环并打开和关闭执行启动和停止脚本。

我遗漏了什么吗?

答案1

如果我理解正确,那么您实际上想要的不是启动/停止脚本,而是处理一系列消息的脚本,以及创建此序列的方法。检查是否可以使用 g 创建此序列rouping-by() 解析器例如使用 ${HOST}${PROGRAM}${PID} 范围。如果这还不够,在较新的 syslog-ng 版本中,你可以编写自己的Python 中的 syslog-ng 目标,这可能为您提供了足够的灵活性来完成工作(但如果需要,您也可以用 Python 编写解析器)。

答案2

据我所知,syslog-ng在启动后调用程序目标并向其发送流。我以类似的方式使用它,但在脚本中有:
if grep -qE "start" ; then <do all you needs>

因此脚本与 syslog 一起运行并等待消息。

相关内容