Adding a monitoring template to a host via the Zabbix API

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.