3
# [SNIPPET_NAME: vmstat Reader]
4
# [SNIPPET_CATEGORIES: csv]
5
# [SNIPPET_DESCRIPTION: Custom CSV reader to read files like vmstat output]
6
# [SNIPPET_AUTHOR: Bruno Girin <brunogirin@gmail.com>]
7
# [SNIPPET_LICENSE: GPL]
9
# This snippet demonstrates how to use the csv module with a custom separator
10
# in order to read space separated value files such as the output of the
12
# The full documentation for the csv module is available here:
13
# http://docs.python.org/library/csv.html
15
# The data used in the companion vmstat.log file was taken bu running the command:
19
# First things first, we need to import the csv module
20
# Also import sys to get argv[0], which holds the name of the script
25
# Derive the name of the CSV file from the name of the script
26
csvFile = sys.argv[0].replace('-reader.py', '.log')
28
# Create a map from minor to major header as the minor headers are easy to
29
# associate to columns, which is not the case for major headers.
37
'inact': 'memory', # to support the vmstat -a option if required
38
'active': 'memory', # to support the vmstat -a option if required
52
# Initialise the content map by creating an empty sub-map against each
54
content = dict([(h, {}) for h in set(minor2major.values())])
56
print('Reading file %s' % csvFile)
57
# Create the reader and specify the delimier to be a space; also set the
58
# skipinitialspace flag to true to ensure that several spaces are seen as a
59
# single delimiter and that initial spaces in a line are ignored
60
reader=csv.reader(open(csvFile), delimiter=' ', skipinitialspace=True)
62
if reader.line_num == 1:
64
Ignore the first line as it contains major headers.
66
elif reader.line_num == 2:
68
If we are on the first line, create the headers list from the first row.
69
We also keep a copy of the minor headers, in the order that they appear
70
in the file to ensure that we can map the values to the correct entry
75
content[minor2major[h]][h] = []
76
elif row[0] != minors[0] and row[0] != minor2major[minors[0]]:
78
If the -n option was not specified when running the vmstat command,
79
major and minor headers are repeated so we need to ensure that we
80
ignore such lines and only deal with lines that contain actual data.
81
For each value in the row, we append it to the respective entry in
82
the content dictionary. In addition, we transform the value to an int
83
before appending it as we know that the content of the log should only
86
for i, v in enumerate(row):
87
content[minor2major[minors[i]]][minors[i]].append(int(v))
89
print "\nThe minor headers read from the file"
91
print "\nThe CPU user process stats"
92
print content['cpu']['us']
93
print "\nMinimum free memory in the data set"
94
print min(content['memory']['free'])
95
print "\nMaximum IO, either input or output"
96
print max([max(l) for l in content['io'].values()])