A short time ago I needed to understand how much free space I had on each table space of a DB2 database, and I did not have access to any management tools that would have made my task easier. My only option was to run db2 list tablespaces show detail
at the command line, but the output of the command cannot be imported into Excel easily. The solution I settled on was to modify a Python script originally created to read ini files. The script is listed at the bottom of this message. To use it, pipe the output of list tablespaces show detail
into a file. Then edit the script to read from the file. The script will output a tab-delimited list of table spaces which can be copied and pasted into Excel.
import sys
import re
SECTION = re.compile('^\s*Tablespace ID\s*=\s*([0-9]+)\s*$')
PARAM = re.compile('^\s*(.*)\s*=\s*(.*)\s*$')
COMMENT = re.compile('^\s*;.*$')
d = {}
f = open('c:\\list_tablespaces_output.txt')
for line in f:
if COMMENT.match(line): continue
m = SECTION.match(line)
if m:
section, = m.groups()
d[section.strip()] = {}
m = PARAM.match(line)
if m:
key, val = m.groups()
d[section.strip()][key.strip()] = val.strip()
f.close()
print "Name\tUsed pages\tTotal pages\tUsed pages\tFree pages\tHigh water mark (pages)\tPage size (bytes)\t% Free"
for k, v in d.items():
#print k, v
if v["Free pages"] != "Not applicable":
pctfree = float(v["Free pages"])/float(v["Total pages"])
else:
pctfree = 0
print "{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}".format(v["Name"]
,v["Used pages"]
,v["Total pages"]
,v["Used pages"]
,v["Free pages"]
,v["High water mark (pages)"]
,v["Page size (bytes)"]
,pctfree)