The script contents are as follows;
#!/usr/bin/python#-*- coding:utf8 -*-import json,sys,argparsefrom zabbix_api import ZabbixAPIserver = "Http://172.16.206.128/zabbix" username = "Admin" password = "Zabbix" Zapi = zabbixapi (server=server, path= "", log_level=0) Zapi.login (username, password) Def get_args (): parser = argparse. Argumentparser () parser.add_argument ("-H", "--host", help= "Host name") parser.add_argument ("-T", "--templates", help= "Template name") # parse the parameters passed in args = parser.parse_args () if not args.host: args.host = Raw_input (' host: ') if not args.templates: args.templates = raw_inpUT (' templates: ') return argsdef get_host_id (host): get_host_id = zapi.host.get ( { "Output": "HostID", "Filter": { "Host": Host.split (",") } }) host_id = [] host_id.append ([i[' HostID '] for i in  GET_HOST_ID]) return host_id[0]def get_templates_id (templates): templates_id = zapi.template.get ( { "Output":   " TemplateID ", " Filter ": { "Host": Templates.split (",") } }) return templates_id def template_massadd (template_id,host_id): template_add = Zapi.template.massadd ( { "Templates": template_id, "Hosts": host_id }) return "Host add template success" if __name__ == "__main__": args = get_ Args () host_id = get_host_id (args.host) template_id = get_templates_id ( args.templates) if len (host_id) == len (Args.host.split (', ')): if len (template_id) == len (Args.templates.split (', ')) : print template_massadd (Template_ ID,HOST_ID) else: print "Template not exist" else: print "Host not exist"
The script needs to pass two parameters: Host (hostname) and templates (template name), the host can be multiple, with a comma "," separated, template can also be multiple, the same comma "," separated.
Usage:
Python template_massadd.py--host= "abc,client"--"templates" = "template OS linux,templates App MySQL" host add template Success
This article is from the "Zengestudy" blog, make sure to keep this source http://zengestudy.blog.51cto.com/1702365/1853581
Adding a monitoring template to a host via the Zabbix API