Parse section configurations

本文介绍了Fluentd中解析配置的使用方法,包括如何指定解析插件类型、配置解析参数等。通过示例展示了不同解析插件的应用场景,如正则表达式解析、CSV解析等,并解释了如何处理时间字段。

Some of Fluentd’s plugins support the <parse> section to specify how to parse raw data.

Table of Contents

Parse section overview

Parse section can be in <source><match> or <filter> sections. It’s enabled for plugins which support parser plugin features.

<source>
  @type tail
  # parameters for input plugin
  <parse>
    # parse section parameters
  </parse>
</source>

parser plugin type

<parse> section requires @type parameter to specify the type of parser plugin. Fluentd core bundles a lot of useful parser plugins. 3rd party plugins are also available when installed.

<parse>
  @type apache2
</parse>

For more details, see plugins documentation.

Parameters

@type

@type key is to specify the type of parser plugin.

<parse>
  @type regexp
  # ...
</parse>

These parsers are built-in by default.

Parse parameters

These parameters default value will be overwritten by individual parser plugins.

  • types (hash) (optional): Specify types for converting field into other type. See below “The detail of types parameter” section.
    • Default: nil
    • string-based hash: field1:type, field2:type, field3:type:option, field4:type:option
    • JSON format:{"field1":"type", "field2":"type", "field3":"type:option", "field4":"type:option"}
    • example: types user_id:integer,paid:bool,paid_usd_amount:float
  • time_key (string) (optional): Specify time field for event time. If the event doesn’t have this field, current time is used.
    • Default: nil
  • null_value_pattern (string) (optional): Specify null value pattern.
    • Default: nil
  • null_empty_string (bool) (optional): If true, empty string field is replaced with nil.
    • Default: false
  • estimate_current_event (bool) (optional): If true, use Fluent::EventTime.now(current time) as a timestamp when time_key is specified.
    • Default: false
  • keep_time_key (bool) (optional): If true, keep time field in the record.
    • Default: false
The detail of types parameter

The list of supported types are shown below:

  • string

Convert field into String type. This uses to_s method for conversion.

  • bool

Convert "true""yes" or "1" string into true. Otherwise, false.

  • integer (“int” would NOT work!)

Convert field into Integer type. This uses to_i method for conversion. For example, "1000" string is converted into 1000.

  • float

Convert field into Float type. This uses to_f method for conversion. For example, "7.45" string is converted into 7.45.

  • time

Convert field into Fluent::EventTime type. This uses Fluentd’s time parser for conversion. For time type, the third field specifies a time format you would in time_format.

date:time:%d/%b/%Y:%H:%M:%S %z # for string with time format
date:time:unixtime             # for integer time
date:time:float                # for float time

See time_type and time_format parameters in Time parameters section.

  • array

Convert string field into Array type. For the “array” type, the third field specifies the delimiter (the default is “,”). For example, if a field called “item_ids” contains the value "3,4,5"types item_ids:array parses it as ["3", "4", "5"]. Alternatively, if the value is "Adam|Alice|Bob"types item_ids:array:| parses it as ["Adam", "Alice", "Bob"].

Time parameters

  • time_type (enum) (optional): parse/format value according to this type
    • Default: float
    • Available values: floatunixtimestring
      • float: seconds from Epoch + nano seconds (e.g. 1510544836.154709804)
      • unixtime: seconds from Epoch (e.g. 1510544815)
      • string: use format specified by time_format, local time or time zone
  • time_format (string) (optional): process value using specified format. This is available only when time_type is string
    • Default: nil
    • Available time format:
      • For more details about formatting, see Time#strftime
      • For more details about parsing, see Time.strptime
      • %iso8601 (only for parsing)
  • localtime (bool) (optional): if true, use local time. Otherwise, UTC is used. This is exclusive with utc.
    • Default: true
  • utc (bool) (optional): if true, use UTC. Otherwise, local time is used. This is exclusive with localtime.
    • Default: false
  • timezone (string) (optional): use specified timezone. one can parse/format the time value in the specified timezone.
    • Default: nil
    • Available time zone format:
      1. [+-]HH:MM (e.g. “+09:00”) (recommended)
      2. [+-]HHMM (e.g. “+0900”)
      3. [+-]HH (e.g. “+09”)
      4. Region/Zone (e.g. “Asia/Tokyo”)
      5. Region/Zone/Zone (e.g. “America/Argentina/Buenos_Aires”)
