手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

rsyslog配置动态日志文件-outchannel补丁

[日期:2012-08-22] 来源:Linux社区  作者:dog250 [字体:大 中 小]

Debian Bug report logs - #477351报出了一个bug,正是我最近头疼的问题,原文如下:
Package: rsyslog
Version: 2.0.2-1~bpo40+1
Severity: normal

Hello,

on my /etc/rsyslog.conf you can read:
$template DynFile,"/var/log/system-%HOSTNAME%.log"
$outchannel porHost,$DynFile,104857600,echo test
*.* $porHost


rsyslog wants to create "/$DynFile" file, and not create
/var/log/system-%HOSTNAME%.log file .

This is with rsyslog from etch-backports.

I'll probe the sid version

-- System Information:
Debian Release: 4.0
  APT prefers stable
  APT policy: (500, 'stable')
Architecture: amd64 (x86_64)
Shell:  /bin/sh linked to /bin/bash
Kernel: Linux 2.6.18-5-xen-amd64
Locale: LANG=es_ES.UTF-8, LC_CTYPE=es_ES.UTF-8 (charmap=UTF-8)

Versions of packages rsyslog depends on:
ii  libc6                  2.3.6.ds1-13etch4 GNU C Library: Shared libraries
ii  lsb-base               3.1-23.2etch1     Linux Standard Base 3.1 init scrip
ii  zlib1g                 1:1.2.3-13        compression library - runtime

