Python CPU utilization script

Who have ready? Just received a mandate to do this, also not familiar with Python, do me a favor.

Started by Dennis at March 01, 2016 - 1:14 PM

Give you two references to



Is under Linux

Posted by Sidon at March 04, 2016 - 2:05 PM


Thanks to xiaoyu9805119
But it seems to use /proc/stat to parse

Posted by Dennis at March 17, 2016 - 2:30 PM

After 1 days of efforts, or barely finished statistics, CPU utilization of sticking out, and I hope to help some later this:

Inside the sendlog we can not, this is I send log to a specific log server processing.

In the Linux system, can use to calculate the CPU with the /proc/stat file rate (detailed explanation can reference: http: //www.linuxhowtos.org/System/procstat.htm). This file contains all the CPU activities, all the values in this file are added to the start time from the system.
Such as:
[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

The output interpretation
Each parameter of each of CPU and CPU0 mean, CPU1, CPU2, CPU3 (the first case) for:
Parameter interpretation
user (432661) From the system start accumulated to the current time, user mode CPU time (unit: jiffies), does not contain the nice value is negative process. 1jiffies=0.01 seconds
nice (13295) From the system start accumulated to the current time, nice value occupied for negative process CPU time (unit: jiffies)
system (86656) From the system start cumulative to the present moment, core time (unit: jiffies)
idle (42214596 From the system start cumulative to the present moment, in addition to the hard disk IO wait time other than waiting time (unit: jiffies)
iowait (171474) From the system start cumulative to the present moment, hard disk IO wait time (unit: jiffies) ,
irq (233) From the system start cumulative to the present moment, hardware interrupt time (unit: jiffies)
softirq (5346) From the system start cumulative to the present moment, soft interrupt time (unit: jiffies)

CPU time =user+system+nice+idle+iowait+irq+softirq

"intr"This trip is interrupted, the first since the system since the start, all occurrences of the interrupt times; then each number corresponds to a specific number of the suspension system since the start.
"ctxt"Given the number of exchange since the system since the start of CPU context.
"btime"Given the system start up to now time (in seconds since the Unix epoch), in seconds.
"processes (total_forks) A number of self system created since the start task.
"procs_running": The number of the run queue tasks.
"procs_blocked": Current number of blocked tasks.

So the utilization rate of CPU can use the following two methods (the latter is more accurate). Take the two sampling points, and then calculate the difference:
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

[the zlzj2010 2008-12-17 14:58 editor]

Posted by Dennis at March 21, 2016 - 3:01 PM

Study

Posted by Lewis at March 22, 2016 - 3:15 PM

Study

Posted by Keith at March 27, 2016 - 3:19 PM

Good writing.

Posted by Allison at March 30, 2016 - 3:49 PM