Skip to content

Conversation

@EmileDqy
Copy link

@EmileDqy EmileDqy commented Jun 13, 2025

Adding -oaddFieldsPosition flag to liblognorm

The -oaddFieldsPosition flag is made to enable the tracking of the position of each field's value in the input log.

The output is a new field under "metadata" which has the same structure as the base fields but the values are replaced with an array of two indices.

Example

$ cat sample.log | head -1 | lognormalizer -r rulebase.rb -oaddFieldsPosition | jq

{
  "parent_data": {
    "logver": "60",
    "timestamp": "1572361274",
    "tz": "UTC+1",
    "srcserver": "1"
  },
  "priority": "189",
  "metadata": {
    "fields_position": {
      "priority": [
        1,
        4
      ],
      "parent_data": {
        "logver": [
          12,
          14
        ],
        "timestamp": [
          25,
          35
        ],
        "tz": [
          40,
          45
        ],
        "srcserver": [
          943,
          944
        ],
        "__fieldposition": [
          5,
          944
        ]
      }
    }
  }
}

A __fieldposition field is also added under each new nested level of json so as to track the whole position of the group.
For example here, the field __fieldposition was added under metadata.fields_position.parent_data and says that the parent_data field started at index 5 and ended at index 944.

Limitations

This feature does not work for the fields created by the following types:

  • json: it uses libfastjson under the hood which breaks the data lineage.
  • checkpoint-lea: Not implemented
  • cisco-interface-spec: Not implemented
  • iptables: Not implemented
  • cisco-interface-spec: Not implemented
  • alternative: Not implemented
  • repeat: Not implemented
  • cee-syslog: Not implemented

src/parser.c Outdated
Comment on lines 3139 to 3154
ln_recordfieldposition(npb, "DeviceVendor", iHdrStart, i-1);
iHdrStart = i;
CHKR(cefGetHdrField(npb, &i, (value == NULL) ? NULL : &product));
ln_recordfieldposition(npb, "DeviceProduct", iHdrStart, i-1);
iHdrStart = i;
CHKR(cefGetHdrField(npb, &i, (value == NULL) ? NULL : &version));
ln_recordfieldposition(npb, "DeviceVersion", iHdrStart, i-1);
iHdrStart = i;
CHKR(cefGetHdrField(npb, &i, (value == NULL) ? NULL : &sigID));
ln_recordfieldposition(npb, "SignatureID", iHdrStart, i-1);
iHdrStart = i;
CHKR(cefGetHdrField(npb, &i, (value == NULL) ? NULL : &name));
ln_recordfieldposition(npb, "Name", iHdrStart, i-1);
iHdrStart = i;
CHKR(cefGetHdrField(npb, &i, (value == NULL) ? NULL : &severity));
ln_recordfieldposition(npb, "Severity", iHdrStart, i-1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i-1 ?
isn't it i+1 ?
And I don't think all fields are 1 char long

Copy link
Author

@EmileDqy EmileDqy Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried with i-1. It works great.

if (npb == NULL || npb->fieldposition == NULL || npb->field_path == NULL || fieldName == NULL) {
return;
}
if (fieldName[0] == '.' && fieldName[1] == '\0') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not?

Suggested change
if (fieldName[0] == '.' && fieldName[1] == '\0') {
if (fieldName[0] == '.' && fieldName[1] == NULL) {

src/pdag.h Outdated
Comment on lines 165 to 166
struct json_object *fieldposition; /**< field logspan data */
struct json_object *field_path; /**< field logspan path stack */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong indentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants