Ad Widget

Collapse

best practice to update a macro value from script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ignace_Frometon
    Junior Member
    • Dec 2021
    • 26

    #1

    best practice to update a macro value from script

    Hello there :-)

    here is what i want to achieve : i need to retreive a authentification token from an API, then use this token in several http agent items

    so i configured a script called SCRIPTNAME in zabbix frontend, containing a curl command like this :


    Code:
    curl -s -X POST -H 'Content-Type: application/json' -d '{"user":"myUser","password":"myPassword"}' http://{HOST.IP}:8080/api | jq '.token' --raw-output

    the script is configured to be played on zabbix proxy (remote commands enabled in configuration file).

    when i set up the script to be manually executed, and i execute it on a host, it works.


    now i need to update a macro with the value returned by the script.

    so i set up the script as "manual event action", and configured the macro in my template like this

    name : {$AUTH}
    value : system.run[{$SCRIPTNAME}]

    but when i use {$AUTH} in a http agent item header, the test returns a 401 code.

    What would be the best approach to do this ?​
  • Ignace_Frometon
    Junior Member
    • Dec 2021
    • 26

    #2
    here is the workaround i found.

    i created a script item which update host macros every 15 minutes.

    item parameters :

    name : hostName
    value : {HOST.NAME}

    name : hostIp
    value : {HOST.IP}


    it runs the following JS code :

    Code:
    var obj = JSON.parse(value);
    const hostIp = obj.hostIp;
    const hostName = obj.hostName;
    const user = "user01";
    const userPassword = "user01Password";
    const APIurl = "http://" + hostIp + ":8080/Auth";
    const zabbixUrl = 'http://10.10.10.1/zabbix/api_jsonrpc.php';
    const zabbixUser = "api";
    const zabbixPassword = "zabbixApiPassword";
    const macroName = '{$' + 'API_REST_TOKEN' + '}';
    
    
    // Get authentication Token from REST API
    var reqDivaltoApiAuth = new CurlHttpRequest();
    reqDivaltoApiAuth.AddHeader('Content-Type: application/json');
    var json_divalto_auth_body = {
        user: user,
        password: userPassword
    };
    var json_divalto_auth_post_data = JSON.stringify(json_divalto_auth_body);
    var response_divalto_auth = JSON.parse(reqDivaltoApiAuth.Post(APIurl, json_divalto_auth_post_data));
    var macroValue = response_divalto_auth.access_token;
    
    // Authenticate with Zabbix API
    var reqZabbixApiAuth = new CurlHttpRequest();
    reqZabbixApiAuth.AddHeader('Content-Type: application/json');
    var json_authZabbixApi_body = {
        jsonrpc: '2.0',
        method: 'user.login',
        params: {
            user: zabbixUser,
            password: zabbixPassword
        },
        id: 1,
        auth: null
    };
    var json_authZabbixApi_post_data = JSON.stringify(json_authZabbixApi_body);
    var response_authZabbixApi = JSON.parse(reqZabbixApiAuth.Post(zabbixUrl, json_authZabbixApi_post_data));
    var zabbixToken = response_authZabbixApi.result;
    
    // Get host ID for the current host
    var reqGetHostId = new CurlHttpRequest();
    reqGetHostId.AddHeader('Content-Type: application/json');
    var json_host_body = {
        jsonrpc: '2.0',
        method: 'host.get',
        params: {
            output: 'extend',
            filter: {
                host: hostName
            }
        },
        id: 1,
        auth: zabbixToken
    };
    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;
    
    // Get macro ID
    var reqGetMacroId = new CurlHttpRequest();
    reqGetMacroId.AddHeader('Content-Type: application/json');
    var json_macro_body = {
        jsonrpc: '2.0',
        method: 'usermacro.get',
        params: {
            output: 'extend',
            filter: {
                macro: macroName,
                hostid: hostId
            }
        },
        id: 1,
        auth: zabbixToken
    };
    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 API REST token
        var reqCreateMacro = new CurlHttpRequest();
        reqCreateMacro.AddHeader('Content-Type: application/json');
        var json_macro_create_body = {
            jsonrpc: '2.0',
            method: 'usermacro.create',
            params: {
                hostid: hostId,
                macro: macroName,
                value: macroValue
            },
            id: 1,
            auth: zabbixToken
        };
        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 CurlHttpRequest();
        reqUpdateMacro.AddHeader('Content-Type: application/json');
        var json_macro_update_body = {
            jsonrpc: '2.0',
            method: 'usermacro.update',
            params: {
                hostmacroid: macroId,
                value: macroValue
            },
            id: 1,
            auth: zabbixToken
        };
        var json_macro_update_post_data = JSON.stringify(json_macro_update_body);
        reqUpdateMacro.Post(zabbixUrl, json_macro_update_post_data);
        return "macro updated";
    }
    
    // Log out from Zabbix API
    var reqLogout = new CurlHttpRequest();
    reqLogout.AddHeader('Content-Type: application/json');
    var json_logout_body = {
        jsonrpc: '2.0',
        method: 'user.logout',
        params: {},
        id: 1,
        auth: zabbixToken
    };
    var json_logout_post_data = JSON.stringify(json_logout_body);
    reqLogout.Post(zabbixUrl, json_logout_post_data);


    it works as expected :-)
    Last edited by Ignace_Frometon; 10-05-2023, 15:34.

    Comment

    • amanu95
      Junior Member
      • Apr 2023
      • 1

      #3
      Hi evrybody,

      Thanks a lot for your help.

      I update the script code for the new version of zabbix.

      Click image for larger version

