Zabbix add host, get template TemplateID

Source: Internet
Author: User

Add a host Name=mail cas_1.1,ip=10.16.3.4, and join the group groupid=30, connect the template templateid=10132.

#-*-coding:utf-8-*-ImportUrllib2,json,cookielib,urllib fromUrllib2ImportRequest, Urlopen, Urlerror, HttperrorGlobalAuth_code,zabbix_url,zabbix_header#Zabbix interface Address, login address, picture addressZabbix_url="http://10.16.2.4/zabbix/api_jsonrpc.php"Zabbix_header= {"Content-type":"Application/json"}zabbix_user="Admin"Zabbix_pass="Password"Auth_code=""Auth_data=Json.dumps ({"Jsonrpc":"2.0",    "Method":"User.login",    "params":            {            "User": Zabbix_user,"Password": Zabbix_pass},"ID": 0}) Request=Urllib2. Request (Zabbix_url,auth_data) forKeyinchZabbix_header:request.add_header (Key,zabbix_header[key])Try: Result=Urllib2.urlopen (Request)exceptHttperror, E:Print 'the server couldn\ ' t fulfill the request, Error code:', E.codeexceptUrlerror, E:Print 'We failed to reach a server. Reason:', E.reasonElse: Response=json.loads (Result.read ())#Print Responseresult.close ()#determine if the SessionID is in the returned dataif  'result'  inchResponse:auth_code=response['result']Else:    Printresponse['Error']['Data']defhttp_access (data): Request=Urllib2. Request (Zabbix_url,data) forKeyinchZabbix_header:request.add_header (Key,zabbix_header[key]) result=Urllib2.urlopen (Request) Response=json.loads (Result.read ())#print result.read ()   # Print Response result.close ()ifLen (response['result']) >0:returnResponse[' Result ']

#配置需要添加的host信息 (Host name,ip,groupid,templateid)defadd_hostdata ():ifLen (Auth_code) <>0:host_data=Json.dumps ({"Jsonrpc":"2.0", "Method":"host.create", "params": { "Host":"Mail cas_1.41",#Host Name "Interfaces": [ { "type": 1, "Main": 1, "Useip": 1, "IP":"10.16.3.4",#Host IP "DNS":"", "Port":"10050" } ], "groups": [ { "GroupID":" -" #group-live-uxdata-windows } ], "Templates": [ { "TemplateID":"10132" #Template ID } ] }, "Auth": Auth_code,"ID": 1 }) returnHost_dataHostdata=Add_hostdata ()PrintHttp_access (Hostdata)#添加主机
#返回信息: {u ' jsonrpc ': U ' 2.0 ', U ' result ': {u ' hostids ': [u ' 10430 ']}, U ' ID ': 1}

Get the TemplateID of the template named Template-windows-basic:

defhttp_access (data): Request=Urllib2. Request (Zabbix_url,data) forKeyinchZabbix_header:request.add_header (Key,zabbix_header[key]) result=Urllib2.urlopen (Request) Response=json.loads (Result.read ())#print result.read ()    #Print Responseresult.close ()ifLen (response['result']) >0:returnresponse['result']#定义模板名称defget_template (): Template_data=Json.dumps ({"Jsonrpc":"2.0",        "Method":"Template.get",        "params": {            "Output":"Extend",            "Filter": {                "Host": [                    "Template-windows-basic" #Define template Name                ]            }        },        "Auth": Auth_code,"ID": 1    })    returnTemplate_datatemplatedata=get_template ()PrintHttp_access (TemplateData) [0]['TemplateID']#返回值为10132

Get all Template:

defhttp_access (data): Request=Urllib2. Request (Zabbix_url,data) forKeyinchZabbix_header:request.add_header (Key,zabbix_header[key]) result=Urllib2.urlopen (Request) Response=json.loads (Result.read ())#print result.read ()    #Print Responseresult.close ()ifLen (response['result']) >0:returnresponse['result']#过滤所有模板defget_template (): Template_data=Json.dumps ({"Jsonrpc":"2.0",        "Method":"Template.get",        "params": {            "Output":"Extend"        },        "Auth": Auth_code,"ID": 1    })    returnTemplate_datatemplatedata=get_template () forTemplateinchhttp_access (TemplateData):Printtemplate['TemplateID'],template['Host']

Return as follows:

10130 Template-web-basic
10131 Template-windows-basic2
10132 Template-windows-basic
10133 Template-windows-controller1
10134 template-hyper-v

......

......

Zabbix add host, get template TemplateID

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.