# -*- coding:utf-8 -*- import json import urllib2 from urllib2 import URLError #import sys #import xlrd class ZabbixTools: def __init__(self): self.url = 'http://10.11.0.215/zabbix/api_jsonrpc.php'] self.header = {"Content-Type": "application/json"} # 登陸驗(yàn)證 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 # 獲取主機(jī) def host_get(self): data = json.dumps({ "jsonrpc": "2.0", "method": "host.get", "params": { "output": ["hostid", "host"], "selectInterfaces": ["interfaceid", "ip"] }, "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() lens = len(response['result']) if lens > 0: return response['result'] else: return "" #################################################################################### # -*- coding: utf-8 -*- import xlrd import xlwt #xlutils結(jié)合xlrd可以達(dá)到修改excel文件目的 from xlutils.copy import copy from openpyxl import Workbook from openpyxl import load_workbook from openpyxl.writer.excel import ExcelWriter ''' 1.類的內(nèi)部調(diào)用:self.<方法名>(參數(shù)列表)。 2.在類的外部調(diào)用:<實(shí)例名>.<方法名>(參數(shù)列表)。 注意:以上兩種調(diào)用方法中,提供的參數(shù)列表中都不用包括self。 ''' class ExcelOperation(object): ''' 設(shè)置默認(rèn)參數(shù):rowsize colsize ,創(chuàng)建對(duì)象時(shí)可以寫、可不寫默認(rèn)參數(shù) ''' def __init__(self, filename, rowsize=0, colsize=0): self.filename = filename self.rowsize = rowsize self.colsize = colsize def open_excel(self): return xlrd.open_workbook(self.filename) ''' table = data.sheets()[0] #通過索引順序獲取 table = data.sheet_by_index(0) #通過索引順序獲取 table = data.sheet_by_name(u'Sheet1')#通過名稱獲取 ''' def read_excel(self): workbook = self.open_excel() sheet = workbook.sheet_by_index(0) nrows = sheet.nrows ncols = sheet.ncols data = [] for loop in range(0,nrows): data.append(sheet.row_values(loop)) return data def write_excel(self): wbk = xlwt.Workbook() sheet = wbk.add_sheet('sheet1') # 第0行第一列 寫入內(nèi)容 sheet.write(0,1,'test ') wbk.save('test.xls') def read_write_excel(self): ''' 4.openpyxl可以對(duì)excel文件進(jìn)行讀寫操作 from openpyxl import Workbook from openpyxl import load_workbook from openpyxl.writer.excel import ExcelWriter workbook_ = load_workbook(u"新歌檢索失敗1477881109469.xlsx") sheetnames =workbook_.get_sheet_names() #獲得表單名字 print sheetnames sheet = workbook_.get_sheet_by_name(sheetnames[0]) print sheet.cell(row=3,column=3).value sheet['A1'] = '47' workbook_.save(u"新歌檢索失敗1477881109469_new.xlsx") wb = Workbook() ws = wb.active ws['A1'] = 4 wb.save("新歌檢索失敗.xlsx") :return: ''' workbook_ = load_workbook(u'test.xlsx') sheetnames = workbook_.get_sheet_names() sheet = workbook_.get_sheet_by_name(sheetnames[0]) print sheet.cell(row=3, column=3).value ################################################################################## # -*- coding: utf-8 -*- from testZabbixApi import ZabbixTools from pyExcel import ExcelOperation # 程序的入口 if __name__ == "__main__": #實(shí)例化excel對(duì)象 tab = ExcelOperation('主機(jī)列表.xlsx') data = tab.read_excel() dirc = {'item{}'.format(i): x for i, x in enumerate(data)} #print dirc['item45'][6] # 實(shí)例化ZabbixTools對(duì)象 test = ZabbixTools() result = test.host_get() for key in dirc: flag = 'F' for i in range(len(result)): if dirc[key][6].strip() == result[i]["interfaces"][0]["ip"]: flag = 'T' if flag == 'T': #print(dirc[key][6] + " zabbix") pass else: print(dirc[key][6] + "not add zabbix")
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前文章:ZABBIXAPI自動(dòng)檢查主機(jī)-創(chuàng)新互聯(lián)
地址分享:http://jinyejixie.com/article10/ddegdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、App開發(fā)、手機(jī)網(wǎng)站建設(shè)、域名注冊(cè)、ChatGPT、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容