Ad Widget

Collapse

Apply Longitude and Lattitude via Template

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BSDUKJ
    Member
    • Aug 2017
    • 38

    #1

    Apply Longitude and Lattitude via Template

    Hi,

    Just wondering if there is anyway way to populate Location Longitude and Location Lattitude Inventory Values via a Template.

    At the moment I'm having to do this all manually in the Inventory via mass updates but if people decide to clone an item to create a new one and simply and change it's name and IP it takes the location data with it.

    Unlike other items such as OS, Software etc, this is not something that can be automatically pulled as this information is simply not on the objects, be it server, appliance etc.

    My idea would be for machine is a certain location to get their Location Longitude and Location Lattitude Inventory Values updated if they are dropped into a ceratin template.

    Is this possible or is there another way for doing this?

    Cheers

    Jay
  • Hamardaban
    Senior Member
    Zabbix Certified SpecialistZabbix Certified Professional
    • May 2019
    • 2713

    #2
    In the template, you can create an item of the "script" type that will return the necessary data to fill in the inventory fields.

    Comment

    • BSDUKJ
      Member
      • Aug 2017
      • 38

      #3
      Originally posted by Hamardaban
      In the template, you can create an item of the "script" type that will return the necessary data to fill in the inventory fields.
      Apologises I'm not quite sure what you mean?

      Is that a script that you create to populate the inventory fields?

      So the script would contain the Longitude and Lattitude you require?

      I've not done stuff with scripting before on Zabbix so an unsure where to start, any tips?

      Cheers

      Jay

      Comment

      • Hamardaban
        Senior Member
        Zabbix Certified SpecialistZabbix Certified Professional
        • May 2019
        • 2713

        #4


        script for both items like this "return 127.523722" :-)

        Comment

        • Henr0
          Junior Member
          • Jun 2024
          • 1

          #5
          Originally posted by Hamardaban
          In the template, you can create an item of the "script" type that will return the necessary data to fill in the inventory fields.
          OK, so for everyone who has landed here wondering how to solve this problem early on in their Zabbix experience, but walked away frustrated... Here you go.

          What I was trying to do was autopopulate long/lat based on the proxy server that discovered the device. I have a cloud hosted Zabbix server and 3 proxies installed at 3 different sites, actively connected to the host. So I wanted to automatically assign long/lat values to each device as it was discovered, depending on the proxy. Here is how I went about that:
          1. Data Collection / Templates / Create New Template.
            • I created a template for each site, "Long and Lat Site 1" "Long and Lat Site 2" ... etc.
            • Assign to Template Group "Templates" (do as you will here) and all other fields left alone.
          2. Once you save it, in the list of templates, click "Items" in the row for your newly created template.
            • Create an item "Site 1 Long"
            • Enter a name
            • Pick "script" for type
            • Enter any unique value into "key,"
            • Pick "Numeric (float)" as the type of information
            • click the pencil next to the script field, then type in "return xx.xxxx" which is the word return, a space, then the value for your longitude
            • pick "Location Longitude" from the "Populates host inventory field" selection.
            • You might opt to edit the history and trends storage to disable them, but as you wish Save this.
          3. Create another item, just as before, but this is for your latitude. Name it appropriately, then change the value you return, and pick "Location Latitude" from the host inventory list. Everything else is the same.
          4. Now, edit your second trigger, doing the same thing, making items for longitude and latitude as proper for each trigger (site).
          That's it. You will also have to explode your host discovery rules to have a unique rule per site. Just clone your existing rule and change the name and subnet.
          Then, edit your discovery actions, and clone those so you have one per site for each type of action as well, and then edit those actions. You will add a condition to check where "Proxy equals" and select the proxy for your site, and then edit operations and add one template to the template already there to correspond to the site. Here I added operations to assign hosts to a particular host group (named after the site), I'm adding some custom host tags, and I add +1 template to the "Link template" options and I'm done.

          This will then update all discovered clients and affect all new ones.

          I'm sure I didn't explain this well enough. I'm typing it in fresh after learning how templates actually work, so I'm all jazzed to go set up some more things that I suddenly know how to do now.

          Good luck out there. If anyone finds this and gets stuck at a particular spot, I'll be into Zabbix for awhile I hope I'd be around to reply and help.

          H

          Comment


          • Brain2000
            Brain2000 commented
            Editing a comment
            Very nice! To make that even easier, assuming we can use a template in a script, you could do a "return {$LATITUDE}" or "return {$LONGITUDE}" and then define that on a template upstream. Then you only have to create one template to script the lat/lon.
        • LucasHokerberg
          Junior Member
          • Apr 2024
          • 8

          #6
          Thanks!

          I ended up creating one template named "Location" and added the two script items (longitude and latitude) in here. As Brain2000 suggested, the scripts returns {$LATITUDE} and {$LONGITUDE}.
          I then created one additional template per site to which I added the "Location" template to. In that way, all site templates will inherit the script items from the base templaste ("Location").
          Each site template then have the macros configured to actually set the coordinates.

          A side note - make sure the host has inventory set to "Automatic".

          Comment

          • Alban
            Junior Member
            • Feb 2025
            • 1

            #7
            For those needing to dynamically assign latitude and longitude to hosts in Zabbix, here’s a solution using Zabbix Agent 2, ensuring each host reports its own location based on its public IP.
            1. Install Zabbix Agent 2 on the host

            2. Deploy a Script to Retrieve Coordinates
            sudo nano /etc/zabbix/get_location.sh

            4. Add this content:
            #!/bin/bash
            IP=$(curl -s https://ifconfig.me/ip)
            GEO_DATA=$(curl -s "http://ip-api.com/json/$IP")
            LAT=$(echo $GEO_DATA | jq '.lat')
            LON=$(echo $GEO_DATA | jq '.lon')

            if [[ "$LAT" != "null" && "$LON" != "null" ]]; then
            echo "$LAT" > /tmp/zabbix_latitude.txt
            echo "$LON" > /tmp/zabbix_longitude.txt
            fi


            5. Make executable
            sudo chmod +x /etc/zabbix/get_location.sh

            6. Automate Execution
            Edit the cron job to run the script every hour:bash
            crontab -e
            Add the following line:
            0 * * * * /etc/zabbix/get_location.sh

            7. Configure Zabbix Agent 2 to Read Data
            Edit the Zabbix Agent 2 configuration file:bash
            sudo nano /etc/zabbix/zabbix_agent2.conf
            Add these lines:
            UserParameter=latitude,cat /tmp/zabbix_latitude.txt
            UserParameter=longitude,cat /tmp/zabbix_longitude.txt

            Restart the agent:
            sudo systemctl restart zabbix-agent2

            8. Configure Zabbix to Store Data
            In Zabbix Web UI (Configuration → Hosts → Items):
            • Create a "Latitude" item
              • Type: Zabbix Agent (passive or active depending on your config)
              • Key: latitude
              • Type of information: Numeric (float)
              • Populates host inventory field: Location Latitude
            • Create a "Longitude" item
              • Type: Zabbix Agent (passive or active depending on your config)
              • Key: longitude
              • Type of information: Numeric (float)
              • Populates host inventory field: Location Longitude
            9. Verify Data
            Go to MonitoringLatest Data and check if latitude and longitude are updated for each host.

            10. If it works, you can create a Template with your items and deploy to all hosts

            This setup ensures that each host dynamically reports its own geographical location, making it available for mapping in Grafana Worldmap (my usage)



            Hope it helps
            Attached Files

            Comment

            • Megachip
              Junior Member
              • May 2025
              • 16

              #8
              Ive getting long and lat via snmp.sysloc ... at the end of the text, the koordes following in [] seperated via , . Is there an easy way to parse it?

              Comment


              • Hamardaban
                Hamardaban commented
                Editing a comment
                By preprocessing step type regexp .

              • Megachip
                Megachip commented
                Editing a comment
                Hamardaban I found preprocessing, I defined the regexp ... but how to assign it to lat/long of an host?
            Working...