Uploaded image for project: 'ZABBIX BUGS AND ISSUES'
  1. ZABBIX BUGS AND ISSUES
  2. ZBX-24392

I want to add registered hosts as elements in map using zabbix API

XMLWordPrintable

    • Icon: Problem report Problem report
    • Resolution: Fixed
    • Icon: Trivial Trivial
    • None
    • None
    • None
    • None
    • zabbix 6.4
      Mac OS ventura 13.5

      We are developing a script to reflect registered hosts in the map using the zabbix API as described in the title.

      I coded it in python referring to the documentation, but the message *“Host added to map with ID:

      {result[‘sysmapids’][0]}

      ”* which is displayed when a correct API response is returned, is displayed, but the host is not added to the map.

      I am trying to add an element using the map.update method. Or should I use another method to add the element?

      Here is the python code.

      import requests
      import json
      
      class ZabbixAPI:
        def __init__(self, url, token):
          self.url = url
          self.token = token
          self.registered_hosts = set() # 既に登録されたホストのIDを追跡するセット
      
        def get_host_info(self):
          headers = {
            'Content-Type': 'application/json-rpc'
          }
      
          data = {
            'jsonrpc': '2.0',
            'method': 'host.get',
            'params': {
              'output': 'extend'
            },
            'id': 1,
            'auth': self.token
          }
      
          response = requests.post(self.url, data=json.dumps(data), headers=headers, verify=False)
      
          if response.status_code == 200:
            return response.json()['result']
          else:
            print(f"Error: {response.status_code}")
      
        def add_host_to_map(self, map_id):
          # ホストIDを取得
          hosts = self.get_host_info()
          if hosts:
            for host in hosts:
              host_id = host['hostid']
              if host_id not in self.registered_hosts:
                # マップにホストを追加
                self.update_map_with_host(map_id, host_id)
                self.registered_hosts.add(host_id)
              else:
                print(f"Host with ID {host_id} is already registered.")
          else:
            print("No hosts found.")
      
        def update_map_with_host(self, map_id, host_id):
          headers = {
            'Content-Type': 'application/json-rpc'
          }
      
          data = {
            'jsonrpc': '2.0',
            'method': 'map.update',
            'params': {
              'sysmapid': map_id,
              'elements': [
                {
                  'elementtype':0,  # ホストを表す要素
                  'elementid': host_id, # ホストのID
                  'iconid_off': 2,  # ホストのアイコン (オフ) のID
                }
              ]
            },
            'auth': self.token,
            'id': 1
          }
      
          response = requests.post(self.url, data=json.dumps(data), headers=headers, verify=False)
      
          if response.status_code == 200:
            result = response.json().get('result')
            if result and result['sysmapids']:
              print(f"Host added to map with ID: {result['sysmapids'][0]}")
            else:
              print("Error updating map with host.")
          else:
            print(f"Error: {response.status_code}")
      
        def generate_random_coordinate(self, min_value=0, max_value=500):
          import random
          return random.randint(min_value, max_value)
      
      if __name__ == "__main__":
        # Zabbix サーバーの URL
        url = 'http://127.0.0.1/zabbix/api_jsonrpc.php'
        # Zabbix API での認証トークン
        token = ''
        # 追加するホストのマップID
        map_id = '5'
      
        zabbix_api = ZabbixAPI(url, token)
        zabbix_api.add_host_to_map(map_id)
      

            azyla Arkadiusz Zyla
            zu zu
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: