Ad Widget

Collapse

Updating Macro with Javascript and API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GCZabb
    Junior Member
    • Feb 2024
    • 1

    #1

    Updating Macro with Javascript and API

    I am attempting to update user macros using javacript and API. I am not receiving any errors and getting a success message. The code follows the if logic correctly based on if the macro id is set however the macro id does not get created or updated when the script runs. I have tried using Zabbix.log in several sections of the code but struggle to get log data. When I run similar code with python I have no issues updating or creating the macros.

    Does anything stand out as wrong in the code below? The variables are removed from the code.

    Code:
    
    // Get host ID for the current host
    var reqGetHostId = new HttpRequest();
         reqGetHostId.addHeader('Authorization: Bearer ' + zTok);
         reqGetHostId.addHeader('Content-Type: application/json-rpc');
     
    var json_host_body = {
        jsonrpc: '2.0',
        method: 'host.get',
        params: {
            output: 'extend',
            filter: {
                host: hostName
            }
        },
        id: 1,
    
    };
    var json_host_post_data = JSON.stringify(json_host_body);
    var response_host = JSON.parse(reqGetHostId.post(zabbixUrl, json_host_post_data));
    var hostId = response_host.result[0].hostid;
    
      // return hostId
    
    // Get macro ID
    var reqGetMacroId = new HttpRequest();
    reqGetMacroId.addHeader('Authorization: Bearer ' + zabbixTok);
    reqGetMacroId.addHeader('Content-Type: application/json-rpc');
    var json_macro_body = {
        jsonrpc: '2.0',
        method: 'usermacro.get',
        params: {
            output: 'extend',
            filter: {
                macro: macroName,
                hostid: hostId
            }
        },
        id: 1,
        
    };
    var json_macro_post_data = JSON.stringify(json_macro_body);
    var response_macro = JSON.parse(reqGetMacroId.post(zabbixUrl, json_macro_post_data));
    var macroId = response_macro.result[0] ? response_macro.result[0].hostmacroid : null;
    
    
    if (!macroId) {
     // create macro value with the CW host config ID
    
        var reqCreateMacro = new HttpRequest();
        reqCreateMacro.addHeader('Authorization: Bearer ' + zabbixTok);
        reqCreateMacro.addHeader('Content-Type: application/json-rpc');
        var json_macro_create_body = {
            jsonrpc: '2.0',
            method: 'usermacro.create',
            params: {
                hostid: hostId,
                macro: macroName,
                value: macroValue
            },
            id: 1,
            
        };
        var json_macro_create_post_data = JSON.stringify(json_macro_create_body);
        reqCreateMacro.post(zabbixUrl, json_macro_create_post_data);
    
        return "macro created";
    } else {
    // Update macro macro value with the API REST token
        var reqUpdateMacro = new HttpRequest();
        reqUpdateMacro.addHeader('Authorization: Bearer ' + zabbixTok);;
        reqUpdateMacro.addHeader('Content-Type: application/json-rpc');
        var json_macro_update_body = {
            jsonrpc: '2.0',
            method: 'usermacro.update',
            params: {
                hostmacroid: macroId,
                value: macroValue
            },
            id: 1,
            
        };
        var json_macro_update_post_data = JSON.stringify(json_macro_update_body);
    
    
        reqUpdateMacro.post(zabbixUrl, json_macro_update_post_data);
      
        if (reqUpdateMacro.getStatus() != 200) {
            throw 'request to ' + url + ' returned unexpected HTTP status code: ' + reqUpdateMaco.getStatus();
        }
    
        return "macro updated";
    }
Working...