~andrew-monkeysailor/python-snippets/ast-snippets

« back to all changes in this revision

Viewing changes to csv/vmstat-reader.py

  • Committer: Jono Bacon
  • Date: 2010-04-01 05:24:05 UTC
  • mfrom: (75.1.2 csv-snippets)
  • Revision ID: jono@system76-pc-20100401052405-8cg566aeyqk4z9kh
Some wicked csv snippets from Bruno!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
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]
 
8
 
 
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
 
11
# vmstat command.
 
12
# The full documentation for the csv module is available here:
 
13
# http://docs.python.org/library/csv.html
 
14
#
 
15
# The data used in the companion vmstat.log file was taken bu running the command:
 
16
# vmstat -n 5
 
17
 
 
18
#
 
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
 
21
#
 
22
import csv
 
23
import sys
 
24
 
 
25
# Derive the name of the CSV file from the name of the script
 
26
csvFile = sys.argv[0].replace('-reader.py', '.log')
 
27
 
 
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.
 
30
minor2major = {
 
31
    'r': 'procs',
 
32
    'b': 'procs',
 
33
    'swpd': 'memory',
 
34
    'free': 'memory',
 
35
    'buff': 'memory',
 
36
    'cache': 'memory',
 
37
    'inact': 'memory',  # to support the vmstat -a option if required
 
38
    'active': 'memory', # to support the vmstat -a option if required
 
39
    'si': 'swap',
 
40
    'so': 'swap',
 
41
    'bi': 'io',
 
42
    'bo': 'io',
 
43
    'in': 'system',
 
44
    'cs': 'system',
 
45
    'us': 'cpu',
 
46
    'sy': 'cpu',
 
47
    'id': 'cpu',
 
48
    'wa': 'cpu'
 
49
}
 
50
minors = []
 
51
 
 
52
# Initialise the content map by creating an empty sub-map against each
 
53
# unique major header
 
54
content = dict([(h, {}) for h in set(minor2major.values())])
 
55
 
 
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)
 
61
for row in reader:
 
62
    if reader.line_num == 1:
 
63
        """
 
64
        Ignore the first line as it contains major headers.
 
65
        """
 
66
    elif reader.line_num == 2:
 
67
        """
 
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
 
71
        in the content map.
 
72
        """
 
73
        minors = row
 
74
        for h in row:
 
75
            content[minor2major[h]][h] = []
 
76
    elif row[0] != minors[0] and row[0] != minor2major[minors[0]]:
 
77
        """
 
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
 
84
        have integer values.
 
85
        """
 
86
        for i, v in enumerate(row):
 
87
            content[minor2major[minors[i]]][minors[i]].append(int(v))
 
88
 
 
89
print "\nThe minor headers read from the file"
 
90
print minors
 
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()])
 
97