-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu_parser.py
64 lines (50 loc) · 1.8 KB
/
cpu_parser.py
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
#! /usr/bin/python
import re
import sys
import os
def read_cpu_file():
"""
Reads the /proc/stat file to get the CPU load
@ret : cpu_lists - [[user, nice, system, idle, iowait, irq, softirq]]
for each cpu
total_cpu_load - [user, nice, system, idle, iowait, irq, softirqi,
ctxt, processes, procs_running, procs_blocked]
when - time info about when the command has been executed
"""
total_cpu_load = []
cpu_lists = []
fd = open ("/proc/stat", "r")
content = fd.readlines()
fd.close()
# Get the total cpu_load
search_item_regex = '(?P<search_item>(\d+( )*)+)'
cpu_regex = 'cpu\d*( )+'+ search_item_regex
sys_regex = '(ctxt|processes|procs_running|procs_blocked)( )+'+ search_item_regex
for line in content:
m = re.search(cpu_regex, line)
if m:
if 'cpu ' in line:
total_cpu_load = m.group('search_item').rsplit()[:7]
else:
cpu_lists.append(m.group('search_item').rsplit()[:7])
else:
m = re.search(sys_regex, line)
if not m:
continue
total_cpu_load.append(m.group('search_item'))
return total_cpu_load, cpu_lists
if __name__ == "__main__":
if len(sys.argv) < 2:
print "Usage: ./cpu_parser.py out_file time"
sys.exit(1)
total_cpu_load, cpu_list = read_cpu_file()
endl = '\n'
# print general statistics
fd = open(sys.argv[1], "a")
fd.write(sys.argv[2] + ' ' + ' '.join(total_cpu_load) + endl)
fd.close()
# print statistics per CPU
for cpu in cpu_list:
fd = open(sys.argv[1]+str(cpu_list.index(cpu)), "a")
fd.write(sys.argv[2] + ' ' + ' '.join(cpu) + endl)
fd.close()