int rtsps_read_metadataconf() { struct uci_context *ctx = NULL; struct uci_package *pkg = NULL; struct uci_element *e, *e1; int ret = -1; int chn = 0; int g_metadata_config_count = 0; // Initialize UCI context ctx = uci_alloc_context(); if (!ctx) { CHM_DPRINTF(MSG_ERROR, "Failed to allocate UCI context\n"); goto cleanup; } // lookuo package pkg = uci_lookup_package(ctx, METADATA_CONF_FILE); if (NULL != pkg) { goto cleanup; } // Load the package if (uci_load(ctx, METADATA_CONF_FILE, &pkg) != 0) { printf("Failed to load UCI package: %s\n", METADATA_CONF_FILE); goto cleanup; } // Reset global config count g_metadata_config_count = 0; // Iterate through all sections in the package uci_foreach_element(&pkg->sections, e) { struct uci_section *s = uci_to_section(e); // Only process metadatacf sections if (strcmp(s->type, METADATA_CONF_SEC_TYPE) != 0) { continue; } // Check if we have space in our global array if (g_metadata_config_count >= MAX_METADATA_SECTIONS) { printf("Reached maximum metadata sections (%d)\n", MAX_METADATA_SECTIONS); break; } // Store section name if (sscanf(s->e.name, METADATA_CONF_SEC_NAME, &chn) == 1) { // Successfully parsed the channel number if (chn >= 0 && chn < MAX_METADATA_SECTIONS) { snprintf(g_metadata_configs[chn - 1].section_name, sizeof(g_metadata_configs[0].section_name), METADATA_CONF_SEC_NAME, chn); } else { continue; } } else { // Handle format mismatch printf("set mis-format metadata configurations section name %s \n", s->e.name); continue; } // Parse options struct uci_option *opt; uci_foreach_element(&s->options, e1) { opt = uci_to_option(e1); if (strcmp(opt->e.name, "events") == 0) { g_metadata_configs[g_metadata_config_count].events = (strcasecmp(opt->v.string, "on") == 0) ? 1 : 0; } else if (strcmp(opt->e.name, "analytics") == 0) { g_metadata_configs[g_metadata_config_count].analytics = (strcasecmp(opt->v.string, "on") == 0) ? 1 : 0; } else if (strcmp(opt->e.name, "ptzstatus") == 0) { g_metadata_configs[g_metadata_config_count].ptzstatus = (strcasecmp(opt->v.string, "on") == 0) ? 1 : 0; } else if (strcmp(opt->e.name, "ptzposition") == 0) { g_metadata_configs[g_metadata_config_count].ptzposition = (strcasecmp(opt->v.string, "on") == 0) ? 1 : 0; } } g_metadata_config_count++; } ret = 0; printf("Successfully loaded %d metadata configurations\n", g_metadata_config_count); cleanup: if (pkg) { uci_unload(ctx, pkg); } if (ctx) { uci_free_context(ctx); } return ret; } void init_metadata_configs() { for (int i = 0; i < MAX_METADATA_SECTIONS; i++) { snprintf(g_metadata_configs[i].section_name, sizeof(g_metadata_configs[0].section_name), METADATA_CONF_SEC_NAME, i + 1); // Set default values: 0,1,0,0 g_metadata_configs[i].events = 0; g_metadata_configs[i].analytics = 1; // Default to 1 as specified g_metadata_configs[i].ptzstatus = 0; g_metadata_configs[i].ptzposition = 0; } } 这些设置需要给g_metadata_config加锁
最新发布
11-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值