Chinaunix

标题: Python做cpu利用率的脚本 [打印本页]

作者: zlzj2010    时间: 2008-12-16 15:39
标题: Python做cpu利用率的脚本
谁有现成的么?刚接到任务做这个,对python也不熟,帮个忙。
作者: xiaoyu9805119    时间: 2008-12-16 15:52
给你两个链接参考下把
http://blog.csdn.net/ustclu/archive/2007/08/01/1721673.aspx
http://topic.csdn.net/u/20080917 ... 1-CF2A09AE0D57.html

是linux下的
作者: zlzj2010    时间: 2008-12-16 17:04

多谢xiaoyu9805119   
看来还是得使用/proc/stat 自己来解析好了
作者: zlzj2010    时间: 2008-12-17 14:53
经过1天的努力,还是勉强完成了cpu利用率的统计,贴出来,希望对后来这有些帮助:

里面的sendlog大家可以不用,这是我发送日志到特定的日志服务器统一处理的。

在Linux系统中,可以用/proc/stat文件来计算cpu的利用率(详细的解释可参考:http: //www.linuxhowtos.org/System/procstat.htm)。这个文件包含了所有CPU活动的信息,该文件中的所有值都是从系统启动开始累计到当前时刻。
如:
[sailorhzr@builder ~]$ cat /proc/stat
cpu 432661 13295 86656 422145968 171474 233 5346
cpu0 123075 2462 23494 105543694 16586 0 4615
cpu1 111917 4124 23858 105503820 69697 123 371
cpu2 103164 3554 21530 105521167 64032 106 334
cpu3 94504 3153 17772 105577285 21158 4 24
intr 1065711094 1057275779 92 0 6 6 0 4 0 3527 0 0 0 70 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7376958 0 0 0 0 0 0 0 1054602 0 0 0 0 0 0 0 30 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 19067887
btime 1139187531
processes 270014
procs_running 1
procs_blocked 0

输出解释
CPU 以及CPU0、CPU1、CPU2、CPU3每行的每个参数意思(以第一行为例)为:
参数 解释
user (432661) 从系统启动开始累计到当前时刻,用户态的CPU时间(单位:jiffies) ,不包含 nice值为负进程。1jiffies=0.01秒
nice (13295) 从系统启动开始累计到当前时刻,nice值为负的进程所占用的CPU时间(单位:jiffies)
system (86656) 从系统启动开始累计到当前时刻,核心时间(单位:jiffies)
idle (42214596 从系统启动开始累计到当前时刻,除硬盘IO等待时间以外其它等待时间(单位:jiffies)
iowait (171474) 从系统启动开始累计到当前时刻,硬盘IO等待时间(单位:jiffies) ,
irq (233) 从系统启动开始累计到当前时刻,硬中断时间(单位:jiffies)
softirq (5346) 从系统启动开始累计到当前时刻,软中断时间(单位:jiffies)

CPU时间=user+system+nice+idle+iowait+irq+softirq

“intr”这行给出中断的信息,第一个为自系统启动以来,发生的所有的中断的次数;然后每个数对应一个特定的中断自系统启动以来所发生的次数。
“ctxt”给出了自系统启动以来CPU发生的上下文交换的次数。
“btime”给出了从系统启动到现在为止的时间(in seconds since the Unix epoch),单位为秒。
“processes (total_forks) 自系统启动以来所创建的任务的个数目。
“procs_running”:当前运行队列的任务的数目。
“procs_blocked”:当前被阻塞的任务的数目。

那么CPU利用率可以使用以下两个方法(后一种比较准确)。先取两个采样点,然后计算其差值:
cpu usage=(idle2-idle1)/(cpu2-cpu1)*100
cpu usage=[(user_2 +sys_2+nice_2) - (user_1 + sys_1+nice_1)]/(total_2 - total_1)*100





  1. import sys
  2. import re
  3. import time
  4. from scribe import scribe
  5. from thrift.transport import TTransport, TSocket
  6. from thrift.protocol import TBinaryProtocol

  7. def read_cpu_usage():
  8.     """Read the current system cpu usage from /proc/stat."""
  9.     lines = open("/proc/stat").readlines()
  10.     for line in lines:
  11.         #print "l = %s" % line
  12.         l = line.split()
  13.         if len(l) < 5:
  14.             continue
  15.         if l[0].startswith('cpu'):
  16.             return l;
  17.     return {}

  18. def sendlog(host,port,messa):
  19.     #"""send log to scribe
  20.     socket = TSocket.TSocket(host=host, port=port)
  21.     transport = TTransport.TFramedTransport(socket)
  22.     protocol = TBinaryProtocol.TBinaryProtocol(trans=transport, strictRead=False, strictWrite=False)
  23.     client = scribe.Client(iprot=protocol, oprot=protocol)
  24.     transport.open()
  25.     log_entry = scribe.LogEntry(dict(category='SYSD', message=messa))
  26.     result = client.Log(messages=[log_entry])
  27.     transport.close()
  28.     return result

  29. if len(sys.argv) >= 2:
  30.   host_port = sys.argv[1].split(':')
  31.   host = host_port[0]
  32.   if len(host_port) > 1:
  33.     port = int(host_port[1])
  34.   else:
  35.     port = 1463
  36. else:
  37.   sys.exit('usage : py.test  host[:port]] ')

  38. cpustr=read_cpu_usage()
  39. down=True
  40. #cpu usage=[(user_2 +sys_2+nice_2) - (user_1 + sys_1+nice_1)]/(total_2 - total_1)*100
  41. usni1=long(cpustr[1])+long(cpustr[2])+long(cpustr[3])+long(cpustr[5])+long(cpustr[6])+long(cpustr[7])+long(cpustr[4])
  42. usn1=long(cpustr[1])+long(cpustr[2])+long(cpustr[3])
  43. #usni1=long(cpustr[1])+long(cpustr[2])+long(cpustr[3])+long(cpustr[4])
  44. while(down):
  45.        time.sleep(2)
  46.        cpustr=read_cpu_usage()
  47.        usni2=long(cpustr[1])+long(cpustr[2])+float(cpustr[3])+long(cpustr[5])+long(cpustr[6])+long(cpustr[7])+long(cpustr[4])
  48.        usn2=long(cpustr[1])+long(cpustr[2])+long(cpustr[3])
  49.        #usni2=long(cpustr[1])+long(cpustr[2])+float(cpustr[3])+long(cpustr[4])
  50.        print usn2
  51.        print usni2
  52.        cpuper=(usn2-usn1)/(usni2-usni1)
  53.        s="CPUTotal used percent =%.4f \r\n" % cpuper
  54.        print s
  55.        sendlog(host,port,s)
  56.        usn1=usn2
  57.        usni1=usni2



复制代码

[ 本帖最后由 zlzj2010 于 2008-12-17 14:58 编辑 ]
作者: luo118    时间: 2008-12-17 14:53
学习
作者: thsniperwolf    时间: 2009-08-26 11:37
标题: 回复 #4 zlzj2010 的帖子
学习了
作者: qingduo04    时间: 2014-01-18 16:42
写的真好。
作者: 国辉110    时间: 2016-11-10 19:27
学习了lol




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2