Ad Widget

Collapse

SyntaxError Preproccessing JavaScript

Collapse
This topic has been answered.
X
X
 
  • Time
  • Show
Clear All
new posts
  • WouterPeeters
    Junior Member
    • Jun 2024
    • 6

    #1

    SyntaxError Preproccessing JavaScript

    I get "SyntaxError: unterminated statement (line 5)" in a preproccessing JavaScript. I don't get this error in my JS online editor

    I'm trying to create a JS that parses IPv6 address in decimal form in the OID of Cisco SNMP to a HEX address.
    I was able to make the code work in an online editor but when I copy the code to my template in Zabbix I get the above error
    It looks like I'm missing a ; on line 5 but I'm not.


    using Template in 6.0

    Value:
    Code:
    [{"{#SNMPINDEX}":"32.1.1.1.1.1.1.1.1.1.1.1.1.1.1.16","{#SNMPVALUE}":"65001"},{"{#SNMPINDEX}":"32.1.1.1.1.1.1.1.1.1.1.1.1.1.1.32","{#SNMPVALUE}":"65002"},{"{#SNMPINDEX}":"32.1.1.1.1.1.1.1.1.1.1.1.1.1.1.48","{#SNMPVALUE}":"65003"}]

    Code:
    value = value.slice(1, -1);
    value = value.replaceAll("},{", "}£{");
    const item = value.split("£");
    value = '';
    let i = 0;
    while (i < item.length) {
        item[i] = item [i].slice(1, -1);
        let part = item[i].split(",");
        let j = 0;
        while (j < part.length) {
            let result = part[j].split(":");
            if ((j % 2) == 0) {
                let k = 0;
                let hex = "";
                while (k < result.length) {
                    if ((k % 2) == 0) {
                    } else {
                        result[k] = result[k].slice(1, -1);
                        let number = result[k].split(".");
                        let l = 0;
                        while (l < number.length) {
                            if ((l % 2) == 0) {
                                number[l] = number[l] * 256;
                            } else {
                                number[l] = parseInt(number[l-1]) + parseInt(number[l]);
                                if (l == 1) {
                                    hex = number[l].toString(16);
                                } else {
                                    hex = hex + ":" + number[l].toString(16);
                                }
                            }
                            l++;
                        }
                    }
                    k++;
                }
                result[1] = hex;
                part[0] = result[0] + ':"' + result[1] + '"';
            }
            j++
        }
        item[i] = part[0] + ',' + part[1];
        if (i == 0) {
            value = item[i];
        } else {
            value = value + '},{' + item[i];
        }
        i++;
    };
    return value;
  • Answer selected by WouterPeeters at 18-06-2024, 11:33.
    WouterPeeters
    Junior Member
    • Jun 2024
    • 6

    I recreated the code with json parse


    Code:
    // Parse the input string into a JavaScript array of objects
    var data = JSON.parse(value);
    // Iterate over each object in the array and add the {#SNMPHEX} key
    var i = 0;
    while (i < data.length) {
    var item = data[i];
    var snmpIndex = item['{#SNMPINDEX}'];
    var parts = snmpIndex.split('.');
    var ipv6 = [];
    var j = 0;
    while (j < parts.length) {
    var combined = (parseInt(parts[j], 10) << 8) + parseInt(parts[j + 1], 10);
    ipv6.push(combined.toString(16)); // no padding with leading zeros
    j += 2;
    }
    item['{#SNMPHEX}'] = ipv6.join(':');
    i++;
    }
    // Convert the modified array back to a JSON string
    var output = JSON.stringify(data, null, 2);
    return output;

    Comment

    • WouterPeeters
      Junior Member
      • Jun 2024
      • 6

      #2
      The problem is defining a variable with "let". If I replace "let" with "var" the error disappears.
      Now I am stuck with a new error. "TypeError: undefined not callable (property 'replaceAll' of '{"{#SNMPINDEX}"
      I traced this to an old version of JS where replaceAll is not yet implemented. I can fix this in my editor by manually defining the function. but this doesn't work in Zabbix.


      Code:
      function escapeRegExp(string) {
        return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
      }
      function replaceAll(str, find, replace) {
        return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
      }
      Edit:
      It appears Zabbix JS doesn't handle functions well. I solved the above be putting the function straight in the code.

      Code:
      value = value.replace(new RegExp("},{".replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'), "}£{");
      Edit2:
      After later consideration is better to convert the value string back to JSON and change the JSON values.
      I will have to rewrite the entire code for this...
      Last edited by WouterPeeters; 13-06-2024, 13:28.

      Comment

      • WouterPeeters
        Junior Member
        • Jun 2024
        • 6

        #3
        I recreated the code with json parse


        Code:
        // Parse the input string into a JavaScript array of objects
        var data = JSON.parse(value);
        // Iterate over each object in the array and add the {#SNMPHEX} key
        var i = 0;
        while (i < data.length) {
        var item = data[i];
        var snmpIndex = item['{#SNMPINDEX}'];
        var parts = snmpIndex.split('.');
        var ipv6 = [];
        var j = 0;
        while (j < parts.length) {
        var combined = (parseInt(parts[j], 10) << 8) + parseInt(parts[j + 1], 10);
        ipv6.push(combined.toString(16)); // no padding with leading zeros
        j += 2;
        }
        item['{#SNMPHEX}'] = ipv6.join(':');
        i++;
        }
        // Convert the modified array back to a JSON string
        var output = JSON.stringify(data, null, 2);
        return output;

        Comment

        Working...