Ad Widget

Collapse

JSONPath with regular expressions and capture group

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • markfree
    Senior Member
    • Apr 2019
    • 868

    #1

    JSONPath with regular expressions and capture group

    I have an interesting scenario where an item collects a text value with multiple lines.
    Each row has an instance name and a status. Something like this:
    Code:
    QMNAME(NAME1)                STATUS(Running)
    QMNAME(NAME2)                STATUS(Running)
    QMNAME(NAME3)                STATUS(Standby)
    My goal is to create 2 items for each row and extract the instance name and its status.
    To do this, I've created a discovery rule that has a "CSV to JSON" preprocessing step and it results in a JSON array.
    Code:
    [
      {
        "1": "QMNAME(NAME1)                  STATUS(Running)"
      },
      {
        "1": "QMNAME(NAME2)                  STATUS(Running)"
      },
      {
        "1": "QMNAME(NAME3)                  STATUS(Standby)"
      }
    ]
    For the discovery LLD Macros, I'm looking for a way to extract the QMNAME and STATUS using JSONPath and regular expressions.
    This way, the discovery rule can have 2 LLD macros that are referenced on the item prototypes
    I could create "calculated" item prototypes and use regular expressions to extract the values, but I was hoping to use "dependent" item prototypes instead.

    Maybe something like $.[?(@ =~ /QMNAMES\((.*)\)\s+STATUS\((.*)\)/)], but with capture groups.

    However, there doesn't seem to be any regex capture groups available in JSONPath.

    Does anyone know if Zabbix JSONPath supports regex with capture groups?
    Or how to further break down the JSON array values?​
  • markfree
    Senior Member
    • Apr 2019
    • 868

    #2
    I could not find a reasonable solution the way I originally wanted.
    I ended up resorting to JavaScript.

    The master item now converts the data to a JSON array that separates the QMNAME and STATUS values.
    This allows me to create a dependent discovery as well as dependent item prototypes.

    This is the script:
    Code:
    // Split text lines
    var lines = value.split('\n');
    
    // Start an array to receive JSON Objects
    var jsonArray = [];
    
    // Loop through each line
    for (var i = 0; i < lines.length; i++) {
        var line = lines[i].trim();
        if (line) { // Check if line is not empty
            // Regex to match the values between parentheses
            var qmnameMatch = line.match(/QMNAME\(([^)]+)\)/);
            var statusMatch = line.match(/STATUS\(([^)]+)\)/);
    
            if (qmnameMatch && statusMatch) {
                jsonArray.push({
                    "QMNAME": qmnameMatch[1],  // Extracted QMNAME value
                    "STATUS": statusMatch[1]   // Extracted STATUS value
                });
            }
        }
    }
    
    // Converts the JSON array to a concatenated string
    var jsonString = JSON.stringify(jsonArray);
    
    return(jsonString);
    And this is the resulting JSON:
    Code:
    [
        {
            "QMNAME": "NAME2",
            "STATUS": "Running"
        },
        {
            "QMNAME": "NAME3",
            "STATUS": "Standby"
        },
        {
            "QMNAME": "NAME4",
            "STATUS": "Running"
        },
        {
            "QMNAME": "NAME5",
            "STATUS": "Standby"
        },
        {
            "QMNAME": "NAME6",
            "STATUS": "Running"
        },
        {
            "QMNAME": "NAME7",
            "STATUS": "Standby"
        }
    ]

    Comment

    Working...