python监控模块
pip install psutil
获取内存信息:
1 2 3 4 5 6 | >>> import psutil >>> mem = psutil.virtual_memory() #获取内存完整信息 >>> mem.total #获取内存总数 8010080256 >>> mem.used #获取内存空闲数 4246765568 |
获取CPU信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | >>> psutil.cpu_times() #显示CPU信息 scputimes(user = 253743.4577502 , system = 85613.45800069999 , idle = 1441121.8095010999 , interrupt = 23814.738257599998 , dpc = 2544.7039121000003 ) >>> psutil.cpu_times(percpu = True ) #显示所有CPU信息 [ scputimes(user = 91456.9746589 , system = 21885.34908980003 , idle = 331843.96999129996 ,interrupt = 1167.7454855 , dpc = 412.5758447 ), scputimes(user = 51238.871652199996 ,system = 35647.80411009997 , idle = 358299.0563741 , interrupt = 21236.5253307 , dpc = 1596.6858350999998 ), scputimes(user = 100969.21363439999 , system = 23486.262552 , idle = 320730.13114919997 , interrupt = 493.66516449999995 , dpc = 104.1774678 ), scputimes(user = 10093.6079022 , system = 4604.509915900009 , idle = 430487.3647167 , interrupt = 922.4807132999999 , dpc = 431.5611664 ) ] >>> psutil.cpu_times().user #获取单项数据信息 253767.450704 >>> psutil.cpu_count() #获取CPU个数 4 >>> psutil.cpu_count(logical = False ) #获取物理CPU个数(实际上是线程数除以2)。 2 >>> psutil.swap_memory() #获取SWAP分区信息 sswap(total = 16018214912 , used = 10366255104 , free = 5651959808 , percent = 64.7 , sin = 0 , sout = 0 ) |
磁盘信息:
psutil.disk_usage 磁盘利用率
磁盘IO信息包括:
read_count(读IO数)write_count(写IO数)
read_bytes(IO读字节数)write_bytes(IO写字节数)
read_time(磁盘读时间)write_time(磁盘写时间)
psutil.disk_partitions 获取磁盘完整信息
psutil.disk_usage 获取分区的使用情况
psutil.disk_io_counters('/') 获取磁盘IO信息
psutil.disk_io_counters(perdisk=True) 按分区获取IO信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | >>> psutil.disk_partitions() [sdiskpart(device = 'C:\\', mountpoint=' C:\\ ', fstype=' NTFS ', opts=' rw,fixed'), sd iskpart(device = 'D:\\', mountpoint=' D:\\ ', fstype=' NTFS ', opts=' rw,fixed'), sdisk part(device = 'E:\\', mountpoint=' E:\\ ', fstype=' NTFS ', opts=' rw,fixed'), sdiskpar t(device = 'F:\\', mountpoint=' F:\\ ', fstype=' CDFS ', opts=' ro,cdrom')] >>> psutil.disk_usage( 'c:\\' ) sdiskusage(total = 107375226880 , used = 80557694976 , free = 26817531904 , percent = 75.0 ) >>> psutil.disk_usage( 'd:\\' ) sdiskusage(total = 429497774080 , used = 58469056512 , free = 371028717568 , percent = 13.6 ) >>> psutil.disk_io_counters() sdiskio(read_count = 10022230 , write_count = 5844260 , read_bytes = 389997929984 , write _bytes = 241317355520 , read_time = 1362723 , write_time = 1205422 ) >>> psutil.disk_io_counters(perdisk = True ) { 'PhysicalDrive0' : sdiskio(read_count = 10022230 , write_count = 5844325 , read_bytes = 389997929984 , write_bytes = 241317739008 , read_time = 1362723 , write_time = 1205422 )} >>> |
网络信息
psutil.net_io_counters()
bytes_sent 发送字节数
bytes_recv 接收字节数
packets_sent 发送数据包数
packets_recv 接收数据包数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | >>> psutil.net_io_counters() snetio(bytes_sent = 87020660 , bytes_recv = 748546277 , packets_sent = 609921 , packets_r ecv = 486979 , errin = 0 , errout = 5429 , dropin = 0 , dropout = 0 ) >>> >>> psutil.net_io_counters(pernic = True ) { 'Npcap Loopback Adapter' : snetio(bytes_sent = 0 , bytes_recv = 0 , packets_sent = 0 , packets_recv = 0 , errin = 0 , errout = 0 , dropin = 0 , dropout = 0 ), 'Bluetooth 网络连接' : snetio(bytes_sent = 0 , bytes_recv = 0 , packets_sent = 0 , packets_recv = 0 , errin = 0 , errout = 0 , dropin = 0 , dropout = 0 ), '本地连接' : snetio(bytes_sent = 87012318 , bytes_recv = 748543604 , packets_sent = 604977 , packets_recv = 486965 , errin = 0 , errout = 0 , dropin = 0 , dropout = 0 ), 'VMware Network Adapter VMnet8' : snetio(bytes_sent = 4901 , bytes_recv = 0 , packets_sent = 4901 , packets_recv = 0 , errin = 0 , errout = 0 , dropin = 0 , dropout = 0 ), 'Loopback Pseudo-Interface 1' : snetio(bytes_sent = 0 , bytes_recv = 0 , packets_sent = 0 , packets_recv = 0 , errin = 0 , errout = 0 , dropin = 0 , dropout = 0 ), 'isatap.{49CF5090-D237-4A65-8E97-5BF88A0C7F1F}' : snetio(bytes_sent = 0 , bytes_recv = 0 , packets_sent = 0 , packets_recv = 0 , errin = 0 , errout = 0 , dropin = 0 , dropout = 0 ), '6TO4 Adapter' : snetio(bytes_sent = 0 , bytes_recv = 0 , packets_sent = 0 , packets_recv = 0 , errin = 0 , errout = 5428 , dropin = 0 , dropout = 0 ), 'isatap.{095E0AA1-265B-449D-8284-C2C534AF37BC}' : snetio(bytes_sent = 0 , bytes_recv = 0 , packets_sent = 0 , packets_recv = 0 , errin = 0 , errout = 0 , dropin = 0 , dropout = 0 ), 'isatap.{449A948D-21F4-43F9-BE38-6D3568A5DD76}' : snetio(bytes_sent = 0 , bytes_recv = 0 , packets_sent = 0 , packets_recv = 0 , errin = 0 , errout = 0 , dropin = 0 , dropout = 0 ), 'isatap.{2B95C896-6750-4C13-A6D1-FFC407250CBF}' : snetio(bytes_sent = 0 , bytes_recv = 0 , packets_sent = 0 , packets_recv = 0 , errin = 0 , errout = 0 , dropin = 0 , dropout = 0 ), 'Teredo Tunneling Pseudo-Interface' : snetio(bytes_sent = 5092 , bytes_recv = 4104 , packets_sent = 62 , packets_recv = 27 , errin = 0 , errout = 1 , dropin = 0 , dropout = 0 ) } |
其他信息:
1 2 3 4 5 6 7 8 9 | >>> psutil.users() #获取当前登录系统的用户信息 [suser(name = 'Administrator' , terminal = None , host = '0.0.0.0' , started = 1563846388.0 , pid = None )] >>> psutil.boot_time() #获取开机时间 1563846338.0 >>> import datetime >>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime(" % Y - % m - % d % H: % M : % S") '2019-07-23 09:45:38' |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现