Versions of packages rsyslog recommends:
ii  logrotate                     3.7.1-3    Log rotation utility
起初,我真的以为这是个bug,后来查了源代码发现,rsyslog根本就没有实现动态文件对outchannel的支持,难道这位老兄不知道察看一下代码吗?既然rsyslog是开源的,源代码都在手上,还有什么不能做的啊?动手修改代码才是王道!以下是自用的patch,应用这个patch后可以解决自己的问题:

  1. diff -urN rsyslog-4.6.5.orig/runtime/conf.c rsyslog-4.6.5/runtime/conf.c  
  2. --- rsyslog-4.6.5.orig/runtime/conf.c    2010-11-24 22:47:07.000000000 +0800  
  3. +++ rsyslog-4.6.5/runtime/conf.c    2012-08-14 08:26:25.000000000 +0800  
  4. @@ -502,6 +502,32 @@  
  5.      RETiRet;  
  6.  }  
  7.    
  8. +/* Get a FileName from a outchannel  
  9. + */  
  10. +rsRetVal channParseFileName(uchar* p, uchar *szBuf)  
  11. +{  
  12. +        DEFiRet;  
  13. +        size_t i;  
  14. +        char szChn[128];  
  15. +        struct outchannel *pOch;  
  16. +  
  17. +        ++p; /* skip '$' */  
  18. +        i = 0;  
  19. +        /* get outchannel name */  
  20. +        while(*p && *p != ';' && *p != ' ' &&  
  21. +              i < sizeof(szChn) / sizeof(char)) {  
  22. +              szChn[i++] = *p++;  
  23. +        }  
  24. +        szChn[i] = '\0';  
  25. +        pOch = ochFind(szChn, i);  
  26. +        if (pOch != NULL &&  
  27. +                pOch->pszFileTemplate != NULL) {  
  28. +                strcpy(szBuf, pOch->pszFileTemplate);  
  29. +        }  
  30. +  
  31. +finalize_it:  
  32. +        RETiRet;  
  33. +}  
  34.    
  35.  /* Helper to cfline() and its helpers. Parses a template name  
  36.   * from an "action" line. Must be called with the Line pointer  
  37. diff -urN rsyslog-4.6.5.orig/runtime/stream.c rsyslog-4.6.5/runtime/stream.c  
  38. --- rsyslog-4.6.5.orig/runtime/stream.c    2010-11-24 22:47:07.000000000 +0800  
  39. +++ rsyslog-4.6.5/runtime/stream.c    2012-08-14 08:30:25.000000000 +0800  
  40. @@ -96,6 +96,8 @@  
  41.  resolveFileSizeLimit(strm_t *pThis, uchar *pszCurrFName)  
  42.  {  
  43.      uchar *pParams;  
  44. +    int len;  
  45. +    uchar params[512] = {0};  
  46.      uchar *pCmd;  
  47.      uchar *p;  
  48.      off_t actualFileSize;  
  49. @@ -121,15 +123,22 @@  
  50.      if(*p == ' ') {  
  51.          *p = '\0'; /* pretend string-end */  
  52.          pParams = p+1;  
  53. -    } else  
  54. +        len = strlen(pParams);  
  55. +        strcpy(params, pParams);  
  56. +    } else {  
  57.          pParams = NULL;  
  58. +        len = 0;  
  59. +    }  
  60. +      
  61. +    //拼接动态文件的名字  
  62. +    strcpy(params+len, pszCurrFName);  
  63.    
  64.      /* the execProg() below is probably not great, but at least is is  
  65.       * fairly secure now. Once we change the way file size limits are  
  66.       * handled, we should also revisit how this command is run (and  
  67.       * with which parameters).   rgerhards, 2007-07-20  
  68.       */  
  69. -    execProg(pCmd, 1, pParams);  
  70. +    execProg(pCmd, 1, params);  
  71.    
  72.      free(pCmd);  
  73.    
  74. diff -urN rsyslog-4.6.5.orig/tools/omfile.c rsyslog-4.6.5/tools/omfile.c  
  75. --- rsyslog-4.6.5.orig/tools/omfile.c    2010-11-24 22:47:07.000000000 +0800  
  76. +++ rsyslog-4.6.5/tools/omfile.c    2012-08-14 08:26:17.000000000 +0800  
  77. @@ -661,19 +661,33 @@  
  78.      pData->iSizeLimit = 0; /* default value, use outchannels to configure! */  
  79.    
  80.      switch(*p) {  
  81. -        case '$':  
  82. -        CODE_STD_STRING_REQUESTparseSelectorAct(1)  
  83. -        /* rgerhards 2005-06-21: this is a special setting for output-channel  
  84. -         * definitions. In the long term, this setting will probably replace  
  85. -         * anything else, but for the time being we must co-exist with the  
  86. -         * traditional mode lines.  
  87. -         * rgerhards, 2007-07-24: output-channels will go away. We keep them  
  88. -         * for compatibility reasons, but seems to have been a bad idea.  
  89. -         */  
  90. -        CHKiRet(cflineParseOutchannel(pData, p, *ppOMSR, 0, OMSR_NO_RQD_TPL_OPTS));  
  91. -        pData->bDynamicName = 0;  
  92. +        case '$': {  
  93. +        uchar szFileName[128] = {0};  
  94. +        channParseFileName(p, szFileName);  
  95. +        if (szFileName[0] == '$') {  
  96. +            CODE_STD_STRING_REQUESTparseSelectorAct(2)  
  97. +            CHKiRet(cflineParseOutchannel(pData, p, *ppOMSR, 0, OMSR_NO_RQD_TPL_OPTS));  
  98. +            strcpy(pData->f_fname, "");  
  99. +            CHKiRet(OMSRsetEntry(*ppOMSR, 1, ustrdup(&szFileName[1]), OMSR_NO_RQD_TPL_OPTS));  
  100. +            pData->bDynamicName = 1;  
  101. +            pData->iCurrElt = -1;          /* no current element */  
  102. +            CHKmalloc(pData->dynCache = (dynaFileCacheEntry**)  
  103. +                calloc(iDynaFileCacheSize, sizeof(dynaFileCacheEntry*)));  
  104. +              
  105. +        } else {  
  106. +            CODE_STD_STRING_REQUESTparseSelectorAct(1)  
  107. +            /* rgerhards 2005-06-21: this is a special setting for output-channel  
  108. +              * definitions. In the long term, this setting will probably replace  
  109. +              * anything else, but for the time being we must co-exist with the  
  110. +              * traditional mode lines.  
  111. +              * rgerhards, 2007-07-24: output-channels will go away. We keep them  
  112. +              * for compatibility reasons, but seems to have been a bad idea.  
  113. +              */  
  114. +            CHKiRet(cflineParseOutchannel(pData, p, *ppOMSR, 0, OMSR_NO_RQD_TPL_OPTS));  
  115. +            pData->bDynamicName = 0;  
  116. +        }  
  117.          break;  
  118. -  
  119. +    }  
  120.      case '?': /* This is much like a regular file handle, but we need to obtain  
  121.             * a template name. rgerhards, 2007-07-03  
  122.             */  

打补丁时,只需要进入rsyslog-4.6.5目录,然后将patch文件放在上一级,执行patch -p1 <../rsyslog-dynchannel.patch即可。此外,还向rsyslog maillist提交了一个正式的patch:

  1. subject:add dynimic filename support for outchannel  
  2. to:rsyslog@lists.adiscon.com  
  3.   
  4. From e82f42dc9eb78fd2132b875c8718151ab9218acf Mon Sep 17 00:00:00 2001  
  5. From: zhaoya <marywangran@126.com>  
  6. Date: Tue, 7 Aug 2012 08:07:12 -0700  
  7. Subject: [PATCH] add dynimic filename support for outchannel  
  8.   
  9. The URL:  
  10. http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=477351  
  11. present a FALSE BUG! Up to the newest version,rsyslogd's "outchannel [still]  
  12. doesn't get variable from template"!  
  13. ---  
  14.  runtime/stream.c |   13 +++++++++-  
  15.  tools/omfile.c   |   63 +++++++++++++++++++++++++++++++++++++++++++----------  
  16.  2 files changed, 62 insertions(+), 14 deletions(-)  
  17.   
  18. diff --git a/runtime/stream.c b/runtime/stream.c  
  19. index bb1a0a4..49e7ec4 100644  
  20. --- a/runtime/stream.c  
  21. +++ b/runtime/stream.c  
  22. @@ -96,6 +96,8 @@ static rsRetVal  
  23.  resolveFileSizeLimit(strm_t *pThis, uchar *pszCurrFName)  
  24.  {  
  25.      uchar *pParams;  
  26. +    uchar params[512] = {0};  
  27. +    int len;  
  28.      uchar *pCmd;  
  29.      uchar *p;  
  30.      off_t actualFileSize;  
  31. @@ -121,15 +123,22 @@ resolveFileSizeLimit(strm_t *pThis, uchar *pszCurrFName)  
  32.      if(*p == ' ') {  
  33.          *p = '\0'; /* pretend string-end */  
  34.          pParams = p+1;  
  35. -    } else  
  36. +        len = strlen(pParams);  
  37. +        strcpy(params, pParams);  
  38. +    } else {  
  39.          pParams = NULL;  
  40. +        len = 0;  
  41. +    }  
  42. +  
  43. +    //脝麓陆脫露炉脤卢脦脛录镁碌脛脙没脳脰  
  44. +    strcpy(params+len, pszCurrFName);  
  45.    
  46.      /* the execProg() below is probably not great, but at least is is  
  47.       * fairly secure now. Once we change the way file size limits are  
  48.       * handled, we should also revisit how this command is run (and  
  49.       * with which parameters).   rgerhards, 2007-07-20  
  50.       */  
  51. -    execProg(pCmd, 1, pParams);  
  52. +    execProg(pCmd, 1, params);  
  53.    
  54.      free(pCmd);  
  55.    
  56. diff --git a/tools/omfile.c b/tools/omfile.c  
  57. index 1a36343..a877555 100644  
  58. --- a/tools/omfile.c  
  59. +++ b/tools/omfile.c  
  60. @@ -333,6 +333,32 @@ rsRetVal setDynaFileCacheSize(void __attribute__((unused)) *pVal, int iNewVal)  
  61.      RETiRet;  
  62.  }  
  63.    
  64. +/* Get a FileName from a outchannel  
  65. + */  
  66. +rsRetVal channParseFileName(uchar* p, uchar *szBuf)  
  67. +{  
  68. +        DEFiRet;  
  69. +        size_t i;  
  70. +        char szChn[128];  
  71. +        struct outchannel *pOch;  
  72. +  
  73. +        ++p; /* skip '$' */  
  74. +        i = 0;  
  75. +        /* get outchannel name */  
  76. +        while(*p && *p != ';' && *p != ' ' &&  
  77. +              i < sizeof(szChn) / sizeof(char)) {  
  78. +              szChn[i++] = *p++;  
  79. +        }  
  80. +        szChn[i] = '\0';  
  81. +        pOch = ochFind(szChn, i);  
  82. +        if (pOch != NULL &&  
  83. +                pOch->pszFileTemplate != NULL) {  
  84. +                strcpy(szBuf, pOch->pszFileTemplate);  
  85. +        }  
  86. +  
  87. +finalize_it:  
  88. +        RETiRet;  
  89. +}  
  90.    
  91.  /* Helper to cfline(). Parses a output channel name up until the first  
  92.   * comma and then looks for the template specifier. Tries  
  93. @@ -1004,19 +1030,32 @@ CODESTARTparseSelectorAct  
  94.      pData->iSizeLimit = 0; /* default value, use outchannels to configure! */  
  95.    
  96.      switch(*p) {  
  97. -        case '$':  
  98. -        CODE_STD_STRING_REQUESTparseSelectorAct(1)  
  99. -        /* rgerhards 2005-06-21: this is a special setting for output-channel  
  100. -         * definitions. In the long term, this setting will probably replace  
  101. -         * anything else, but for the time being we must co-exist with the  
  102. -         * traditional mode lines.  
  103. -         * rgerhards, 2007-07-24: output-channels will go away. We keep them  
  104. -         * for compatibility reasons, but seems to have been a bad idea.  
  105. -         */  
  106. -        CHKiRet(cflineParseOutchannel(pData, p, *ppOMSR, 0, OMSR_NO_RQD_TPL_OPTS));  
  107. -        pData->bDynamicName = 0;  
  108. +        case '$': {  
  109. +        uchar szFileName[128] = {0};  
  110. +        channParseFileName(p, szFileName);  
  111. +        if (szFileName[0] == '$') {  
  112. +            CODE_STD_STRING_REQUESTparseSelectorAct(2)  
  113. +            CHKiRet(cflineParseOutchannel(pData, p, *ppOMSR, 0, OMSR_NO_RQD_TPL_OPTS));  
  114. +            strcpy(pData->f_fname, "");  
  115. +            CHKiRet(OMSRsetEntry(*ppOMSR, 1, ustrdup(&szFileName[1]), OMSR_NO_RQD_TPL_OPTS));  
  116. +            pData->bDynamicName = 1;  
  117. +            pData->iCurrElt = -1;          /* no current element */  
  118. +            CHKmalloc(pData->dynCache = (dynaFileCacheEntry**)  
  119. +                calloc(iDynaFileCacheSize, sizeof(dynaFileCacheEntry*)));  
  120. +        } else {      
  121. +            CODE_STD_STRING_REQUESTparseSelectorAct(1)  
  122. +            /* rgerhards 2005-06-21: this is a special setting for output-channel  
  123. +             * definitions. In the long term, this setting will probably replace  
  124. +             * anything else, but for the time being we must co-exist with the  
  125. +             * traditional mode lines.  
  126. +             * rgerhards, 2007-07-24: output-channels will go away. We keep them  
  127. +             * for compatibility reasons, but seems to have been a bad idea.  
  128. +             */  
  129. +            CHKiRet(cflineParseOutchannel(pData, p, *ppOMSR, 0, OMSR_NO_RQD_TPL_OPTS));  
  130. +            pData->bDynamicName = 0;  
  131. +        }  
  132.          break;  
  133. -  
  134. +    }  
  135.      case '?': /* This is much like a regular file handle, but we need to obtain  
  136.             * a template name. rgerhards, 2007-07-03  
  137.             */  
  138. --   
  139. 1.7.4.1  

此patch的意义绝大部分是为了自用,本着GPL才提交的。如此patch,就可以在配置文件中使用下面的配置了:
$template DynFile,"/var/log/system-%HOSTNAME%.log"
$outchannel porHost,$DynFile,104857600,echo test
*.* $porHost

 

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款