Ad Widget

Collapse

Javascript in preprocessing using fetch() function.

Collapse
This topic has been answered.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vijayk
    Senior Member
    • May 2023
    • 305

    #1

    Javascript in preprocessing using fetch() function.

    Hello,

    Anyone have idea How to use fetch() function in preprocessing to return the value from json data?

    I have tried but script showing syntax error. But while run the script in server console with node command it will return the result as I expect.
  • Answer selected by vijayk at 02-10-2023, 13:47.
    dimir
    Zabbix developer
    • Apr 2011
    • 1080

    Untested, but hope you will get the idea:

    Code:
    var result = "";
    ​
    try {
        var req = new HttpRequest();
        var url = 'https:/example.com';
        var resp = req.get(url);
    
        if (req.getStatus() != 200) {
            throw 'request to ' + url + ' returned unexpected HTTP status code: ' + req.getStatus();
        }
    
        resp = JSON.parse(resp); // converts response to JSON
        result = resp.something; // gets the value of "something"
    } catch (error) {
        Zabbix.log(3, 'Preprocessing failed: ' + error);
    }
    
    return result;​
    Last edited by dimir; 02-10-2023, 14:21.

    Comment

    • dimir
      Zabbix developer
      • Apr 2011
      • 1080

      #2
      In preprocessing you should be using the HttpRequest class that is provided by Zabbix for fetching data over HTTP protocol.

      Comment

      • vijayk
        Senior Member
        • May 2023
        • 305

        #3
        Do you have any example? I read it but not able to clearly understand.

        Comment

        • dimir
          Zabbix developer
          • Apr 2011
          • 1080

          #4
          Untested, but hope you will get the idea:

          Code:
          var result = "";
          ​
          try {
              var req = new HttpRequest();
              var url = 'https:/example.com';
              var resp = req.get(url);
          
              if (req.getStatus() != 200) {
                  throw 'request to ' + url + ' returned unexpected HTTP status code: ' + req.getStatus();
              }
          
              resp = JSON.parse(resp); // converts response to JSON
              result = resp.something; // gets the value of "something"
          } catch (error) {
              Zabbix.log(3, 'Preprocessing failed: ' + error);
          }
          
          return result;​
          Last edited by dimir; 02-10-2023, 14:21.

          Comment

          • vijayk
            Senior Member
            • May 2023
            • 305

            #5
            Originally posted by dimir
            Untested, but hope you will get the idea:

            Code:
            var result = "";
            ​
            try {
            var req = new HttpRequest();
            var resp = req.get('https:/example.com');
            
            if (req.getStatus() != 200) {
            throw 'Response code: ' + req.getStatus();
            }
            
            resp = JSON.parse(resp); // converts response to JSON
            result = resp.something; // gets the value of "something"
            } catch (error) {
            Zabbix.log(4, 'jira issue creation failed : '+error);
            }
            
            return result;​
            dimir, thanks got the hint. Now I'm able to get what i want.

            Comment

            Working...