我希望将rsyslog(服务)的输出转储到选定的
位置。
以下是我尝试过的方法:
进行了更改
#################
#### 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程序,它从stdin读取行并将其写入
file (/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
- EDIT -启动rsyslog服务后,
我给出了以下命令:
rsyslogd -c5 -d -n 当我使用以下代码时,它工作得很好:
cat /var/log/syslog | ./dmsg谢谢。
发布于 2015-02-27 16:47:20
你的代码中至少有一个主要的错误:
char* lineptr;
...
while(getline(&lineptr, &size, stdin)>0)您从来没有为存储在*lineptr中的字符串分配内存,但是您也没有告诉getline()为您分配内存。由此产生的缓冲区溢出可能会导致在不可避免的崩溃之前出现各种令人兴奋的bug(例如,在我的测试运行中,log_syslog_file获得了权限---x--x--T)。
发布于 2015-02-27 16:48:20
首先,@Mark说的是什么。除此之外,要确保你有类似这样的东西
*.* :omprog:在你的rsyslog.conf里。这会将所有消息重定向到您的程序。
https://stackoverflow.com/questions/28760262
复制相似问题