免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 19653 | 回复: 8
打印 上一主题 下一主题

Python做cpu利用率的脚本 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-16 15:39 |只看该作者 |倒序浏览
谁有现成的么?刚接到任务做这个,对python也不熟,帮个忙。

论坛徽章:
0
2 [报告]
发表于 2008-12-16 15:52 |只看该作者

论坛徽章:
0
3 [报告]
发表于 2008-12-16 17:04 |只看该作者

多谢xiaoyu9805119   
看来还是得使用/proc/stat 自己来解析好了

论坛徽章:
0
4 [报告]
发表于 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 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2008-12-17 14:53 |只看该作者
学习

论坛徽章:
0
6 [报告]
发表于 2009-08-26 11:37 |只看该作者

回复 #4 zlzj2010 的帖子

学习了

论坛徽章:
18
卯兔
日期:2013-09-27 17:41:0615-16赛季CBA联赛之佛山
日期:2016-07-09 17:34:45操作系统版块每周发帖之星
日期:2015-12-02 15:01:04IT运维版块每日发帖之星
日期:2015-12-02 06:20:00IT运维版块每日发帖之星
日期:2015-10-07 06:20:00IT运维版块每日发帖之星
日期:2015-10-03 06:20:00IT运维版块每日发帖之星
日期:2015-10-01 06:20:00羊年新春福章
日期:2015-04-01 17:56:06拜羊年徽章
日期:2015-04-01 17:56:062015年迎新春徽章
日期:2015-03-04 09:49:452015年辞旧岁徽章
日期:2015-03-03 16:54:15天秤座
日期:2015-01-14 06:39:28
7 [报告]
发表于 2014-01-18 16:42 |只看该作者
写的真好。

论坛徽章:
0
8 [报告]
发表于 2016-11-10 19:27 |只看该作者
学习了lol
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP