-
Notifications
You must be signed in to change notification settings - Fork 1
/
oswatcher_top_to_csv.py
73 lines (64 loc) · 2.51 KB
/
oswatcher_top_to_csv.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
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
"""
More information on how to install oswatcher tool.
https://www.2daygeek.com/oswbb-how-to-install-and-configure-oswatcher-black-box-for-system-diagnostics/
"""
#OWSTOP file path
OSWATCHER_TOP_FILE = './test.dat'
FILE_TO_WRITE = './output.csv'
CURRENT = None
parts = []
replacements =[
#find -> replace
(' ',''),
('%us',''),
('%sy',''),
('%ni',''),
('%id',''),
('%wa',''),
('%hi',''),
('%si',''),
('%st',''),
('%total',''),
('%used',''),
('%free',''),
('%buffers',''),
('%cached',''),
('%running',''),
('%sleeping',''),
('%stopped',''),
('%zombie','')
]
#Iterate through the oswtop file and extract time & system stats.
with open(OSWATCHER_TOP_FILE,'r') as f:
for line in f:
if line.startwith('zzz'):
CURRENT = [(line.replace('zzz ***','')).strip()]
parts.append(CURRENT)
elif CURRENT is not None:
if 'load average:' in line:
linestr = line[line.find('load average:')]
for target, replacement in replacements:
linestr = linestr.replace(target, replacement)
CURRENT.append((linestr.replace('loadaverage:','')).strip())
#print(current)
if line.startwith('Cpu(s):'):
for target, replacement in replacements:
linestr = linestr.replace(target, replacement)
CURRENT.append((linestr.replace('Cpu(s):','')).strip())
if line.startwith('Mem:'):
for target, replacement in replacements:
linestr = linestr.replace(target, replacement)
CURRENT.append((linestr.replace('Mem:','')).strip())
if line.startwith('Swap:'):
for target, replacement in replacements:
linestr = linestr.replace(target, replacement)
CURRENT.append((linestr.replace('Swap:','')).strip())
if line.startwith('Tasks:'):
for target, replacement in replacements:
linestr = linestr.replace(target, replacement)
CURRENT.append((linestr.replace('Tasks:','')).strip())
#Save extracted data to the file
with open(FILE_TO_WRITE,'w+') as f:
f.write("time,1MinLoad,5MinLoad,15MinLoad,Task-Total,Task-run,Tasks-sleep,Task-stop,Task-zombie,%us,%sy,%ni,%id,%wa,%hi,%si,%st,Mem-total,Mem-used,Mem-free,Mem-buffer,Swp-total,Swp-used,Swp-free,Swp-cached\n")
f.write('\n'.join((','.join(part) for part in parts)))