fbac9f
#!/usr/bin/python3
fbac9f
#
fbac9f
# https://bugzilla.redhat.com/show_bug.cgi?id=665817
fbac9f
#
fbac9f
# Usage:
fbac9f
#
fbac9f
#   virt-top --csv data.csv
fbac9f
#   processcsv.py < data.csv
fbac9f
#
fbac9f
# Note this OVERWRITES the following files in the current directory:
fbac9f
#
fbac9f
#   global.csv         # all the global data
fbac9f
#   domain<NN>.csv     # data for domain ID <NN> (multiple files)
fbac9f
fbac9f
import sys
fbac9f
import csv
fbac9f
fbac9f
rows = csv.reader (sys.stdin)
fbac9f
fbac9f
# Get the header row.
fbac9f
header = next(rows)
fbac9f
fbac9f
# Find the index of the 'Hostname' and 'Time' cols (usually first two).
fbac9f
hostname_i = header.index ("Hostname")
fbac9f
time_i = header.index ("Time")
fbac9f
fbac9f
# Find the index of the 'Domain ID' column (i) and the number of
fbac9f
# columns per domain (w).
fbac9f
i = header.index ("Domain ID")
fbac9f
w = len (header) - i
fbac9f
fbac9f
dom_header = header[i:i+w]
fbac9f
dom_header.insert (0, "Hostname")
fbac9f
dom_header.insert (1, "Time")
fbac9f
fbac9f
gfile = open ("global.csv", "w")
fbac9f
gfile_writer = csv.writer (gfile)
fbac9f
gfile_writer.writerow (header[0:i])
fbac9f
fbac9f
dfiles = dict()
fbac9f
fbac9f
# Process all the remaining data rows.
fbac9f
for data in rows:
fbac9f
    # Global data is columns 0..i-1
fbac9f
    gfile_writer.writerow (data[0:i])
fbac9f
fbac9f
    hostname = data[hostname_i]
fbac9f
    time = data[time_i]
fbac9f
fbac9f
    # For each domain ...
fbac9f
    for j in range(i,len(data),w):
fbac9f
        dom = data[j:j+w]
fbac9f
        domid = dom[0]
fbac9f
fbac9f
        if domid in dfiles:
fbac9f
            dfile_writer = dfiles[domid]
fbac9f
        else:
fbac9f
            dfile = open ("domain%s.csv" % domid, "w")
fbac9f
            dfile_writer = csv.writer (dfile)
fbac9f
            dfile_writer.writerow (dom_header)
fbac9f
            dfiles[domid] = dfile_writer
fbac9f
fbac9f
        dom.insert (0, hostname)
fbac9f
        dom.insert (1, time)
fbac9f
        dfile_writer.writerow (dom)