網站首頁 編程語言 正文
Zabbix 是一款強大的開源網管監控工具,該工具的客戶端與服務端是分開的,我們可以直接使用自帶的zabbix_get
命令來實現拉取客戶端上的各種數據,在本地組裝參數并使用Popen開子線程執行該命令,即可實現批量監測。
封裝Engine類:?該類的主要封裝了Zabbix接口的調用,包括最基本的參數收集.
import subprocess,datetime,time,math class 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檢測 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 # 獲取主機組基本信息 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 # 獲取內存利用率 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]") # 計算百分比: 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 # 獲取磁盤數據 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 # 獲取進程狀態 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_ # 獲取端口開放狀態 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_ # 檢測Web服務器狀態 通過本地地址:端口 => 檢測目標地址:端口 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_
當我們需要使用時,只需要定義變量調用即可,其調用代碼如下。
from engine import Engine if __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("地址: {} --> 盤符: {} --> 格式: {} --> 剩余空間: {}".format(addr,name,type,space))
原文鏈接:https://www.cnblogs.com/LyShark/p/16512849.html
相關推薦
- 2022-09-05 C語言之關于二維數組在函數中的調用問題_C 語言
- 2023-02-25 React18之update流程從零實現詳解_React
- 2022-06-08 Spring Cloud Nacos NacosWatch
- 2022-10-22 React拖拽調整大小的組件_React
- 2023-04-03 python中super().__init__()作用詳解_python
- 2023-11-14 k8s安裝部署metrics-server;監測集群狀況
- 2022-05-31 在.NET?MAUI應用中配置應用生命周期事件_實用技巧
- 2023-07-28 el-table 合并單元格(合并行)
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支