05
2020
06

syslog Templates



推荐点击下面图片,通过本站淘宝优惠价购买:

image.png

Templates

简介:

  模板是rsyslog的一个关键特性,允许用户指定任何格式,也可用于文件名动态生成,每个使用template的rsyslog的输出都
  适用于文件,用户信息等,template是一个类似sql语句,是非常灵活的。
  Templates被Templates()指定。


   The template() statement

    template(parameters) { list-descriptions }

    每个模板的参数名称,它指定模板名称、模板和参数类型,指定类型。参数名称必须是惟一的。  

    模板有以下类型:   

      list
      subtree
      string
      plugin

    list:

       生成的模板是常量和变量声明的列表,支持结构化(mongodb)输出和文本化输出。

       示例:

复制代码
                template(name="tpl1" type="list") {
                    constant(value="Syslog MSG is: '")    
                    property(name="msg")
                    constant(value="', ")
                    property(name="timereported" dateFormat="rfc3339" caseConversion="lower")
                    constant(value="\n")
                    }     
复制代码

        constant:描述常量,它主要用于基于文本的输出,如果想结构化输出需要加上outname参数。
          value - the constant value to use
          outname - output field name (for structured outputs)

         property:描述属性值,里面包含很多选项,大多数选项用于提取部分文本或修改文本输出内容

          支持的选项:  

复制代码
                        name:访问属性的名称
                        outname:输出字段名(用于结构化输出)
                        dateformat:使用的时间格式
                        caseconversion:转换文本,可以使用lower和upper
                        controlcharacters:指定如何处理控制字符,可以有3个值escape,space,drop。
                        securepath:用于创建路径名适用于dynafile模板
                        format:指定格式字段的基础上。支持的值是:csv,json,jsonr,jsonf,jsonfr。
                        position.from:获得子串的位置
                        position.to:获取到子串的截止位置
                        position.relativeToEnd:从字符串结束的位置开始,而不是起始位置
                        field.number:获得该字段匹配
                        field.delimiter:小数(小数)值的字段分隔符字符提取
                        regex.expression:使用正则表达式
                        regex.type:使用正则表达式的类型
                        regex.nomatchmode:如果没有匹配上做什么处理
                        regex.match:使用匹配
                        regex.submatch:使用sunmatch匹配
                        droplastlf:去掉LF(换行),如果存在的话
                        mandatory:表明(表示)字段是强制性的。如果设置为"on",这个领域总是会出现在数据传递到结构化输出,即使它是空的。如果"关闭"(默认)空字段将不会传递到结构化输出。
                        这是特别有用的输出,支持动态模式(模式)(如ommongodb)。
