【问题标题】:dumping rsyslogd output to some file将 rsyslogd 输出转储到某个文件
【发布时间】:2015-04-29 21:09:04
【问题描述】:

我希望将 rsyslog(service) 的输出转储到所选文件中的某个文件
位置。
以下是我尝试过的:
1. 修改/etc/rsyslog.conf

#################
#### MODULES ####
#################
$ModLoad imfile

$ModLoad omprog   <----- NEWLY ADDED ------>

$ModLoad imuxsock # provides support for local system logging
$ModLoad imklog   # provides kernel logging support
#$ModLoad immark  # provides --MARK-- message capability

###########################
#### GLOBAL DIRECTIVES ####
###########################

#
# Use traditional timestamp format.
# To enable high precision timestamps, comment out the following line.
#
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat   

$ActionOMProgBinary /home/test/dmsg <----- NEWLY ADDED ------>  

# Filter duplicated messages    

dmsg : 是一个从标准输入读取行并将其写入的 C 程序
文件(/home/test/log_syslog_file)

我希望将输出转储到 /home/test/log_syslog_file
但什么也没有发生。

dmsg 代码 (dmsg.c) ::

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
int main(){
char* lineptr;
size_t size = 0;   
int fd = open("log_syslog_file", O_CREAT| O_WRONLY);
        while(getline(&lineptr, &size, stdin)>0){
                if(write(fd, lineptr, strlen(lineptr))<0){
                        fprintf(stderr, "write failure");
                        break;
                }
        }
        free(lineptr);
        close(fd);
return 0;
}

我使用的是 Ubuntu 14.04

-------- 编辑 --------- 启动 rsyslog 服务后,
我给出以下命令:

rsyslogd -c5 -d -n 

当我使用以下它工作正常:

cat /var/log/syslog | ./dmsg

谢谢。

【问题讨论】:

    标签: c linux rsyslog


    【解决方案1】:

    首先是@Mark 所说的。除此之外,请确保 你有类似的东西

    *.* :omprog:
    

    在您的 rsyslog.conf 中。 这会将所有消息重定向到您的程序。

    【讨论】:

    • 我应该准确地添加这个语句吗?能举个例子吗?
    • 如果你想要 ALL 消息使用 *.* 部分。语法:rsyslog.com/doc/omprog.html
    【解决方案2】:

    您的代码中至少有一个主要错误:

    char* lineptr;
    ...
    while(getline(&lineptr, &size, stdin)>0)
    

    您永远不会为存储在*lineptr 中的字符串分配内存,但您也不会告诉getline() 为您分配内存。由此产生的缓冲区溢出可能会导致在不可避免的崩溃之前出现各种令人兴奋的错误(例如,在我的测试运行中,log_syslog_file 获得了权限---x--x--T)。

    【讨论】:

      猜你喜欢
      • 2020-05-01
      • 1970-01-01
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      • 2015-07-06
      • 1970-01-01
      相关资源
      最近更新 更多