python 监控 linux cpu 使用率,pythonlinux,[Python]代码
python 监控 linux cpu 使用率,pythonlinux,[Python]代码
[Python]代码
def _read_cpu_usage(self): """Read the current system cpu usage from /proc/stat.""" try: fd = open("/proc/stat", 'r') lines = fd.readlines() finally: if fd: fd.close() for line in lines: l = line.split() if len(l) < 5: continue if l[0].startswith('cpu'): return l return [] def get_cpu_usage(self): """ get cpu avg used by percent """ cpustr=self._read_cpu_usage() if not cpustr: return 0 #cpu usage=[(user_2 +sys_2+nice_2) - (user_1 + sys_1+nice_1)]/(total_2 - total_1)*100 usni1=long(cpustr[1])+long(cpustr[2])+long(cpustr[3])+long(cpustr[5])+long(cpustr[6])+long(cpustr[7])+long(cpustr[4]) usn1=long(cpustr[1])+long(cpustr[2])+long(cpustr[3]) #usni1=long(cpustr[1])+long(cpustr[2])+long(cpustr[3])+long(cpustr[4]) # self.sleep=2 time.sleep(self.sleep) cpustr=self._read_cpu_usage() if not cpustr: return 0 usni2=long(cpustr[1])+long(cpustr[2])+float(cpustr[3])+long(cpustr[5])+long(cpustr[6])+long(cpustr[7])+long(cpustr[4]) usn2=long(cpustr[1])+long(cpustr[2])+long(cpustr[3]) cpuper=(usn2-usn1)/(usni2-usni1) return 100*cpupe
相关内容
- Python 收取Gmail新邮件并发送到gtalk,pythongtalk,[Python]代码
- python矩阵乘法,测试运行时间的程序,python矩阵,有一个
- 常用的python代码,常用python代码,今天因为某个原因再次
- python指定大小的字典实现,python指定字典,一段示例程序
- python正则表达式学习代码,python正则表达式,Regexp.py''
- web.py中自定义jinja2模块,web.pyjinja2模块,web.py是一个轻量
- 用Tornado连接新浪微博,Tornado连接新浪,前几天心血来潮
- python金额小写转换代码,python金额小写,[Python]代码de
- Python 获取文件夹的大小,python获取文件夹,[Python]代码
- python四舍五入,,python的内置函数r
评论关闭