以下是关于python来获取服务器每颗CPU使用率的使用脚本。

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import re,time
  5.  
  6. def _read_cpu_usage():
  7. """Read the current system cpu usage from /proc/stat"""
  8. statfile = "/proc/stat"
  9. cpulist = []
  10. try:
  11. f = open(statfile, 'r')
  12. lines = f.readlines()
  13. except:
  14. print "ERROR: Can not open %s,The system cannot continue to run" % (statfile)
  15. return []
  16. for line in lines:
  17. tmplist = line.split()
  18. if len(tmplist) < 5:
  19. continue
  20. for b in tmplist:
  21. m = re.search(r'cpu\d+',b)
  22. if m is not None:
  23. cpulist.append(tmplist)
  24. f.close()
  25. return cpulist
  26.  
  27. def get_cpu_usage():
  28. cpuusage = {}
  29. cpustart = {}
  30. cpuend = {}
  31. linestart = _read_cpu_usage()
  32. if not linestart:
  33. return 0
  34. for cpustr in linestart:
  35. usni=long(cpustr[1])+long(cpustr[2])+long(cpustr[3])+long(cpustr[5])+long(cpustr[6])+long(cpustr[7])+long(cpustr[4])
  36. usn=long(cpustr[1])+long(cpustr[2])+long(cpustr[3])
  37. cpustart[cpustr[0]] = str(usni)+":"+str(usn)
  38. sleep = 2
  39. time.sleep(sleep)
  40. lineend = _read_cpu_usage()
  41. if not lineend:
  42. return 0
  43. for cpustr in lineend:
  44. usni=long(cpustr[1])+long(cpustr[2])+long(cpustr[3])+long(cpustr[5])+long(cpustr[6])+long(cpustr[7])+long(cpustr[4])
  45. usn=long(cpustr[1])+long(cpustr[2])+long(cpustr[3])
  46. cpuend[cpustr[0]] = str(usni)+":"+str(usn)
  47. for line in cpustart:
  48. start = cpustart[line].split(':')
  49. usni1,usn1 = float(start[0]),float(start[1])
  50. end = cpuend[line].split(':')
  51. usni2,usn2 = float(end[0]),float(end[1])
  52. cpuper=(usn2-usn1)/(usni2-usni1)
  53. cpuusage[line] = int(100*cpuper)
  54.  
  55. return cpuusage
  56.  
  57. if __name__ == '__main__':
  58. a = get_cpu_usage()
  59. print a

python获取每颗cpu使用率的更多相关文章

  1. Java获取Linux系统cpu使用率

    原文:http://www.open-open.com/code/view/1426152165201 import java.io.BufferedReader; import java.io.Fi ...

  2. 获取Windows操作系统的CPU使用率以及内存使用率

    此功能参考了ProcessHacker项目的代码. 声明定义 typedef struct _UINT64_DELTA { ULONG64 Value; ULONG64 Delta; } UINT64 ...

  3. [Python Study Notes]计算cpu使用率v0.1

    V0.1 更新日志: 1.加入平台判断,支持windows与linux 2.自动清屏显示,显示更加直观 '''''''''''''''''''''''''''''''''''''''''''''''' ...

  4. [Python Study Notes]计算cpu使用率

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  5. [No0000112]ComputerInfo,C#获取计算机信息(cpu使用率,内存占用率,硬盘,网络信息)

    github地址:https://github.com/charygao/SmsComputerMonitor 软件用于实时监控当前系统资源等情况,并调用接口,当资源被超额占用时,发送警报到个人手机: ...

  6. Golang利用第三方包获取本机cpu使用率以及内存使用情况

    第三方包下载 $ github.com/shirou/gopsutil 获取内存方面的信息 package main import ( "fmt" "github.com ...

  7. 为什么在Python里推荐使用多进程而不是多线程?(为什么python多线程无法增加CPU使用率?)

    最近在看Python的多线程,经常我们会听到老手说:“Python下多线程是鸡肋,推荐使用多进程!”,但是为什么这么说呢? 要知其然,更要知其所以然.所以有了下面的深入研究: 首先强调背景:     ...

  8. Python 获取服务器的CPU个数

    在使用gunicorn时,需要设置workers, 例如: gunicorn --workers=3 app:app -b 0.0.0.0:9000 其中,worker的数量并不是越多越好,推荐值是C ...

  9. 使用shell脚本获取虚拟机中cpu使用率(读/proc/statc)

    #!/bin/bash interval= cpu_num=`-] -c` start_idle=() start_total=() cpu_rate=() cpu_rate_file=./`host ...

随机推荐

  1. RESTful API 设计最佳实践(转)

    摘要:目前互联网上充斥着大量的关于RESTful API(为了方便,以后API和RESTful API 一个意思)如何设计的文章,然而却没有一个”万能“的设计标准:如何鉴权?API格式如何?你的API ...

  2. HDU 4602 Magic Ball Game(离线处理,树状数组,dfs)

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. Linux-Ubuntu 启用root账户

    Ubuntu Linux有一个与众不同的特点,那就是初次使用时,你无法作为root来登录系统,为什么会这样?这就要从系统的安装说起.对于其他Linux系统来 说,一般在安装过程就设定root密码,这样 ...

  4. Umbraco中更换IndexSet中的NodeType后,搜索页面没有做出对应更改的效果

    在项目开发中,使用ExternalSearcher,有一个ExamineIndex.config文件中存放ExternalIndexSet 开始时是这样的 <!-- Default Indexs ...

  5. POJ-2785 4 Values whose Sum is 0(折半枚举 sort + 二分)

    题目链接:http://poj.org/problem?id=2785 题意是给你4个数列.要从每个数列中各取一个数,使得四个数的sum为0,求出这样的组合的情况个数. 其中一个数列有多个相同的数字时 ...

  6. springMVC+JAP整合彻底摆脱persistence.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  7. 数据库:mongodb与关系型数据库相比的优缺点

      与关系型数据库相比,MongoDB的优点:①弱一致性(最终一致),更能保证用户的访问速度:举例来说,在传统的关系型数据库中,一个COUNT类型的操作会锁定数据集,这样可以保证得到“当前”情况下的精 ...

  8. installshield Basic 工程每次安装完提示重启电脑

     将Sequence中的ScheduleReboot Action的Condition清空即可. 

  9. 如何设置box shadow的透明度

    (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-04-24) 今天发现使用box-shadow属性,可以很好的给div添加阴影效果,但是添加的效果如果是: -moz-box- ...

  10. PL/pgSQL函数带output参数例子

    例子1,不带returns : [postgres@cnrd56 bin]$ ./psql psql () Type "help" for help. postgres=# CRE ...