Name:	Cattura.png
Views:	1104
Size:	26.3 KB
ID:	481726

      Code:
      var obj = JSON.parse(value);
      
      const mmUrlApi = "https://" + obj.mmurl + "/api/login/login";
      const mmuser = obj.mmuser;
      const mmpass = obj.mmpass;
      
      const zabbixUrl = 'http://127.0.0.1/zabbix/api_jsonrpc.php';
      const zabbixToken = "";  // Zabbix API Token
      
      const hostName = obj.hostName;
      const macroName = '{$' + 'TOKEN' + '}';
      
      
      
      // Get authentication Token from REST API
      var reqMultimonitorApiAuth = new HttpRequest();
      reqMultimonitorApiAuth.addHeader('Content-Type: application/json');
      var json_Multimonitor_auth_body = {
          username: mmuser,
          password: mmpass
      };
      var json_multimoniotr_auth_post_data = JSON.stringify(json_Multimonitor_auth_body);
      var response_multimoniotr_auth = JSON.parse(reqMultimonitorApiAuth.post(mmUrlApi, json_multimoniotr_auth_post_data));
      var macroValue = response_multimoniotr_auth.token;
      
      
      // Get host ID for the current host
      var reqGetHostId = new HttpRequest();
      reqGetHostId.addHeader('Content-Type: application/json');
      var json_host_body = {
          jsonrpc: '2.0',
          method: 'host.get',
          params: {
              output: 'extend',
              filter: {
                  host: hostName
              }
          },
          id: 1,
          auth: zabbixToken
      };
      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;
      
      
      // Get macro ID
      var reqGetMacroId = new HttpRequest();
      reqGetMacroId.addHeader('Content-Type: application/json');
      var json_macro_body = {
          jsonrpc: '2.0',
          method: 'usermacro.get',
          params: {
              output: 'extend',
              filter: {
                  macro: macroName,
                  hostid: hostId
              }
          },
          id: 1,
          auth: zabbixToken
      };
      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 API REST token
          var reqCreateMacro = new HttpRequest();
          reqCreateMacro.addHeader('Content-Type: application/json');
          var json_macro_create_body = {
              jsonrpc: '2.0',
              method: 'usermacro.create',
              params: {
                  hostid: hostId,
                  macro: macroName,
                  value: macroValue
              },
              id: 1,
              auth: zabbixToken
          };
          var json_macro_create_post_data = JSON.stringify(json_macro_create_body);
          reqCreateMacro.post(zabbixUrl, json_macro_create_post_data);
          return "macro created " + macroValue;
      }
      
      else {
      // Update macro macro value with the API REST token
          var reqUpdateMacro = new HttpRequest();
          reqUpdateMacro.addHeader('Content-Type: application/json');
          var json_macro_update_body = {
              jsonrpc: '2.0',
              method: 'usermacro.update',
              params: {
                  hostmacroid: macroId,
                  value: macroValue
              },
              id: 1,
              auth: zabbixToken
          };
          var json_macro_update_post_data = JSON.stringify(json_macro_update_body);
          reqUpdateMacro.post(zabbixUrl, json_macro_update_post_data);
          return "macro updated " + macroValue;
      }

      Comment

      Working...