#!/usr/bin/python3 from zabbix_utils import ZabbixAPI from datetime import datetime # Zabbix server details zabbix_url = "https://student-XX-zbxtr-YYYY.zabbix.training" api = ZabbixAPI(url=zabbix_url) api.login(token="") print("Connected to Zabbix API Version %s" % api.api_version()) # Read file file = open('/usr/lib/zabbix/apiscripts/hosts.txt').read().splitlines() # Create a host group host_group = api.hostgroup.create(name="Imported hosts") # Create hosts for line in file: line_list = line.split() host_ip = line_list[0] host_name = line_list[1] host_dns = line_list[2] if len(line_list) > 2 else None # Check if host_dns is present # Use host_name as dns if host_dns is missing if host_dns: useip = 0 else: host_dns = host_name useip = 0 try: result = api.host.create( host=host_name, groups=[{"groupid": host_group['groupids'][0]}], interfaces=[{ "type": 1, "main": 1, "useip": useip, "ip": host_ip, "dns": host_dns, "port": 10050 }] ) except: print('host ' + host_name + ' not created: ' + str(e)) else: print('host ' + host_name + ' created: ' + str(result))