复制代码

     subtree:

        在7.1.4版本之后有效,模板生成基于一个完整的子树,这种类型的模板适用于怎样处理分层的结构,如ommongodb
        name="tpl1" type="subtree" subtree="$!" #包含所有完整的数据
        name="tpl2" type="subtree" subtree="$!usr!tpl2 #只包含从$!usr!tpl2开始的字符串
        示例:  

                set $!usr!tpl2!msg = $msg;
                set $!usr!tpl2!dataflow = field($msg, 58, 2);
                template(name="tpl2" type="subtree" subtree="$!usr!tpl2")

      string:

          有些类似于legacy 模板的格式,有一个固定的参数string。
          示例:

                template(name="tpl3" type="string"
                    string="%TIMESTAMP:::date-rfc3339% %HOSTNAME% %syslogtag%%msg:::sp-if-no-1st-sp%%msg:::drop-last-lf%\n"
                )

          字符串模板的%里面的变量是由property_replacer解释,链接:http://www.rsyslog.com/doc/v7-stable/configuration/properties.html
          百分号外的文本全部为常量字符串组成。

         plugin:   

          该类型的模板是有插件生成,通常性能比较好,这种类型的参数必须指定插件和必须包含插件的名称标识本身,在使用之前必须先加载插件。
          示例:  

                template(name="tpl4" type="plugin" plugin="mystrgen")

      options:

          这部分是可选的,可以影响整个模板和部分模板参数。
          option.sql:格式字符串适合在MySQL中SQL语句的格式,将替换字符串中的单引号和转义符号。注意:NO_BACKSLASH_ESCAPES模式必须在关闭状态。
          option.stdsql:标准的字符串sql格式输出,用两个单引号替换单引号,如果使用这种格式输出到mysql里面,必须开启NO_BACKSLASH_ESCAPES
          option.json:适合一个json语句格式的字符串。这将替换单引号("")由两个单引号("")在每个字段。

          示例:

                template (name="TraditionalFormat" type="string"
                string="%timegenerated% %HOSTNAME% %syslogtag%%msg%\\n"

 

Examples

示例1:

复制代码
        Standard Template for Writing to Files(写入文件的标准模板格式)
            template(name="FileFormat" type="list") {            #定义模板名称和类型
            property(name="timestamp" dateFormat="rfc3339")        #定义访问属性值
            constant(value=" ")                                    #定义常量文本内容    
            property(name="hostname")
            constant(value=" ")
            property(name="syslogtag")
            property(name="msg" spifno1stsp="on" )
            property(name="msg" droplastlf="on" )
            constant(value="\n")
            }
            #以上标准模板的string格式如下:
            template(name="FileFormat" type="string"
                string= "%TIMESTAMP% %HOSTNAME% %syslogtag%%msg:::sp-if-no-1st-sp%%msg:::drop-last-lf%\n"
            )
复制代码

示例2:

复制代码
        Standard Template for Forwarding to a Remote Host (RFC3164 mode)
            template(name="ForwardFormat" type="list") {
            constant(value="<")
            property(name="pri")
            constant(value=">")
            property(name="timestamp" dateFormat="rfc3339")
            constant(value=" ")
            property(name="hostname")
            constant(value=" ")
            property(name="syslogtag" position.from="1" position.to="32")
            property(name="msg" spifno1stsp="on" )
            property(name="msg")
            }
复制代码

示例3:

复制代码
        Standard Template for write to the MySQL database    #相当于编织出一条符合sql语句的字符串
            template(name="StdSQLformat" type="list" option.sql="on") {
                constant(value="insert into SystemEvents (Message, Facility, FromHost, Priority, DeviceReportedTime, ReceivedAt, InfoUnitID, SysLogTag)")
                constant(value=" values ('")
                property(name="msg")
                constant(value="', ")
                property(name="syslogfacility")
                constant(value=", '")
                property(name="hostname")
                constant(value="', ")
                property(name="syslogpriority")
                constant(value=", '")
                property(name="timereported" dateFormat="mysql")
                constant(value="', '")
                property(name="timegenerated" dateFormat="mysql")
                constant(value="', ")
                property(name="iut")
                constant(value=", '")
                property(name="syslogtag")
                constant(value="')")
复制代码

    等价的字符串模板格式如下:

                template(name="stdSQLformat" type="string" option.sql="on"
                    string="insert into SystemEvents (Message, Facility, FromHost, Priority, DeviceReportedTime, ReceivedAt, InfoUnitID, SysLogTag) values ('%msg%', %syslogfacility%, '%HOSTNAME%', %syslogpriority%, '%timereported:::date-mysql%', '%timegenerated:::date-mysql%', %iut%, '%syslogtag%')"
                )

 

Creating Dynamic File Names for omfile 创建动态文件名

  将不同主机发来的信息输出到不同的文件
  示例:
    template (name="DynFile" type="string" string="/var/log/system-%HOSTNAME%.log")
  Legacy示例:
    $template DynFile,"/var/log/system-%HOSTNAME%.log"


legacy format:

  以$配置开头,可以在V7版本中使用。
  $template name,param[,options] #name指的是模板的名称,param指定模板的内容,options设置模板的选项。
  示例:
    $template strtpl,"PRI: %pri%, MSG: %msg%\n"
    legacy不支持list模板类型,复杂的工作应该用复杂的property replacer来实现。
  plugin
    $template plugintpl,=myplugin


Reserved Template Names

  以RSYSLOG_开头的是预留模板的标识。
    RSYSLOG_TraditionalFileFormat:"旧式"默认日志文件格式和时间戳精度低
    RSYSLOG_FileFormat:现代类型日志文件的格式,高精度时间戳和时区信息。
    RSYSLOG_TraditionalForwardFormat:传统的转发与时间戳精度格式。
    RSYSLOG_SysklogdFileFormat:sysklogd日志文件格式兼容
    RSYSLOG_ForwardFormat:一种新的高精度转发格式非常相似的传统。
    RSYSLOG_SyslogProtocol23Format:略
    RSYSLOG_DebugFormat:一个特殊的格式用于故障诊断的产权问题。


 

 


本文链接:https://hqyman.cn/post/1360.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:





休息一下,本站随机推荐观看栏目:


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

您的IP地址是: