Ad Widget

Collapse

Inventory field as macro in script item

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • focar
    Junior Member
    • Dec 2023
    • 17

    #1

    Inventory field as macro in script item

    Hello

    i'm attempting to create an item that requests and sets the received accesstoken as an inventory field (Works). Then use this macro {INVENTORY.NOTES} in my script for authentication. Documentation says it is possible to use inventory macro in script items. I've tried different ways to call the macro in the script but it keeps failing.
    here is the script:

    ```
    var params = JSON.parse(value);
    var url = "https://" + params.fqdn + ":" + params.port + "/management/v1/aps";
    var token = "{INVENTORY.NOTES}";

    var request = new HttpRequest();
    request.addHeader("Authorization: Bearer " + token);

    var response = request.get(url);

    if (request.getStatus() === 200) {
    return response;
    } else {
    throw "HTTP Error: " + request.getStatus() + " - " + response;
    }

    ```
    even if i set the parameter token : {INVENTORY.NOTES}
    and use this code:

    ```
    var params = JSON.parse(value);
    var url = "https://" + params.fqdn + ":" + params.port + "/management/v1/aps";
    var token = params.token;

    var request = new HttpRequest();
    request.addHeader("Authorization: Bearer " + token);

    var response = request.get(url);

    if (request.getStatus() === 200) {
    return response;
    } else {
    throw "HTTP Error: " + request.getStatus() + " - " + response;
    }
    ```

    it keeps returning "UNAUTHORIZED"

    When i manually set the token it works like a charm.

    appreciate the help in this or tell me the correct wy to use oauth2 in zabbix.
  • focar
    Junior Member
    • Dec 2023
    • 17

    #2
    Ok, scratch that, it is working. it was the item test that was failing. the actual script item seems to work... (the second code)

    Comment

    Working...