Calling the Zabbix API using Python
#!/usr/bin/env python#Version = 3.6.1#__auth__ = ' Warren 'ImportJSON fromUrllibImportrequest, Parsezabbix_url='Http://192.168.22.22/zabbix'Zabbix_username="Admin"Zabbix_password="123456"#define the host information to be added in list formhostlist=["192.168.22.111","192.168.22.112"]#Traverse host list defines the host group ID and template ID to add forHostinchHostlist:url="{}/api_jsonrpc.php". Format (zabbix_url) header= {"Content-type":"Application/json"} #auth user and passworddata = { "Jsonrpc":"2.0", "Method":"host.create", "params": { "Host": Host,"Interfaces": [ { "type": 1, "Main": 1, "Useip": 1, "IP": Host,"DNS":"", "Port":"10050" } ], "groups": [ { "GroupID":" +" } ], "Templates": [ { "TemplateID":"10081" } ], "Inventory_mode": 0,"Inventory": { "macaddress_a":"01234", "Macaddress_b":"56768" } }, "Auth":"9afc764edb5b6bbd09369f7028231b70", "ID": 1 } #because the API receives a JSON string, you need to convertValue = Json.dumps (data). Encode ('Utf-8') #wrapping the requestreq = Request. Request (URL, Headers=header, data=value)#Verify and obtain the Auth ID Try: #open a wrapped URLresult =Request.urlopen (req)exceptException as E:Print("Auth Failed, please Check Your Name and Password:", E)Else: Response=Result.read ()#The above is the bytes type data, it needs to decode converted into a stringpage = Response.decode ('Utf-8') #Convert This JSON string to a python dictionarypage =json.loads (page) result.close ()#Printing Information Print("Create host successful. The host ID is: {}". Format (Page.get ('result'))) #If added repeatedly, the host ID will be returned Is:none
Zabbix Bulk Add host monitoring-zabbix API calls