Advertisement
Stybyk

CPU

May 8th, 2019
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.29 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. '''
  5. Created on 04.12.2014
  6. add to /usr/bin/CPU.py
  7. @author: plagtag
  8. '''
  9. from time import sleep
  10. import sys
  11.  
  12. class GetCpuLoad(object):
  13.     '''
  14.    classdocs
  15.    '''
  16.  
  17.  
  18.     def __init__(self, percentage=True, sleeptime = 1):
  19.         '''
  20.        @parent class: GetCpuLoad
  21.        @date: 04.12.2014
  22.        @author: plagtag
  23.        @info:
  24.        @param:
  25.        @return: CPU load in percentage
  26.        '''
  27.         self.percentage = percentage
  28.         self.cpustat = '/proc/stat'
  29.         self.sep = ' '
  30.         self.sleeptime = sleeptime
  31.  
  32.     def getcputime(self):
  33.         '''
  34.        http://stackoverflow.com/questions/23367857/accurate-calculation-of-cpu-usage-given-in-percentage-in-linux
  35.        read in cpu information from file
  36.        The meanings of the columns are as follows, from left to right:
  37.            0cpuid: number of cpu
  38.            1user: normal processes executing in user mode
  39.            2nice: niced processes executing in user mode
  40.            3system: processes executing in kernel mode
  41.            4idle: twiddling thumbs
  42.            5iowait: waiting for I/O to complete
  43.            6irq: servicing interrupts
  44.            7softirq: servicing softirqs
  45.  
  46.        #the formulas from htop
  47.             user    nice   system  idle      iowait irq   softirq  steal  guest  guest_nice
  48.        cpu  74608   2520   24433   1117073   6176   4054  0        0      0      0
  49.  
  50.  
  51.        Idle=idle+iowait
  52.        NonIdle=user+nice+system+irq+softirq+steal
  53.        Total=Idle+NonIdle # first line of file for all cpus
  54.  
  55.        CPU_Percentage=((Total-PrevTotal)-(Idle-PrevIdle))/(Total-PrevTotal)
  56.        '''
  57.         cpu_infos = {} #collect here the information
  58.         with open(self.cpustat,'r') as f_stat:
  59.             lines = [line.split(self.sep) for content in f_stat.readlines() for line in content.split('\n') if line.startswith('cpu')]
  60.  
  61.             #compute for every cpu
  62.             for cpu_line in lines:
  63.                 if '' in cpu_line: cpu_line.remove('')#remove empty elements
  64.                 cpu_line = [cpu_line[0]]+[float(i) for i in cpu_line[1:]]#type casting
  65.                 cpu_id,user,nice,system,idle,iowait,irq,softrig,steal,guest,guest_nice = cpu_line
  66.  
  67.                 Idle=idle+iowait
  68.                 NonIdle=user+nice+system+irq+softrig+steal
  69.  
  70.                 Total=Idle+NonIdle
  71.                 #update dictionionary
  72.                 cpu_infos.update({cpu_id:{'total':Total,'idle':Idle}})
  73.             return cpu_infos
  74.  
  75.     def getcpuload(self):
  76.         '''
  77.        CPU_Percentage=((Total-PrevTotal)-(Idle-PrevIdle))/(Total-PrevTotal)
  78.  
  79.        '''
  80.         start = self.getcputime()
  81.         #wait a second
  82.         sleep(self.sleeptime)
  83.         stop = self.getcputime()
  84.  
  85.         cpu_load = {}
  86.  
  87.         for cpu in start:
  88.             Total = stop[cpu]['total']
  89.             PrevTotal = start[cpu]['total']
  90.  
  91.             Idle = stop[cpu]['idle']
  92.             PrevIdle = start[cpu]['idle']
  93.             CPU_Percentage=((Total-PrevTotal)-(Idle-PrevIdle))/(Total-PrevTotal)*100
  94.         CPU_Percentage=round(CPU_Percentage,1)
  95.             cpu_load.update({cpu: CPU_Percentage})
  96.         return cpu_load
  97.  
  98.  
  99. if __name__=='__main__':
  100.  
  101.     x = GetCpuLoad()    
  102.     data = x.getcpuload()
  103.     print data
  104.     sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement