-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
52 lines (39 loc) · 1.51 KB
/
parse.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
from xml.etree import ElementTree
import subprocess
import json
import sys
if __name__ == '__main__':
document = ElementTree.parse(sys.argv[1])
mems = document.findall('.//fb_memory_usage')
utils = document.findall('.//utilization')
procs = document.findall('.//process_info')
ret = {'date': sys.argv[2]}
for mem in mems:
for mem_ in mem:
ret[mem_.tag] = mem_.text
for util in utils:
for util_ in util:
ret[util_.tag] = util_.text
ret['procs'] = []
for proc in procs:
mini_dic = {}
for proc_ in proc:
if proc_.tag == 'pid':
cmd = 'ps aux| grep {}'.format(proc_.text)
ps = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
for line in ps.communicate()[0].decode().split('\n'):
if proc_.text in line:
elements = line.split()
pid = elements[1]
time = elements[9]
if proc_.text == pid:
mini_dic['time'] = time
if proc_.tag == 'used_memory':
mini_dic[proc_.tag] = proc_.text
ret['procs'].append(mini_dic)
with open(sys.argv[1].replace('xml', 'json'), 'a+') as log_file:
log_file.seek(len(log_file.readline()) + 1)
log_file.write(json.dumps(ret))
log_file.write('\n')