Ad Widget

Collapse

Howo escape macro in javascript preprocessing?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aschild
    Junior Member
    • Sep 2014
    • 25

    #1

    Howo escape macro in javascript preprocessing?

    Hello,

    I have a LLD which returns this JSON:

    Code:
    { "disks": [
    {
    "{#DISK.MINIMUM.SIZE.GB}": "16",
    "{#DISK.BLOCK.SIZE}": "33554432",
    "{#DISK.VHD.TYPE}": "Dynamic",
    "{#DISK.VHD.FORMAT}": "VHDX",
    "{#VM.ID}": "b7dbb0ba-9bc0-4358-9cda-cc7ebdf0fdcf",
    "{#VM.NAME}": "apps-aarboard",
    "{#DISK.ALIGNMENT}": "1",
    "{#DISK.LOGICAL.SECTOR.SIZE}": "512",
    "{#DISK.LOCATION}": "0",
    "{#DISK.PATH}": "D:\\Hyper-V\\Hyper-V Replica\\Virtual hard disks\\B7DBB0BA-9BC0-4358-9CDA-CC7EBDF0FDCF\\APPS aarboard innovaphone.vhdx",
    "{#DISK.CONTROLLER}": "IDE",
    "{#DISK.NUMBER}": "0",
    "{#DISK.SIZE.GB}": "16",
    "{#DISK.FILE.SIZE.GB}": "9.5",
    "{#DISK.PHYSICAL.SECTOR.SIZE}": "4096",
    "{#DISK.FRAGMENTATION}": "3",
    "{#DISK.ID}": "apps-aarboard_IDE_0_0"
    }]
    }
    Now I with to extract the {#DISK.SIZE.GB} value via a data prototype, this works fine, when I create the item manually

    Code:
    var data = JSON.parse(value);
    var diskId = "apps-aarboard_IDE_0_0";
    
    // Debug: Return info about what we found
    if (!data.disks || data.disks.length === 0) {
    throw "ZBX_UNSUPPORTED: No disks array found";
    }
    
    for (var i = 0; i < data.disks.length; i++) {
    var disk = data.disks[i];
    if (disk["{#DISK.ID}"] === diskId) {
    var fileSize = disk["{#DISK.FILE.SIZE.GB}"];
    if (!fileSize) {
    throw "ZBX_UNSUPPORTED: Field {#DISK.FILE.SIZE.GB} not found or empty";
    }
    return fileSize;
    }
    }
    
    // Debug: Show what disk IDs we found
    var foundIds = [];
    for (var i = 0; i < data.disks.length; i++) {
    foundIds.push(data.disks[i]["{#DISK.ID}"]);
    }
    throw "ZBX_UNSUPPORTED: Disk ID '" + diskId + "' not found. Found: " + foundIds.join(", ") + "Values: "+value;
    But when I use the template like below, all macros are resolved, which then does not work.
    How can I escape the macros in the javascript, so only the instances I wish are replaced by the values?
    Unfortunalty, the {{#DISK.ID}} gets expanded to {apps-aarboard_IDE_0_0} value

    Code:
    var data = JSON.parse(value);
    var diskId = "{#DISK.ID}";
    
    // Debug: Return info about what we found
    if (!data.disks || data.disks.length === 0) {
    throw "ZBX_UNSUPPORTED: No disks array found";
    }
    
    for (var i = 0; i < data.disks.length; i++) {
    var disk = data.disks[i];
    if (disk["{{#DISK.ID}}"] === diskId) {
    var fileSize = disk["{#DISK.FILE.SIZE.GB}"];
    if (!fileSize) {
    throw "ZBX_UNSUPPORTED: Field {#DISK.FILE.SIZE.GB} not found or empty";
    }
    return fileSize;
    }
    }
    
    // Debug: Show what disk IDs we found
    var foundIds = [];
    for (var i = 0; i < data.disks.length; i++) {
    foundIds.push(data.disks[i]["{#DISK.ID}"]);
    }
    throw "ZBX_UNSUPPORTED: Disk ID '" + diskId + "' not found. Found: " + foundIds.join(", ") + "Values: "+value;
  • PSklyar
    Member
    • Sep 2025
    • 30

    #2
    Hey, hey aschild !
    In Zabbix using double braces {{macro}} tells Zabbix to only expand user macros and low-level discovery macros, while single braces {macro} are used for other types of macros.
    When you use double braces in JavaScript string literals, Zabbix will replace them with the actual values while leaving your JavaScript syntax intact.
    The key is that in your data prototype, you want the macro {#DISK.ID} to be expanded to its actual value, but in your JavaScript code, you're using it as a string literal that should remain as-is until the JSON parsing happens.
    You can try to create user macros that won't be auto-expanded by LLD:
    Name: {$DISK.ID.MACRO}
    Value: {#DISK.ID}
    Last edited by PSklyar; 03-10-2025, 15:52.

    Comment

    • aschild
      Junior Member
      • Sep 2014
      • 25

      #3
      I got it working for most usecases.

      Code:
      var adapterId = "{ADAPTER.ID}";
      var adapterIdMacro= "{"+"#ADAPTER.ID}";
      var searchKey= "{"+"#ADAPTER.MAC}";
      The only exception is, when I have macros which have for example a \ in it, like this:
      Code:
      var nicId = "Microsoft:B7DBB0BA-9BC0-4358-9CDA-CC7EBDF0FDCF\FD10FE56-16D3-4830-86E2-7548098A8423";
      The inserted value has a \ in it, and this i is then interpreted as JS escape, which is stripped out.

      This did not work either:
      Code:
      var adapterId = "{ADAPTER.ID}.tr("\\", "\\\\")";
      Any ideas?

      Comment

      • aschild
        Junior Member
        • Sep 2014
        • 25

        #4
        I did solve this, by already returning it in the LLD as it has to be used later.

        Comment

        Working...