SAR is a tool to get usage metrics from all the resources in your system.
Here I present a method to parse the disk usage file in order to plot it for a better visualization.
In [45]:
with open('metrics/disk.txt', 'r') as f:
metrics = f.readlines()
All the collected txt files start with this line:
In [46]:
print metrics[0]
And the first line of data contains all the labels:
In [47]:
print metrics[2]
DEV indicate the device from where the metrics are taken, so we end up with a line per device:
In [49]:
for l in metrics[3:21]:
print l
We can save the data in a dictionary containing as a key the device string, and as values, tuples containing (time, metrics):
In [245]:
labels = metrics[2].split()[1:]
y, m, d = metrics[0].split()[3].split('-')
print labels, y, m, d
In [246]:
import string
import datetime
devs = {}
for l in metrics[3:]:
metrics_line = l.split()
dev = metrics_line[1]
t = metrics_line[0]
data = metrics_line[2:]
#We have to convert the timestamps to python datetime objects, and comma
#separated floats to point separated floats
t = datetime.datetime(int(y), int(m), int(d), int(t.split(':')[0]), int(t.split(':')[1]), int(t.split(':')[2]))
for i in range(len(data)):
data[i] = string.atof(data[i].replace(',', '.'))
#And then we save it
if devs.has_key(dev):
devs[dev].append((t, data))
else:
devs[dev] = [(t, data)]
In [248]:
#These are our devices
devs.keys()
Out[248]:
In [271]:
for dev in devs.iterkeys():
for i in range(len(labels[1:])): #We do not count the DEV label
dates = []
values= []
[dates.append(t) for t, v in devs[dev]]
[values.append(v[i]) for t, v in devs[dev]]
fig = figure()
axes = fig.add_subplot(111)
axes.grid(True)
axes.set_title(str(dev))
axes.set_xlabel("Time")
axes.set_ylabel(labels[1+i])
axes.plot(dates, values, linestyle='-')
In [ ]: