主机资源情况监视器demo

发布时间 2023-04-17 13:27:20作者: No九五二七9527
import paramiko # 导入paramiko模块用于SSH连接
import psutil # 导入psutil模块用于获取系统信息
import wx # 导入wx模块用于构建GUI应用程序

class RemoteMonitor(wx.Frame): # 定义RemoteMonitor类,继承自wx.Frame类
    def __init__(self, host, username, password, port=22):
        super().__init__(None, title="Remote Monitor") # 初始化wx.Frame对象,设置窗口标题
        self.host = host # 保存主机名
        self.username = username # 保存用户名
        self.password = password # 保存密码
        self.port = port # 保存端口号,默认为22
        self.cpu_label = wx.StaticText(self, label="CPU usage:") # 创建CPU使用率标签
        self.mem_label = wx.StaticText(self, label="Memory usage:") # 创建内存使用率标签
        self.disk_label = wx.StaticText(self, label="Disk usage:") # 创建磁盘使用率标签
        self.cpu_value = wx.StaticText(self, label="") # 创建显示CPU使用率的静态文本框
        self.mem_value = wx.StaticText(self, label="") # 创建显示内存使用率的静态文本框
        self.disk_value = wx.StaticText(self, label="") # 创建显示磁盘使用率的静态文本框
        self.init_ui() # 调用初始化UI方法
        self.connect_ssh() # 连接SSH并保存客户端对象

    def init_ui(self):
        sizer = wx.BoxSizer(wx.VERTICAL) # 创建垂直方向的BoxSizer对象
        sizer.Add(self.cpu_label, 0, wx.ALL, 5) # 添加CPU使用率标签到Sizer中
        sizer.Add(self.cpu_value, 0, wx.ALL, 5) # 添加显示CPU使用率的文本框到Sizer中
        sizer.Add(self.mem_label, 0, wx.ALL, 5) # 添加内存使用率标签到Sizer中
        sizer.Add(self.mem_value, 0, wx.ALL, 5) # 添加显示内存使用率的文本框到Sizer中
        sizer.Add(self.disk_label, 0, wx.ALL, 5) # 添加磁盘使用率标签到Sizer中
        sizer.Add(self.disk_value, 0, wx.ALL, 5) # 添加显示磁盘使用率的文本框到Sizer中
        self.SetSizer(sizer) # 将Sizer设置为窗口的Sizer
        self.Fit() # 让窗口自适应大小

    def connect_ssh(self):
        self.client = paramiko.SSHClient() # 创建SSHClient对象
        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 设置自动添加主机密钥的策略
        self.client.connect(self.host, self.port, self.username, self.password) # 通过SSH连接到远程主机

    def get_cpu_usage(self):
        cpu_percent = psutil.cpu_percent() # 获取CPU使用率
        self.cpu_value.SetLabel(f"{cpu_percent}%") # 在界面上更新CPU使用率

    def get_mem_usage(self):
        mem_percent = psutil.virtual_memory().percent # 获取内存使用率
        self.mem_value.SetLabel(f"{mem_percent}%") # 在界面上更新内存使用率

    def get_disk_usage(self):
        disk_percent = psutil.disk_usage('/').percent # 获取磁盘使用率
        self.disk_value.SetLabel(f"{disk_percent}%") # 在界面上更新磁盘使用率

    def update_usage(self, event):
        self.get_cpu_usage() # 更新CPU使用率信息
        self.get_mem_usage() # 更新内存使用率信息
        self.get_disk_usage() # 更新磁盘使用率信息
        self.send_usage() # 发送使用率信息到远程主机

    def send_usage(self):
        stdin, stdout, stderr = self.client.exec_command(f"echo {psutil.cpu_percent()} {psutil.virtual_memory().percent} {psutil.disk_usage('/').percent}") # 执行命令并获取输入、输出和错误流
        response = stdout.read().decode() # 获取输出流并将字节解码为字符串
        if response:
            print(response) # 输出远程主机的响应
if __name__ == '__main__':
    app = wx.App() # 创建wx.App对象
    monitor = RemoteMonitor(hostIP,name,password) # 创建RemoteMonitor对象,连接到指定的远程主机
    monitor.Show() # 显示RemoteMonitor窗口
    timer = wx.Timer(monitor) # 创建定时器对象,关联到RemoteMonitor窗口
    timer.Start(1000) # 启动定时器,并设置间隔时间为1秒
    monitor.Bind(wx.EVT_TIMER, monitor.update_usage) # 绑定定时器事件到update_usage方法上
    app.MainLoop() # 进入wxPython的主事件循环