在Ubuntu虚拟机中进行系统监控,你可以使用多种工具和方法。以下是一些常用的工具和步骤:
top
命令可以实时显示系统中各个进程的资源占用情况,包括CPU和内存使用率。htop
是一个增强版的 top
命令,提供了更丰富的界面和更多的功能。可以通过 sudo apt update && sudo apt install htop
来安装。netstat -tulpn
来查看当前TCP/UDP连接及其对应的进程。sudo apt update && sudo apt upgrade -y
sudo apt install cockpit* -y
sudo systemctl start cockpit
和 sudo systemctl enable cockpit
https://<你的服务器 IP>:9090
,首次访问时接受自签名证书,用有 sudo 权限的用户登录。你可以使用Python编写脚本来监控系统资源的使用情况。以下是一个简单的示例脚本,使用 psutil
模块来监控CPU使用率、内存使用量、磁盘使用情况等:
import psutil
import time
def get_system_info():
# 获取CPU信息
cpu_usage = psutil.cpu_percent(interval=1)
# 获取内存信息
memory_info = psutil.virtual_memory()
# 获取磁盘信息
disk_info = psutil.disk_usage('/')
# 获取网络信息
net_info = psutil.net_io_counters()
print(f"CPU Usage: {cpu_usage}%")
print(f"Memory Usage: {memory_info.percent}%")
print(f"Disk Usage: {disk_info.percent}%")
print(f"Network: Sent {net_info.bytes_sent}, Received {net_info.bytes_recv}")
while True:
get_system_info()
time.sleep(5) # 每5秒刷新一次信息
通过上述方法,你可以轻松地在Ubuntu虚拟机中进行系统监控,选择合适的工具可以让您更好地了解和优化虚拟机的运行状态。