Python封裝zabbix-get接口得代碼分享

Zabbix 是一款強(qiáng)大得開(kāi)源網(wǎng)管監(jiān)控工具,該工具得客戶端與服務(wù)端是分開(kāi)得,我們可以直接使用自帶得zabbix_get命令來(lái)實(shí)現(xiàn)拉取客戶端上得各種數(shù)據(jù),在本地組裝參數(shù)并使用Popen開(kāi)子線程執(zhí)行該命令,即可實(shí)現(xiàn)批量監(jiān)測(cè)。

封裝Engine類: 該類得主要封裝了Zabbix接口得調(diào)用,包括最基本得參數(shù)收集.

import subprocess,datetime,time,mathclass Engine():    def __init__(self,address,port):        self.address = address        self.port = port    def GetValue(self,key):        try:            command = "get.exe -s {0} -p {1} -k {2}".format(self.address,self.port,key).split(" ")            start = datetime.datetime.now()            process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)            while process.poll() is None:                time.sleep(1)                now = datetime.datetime.now()                if (now - start).seconds > 2:                    return 0            return str(process.stdout.readlines()[0].split()[0],"utf-8")        except Exception:            return 0    # ping檢測(cè)    def GetPing(self):        ref_dict = {"Address":0,"Ping":0}        ref_dict["Address"] = self.address        ref_dict["Ping"] = self.GetValue("agent.ping")        if ref_dict["Ping"] == "1":            return ref_dict        else:            ref_dict["Ping"] = "0"            return ref_dict        return ref_dict    # 獲取主機(jī)組基本信息    def GetSystem(self):        ref_dict = { "Address" : 0 ,"HostName" : 0,"Uname":0 }        ref_dict["Address"] = self.address        ref_dict["HostName"] = self.GetValue("system.hostname")        ref_dict["Uname"] = self.GetValue("system.uname")        return ref_dict    # 獲取CPU利用率    def GetCPU(self):        ref_dict = { "Address": 0 ,"Core": 0,"Active":0 , "Avg1": 0 ,"Avg5":0 , "Avg15":0 }        ref_dict["Address"] = self.address        ref_dict["Core"] = self.GetValue("system.cpu.num")        ref_dict["Active"] = math.ceil(float(self.GetValue("system.cpu.util")))        ref_dict["Avg1"] = self.GetValue("system.cpu.load[,avg1]")        ref_dict["Avg5"] = self.GetValue("system.cpu.load[,avg5]")        ref_dict["Avg15"] = self.GetValue("system.cpu.load[,avg15]")        return ref_dict    # 獲取內(nèi)存利用率    def GetMemory(self):        ref_dict = { "Address":"0","Total":"0","Free":0,"Percentage":"0" }        ref_dict["Address"] = self.address        fps = self.GetPing()        if fps['Ping'] != "0":            ref_dict["Total"] = self.GetValue("vm.memory.size[total]")            ref_dict["Free"] = self.GetValue("vm.memory.size[free]")            # 計(jì)算百分比: percentage = 100 - int(Free/int(Total/100))            ref_dict["Percentage"] = str( 100 - int( int(ref_dict.get("Free")) / (int(ref_dict.get("Total"))/100)) ) + "%"            return ref_dict        else:            return ref_dict    # 獲取磁盤(pán)數(shù)據(jù)    def GetDisk(self):        ref_list = []        fps = self.GetPing()        if fps['Ping'] != "0":            disk_ = eval( self.GetValue("vfs.fs.discovery"))            for x in range(len(disk_)):                dict_ = {"Address": 0, "Name": 0, "Type": 0, "Free": 0}                dict_["Address"] = self.address                dict_["Name"] = disk_[x].get("{#FSNAME}")                dict_["Type"] = disk_[x].get("{#FSTYPE}")                if dict_["Type"] != "UNKNOWN":                    pfree = self.GetValue("vfs.fs.size["{0}",pfree]".format(dict_["Name"]))                    dict_["Free"] = str(math.ceil(float(pfree)))                else:                    dict_["Free"] = -1                ref_list.append(dict_)            return ref_list        return ref_list    # 獲取進(jìn)程狀態(tài)    def GetProcessStatus(self,process_name):        fps = self.GetPing()        dict_ = {"Address": '0', "ProcessName": '0', "ProcessCount": '0', "Status": '0'}        if fps['Ping'] != "0":            proc_id = self.GetValue("proc.num["{}"]".format(process_name))            dict_['Address'] = self.address            dict_['ProcessName'] = process_name            if proc_id != "0":                dict_['ProcessCount'] = proc_id                dict_['Status'] = "True"            else:                dict_['Status'] = "False"            return dict_        return dict_    # 獲取端口開(kāi)放狀態(tài)    def GetNetworkPort(self,port):        dict_ = {"Address": '0', "Status": 'False'}        dict_['Address'] = self.address        fps = self.GetPing()        if fps['Ping'] != "0":            port_ = self.GetValue("net.tcp.listen[{}]".format(port))            if port_ == "1":                dict_['Status'] = "True"            else:                dict_['Status'] = "False"            return dict_        return dict_    # 檢測(cè)Web服務(wù)器狀態(tài) 通過(guò)本地地址:端口 => 檢測(cè)目標(biāo)地址:端口    def CheckWebServerStatus(self,check_addr,check_port):        dict_ = {"local_address": "0", "remote_address": "0", "remote_port": "0", "Status":"False"}        fps = self.GetPing()        dict_['local_address'] = self.address        dict_['remote_address'] = check_addr        dict_['remote_port'] = check_port        if fps['Ping'] != "0":            check_ = self.GetValue("net.tcp.port["{}","{}"]".format(check_addr,check_port))            if check_ == "1":                dict_['Status'] = "True"            else:                dict_['Status'] = "False"            return dict_        return dict_

