-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.py
More file actions
72 lines (61 loc) · 2.83 KB
/
cpu.py
File metadata and controls
72 lines (61 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import time
import multiprocessing
from datetime import datetime as dt
import sys
import argparse
import math
def cpuUsage(t=10,i=1,**kwargs):
jiffy = os.sysconf(os.sysconf_names['SC_CLK_TCK'])
num_cpu = multiprocessing.cpu_count()
date=[]
userl=[]
systeml=[]
idlel=[]
nicel=[]
iowaitl=[]
irql=[]
stat_fd = open('/proc/stat')
stat_buf = stat_fd.readlines()[0].split()
user, nice, system, idle, iowait, irq, sirq = ( float(stat_buf[1]), float(stat_buf[2]),
float(stat_buf[3]), float(stat_buf[4]),
float(stat_buf[5]), float(stat_buf[6]),
float(stat_buf[7]) )
stat_fd.close()
for x in range(int(math.ceil(t/i))):
time.sleep(i)
stat_fd = open('/proc/stat')
stat_buf = stat_fd.readlines()[0].split()
user_n, nice_n, sys_n, idle_n, iowait_n, irq_n, sirq_n = ( float(stat_buf[1]), float(stat_buf[2]),
float(stat_buf[3]), float(stat_buf[4]),
float(stat_buf[5]), float(stat_buf[6]),
float(stat_buf[7]) )
stat_fd.close()
date.append(dt.now().strftime("%Y-%m-%d %H:%M:%S"))
systeml.append(((sys_n - system) * 100 / jiffy / i) / num_cpu)
userl.append(((user_n - user) * 100 / jiffy / i) / num_cpu)
idlel.append(((idle_n - idle) * 100 / jiffy / i) / num_cpu)
nicel.append(((nice_n - nice) * 100 / jiffy / i) / num_cpu)
iowaitl.append(((iowait_n - iowait) * 100 / jiffy / i) / num_cpu)
irql.append(((irq_n - irq) * 100 / jiffy / i) / num_cpu)
user=user_n
system=sys_n
idle=idle_n
iowait=iowait_n
nice=nice_n
irq=irq_n
jsfile=open("cpu.js","w+")
jsfile.write("""var usercpu={{'x':{0},'y':{1},'type':'scatter','name':'User%'}};
var syscpu={{
'x':{2},'y':{3},'type':'scatter','name':'System%'}};
var idlecpu={{
'x':{4},'y':{5},'type':'scatter','name':'Idle%'}};
var nice={{'x':{0},'y':{6},'type':'scatter','name':'Nice%'}};
var iowait={{
'x':{2},'y':{7},'type':'scatter','name':'iowait%'}};
var irq={{
'x':{4},'y':{8},'type':'scatter','name':'Interrupts/s%'}};
var data=[usercpu,syscpu,idlecpu,nice,iowait,irq]; var layout = {{
title:'CPU Usage Line Graph'
}};""".format(date,userl,date,systeml,date,idlel,nicel,iowaitl,irql))
#jsfile.write("""var usercpu=\{{ x:{{0}},\y:{{1}},type:'scatter',name:'User%'\}}; var syscpu=\{{ x:{{0}},y:{{2}},type:'scatter',name:'System%'\}}; var idlecpu=\{{ x:{{0}},y:{{3}},type:'scatter',name:'Idle%'\}};var data = [usercpu,syscpu,idlecpu];""".format(date,userl,systeml,idlel))