Ad Widget

Collapse

Zabbix API from within script item

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • m.soender
    Junior Member
    • Apr 2020
    • 2

    #1

    Zabbix API from within script item

    I'm working on a script where I would like to store an access token from a webservice in a macro on my host where the script item is created from. I can retrieve the access token from my webservice but when I would like to use the Zabbix API to update the macro on my host I'm then not having as much success. When testing the script with the zabbix_js application it works fine, but when executed from within the script item I'm getting "Error: cannot get URL: Couldn't connect to server." as a return from my HTTP request from the script. Below is the script listed:

    Code:
    //Zabbix token updater
    var req = new HttpRequest();
    var result = "0";
    var macroValue;
    const zabbixUrl = 'http://127.0.0.1:8080/api_jsonrpc.php';
    const zabbixToken = 'b873b224b9444a986b71206fab63f0c69d7330041fd67e43f 9929ab9adb6f0b6'; // Zabbix API Token
    const macroName = '{$' + 'TOKEN' + '}';
    
    //Get Host ID from Zabbix
    try {
    var hostIdFields = {
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
    "filter": {
    "host": [
    "HPE 5GC"
    ]
    }
    },
    "id": 1
    };
    
    req.clearHeader();
    req.addHeader('Content-Type: application/json');
    req.addHeader('Authorization: Bearer '+zabbixToken);
    var respHostId;
    respHostId = req.post('http://127.0.0.1:8080/api_jsonrpc.php', JSON.stringify(hostIdFields));
    
    if (req.getStatus() != 200) {
    Zabbix.log(3,reqHostId.getStatus() );
    throw 'Response code: '+reqHostId.getStatus();
    }
    respHostId = JSON.parse(respHostId);
    Zabbix.log(3, 'hostId: '+respHostId.result[0].hostid);
    result = respHostId.result[0].hostid;
    } catch (error) {
    Zabbix.log(3, 'GET Host ID Error: '+error);
    result = error;
    }
    
    try {
    var jsonFields = {};
    jsonFields.username = "admin";
    jsonFields.password = "myverysecretpassword";
    req.clearHeader();
    req.addHeader('Content-Type: application/json');
    var resp;
    resp = req.post('https://10.0.50.7/core/pls/api/1/auth/login',
    JSON.stringify(jsonFields)
    );
    if (req.getStatus() != 200) {
    throw 'Response code: '+req.getStatus();
    }
    resp = JSON.parse(resp);
    macroValue = resp.access_token;
    //result = macroValue;
    } catch (error) {
    Zabbix.log(3, 'AuthToken failed : '+error);
    result = -99;
    }
    
    
    return result;
    Details on the installed system:
    OS: Oracle Linux 9
    Zabbix: V7.0.6
    zabbix_js 7.0.6
    NGINX as web server



    If there are other ways to interact with the API from the script then I'm very open to suggestions. I know that the code is a little hackish at the moment, it will be cleaned up when I have the interface to Zabbix up and running
  • m.soender
    Junior Member
    • Apr 2020
    • 2

    #2
    Did find the fault. SELinux can be both your friend and your worst enemy. Needed to allow Zabbix to do network traffic since I'm using port 8080 for my Zabbix instance.

    Comment

    Working...