添加一个Host Name=Mail CAS_1.1,IP=10.16.3.4的主机,并加入组groupID=30,连接模板templateID=10132.
# -*- coding: UTF-8 -*- import urllib2,json,cookielib,urllib from urllib2 import Request, urlopen, URLError, HTTPError global auth_code,zabbix_url,zabbix_header #zabbix接口地址、登录地址、图片地址 zabbix_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) for key in zabbix_header: request.add_header(key,zabbix_header[key]) try: result = urllib2.urlopen(request) except HTTPError, e: print ‘The server couldn\‘t fulfill the request, Error code: ‘, e.code except URLError, e: print ‘We failed to reach a server.Reason: ‘, e.reason else: response=json.loads(result.read()) #print response result.close() #判断SESSIONID是否在返回的数据中 if ‘result‘ in response: auth_code=response[‘result‘] else: print response[‘error‘][‘data‘] def Http_access(data): request = urllib2.Request(zabbix_url,data) for key in zabbix_header: request.add_header(key,zabbix_header[key]) result = urllib2.urlopen(request) response = json.loads(result.read()) # print result.read() # print response result.close() if len(response[‘result‘]) > 0: return response[‘result‘] #配置需要添加的host信息(Host Name,IP,groupID,templateID) def add_hostdata(): if len(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": "30" #group-live-uxdata-windows } ], "templates": [ { "templateid": "10132" #template ID } ] }, "auth": auth_code, "id": 1 }) return host_data hostdata =add_hostdata() print Http_access(hostdata) #添加主机#返回信息:{u‘jsonrpc‘: u‘2.0‘, u‘result‘: {u‘hostids‘: [u‘10430‘]}, u‘id‘: 1}
获得名为template-windows-basic的模板的templateID:
def Http_access(data): request = urllib2.Request(zabbix_url,data) for key in zabbix_header: request.add_header(key,zabbix_header[key]) result = urllib2.urlopen(request) response = json.loads(result.read()) # print result.read() #print response result.close() if len(response[‘result‘]) > 0: return response[‘result‘] #定义模板名称 def get_template(): template_data = json.dumps({ "jsonrpc": "2.0", "method": "template.get", "params": { "output": "extend", "filter": { "host": [ "template-windows-basic" #定义模板名称 ] } }, "auth": auth_code, "id": 1 }) return template_data templatedata = get_template() print Http_access(templatedata)[0][‘templateid‘] #返回值为10132
获取所有template:
def Http_access(data): request = urllib2.Request(zabbix_url,data) for key in zabbix_header: request.add_header(key,zabbix_header[key]) result = urllib2.urlopen(request) response = json.loads(result.read()) # print result.read() #print response result.close() if len(response[‘result‘]) > 0: return response[‘result‘] #过滤所有模板 def get_template(): template_data = json.dumps({ "jsonrpc": "2.0", "method": "template.get", "params": { "output": "extend" }, "auth": auth_code, "id": 1 }) return template_data templatedata = get_template() for template in Http_access(templatedata): print template[‘templateid‘],template[‘host‘]
返回如下:
10130 template-web-basic
10131 template-windows-basic2
10132 template-windows-basic
10133 template-windows-Controller1
10134 template-Hyper-V
......
......
时间: 2024-10-10 00:26:07