當(dāng)我們需要使用時(shí),只需要定義變量調(diào)用即可,其調(diào)用代碼如下。

from engine import Engineif __name__ == "__main__":    ptr_windows = Engine("127.0.0.1","10050")    ret = ptr_windows.GetDisk()    if len(ret) != 0:        for item in ret:            addr = item.get("Address")            name = item.get("Name")            type = item.get("Type")            space = item.get("Free")            if type != "UNKNOWN" and space != -1:                print("地址: {} --> 盤(pán)符: {} --> 格式: {} --> 剩余空間: {}".format(addr,name,type,space))

到此這篇關(guān)于Python封裝zabbix-get接口得代碼分享得內(nèi)容就介紹到這了,更多相關(guān)Python封裝zabbix-get接口內(nèi)容請(qǐng)搜索之家以前得內(nèi)容或繼續(xù)瀏覽下面得相關(guān)內(nèi)容希望大家以后多多支持之家!

聲明:所有內(nèi)容來(lái)自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。
發(fā)表評(píng)論
更多 網(wǎng)友評(píng)論1 條評(píng)論)
暫無(wú)評(píng)論

返回頂部

主站蜘蛛池模板: 国产大片内射1区2区| 二代妖精在线观看免费观看| 午夜福利一区二区三区高清视频| 亚洲综合久久1区2区3区| 久久综合九色综合欧美狠狠| yy6080影院| 精品视频在线观看你懂的一区| 精品国产免费一区二区三区香蕉| 韩国亚洲伊人久久综合影院| 理论亚洲区美一区二区三区| 日韩免费观看视频| 在线亚洲v日韩v| 另类图片亚洲校园小说区| 亚洲а∨精品天堂在线| 一本久到久久亚洲综合| 黄网站色成年片大免费高清 | 特级无码毛片免费视频尤物| 日本高清免费网站| 国模大胆一区二区三区| 啊好深好硬快点用力视频| 中国乱子伦xxxx| 麻豆aⅴ精品无码一区二区| 欧美性猛交xxxx免费看手交| 好大的奶女好爽视频| 国产亚洲一区二区在线观看| 亚洲日韩一区二区三区| 一区二区三区四区精品视频| 色欲狠狠躁天天躁无码中文字幕| 欧美一级视频免费看| 在线免费一区二区| 亚洲成人高清在线| 99视频精品国在线视频艾草| 欧美在线观看视频一区| 国模吧一区二区| 亚洲国产精品成人午夜在线观看 | 日本一道本在线视频| 国产精品18久久久久久麻辣| 亚洲精品亚洲人成在线播放 | 日本最刺激夫妇交换影片| 国产熟女一区二区三区五月婷| 亚洲日韩欧美一区二区三区|