Python获取CPU使用率、内存使用率、网络使用状态
来源:互联网 发布:车削中心的编程视频 编辑:程序博客网 时间:2024/06/16 13:20
参考文章:http://blog.csdn.net/bubu8633/article/details/8258342
转自:http://blog.csdn.net/wst521/article/details/46831497
由于psutil已更新到3.0.1版本,最新的代码如下:
#!/usr/bin/env pythonimport osimport timeimport sysimport atexitimport psutil#print "Welcome,current system is",os.name," 3 seconds late start to get data"time.sleep(3)line_num = 1#function of Get cpu statedef getCPUstate(interval=1): return (" CPU:"+str(psutil.cpu_percent(interval))+"%")def getMemorystate(): phymem = psutil.virtual_memory() line = "Memory: %5s%% %6s/%s"%( phymem.percent, str(int(phymem.used/1024/1024))+"M", str(int(phymem.total/1024/1024))+"M" ) return linedef bytes2human(n): """ >>>bytes2human(10000) '9.8k' >>>bytes2human(100001221) '95.4M' """ symbols = ('K','M','G','T','P','E','Z','Y') prefix = {} for i ,s in enumerate(symbols): prefix[s] = 1 << (i+1)*10 for s in reversed(symbols): if n >=prefix[s]: value = float(n) / prefix[s] return '%.2f %s'%(value,s) return '%.2fB'%(n)def poll(interval): """Retrieve raw stats within an interval window.""" tot_before = psutil.net_io_counters() pnic_before = psutil.net_io_counters(pernic=True) #sleep some time time.sleep(interval) tot_after = psutil.net_io_counters() pnic_after = psutil.net_io_counters(pernic=True) #get cpu stats cpu_state = getCPUstate(interval) #get memory memory_state = getMemorystate() return (tot_before,tot_after,pnic_before,pnic_after,cpu_state,memory_state)def refresh_window(tot_before,tot_after,pnic_before,pnic_after,cpu_state,memory_state): """print stats on screen""" #print current time,cpu state,memory print (time.asctime() +" | "+cpu_state+" | "+ memory_state) #total print(" NetStates:") print(" total bytes: sent: %-10s received: %s"%(\ bytes2human(tot_after.bytes_sent), \ bytes2human(tot_after.bytes_recv))) print( " total packets: sent: %-10s received: %s"%(\ tot_after.packets_sent,\ tot_after.packets_recv)) # per-network interface details: let's sort network interfaces so # that the ones which generated more traffic are shown first print( " ") nic_names = pnic_after.keys() #nic_names.sort(key=lambda x: sum(pnic_after[x]), reverse=True) for name in nic_names: stats_before = pnic_before[name] stats_after = pnic_after[name] templ = "%-15s %15s %15s" print(templ % (name, "TOTAL", "PER-SEC")) print(templ % ( "bytes-sent", bytes2human(stats_after.bytes_sent), bytes2human(stats_after.bytes_sent - stats_before.bytes_sent) + '/s', )) print(templ % ( "bytes-recv", bytes2human(stats_after.bytes_recv), bytes2human(stats_after.bytes_recv- stats_before.bytes_recv) + '/s', )) print(templ % ( "pkts-sent", stats_after.packets_sent, stats_after.packets_sent - stats_before.packets_sent, )) print((templ %( "pkts-recv", stats_after.packets_recv, stats_after.packets_recv - stats_before.packets_recv, ))) print( " ")try: interval = 0 while 1: args = poll(interval) refresh_window(*args) interval = 1except (KeyboardInterrupt,SystemExit): pass
0 0
- Python获取CPU使用率、内存使用率、网络使用状态
- Python获取CPU使用率、内存使用率、网络使用状态
- Python获取CPU使用率、内存使用率、网络使用状态
- Python获取CPU使用率、内存使用率、网络使用状态
- Python获取CPU使用率、内存使用率、网络使用状态
- 获取系统的CPU使用率、内存使用率
- Python在Linux下获取CPU温度、使用率、内存使用率、硬盘使用率
- java获取cpu使用率/内存使用率/硬盘的使用率
- java获取cpu使用率/内存使用率/硬盘的使用率
- java获取cpu使用率/内存使用率/硬盘的使用率
- 获取内存和CPU使用率
- python获取全部进程的CPU、内存使用率
- 转载:python获取全部进程的CPU、内存使用率
- linux下实现CPU使用率和内存使用率获取方法
- linux下获取内存使用率及cpu使用率
- Linux下,java获取CPU使用率、内存使用率
- Linux下,java获取CPU使用率、内存使用率
- C++ 获取CPU使用率、内存使用率、磁盘空间等
- ios 判断小数点. 后几位 && 小数点后有数据显示数据,没有则不显示数据
- Linux性能优化大师——互动出版网
- Windows下pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat
- allowInvalidCertificates
- 注册JDBC驱动的三种方式
- Python获取CPU使用率、内存使用率、网络使用状态
- 无法安装net framework 3.5 的解决方法
- 17个新手常见Python运行时错误
- Binary Search Tree Iterator
- 关于Unity3d 使用rigidbody.velocity代替Transform.Translate()来使物体向前移动
- 《数据结构与算法分析》伸展树(自底向上)详解
- 刨根问底(二):从INode客户端看如何培养兴趣 (续)
- 聚类之详解FCM算法原理及应用
- 图结构练习——判断给定图是否存在合法拓扑序列