Blame SOURCES/processcsv.py

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