通過zabbix自帶api進行主機的批量添加操作
阿新 • • 發佈:2017-09-30
3.2 name 批量添加 監控 界面 eth 實例化 fail 我們
通過zabbix自帶api進行批量添加主機
我們需要監控一臺服務器的時候,當客戶端裝好zabbix-agent端並正確配置以後,需要在zabbix-server的web gui界面進行添加zabbix-agent操作,填寫host_name,訪問名,ip地址,端口還有模板等信息
這樣操作比較方便,但當我們需要添加的主機一次性有上百臺甚至前臺的時候這種方式效率就比較低了,一般有如下解決方式:
1.通過自動發現,自動註冊主機
2.通過zabbix_api的方式進行批量操作
今天我們使用第二種方式進行操作,定制性較強,不容易出錯
主要是通過讀取excel主機列表信息,發送post請求給zabbix的api進行主機添加操作
環境:
zabbix版本: zabbix3.0.4
python版本:python2.7
api參考文檔
# https://www.zabbix.com/documentation/3.0/manual/api
host列表信息如下
批量添加主機的zabbix_api代碼:
#coding:utf-8 import json import urllib2 from urllib2 import URLError import sys import xlrd class ZabbixTools: def __init__(self): self.url = ‘http://192.168.3.200/zabbix/api_jsonrpc.php‘ self.header = {"Content-Type":"application/json"} # 登陸驗證 def user_login(self): data = json.dumps({ "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "zabbix" }, "id": 0 }) request = urllib2.Request(self.url, data) for key in self.header: request.add_header(key, self.header[key]) try: result = urllib2.urlopen(request) except URLError as e: print "Auth Failed, please Check your name and password:", e.code else: response = json.loads(result.read()) result.close() self.authID = response[‘result‘] return self.authID # 獲取主機 def host_get(self,hostName): data = json.dumps({ "jsonrpc":"2.0", "method":"host.get", "params":{ "output":["hostid","name"], "filter":{"host":hostName} }, "auth":self.user_login(), "id":1, }) request = urllib2.Request(self.url, data) for key in self.header: request.add_header(key, self.header[key]) try: result = urllib2.urlopen(request) except URLError as e: if hasattr(e, ‘reason‘): print ‘We failed to reach a server.‘ print ‘Reason: ‘, e.reason elif hasattr(e, ‘code‘): print ‘The server could not fulfill the request.‘ print ‘Error code: ‘, e.code else: response = json.loads(result.read()) result.close() print "Number Of %s: " % hostName, len(response[‘result‘]) lens=len(response[‘result‘]) if lens > 0: return response[‘result‘][0][‘name‘] else: return "" # 獲取主機組 def hostgroup_get(self, hostgroupName): data = json.dumps({ "jsonrpc":"2.0", "method":"hostgroup.get", "params":{ "output": "extend", "filter": { "name": [ hostgroupName, ] } }, "auth":self.user_login(), "id":1, }) request = urllib2.Request(self.url, data) for key in self.header: request.add_header(key, self.header[key]) try: result = urllib2.urlopen(request) except URLError as e: print "Error as ", e else: response = json.loads(result.read()) result.close() lens=len(response[‘result‘]) if lens > 0: self.hostgroupID = response[‘result‘][0][‘groupid‘] return response[‘result‘][0][‘groupid‘] else: print "no GroupGet result" return "" # 獲取模板 def template_get(self, templateName): data = json.dumps({ "jsonrpc":"2.0", "method": "template.get", "params": { "output": "extend", "filter": { "host": [ templateName, ] } }, "auth":self.user_login(), "id":1, }) request = urllib2.Request(self.url, data) for key in self.header: request.add_header(key, self.header[key]) try: result = urllib2.urlopen(request) except URLError as e: print "Error as ", e else: response = json.loads(result.read()) result.close() self.templateID = response[‘result‘][0][‘templateid‘] return response[‘result‘][0][‘templateid‘] # 創建host #def host_create(self, hostName,visibleName,hostIp,dnsName,proxyName, hostgroupName, templateName1, templateName2): def host_create(self, hostName,visibleName,hostIp, hostgroupName, templateName1, templateName2): data = json.dumps({ "jsonrpc":"2.0", "method":"host.create", "params":{ "host": hostName, "name": visibleName, #"proxy_hostid": self.proxy_get(proxyName), "interfaces": [ { "type": 1, "main": 1, "useip": 1, "ip": hostIp, "dns": "", "port": "20050" } ], "groups": [ { "groupid": self.hostgroup_get(hostgroupName) } ], "templates": [ { "templateid": self.template_get(templateName1) }, { "templateid": self.template_get(templateName2) } ], }, "auth": self.user_login(), "id":1 }) request = urllib2.Request(self.url, data) for key in self.header: request.add_header(key, self.header[key]) try: result = urllib2.urlopen(request) except URLError as e: print "Error as ", e else: response = json.loads(result.read()) result.close() print "host : %s is created! id is %s\n" % (hostip, response[‘result‘][‘hostids‘][0]) self.hostid = response[‘result‘][‘hostids‘] return response[‘result‘][‘hostids‘] # zabbix_proxy獲取函數 def proxy_get(self, ProxyName): data = json.dumps({ "jsonrpc":"2.0", "method": "proxy.get", "params": { "output": "extend", "selectInterface": "extend", "filter": { "host": [ ProxyName, ] } }, "auth":self.user_login(), "id":1, }) request = urllib2.Request(self.url, data) for key in self.header: request.add_header(key, self.header[key]) try: result = urllib2.urlopen(request) except URLError as e: print "Error as ", e else: response = json.loads(result.read()) result.close() self.templateID = response[‘result‘][0][‘proxyid‘] return response[‘result‘][0][‘proxyid‘] # 程序的入口 if __name__ == "__main__": # 實例化ZabbixTools對象 test = ZabbixTools() #result = test.host_get(‘node5.chinasoft.com‘) #print result # 獲取host列表 workbook = xlrd.open_workbook(‘host_list.xls‘) for row in xrange(workbook.sheets()[0].nrows): hostname=workbook.sheets()[0].cell(row,0).value visible=workbook.sheets()[0].cell(row,1).value hostip=workbook.sheets()[0].cell(row,2).value #dnsname=workbook.sheets()[0].cell(row,3).value #proxy=workbook.sheets()[0].cell(row,4).value hostgroup=workbook.sheets()[0].cell(row,3).value hosttemp=workbook.sheets()[0].cell(row,4).value hosttemp1=workbook.sheets()[0].cell(row,5).value hostgroup=hostgroup.strip() hostnameGet=test.host_get(hostname) #print ‘hostnameget : ‘ + hostnameGet print hostname + ‘,‘ + visible + ‘,‘ + hostip + ‘,‘ + hostgroup + ‘,‘ + hosttemp + ‘,‘ + hosttemp1 if hostnameGet.strip() == ‘‘: #test.host_create(hostname,visible,hostip,dnsname,proxy,hostgroup,hosttemp,hosttemp1) test.host_create(hostname,visible,hostip,hostgroup,hosttemp,hosttemp1) else: print "%s have exist! Cannot recreate !\n" % hostnameGet
通過zabbix自帶api進行主機的批量添加操作