Ad Widget

Collapse

Template Item Script help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bboy8012
    Junior Member
    • Oct 2020
    • 12

    #1

    Template Item Script help

    Hi All,

    Try to create a script in a template to get latitude coordinates from the location of the host and populate it on the host. I have the below code but it seems to be failing to run, can someone point me to the right way? Below is exerpt of the script:

    Code:
    var address = obj.location
    var apiKey = obj.apiKey
    
    // Construct the URL for the geocoding API request
    const geocodingUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent(address)}&key=${apiKey}`;
    
    // Make an HTTP GET request to the geocoding API
    fetch(geocodingUrl)
      .then((response) => response.json())
      .then((data) => {
        if (data.status === 'OK' && data.results.length > 0) {
          const location = data.results[0].geometry.location;
          const latitude = location.lat;
    
          obj.latitude(${latitude});
        } else {
          zabbix.log('Geocoding failed for the provided address.');
        }
      })
      .catch((error) => {
        zabbix.log('An error occurred:', error);
      });​
    Click image for larger version

Name:	zabbix-item-request.png
Views:	250
Size:	10.4 KB
ID:	471680
  • adrian_c
    Junior Member
    • Nov 2018
    • 12

    #2
    To access parameters you will need to do something like:

    Code:
    params = JSON.parse(value),
    address = params.location
    ...
    And to make http requests you will need to use javascript object HttpRequest and I don't think the function encodeURIComponent is available.

    See https://www.zabbix.com/documentation...script_objects

    Comment

    Working...