#!/usr/bin/env python

# This library is free software; you can redistribute it and/or
# modify it under the terms of version 2.1 of the GNU Lesser General Public
# License as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Copyright (c) 2005, 2007 XenSource Ltd.


#
# To add new entries to the bugtool, you need to:
#
# Create a new capability.  These declare the new entry to the GUI, including
# the expected size, time to collect, privacy implications, and whether the
# capability should be selected by default.  One capability may refer to
# multiple files, assuming that they can be reasonably grouped together, and
# have the same privacy implications.  You need:
#
#   A new CAP_ constant.
#   A cap() invocation to declare the capability.
#
# You then need to add calls to main() to collect the files.  These will
# typically be calls to the helpers file_output(), tree_output(), cmd_output(),
# or func_output().
#

import getopt
import re
import os
import StringIO
import sys
import tarfile
import time
import commands
import pprint
from xml.dom.minidom import parse, getDOMImplementation
import zipfile
from subprocess import Popen, PIPE
from select import select
from signal import SIGTERM, SIGUSR1, SIGHUP
import platform
import fcntl
import glob
import urllib
import socket
import base64

try:
    import hashlib
    def md5_new():
        return hashlib.md5()
except:
    import md5
    def md5_new():
        return md5.new()

# Monkey-patch zipfile's __del__ function to be less stupid
#   Specifically, it calls close which further writes to the file, which
#   fails with ENOSPC if the root filesystem is full
zipfile_del = zipfile.ZipFile.__del__
def exceptionless_del(*argl, **kwargs):
    try:
        zipfile_del(*argl, **kwargs)
    except:
        pass
zipfile.ZipFile.__del__ = exceptionless_del

import XenAPI
from xen.lowlevel.xc import xc

OS_RELEASE = platform.release()

#
# Files & directories
#

BUG_DIR = "/var/opt/xen/bug-report"
PLUGIN_DIR = "/etc/xensource/bugtool"
XAPI_BLOBS = '/var/xapi/blobs'
EXTLINUX_CONFIG = '/boot/extlinux.conf'
BOOT_KERNEL = '/boot/vmlinuz-' + OS_RELEASE
BOOT_INITRD = '/boot/initrd-' + OS_RELEASE + '.img'
PROC_PARTITIONS = '/proc/partitions'
FSTAB = '/etc/fstab'
PROC_MOUNTS = '/proc/mounts'
ISCSI_CONF = '/etc/iscsi/iscsid.conf'
ISCSI_INITIATOR = '/etc/iscsi/initiatorname.iscsi'
LVM_CACHE = '/etc/lvm/cache/.cache'
LVM_CONFIG = '/etc/lvm/lvm.conf'
PROC_CPUINFO = '/proc/cpuinfo'
PROC_MEMINFO = '/proc/meminfo'
PROC_SLABINFO = '/proc/slabinfo'
PROC_ZONEINFO = '/proc/zoneinfo'
PROC_IOMEM = '/proc/iomem'
PROC_IOPORTS = '/proc/ioports'
PROC_INTERRUPTS = '/proc/interrupts'
PROC_SCSI = '/proc/scsi/scsi'
FIRSTBOOT_DIR = '/etc/firstboot.d'
PROC_VERSION = '/proc/version'
PROC_MODULES = '/proc/modules'
PROC_DEVICES = '/proc/devices'
PROC_FILESYSTEMS = '/proc/filesystems'
PROC_CMDLINE = '/proc/cmdline'
PROC_CONFIG = '/proc/config.gz'
PROC_USB_DEV = '/proc/bus/usb/devices'
PROC_XEN_BALLOON = '/proc/xen/balloon'
PROC_NET_BONDING_DIR = '/proc/net/bonding'
PROC_NET_VLAN_DIR = '/proc/net/vlan'
PROC_NET_SOFTNET_STAT = '/proc/net/softnet_stat'
PROC_DRIVER_CCISS_DIR = '/proc/driver/cciss'
MODPROBE_CONF = '/etc/modprobe.conf'
MODPROBE_DIR = '/etc/modprobe.d'
BOOT_TIME_CPUS = '/etc/xensource/boot_time_cpus'
BOOT_TIME_MEMORY = '/etc/xensource/boot_time_memory'
SYSCONFIG_CLOCK = '/etc/sysconfig/clock'
SYSCONFIG_HWCONF = '/etc/sysconfig/hwconf'
SYSCONFIG_NETWORK = '/etc/sysconfig/network'
SYSCONFIG_NETWORK_SCRIPTS = '/etc/sysconfig/network-scripts'
SYSCONFIG_RENAME_DATA_DIR = '/etc/sysconfig/network-scripts/interface-rename-data'
NET_UDEV_RULES = '/etc/udev/rules.d/60-net.rules'
IFCFG_RE = re.compile(r'^.*/ifcfg-.*')
ROUTE_RE = re.compile(r'^.*/route-.*')
NETWORK_DBCACHE = '/var/xapi/network.dbcache'
NETWORKD_DB = '/var/xapi/networkd.db'
RESOLV_CONF = '/etc/resolv.conf'
MPP_CONF = '/etc/mpp.conf'
MULTIPATH_CONF = '/etc/multipath.conf'
NSSWITCH_CONF = '/etc/nsswitch.conf'
NTP_CONF = '/etc/ntp.conf'
IPTABLES_CONFIG = '/etc/sysconfig/iptables-config'
HOSTS = '/etc/hosts'
HOSTS_ALLOW = '/etc/hosts.allow'
HOSTS_DENY = '/etc/hosts.deny'
DHCP_LEASE_DIR = '/var/lib/dhclient'
OPENVSWITCH_CORE_DIR = '/var/xen/openvswitch'
OPENVSWITCH_CONF = '/etc/ovs-vswitchd.conf'
OPENVSWITCH_CONF_DB = '/etc/openvswitch/conf.db'
OPENVSWITCH_VSWITCHD_PID = '/var/run/openvswitch/ovs-vswitchd.pid'
VAR_LOG_DIR = '/var/log/'
VNCTERM_CORE_DIR = '/var/xen/vncterm'
XENSOURCE_INVENTORY = '/etc/xensource-inventory'
OEM_CONFIG_DIR = '/var/xsconfig'
OEM_CONFIG_FILES_RE = re.compile(r'^.*xensource-inventory$')
OEM_DB_FILES_RE = re.compile(r'^.*state\.db')
INITIAL_INVENTORY = '/opt/xensource/etc/initial-inventory'
VENDORKERNEL_INVENTORY = '/etc/vendorkernel-inventory'
STATIC_VDIS = '/etc/xensource/static-vdis'
POOL_CONF = '/etc/xensource/pool.conf'
NETWORK_CONF = '/etc/xensource/network.conf'
PTOKEN = '/etc/xensource/ptoken'
XAPI_CONF = '/etc/xensource/xapi.conf'
XAPI_SSL_CONF = '/etc/xensource/xapi-ssl.conf'
XAPI_GLOBS_CONF = '/etc/xensource/xapi_globs.conf'
DB_CONF = '/etc/xensource/db.conf'
DB_CONF_RIO = '/etc/xensource/db.conf.rio'
DB_DEFAULT_FIELDS = '/etc/xensource/db-default-fields'
DB_SCHEMA_SQL = '/etc/xensource/db_schema.sql'
XENSTORED_DB = '/var/lib/xenstored/tdb'
HOST_CRASHDUMPS_DIR = '/var/crash'
HOST_CRASHDUMP_LOGS_EXCLUDES_RE = re.compile('.*/\.sacrificial-space-for-logs$')
XAPI_DEBUG_DIR = '/var/xapi/debug'
LOG_CONF = '/etc/xensource/log.conf'
INSTALLED_REPOS_DIR = '/etc/xensource/installed-repos'
PATCH_APPLIED_DIR = '/var/patch/applied'
XENSERVER_LOGS = \
    [ VAR_LOG_DIR + x for x in
      ['xensource.log', 'audit.log', 'xenstored-access.log', 'SMlog', 'isl_trace.log',  'xen/xenstored-trace.log',
       'xen/xen-hotplug.log', 'xen/domain-builder-ng.log', 'squeezed.log',
       'openvswitch/ovs-brcompatd.log', 'openvswitch/ovs-vswitchd.log', 'openvswitch/ovsdb-server.log', 'xcp-networkd.log' ] +
      [ f % n for n in range(1, 20) \
            for f in ['xensource.log.%d', 'xensource.log.%d.gz','SMlog.%d', 'SMlog.%d.gz', 'isl_trace.log.%d', 'isl_trace.log.%d.gz',
                      'audit.log.%d', 'audit.log.%d.gz', 'xenstored-access.log.%d', 'xenstored-access.log.%d.gz', \
                      'xen/xenstored-access.log.%d', 'xen/xenstored-access.log.%d.gz', 'squeezed.log.%d', \
                      'openvswitch/ovs-brcompatd.log.%d', 'openvswitch/ovs-brcompatd.log.%d.gz', \
                      'openvswitch/ovs-vswitchd.log.%d', 'openvswitch/ovs-vswitchd.log.%d.gz', \
                      'openvswitch/ovsdb-server.log.%d', 'openvswitch/ovsdb-server.log.%d.gz', \
                      'xcp-networkd.log.%d', 'xcp-networkd.log.%d.gz']]] \
      + glob.glob('/tmp/qemu.[0-9]*') + glob.glob('/var/log/sa/sa[0-9][0-9]')
OEM_XENSERVER_LOGS_RE = re.compile(r'^.*xensource\.log$')
XHA_LOG = '/var/log/xha.log'
XHAD_CONF = '/etc/xensource/xhad.conf'
YUM_LOG = '/var/log/yum.log'
YUM_REPOS_DIR = '/etc/yum.repos.d'
PAM_DIR = '/etc/pam.d'
FIST_RE = re.compile(r'.*/fist_')
LICENSE_LOGS = \
             [ VAR_LOG_DIR + x for x in
               ['v6d.log'] +
               [ f % n for n in range(1, 20) \
                 for f in ['v6d.log.%d', 'v6d.log.%d.gz' ]]]
LWIDENTITY_JOIN_LOG = '/tmp/lwidentity.join.log'
HOSTS_LWIDENTITY_ORIG = '/etc/hosts.lwidentity.orig'
KRB5_CONF = '/etc/krb5.conf'
LIKEWISE_DIR = '/var/lib/likewise'
XENGUEST_LOG = '/tmp/xenguest.log'
QEMU_DIR = '/var/lib/xen'
QEMU_RESUME_RE = re.compile(r'.*/qemu-resume\..*')
INTERFACE_RENAME_LOG = '/var/log/interface-rename.log'
SYS_VIF_GROUPS = '/sys/devices/xen-backend/vif-*-*/group'

#
# External programs
#

def find_lvm(name):
    return (os.path.isfile('/sbin/' + name) and '/sbin/' or '/usr/sbin/') + name


ACPIDUMP = '/usr/sbin/acpidump'
ARP = '/sbin/arp'
ARPTABLES = '/sbin/arptables'
BIN_STATIC_VDIS = '/opt/xensource/bin/static-vdis'
BIOSDEVNAME = '/sbin/biosdevname'
BRCTL = '/usr/sbin/brctl'
CAT = '/bin/cat'
CHKCONFIG = '/sbin/chkconfig'
CSL = '/opt/Citrix/StorageLink/bin/csl'
DF = '/bin/df'
DU = '/usr/bin/du'
DMESG = '/bin/dmesg'
DMIDECODE = '/usr/sbin/dmidecode'
DMSETUP = '/sbin/dmsetup'
EBTABLES = '/sbin/ebtables'
ETHTOOL = '/sbin/ethtool'
FDISK = '/sbin/fdisk'
FIND = '/usr/bin/find'
HA_QUERY_LIVESET = '/opt/xensource/debug/debug_ha_query_liveset'
HDPARM = '/sbin/hdparm'
IFCONFIG = '/sbin/ifconfig'
IPTABLES = '/sbin/iptables'
ISCSIADM = '/sbin/iscsiadm'
LIST_DOMAINS = '/opt/xensource/bin/list_domains'
LOSETUP = '/sbin/losetup'
LS = '/bin/ls'
LSPCI = '/sbin/lspci'
LVDISPLAY = find_lvm('lvdisplay')
LVS = find_lvm('lvs')
MD5SUM = '/usr/bin/md5sum'
MODINFO = '/sbin/modinfo'
MULTIPATHD = '/sbin/multipathd'
NETSTAT = '/bin/netstat'
NTPQ = '/usr/sbin/ntpq'
OVS_APPCTL = '/usr/bin/ovs-appctl'
OVS_DPCTL = '/usr/bin/ovs-dpctl'
OVS_OFCTL = '/usr/bin/ovs-ofctl'
OVS_VSCTL = '/usr/bin/ovs-vsctl'
PS = '/bin/ps'
PVS = find_lvm('pvs')
QLOGIC_FW = '/usr/bin/strings /lib/firmware/ql2*.bin | /bin/grep -i ver'
ROUTE = '/sbin/route'
RPM = '/bin/rpm'
SG_MAP = '/usr/bin/sg_map'
SQLITE = '/usr/bin/sqlite3'
SYSCTL = '/sbin/sysctl'
TC = '/sbin/tc'
ULIMIT = 'ulimit -a'
UPTIME = '/usr/bin/uptime'
VGS = find_lvm('vgs')
VGSCAN = find_lvm('vgscan')
XAPI_DB_PROCESS = '/opt/xensource/bin/xapi-db-process'
XE = '/opt/xensource/bin/xe'
XENPM = '/usr/sbin/xenpm'
XENSTORE_LS = '/usr/bin/xenstore-ls'
XL = '/usr/sbin/xl'
XS = '/opt/xensource/debug/xs'
ZCAT = '/bin/zcat'

#
# PII -- Personally identifiable information.  Of particular concern are
# things that would identify customers, or their network topology.
# Passwords are never to be included in any bug report, regardless of any PII
# declaration.
#
# NO            -- No PII will be in these entries.
# YES           -- PII will likely or certainly be in these entries.
# MAYBE         -- The user may wish to audit these entries for PII.
# IF_CUSTOMIZED -- If the files are unmodified, then they will contain no PII,
# but since we encourage customers to edit these files, PII may have been
# introduced by the customer.  This is used in particular for the networking
# scripts in dom0.
#

PII_NO            = 'no'
PII_YES           = 'yes'
PII_MAYBE         = 'maybe'
PII_IF_CUSTOMIZED = 'if_customized'
KEY      = 0
PII      = 1
MIN_SIZE = 2
MAX_SIZE = 3
MIN_TIME = 4
MAX_TIME = 5
MIME     = 6
CHECKED  = 7
HIDDEN   = 8

MIME_DATA = 'application/data'
MIME_TEXT = 'text/plain'

INVENTORY_XML_ROOT = "system-status-inventory"
INVENTORY_XML_SUMMARY = 'system-summary'
INVENTORY_XML_ELEMENT = 'inventory-entry'
CAP_XML_ROOT = "system-status-capabilities"
CAP_XML_ELEMENT = 'capability'


CAP_BLOBS                = 'blobs'
CAP_BOOT_LOADER          = 'boot-loader'
CAP_CVSM                 = 'CVSM'
CAP_DEVICE_MODEL         = 'device-model'
CAP_DISK_INFO            = 'disk-info'
CAP_FIRSTBOOT            = 'firstboot'
CAP_HARDWARE_INFO        = 'hardware-info'
CAP_HDPARM_T             = 'hdparm-t'
CAP_HIGH_AVAILABILITY    = 'high-availability'
CAP_HOST_CRASHDUMP_LOGS  = 'host-crashdump-logs'
CAP_KERNEL_INFO          = 'kernel-info'
CAP_LOSETUP_A            = 'loopback-devices'
CAP_MULTIPATH            = 'multipath'
CAP_NETWORK_CONFIG       = 'network-config'
CAP_NETWORK_STATUS       = 'network-status'
CAP_OEM                  = 'oem'
CAP_PAM                  = 'pam'
CAP_PROCESS_LIST         = 'process-list'
CAP_PERSISTENT_STATS     = 'persistent-stats'
CAP_SYSTEM_LOGS          = 'system-logs'
CAP_SYSTEM_SERVICES      = 'system-services'
CAP_TAPDISK_LOGS         = 'tapdisk-logs'
CAP_VNCTERM              = 'vncterm'
CAP_XAPI_DEBUG           = 'xapi-debug'
CAP_XAPI_SUBPROCESS      = 'xapi-subprocess'
CAP_XENRT                = 'xenrt'
CAP_XENSERVER_CONFIG     = 'xenserver-config'
CAP_XENSERVER_DOMAINS    = 'xenserver-domains'
CAP_XENSERVER_DATABASES  = 'xenserver-databases'
CAP_XENSERVER_INSTALL    = 'xenserver-install'
CAP_XENSERVER_LOGS       = 'xenserver-logs'
CAP_XEN_INFO             = 'xen-info'
CAP_XHA_LIVESET          = 'xha-liveset'
CAP_YUM                  = 'yum'

KB = 1024
MB = 1024 * 1024

# max size of vswitch database
CAP_NETWORK_CONFIG_OVERHEAD = 10 * MB

# max size of xenserver databases
CAP_XENSERVER_DATABASES_SIZE_OVERHEAD = 2 * MB
# max capture time of xenserver databases
CAP_XENSERVER_DATABASES_TIME_OVERHEAD = 40

caps = {}
cap_sizes = {}
unlimited_data = False
unlimited_time = False
dbg = False

def cap(key, pii=PII_MAYBE, min_size=-1, max_size=-1, min_time=-1,
        max_time=-1, mime=MIME_TEXT, checked=True, hidden=False):
    if  os.getenv('XEN_RT') and max_time > 0:
        max_time *= 5
    caps[key] = (key, pii, min_size, max_size, min_time, max_time, mime,
                 checked, hidden)
    cap_sizes[key] = 0


cap(CAP_BLOBS,               PII_NO,                    max_size=5*MB)
cap(CAP_BOOT_LOADER,         PII_NO,                    max_size=3*KB,
    max_time=10)
cap(CAP_CVSM,                PII_NO,                    max_size=3*MB,
    max_time=120)
cap(CAP_DEVICE_MODEL,        PII_YES,   min_size=200*KB, max_size=8*MB)
cap(CAP_DISK_INFO,           PII_MAYBE,                 max_size=8*MB,
    max_time=120)
cap(CAP_FIRSTBOOT,           PII_YES,   min_size=60*KB, max_size=80*KB)
cap(CAP_HARDWARE_INFO,       PII_MAYBE,                 max_size=800*KB,
    max_time=60)
cap(CAP_HDPARM_T,            PII_NO,    min_size=0,     max_size=5*KB,
    min_time=20, max_time=90, checked=False, hidden=True)
cap(CAP_HIGH_AVAILABILITY,   PII_MAYBE,                 max_size=5*MB)
cap(CAP_HOST_CRASHDUMP_LOGS, PII_MAYBE)
cap(CAP_KERNEL_INFO,         PII_MAYBE,                 max_size=360*KB,
    max_time=50)
cap(CAP_LOSETUP_A,           PII_MAYBE,                 max_size=KB, max_time=5)
cap(CAP_MULTIPATH,           PII_MAYBE,                 max_size=20*KB,
    max_time=10)
cap(CAP_NETWORK_CONFIG,      PII_IF_CUSTOMIZED,
                                        min_size=0,     max_size=100*KB)
cap(CAP_NETWORK_STATUS,      PII_YES,                   max_size=20*KB,
    max_time=30)
cap(CAP_PAM,                 PII_NO,                    max_size=50*KB)
cap(CAP_PERSISTENT_STATS,    PII_NO,                    max_size=50*MB,
    max_time=60, checked=False, hidden=True)
cap(CAP_PROCESS_LIST,        PII_YES,                   max_size=30*KB,
    max_time=60)
cap(CAP_SYSTEM_LOGS,         PII_MAYBE,                 max_size=50*MB,
    max_time=10)
cap(CAP_SYSTEM_SERVICES,     PII_NO,                    max_size=5*KB,
    max_time=20)
cap(CAP_TAPDISK_LOGS,        PII_NO,                    max_size=10*MB)
cap(CAP_VNCTERM,             PII_MAYBE, checked = False)
cap(CAP_XAPI_DEBUG,          PII_MAYBE,                 max_size=10*MB)
cap(CAP_XAPI_SUBPROCESS,     PII_NO,                    max_size=5*KB,
    max_time=10)
cap(CAP_XENRT,               PII_NO,    min_size=0,     max_size=500*MB,
    checked=False, hidden=True)
cap(CAP_XENSERVER_CONFIG,    PII_MAYBE,                 max_size=80*KB,
    max_time=10)
cap(CAP_XENSERVER_DOMAINS,   PII_NO,                    max_size=1*KB,
    max_time=10)
cap(CAP_XENSERVER_DATABASES, PII_YES,                   max_size=10*KB,
    max_time=5)
cap(CAP_XENSERVER_INSTALL,   PII_MAYBE, min_size=10*KB, max_size=600*KB)
cap(CAP_XENSERVER_LOGS,      PII_MAYBE, min_size=0,     max_size=70*MB)
cap(CAP_XEN_INFO,            PII_MAYBE,                 max_size=20*KB,
    max_time=10)
cap(CAP_XHA_LIVESET,         PII_MAYBE,                 max_size=10*KB,
    max_time=10)
cap(CAP_YUM,                 PII_IF_CUSTOMIZED,         max_size=30*KB,
    max_time=30)

ANSWER_YES_TO_ALL = False
SILENT_MODE = False
entries = None
data = {}
dev_null = open('/dev/null', 'r+')

def no_unicode(x):
    if isinstance(x, unicode):
        return x.encode('utf-8')
    return x

def output(x):
    global SILENT_MODE
    if not SILENT_MODE:
        print x

def output_ts(x):
    output("[%s]  %s" % (time.strftime("%x %X %Z"), x))

def cmd_output(cap, args, label = None, filter = None):
    if cap in entries:
        if not label:
            if isinstance(args, list):
                a = [aa for aa in args]
                a[0] = os.path.basename(a[0])
                label = ' '.join(a)
            else:
                label = args
        data[label] = {'cap': cap, 'cmd_args': args, 'filter': filter}

def dir_list(cap, path_list, recursive = False):
    flags = '-l'
    if recursive:
        flags = '-lR'

    pl = []
    map(lambda x: pl.extend(glob.glob(x)), path_list)

    for p in pl:
        cmd_output(cap, [LS, flags, p])

def file_output(cap, path_list):
    if cap in entries:
        pl = []
        map(lambda x: pl.extend(glob.glob(x)), path_list)

        for p in pl:
            try:
                s = os.stat(p)
                if unlimited_data or caps[cap][MAX_SIZE] == -1 or \
                        cap_sizes[cap] < caps[cap][MAX_SIZE] or s.st_size == 0:
                    data[p] = {'cap': cap, 'filename': p}
                    cap_sizes[cap] += s.st_size
                else:
                    output("Omitting %s, size constraint of %s exceeded" % (p, cap))
            except:
                pass

def tree_output(cap, path, pattern = None, negate = False):
    if cap in entries:
        if os.path.exists(path):
            for f in os.listdir(path):
                fn = os.path.join(path, f)
                if os.path.isfile(fn) and matches(fn, pattern, negate):
                    file_output(cap, [fn])
                elif os.path.isdir(fn):
                    tree_output(cap, fn, pattern, negate)

def func_output(cap, label, func):
    if cap in entries:
        t = str(func).split()
        data[label] = {'cap': cap, 'func': func}

def collect_data(subdir, archive):
    process_lists = {}

    for (k, v) in data.items():
        name = construct_filename(subdir, k, v)
        cap = v['cap']
        filename = v.get('filename')
        if v.has_key('cmd_args'):
            v['output'] = StringIOmtime()
            if not process_lists.has_key(cap):
                process_lists[cap] = []
            process_lists[cap].append(ProcOutputAndArchive(v['cmd_args'], caps[cap][MAX_TIME], name, archive, v))
        elif filename and (filename.startswith('/proc/') or
                           filename.startswith('/sys/')):
            # proc files must be read into memory
            try:
                f = open(v['filename'], 'r')
                s = f.read(unlimited_data and -1 or caps[cap][MAX_SIZE])
                f.close()
                if unlimited_data or caps[cap][MAX_SIZE] == -1 or \
                        cap_sizes[cap] < caps[cap][MAX_SIZE] or len(s) == 0:
                    v['output'] = StringIOmtime(s)
                    archive.addFile(name, v['output'])
                    v['md5'] = md5sum(v)
                    del v['output']
                    cap_sizes[cap] += len(s)
                else:
                    output("Omitting %s, size constraint of %s exceeded" % (v['filename'], cap))
            except:
                pass
        elif v.has_key('func'):
            try:
                s = v['func'](cap)
            except Exception, e:
                s = str(e)
            if unlimited_data or caps[cap][MAX_SIZE] == -1 or \
                    cap_sizes[cap] < caps[cap][MAX_SIZE]:
                v['output'] = StringIOmtime(s)
                archive.addFile(name, v['output'])
                v['md5'] = md5sum(v)
                del v['output']
                cap_sizes[cap] += len(s)
            else:
                output("Omitting %s, size constraint of %s exceeded" % (k, cap))
        elif filename:
            try:
                archive.addRealFile(name, filename)
            except:
                pass

    run_procs(process_lists.values())

    # collect all output (output from processes)
    for (k, v) in data.items():
        if 'output' in v:
            name = construct_filename(subdir, k, v)
            archive.addFile(name, v['output'])
            v['md5'] = md5sum(v)
            del v['output']

def usage():
    return '''Usage: xenserver-status-report [OPTION]...
Capture information to help diagnose bugs.

 --capabilities      output capabilities informations
 --output            specify output format (tar, tar.bz2 or zip)
 -s, --silent        silent mode
 --entries=<list>    specify which capabilities (separated by commas)
 -y, --yestoall      confirm every file automatically
 --outfd=<file>      specify output file
 -a, --all           enable all capabilities
 -u, --unlimited     do not limit file size
 -d, --debug         enable debug output
 --help              this help'''

def main(argv = None):
    global ANSWER_YES_TO_ALL, SILENT_MODE
    global entries, data, dbg
    global unlimited_data, unlimited_time

    output_type = 'tar.bz2'
    output_fd = -1

    if argv is None:
        argv = sys.argv

    try:
        (options, params) = getopt.gnu_getopt(
            argv, 'adsuy', ['capabilities', 'silent', 'yestoall', 'entries=',
                            'output=', 'outfd=', 'all', 'unlimited', 'debug',
                            'help'])
    except getopt.GetoptError, opterr:
        print >>sys.stderr, opterr
        print >>sys.stderr, usage()
        return 2

    for (k, v) in options:
        if k == '--help':
            print usage()
            return 0

    # we need access to privileged files, exit if we are not running as root
    if os.getuid() != 0:
        print >>sys.stderr, "Error: xen-bugtool must be run as root"
        return 1

    try:
        load_plugins(True)
    except:
        pass

    inventory = readKeyValueFile(XENSOURCE_INVENTORY)
    if inventory.has_key('OEM_BUILD_NUMBER'):
        cap(CAP_OEM,                 PII_MAYBE,                 max_size=5*MB,
            max_time=90)

    if  os.getenv('XEN_RT'):
        entries = [CAP_BLOBS, CAP_BOOT_LOADER, CAP_CVSM, CAP_DEVICE_MODEL, CAP_DISK_INFO, CAP_FIRSTBOOT, CAP_HARDWARE_INFO,
                   CAP_HOST_CRASHDUMP_LOGS, CAP_KERNEL_INFO, CAP_LOSETUP_A,
                   CAP_NETWORK_CONFIG, CAP_NETWORK_STATUS, CAP_PROCESS_LIST, CAP_HIGH_AVAILABILITY,
                   CAP_PAM, CAP_MULTIPATH,
                   CAP_SYSTEM_LOGS, CAP_SYSTEM_SERVICES, CAP_TAPDISK_LOGS,
                   CAP_VNCTERM, CAP_XAPI_DEBUG, CAP_XAPI_SUBPROCESS,
                   CAP_XENRT, CAP_XENSERVER_CONFIG, CAP_XENSERVER_DOMAINS, CAP_XENSERVER_DATABASES,
                   CAP_XENSERVER_INSTALL, CAP_XENSERVER_LOGS, CAP_XEN_INFO, CAP_XHA_LIVESET, CAP_YUM]
    else:
        entries = [e for e in caps.keys() if caps[e][CHECKED]]

    update_capabilities()

    for (k, v) in options:
        if k == '--capabilities':
            print_capabilities()
            return 0

        if k == '--output':
            if  v in ['tar', 'tar.bz2', 'zip']:
                output_type = v
            else:
                print >>sys.stderr, "Invalid output format '%s'" % v
                return 2

        # "-s" or "--silent" means suppress output (except for the final
        # output filename at the end)
        if k in ['-s', '--silent']:
            SILENT_MODE = True

        if k == '--entries' and v != '':
            entries = v.split(',')

        # If the user runs the script with "-y" or "--yestoall" we don't ask
        # all the really annoying questions.
        if k in ['-y', '--yestoall']:
            ANSWER_YES_TO_ALL = True

        if k == '--outfd':
            output_fd = int(v)
            try:
                old = fcntl.fcntl(output_fd, fcntl.F_GETFD)
                fcntl.fcntl(output_fd, fcntl.F_SETFD, old | fcntl.FD_CLOEXEC)
            except:
                print >>sys.stderr, "Invalid output file descriptor", output_fd
                return 2

        elif k in ['-a', '--all']:
            entries = caps.keys()
        elif k in ['-u', '--unlimited']:
            unlimited_data = True
            unlimited_time = True
        elif k in ['-d', '--debug']:
            dbg = True
            ProcOutput.debug = True

    if len(params) != 1:
        print >>sys.stderr, "Invalid additional arguments", str(params)
        return 2

    if output_fd != -1 and output_type != 'tar':
        print >>sys.stderr, "Option '--outfd' only valid with '--output=tar'"
        return 2

    if ANSWER_YES_TO_ALL:
        output("Warning: '--yestoall' argument provided, will not prompt for individual files.")

    output('''
This application will collate the Xen dmesg output, details of the
hardware configuration of your machine, information about the build of
Xen that you are using, plus, if you allow it, various logs.

The collated information will be saved as a .%s for archiving or
sending to a Technical Support Representative.

The logs may contain private information, and if you are at all
worried about that, you should exit now, or you should explicitly
exclude those logs from the archive.

''' % output_type)

    # assemble potential data
    tree_output(CAP_BLOBS, XAPI_BLOBS)

    file_output(CAP_BOOT_LOADER, [EXTLINUX_CONFIG])
    cmd_output(CAP_BOOT_LOADER, [LS, '-lR', '/boot'])
    cmd_output(CAP_BOOT_LOADER, [MD5SUM, BOOT_KERNEL, BOOT_INITRD], label='vmlinuz-initrd.md5sum')

    func_output(CAP_CVSM, 'csl_logs', csl_logs)

    tree_output(CAP_DEVICE_MODEL, QEMU_DIR, QEMU_RESUME_RE)

    cmd_output(CAP_DISK_INFO, [FDISK, '-l'])
    file_output(CAP_DISK_INFO, [PROC_PARTITIONS, PROC_MOUNTS])
    file_output(CAP_DISK_INFO, [FSTAB, ISCSI_CONF, ISCSI_INITIATOR])
    cmd_output(CAP_DISK_INFO, [DF, '-alT'])
    cmd_output(CAP_DISK_INFO, [DF, '-alTi'])
    cmd_output(CAP_DISK_INFO, [DU, '-ax', '/'])
    for d in disk_list():
        cmd_output(CAP_DISK_INFO, [HDPARM, '-I', '/dev/%s' % d])
    if len(pidof('iscsid')) != 0:
        cmd_output(CAP_DISK_INFO, [ISCSIADM, '-m', 'node'])
        cmd_output(CAP_DISK_INFO, [ISCSIADM, '-m', 'session', '-P', '3'])
    cmd_output(CAP_DISK_INFO, [VGSCAN])
    cmd_output(CAP_DISK_INFO, [PVS])
    cmd_output(CAP_DISK_INFO, [VGS])
    cmd_output(CAP_DISK_INFO, [LVS])
    file_output(CAP_DISK_INFO, [LVM_CACHE, LVM_CONFIG])
    cmd_output(CAP_DISK_INFO, [LS, '-R', '/sys/class/scsi_host'])
    cmd_output(CAP_DISK_INFO, [LS, '-R', '/sys/class/scsi_disk'])
    cmd_output(CAP_DISK_INFO, [LS, '-R', '/sys/class/fc_transport'])
    cmd_output(CAP_DISK_INFO, [SG_MAP, '-x'])
    func_output(CAP_DISK_INFO, 'scsi-hosts', dump_scsi_hosts)
    tree_output(CAP_DISK_INFO, PROC_DRIVER_CCISS_DIR)
    cmd_output(CAP_DISK_INFO, [LVDISPLAY, '--map'])

    tree_output(CAP_FIRSTBOOT, FIRSTBOOT_DIR)

    file_output(CAP_HARDWARE_INFO, [PROC_CPUINFO, PROC_MEMINFO, PROC_IOMEM, PROC_IOPORTS, PROC_INTERRUPTS])
    file_output(CAP_HARDWARE_INFO, [PROC_SLABINFO])
    file_output(CAP_HARDWARE_INFO, [PROC_ZONEINFO])
    cmd_output(CAP_HARDWARE_INFO, [DMIDECODE])
    cmd_output(CAP_HARDWARE_INFO, [LSPCI, '-n'])
    cmd_output(CAP_HARDWARE_INFO, [LSPCI, '-tv'])
    cmd_output(CAP_HARDWARE_INFO, [LSPCI, '-vv'])
    cmd_output(CAP_HARDWARE_INFO, [LSPCI, '-nm'])
    cmd_output(CAP_HARDWARE_INFO, [LSPCI, '-nnm'])
    cmd_output(CAP_HARDWARE_INFO, [ACPIDUMP])
    file_output(CAP_HARDWARE_INFO, [PROC_USB_DEV, PROC_SCSI])
    file_output(CAP_HARDWARE_INFO, [BOOT_TIME_CPUS, BOOT_TIME_MEMORY])
    file_output(CAP_HARDWARE_INFO, [SYSCONFIG_HWCONF])
    cmd_output(CAP_HARDWARE_INFO, [LS, '-lR', '/dev'])
    cmd_output(CAP_HARDWARE_INFO, [XENPM, 'get-cpu-topology'])
    cmd_output(CAP_HARDWARE_INFO, [XENPM, 'get-cpufreq-states'])
    cmd_output(CAP_HARDWARE_INFO, [XENPM, 'get-cpuidle-states'])
    # FIXME IDE?

    for d in disk_list():
        cmd_output(CAP_HDPARM_T, [HDPARM, '-tT', '/dev/%s' % d])

    file_output(CAP_HIGH_AVAILABILITY, [XHAD_CONF, XHA_LOG])

    tree_output(CAP_HOST_CRASHDUMP_LOGS, HOST_CRASHDUMPS_DIR,
                HOST_CRASHDUMP_LOGS_EXCLUDES_RE, True)

    file_output(CAP_KERNEL_INFO, [PROC_VERSION, PROC_MODULES, PROC_DEVICES,
                                  PROC_FILESYSTEMS, PROC_CMDLINE])
    cmd_output(CAP_KERNEL_INFO, [ZCAT, PROC_CONFIG], label='config')
    cmd_output(CAP_KERNEL_INFO, [SYSCTL, '-A'])
    file_output(CAP_KERNEL_INFO, [MODPROBE_CONF])
    tree_output(CAP_KERNEL_INFO, MODPROBE_DIR)
    func_output(CAP_KERNEL_INFO, 'modinfo', module_info)
    cmd_output(CAP_KERNEL_INFO, QLOGIC_FW, label='qlogic_fw')
    cmd_output(CAP_KERNEL_INFO, ULIMIT, label='ulimit-a')

    cmd_output(CAP_LOSETUP_A, [LOSETUP, '-a'])

    file_output(CAP_MULTIPATH, [MULTIPATH_CONF, MPP_CONF])
    cmd_output(CAP_MULTIPATH, [DMSETUP, 'table'])
    func_output(CAP_MULTIPATH, 'multipathd_topology', multipathd_topology)

    file_output(CAP_NETWORK_CONFIG, [NET_UDEV_RULES])
    tree_output(CAP_NETWORK_CONFIG, SYSCONFIG_RENAME_DATA_DIR)
    file_output(CAP_NETWORK_CONFIG, [INTERFACE_RENAME_LOG])
    file_output(CAP_NETWORK_CONFIG, [NETWORK_CONF])
    file_output(CAP_NETWORK_CONFIG, [NETWORK_DBCACHE])
    file_output(CAP_NETWORK_CONFIG, [NETWORKD_DB])
    tree_output(CAP_NETWORK_CONFIG, SYSCONFIG_NETWORK_SCRIPTS, IFCFG_RE)
    tree_output(CAP_NETWORK_CONFIG, SYSCONFIG_NETWORK_SCRIPTS, ROUTE_RE)
    file_output(CAP_NETWORK_CONFIG, [SYSCONFIG_NETWORK, RESOLV_CONF, NSSWITCH_CONF, HOSTS])
    file_output(CAP_NETWORK_CONFIG, [NTP_CONF, IPTABLES_CONFIG, HOSTS_ALLOW, HOSTS_DENY])
    file_output(CAP_NETWORK_CONFIG, [OPENVSWITCH_CONF, OPENVSWITCH_CONF_DB])
    file_output(CAP_NETWORK_CONFIG, [SYS_VIF_GROUPS])

    cmd_output(CAP_NETWORK_STATUS, [IFCONFIG, '-a'])
    cmd_output(CAP_NETWORK_STATUS, [ROUTE, '-n'])
    cmd_output(CAP_NETWORK_STATUS, [ARP, '-n'])
    cmd_output(CAP_NETWORK_STATUS, [NETSTAT, '-an'])
    tree_output(CAP_NETWORK_STATUS, DHCP_LEASE_DIR)
    cmd_output(CAP_NETWORK_STATUS, [ARPTABLES, '-nvL'])
    cmd_output(CAP_NETWORK_STATUS, [EBTABLES, '-L'])
    cmd_output(CAP_NETWORK_STATUS, [IPTABLES, '-nvL'])
    cmd_output(CAP_NETWORK_STATUS, [BRCTL, 'show'])
    cmd_output(CAP_NETWORK_STATUS, [BIOSDEVNAME, '-d'])
    for p in os.listdir('/sys/class/net/'):
        if os.path.isdir('/sys/class/net/%s/bridge' % p):
            cmd_output(CAP_NETWORK_STATUS, [BRCTL, 'showmacs', p])
        else:
            try:
                f = open('/sys/class/net/%s/type' % p, 'r')
                t = f.readline()
                f.close()
                if int(t) == 1:
                    # ARPHRD_ETHER
                    cmd_output(CAP_NETWORK_STATUS, [ETHTOOL, p])
                    cmd_output(CAP_NETWORK_STATUS, [ETHTOOL, '-S', p])
                    cmd_output(CAP_NETWORK_STATUS, [ETHTOOL, '-k', p])
                    cmd_output(CAP_NETWORK_STATUS, [ETHTOOL, '-i', p])
                    cmd_output(CAP_NETWORK_STATUS, [ETHTOOL, '-c', p])
                    cmd_output(CAP_NETWORK_STATUS, [ETHTOOL, '-g', p])
            except:
                pass
    tree_output(CAP_NETWORK_STATUS, PROC_NET_BONDING_DIR)
    tree_output(CAP_NETWORK_STATUS, PROC_NET_VLAN_DIR)
    cmd_output(CAP_NETWORK_STATUS, [TC, '-s', 'qdisc'])
    cmd_output(CAP_NETWORK_STATUS, [NTPQ, '-c', 'peers'])
    file_output(CAP_NETWORK_STATUS, [PROC_NET_SOFTNET_STAT])
    tree_output(CAP_NETWORK_STATUS, OPENVSWITCH_CORE_DIR)
    if os.path.exists(OPENVSWITCH_VSWITCHD_PID) and CAP_NETWORK_STATUS in entries:
        cmd_output(CAP_NETWORK_STATUS, [OVS_VSCTL, 'list', 'open_vswitch'])
        cmd_output(CAP_NETWORK_STATUS, [OVS_VSCTL, 'list', 'bridge'])
        cmd_output(CAP_NETWORK_STATUS, [OVS_VSCTL, 'list', 'port'])
        cmd_output(CAP_NETWORK_STATUS, [OVS_VSCTL, 'list', 'interface'])
        cmd_output(CAP_NETWORK_STATUS, [OVS_VSCTL, 'list-br'])
        cmd_output(CAP_NETWORK_STATUS, [OVS_APPCTL, 'upcall/show'])
        cmd_output(CAP_NETWORK_STATUS, [OVS_APPCTL, 'memory/show'])
        cmd_output(CAP_NETWORK_STATUS, [OVS_APPCTL, 'coverage/show'])
        cmd_output(CAP_NETWORK_STATUS, [OVS_APPCTL, 'dpif/show'])
	cmd_output(CAP_NETWORK_STATUS, [OVS_VSCTL, 'list', 'controller'])
        for b in br_list():
            cmd_output(CAP_NETWORK_STATUS, [OVS_VSCTL, 'list-ports', b])
            cmd_output(CAP_NETWORK_STATUS, [OVS_VSCTL, 'list-ifaces', b])
            cmd_output(CAP_NETWORK_STATUS, [OVS_APPCTL, 'fdb/show', b])
            # Assumed br has one-to-one mapping to dp
            cmd_output(CAP_NETWORK_STATUS, [OVS_APPCTL, 'dpif/dump-flows', b])
            cmd_output(CAP_NETWORK_STATUS, [OVS_OFCTL, 'show', b])
            cmd_output(CAP_NETWORK_STATUS, [OVS_OFCTL, 'dump-flows', b])
        cmd_output(CAP_NETWORK_STATUS, [OVS_DPCTL, 'show'])
        cmd_output(CAP_NETWORK_STATUS, [OVS_DPCTL, 'show', '-s'])
        for d in dp_list():
            cmd_output(CAP_NETWORK_STATUS, [OVS_DPCTL, 'dump-flows', d])
        cmd_output(CAP_NETWORK_STATUS, [OVS_APPCTL, 'bond/list'])
        for b in bond_list():
            cmd_output(CAP_NETWORK_STATUS, [OVS_APPCTL, 'bond/show', b])

    tree_output(CAP_PAM, PAM_DIR)
    file_output(CAP_PAM, [KRB5_CONF])
    tree_output(CAP_PAM, LIKEWISE_DIR)

    func_output(CAP_PERSISTENT_STATS, 'xapi_rrd-host', dump_xapi_rrds)

    cmd_output(CAP_PROCESS_LIST, [PS, 'wwwaxf', '-eo', 'pid,tty,stat,time,nice,psr,pcpu,pmem,nwchan,wchan:25,args'], label='process-tree')
    func_output(CAP_PROCESS_LIST, 'fd_usage', fd_usage)

    file_output(CAP_SYSTEM_LOGS,
         [ VAR_LOG_DIR + x for x in
           [ 'crit.log', 'kern.log', 'daemon.log', 'user.log', 'syslog', 'messages',
             'monitor_memory.log', 'secure', 'debug', 'dmesg', 'boot.msg', 'blktap.log',
             'xen-dmesg', 'wtmp', 'xen/hypervisor.log'] +
           [ f % n for n in range(1, 20) \
                 for f in ['crit.log.%d', 'crit.log.%d.gz',
                           'kern.log.%d', 'kern.log.%d.gz',
                           'daemon.log.%d', 'daemon.log.%d.gz',
                           'user.log.%d', 'user.log.%d.gz',
                           'messages.%d', 'messages.%d.gz',
                           'monitor_memory.log.%d', 'monitor_memory.log.%d.gz',
                           'secure.%d', 'secure.%d.gz',
                           'xen/hypervisor.log.%d', 'xen/hypervisor.log.%d.gz',
                           'blktap.log.%d', 'wtmp.%d.gz']]])
    if not os.path.exists('/var/log/dmesg') and not os.path.exists('/var/log/boot.msg'):
        cmd_output(CAP_SYSTEM_LOGS, [DMESG])
    file_output(CAP_SYSTEM_LOGS, [LWIDENTITY_JOIN_LOG, HOSTS_LWIDENTITY_ORIG])

    cmd_output(CAP_SYSTEM_SERVICES, [CHKCONFIG, '--list'])

    #file_output(CAP_TAPDISK_LOGS,
    #            glob.glob("/sys/class/blktap2/blktap*/debug"))
    if CAP_TAPDISK_LOGS in entries:
        generate_tapdisk_logs()

    tree_output(CAP_VNCTERM, VNCTERM_CORE_DIR)

    tree_output(CAP_XAPI_DEBUG, XAPI_DEBUG_DIR)

    func_output(CAP_XAPI_SUBPROCESS, 'xapi_subprocesses', dump_xapi_subprocess_info)

    tree_output(CAP_XENRT, '/tmp', FIST_RE)
    # CA-45540: capture QEMU core files
    tree_output(CAP_XENRT, '/var/xen/qemu')
    file_output(CAP_XENRT, ['/core.*'])
    tree_output(CAP_XENRT, '/tmp', re.compile(r'^.*xen\.qemu-dm\.'))

    file_output(CAP_XENSERVER_CONFIG, [INITIAL_INVENTORY])
    file_output(CAP_XENSERVER_CONFIG, [POOL_CONF, PTOKEN, XAPI_CONF, XAPI_SSL_CONF, XAPI_GLOBS_CONF,
                                       XENSOURCE_INVENTORY, VENDORKERNEL_INVENTORY])
    cmd_output(CAP_XENSERVER_CONFIG, [LS, '-lR', '/opt/xensource'])
    cmd_output(CAP_XENSERVER_CONFIG, [BIN_STATIC_VDIS, 'list'])
    tree_output(CAP_XENSERVER_CONFIG, OEM_CONFIG_DIR, OEM_CONFIG_FILES_RE)
    tree_output(CAP_XENSERVER_CONFIG, STATIC_VDIS)
    cmd_output(CAP_XENSERVER_CONFIG, [LS, '-lR', STATIC_VDIS])
    file_output(CAP_XENSERVER_CONFIG, [SYSCONFIG_CLOCK])

    func_output(CAP_XENSERVER_DATABASES, 'xapi-db.xml', dump_filtered_xapi_db)
    cmd_output(CAP_XENSERVER_DATABASES, [XENSTORE_LS, '-f'], filter=filter_xenstore_secrets)
    file_output(CAP_XENSERVER_DATABASES, [DB_CONF, DB_CONF_RIO, DB_DEFAULT_FIELDS, DB_SCHEMA_SQL])
    tree_output(CAP_XENSERVER_DATABASES, OEM_CONFIG_DIR, OEM_DB_FILES_RE)
    file_output(CAP_XENSERVER_DATABASES, [XENSTORED_DB, XENSTORED_DB + '.bak'])
    cmd_output(CAP_XENSERVER_DATABASES, [XE, 'pool-dump-database', 'file-name='],
               label="xapi-db-dumped.xml", filter=filter_db_pii)

    cmd_output(CAP_XENSERVER_DOMAINS, [LIST_DOMAINS])

    tree_output(CAP_XENSERVER_INSTALL, VAR_LOG_DIR + 'installer')
    file_output(CAP_XENSERVER_INSTALL,
                [ VAR_LOG_DIR + x for x in
                  [ 'firstboot-SR-commands-log',
                    'upgrade-commands-log', 'generate-iscsi-iqn-log']] +
                [ '/root/' + x for x in
                  [ 'blockdevs-log', 'cmdline-log', 'devcontents-log',
                    'dmesg-log', 'install-log', 'lspci-log', 'modules-log',
                    'pci-log', 'processes-log', 'tty-log', 'uname-log',
                    'vgscan-log']])
    tree_output(CAP_XENSERVER_INSTALL, INSTALLED_REPOS_DIR)
    tree_output(CAP_XENSERVER_INSTALL, PATCH_APPLIED_DIR)

    file_output(CAP_XENSERVER_LOGS, [LOG_CONF, XENGUEST_LOG])
    file_output(CAP_XENSERVER_LOGS, XENSERVER_LOGS)
    file_output(CAP_XENSERVER_LOGS, LICENSE_LOGS)
    tree_output(CAP_XENSERVER_LOGS, OEM_CONFIG_DIR, OEM_XENSERVER_LOGS_RE)

    cmd_output(CAP_XEN_INFO, [XL, 'dmesg'])
    cmd_output(CAP_XEN_INFO, [XL, 'info'])
    file_output(CAP_XEN_INFO, [PROC_XEN_BALLOON])

    cmd_output(CAP_XHA_LIVESET, [HA_QUERY_LIVESET])

    file_output(CAP_YUM, [YUM_LOG])
    tree_output(CAP_YUM, YUM_REPOS_DIR)
    cmd_output(CAP_YUM, [RPM, '-qa'])

    try:
        load_plugins()
    except:
        pass

    # permit the user to filter out data
    for k in sorted(data.keys()):
        if not ANSWER_YES_TO_ALL and not yes("Include '%s'? [Y/n]: " % k):
            del data[k]

    subdir = os.getenv('XENRT_BUGTOOL_BASENAME')
    if subdir:
        subdir = os.path.basename(subdir)
        if subdir == '..' or subdir == '.':
            subdir = None
    if not subdir:
        subdir = "bug-report-%s" % time.strftime("%Y%m%d%H%M%S")

    # create archive
    if output_fd == -1 and not os.path.exists(BUG_DIR):
        try:
            os.makedirs(BUG_DIR)
        except:
            pass

    if output_fd == -1:
        output_ts('Creating output file')

    if output_type.startswith('tar'):
        archive = TarOutput(subdir, output_type, output_fd)
    else:
        archive = ZipOutput(subdir)

    # collect selected data now
    output_ts('Running commands to collect data')
    collect_data(subdir, archive)

    # include inventory
    inventory = {'cap': None, 'output': StringIOmtime(no_unicode(make_inventory(data, subdir)))}
    archive.addFile(construct_filename(subdir, 'inventory.xml', inventory),
                StringIOmtime(no_unicode(make_inventory(data, subdir))))

    if archive.close():
        res = 0
    else:
        res = 1

    clean_tapdisk_logs()

    if dbg:
        print >>sys.stderr, "Category sizes (max, actual):\n"
        for c in caps.keys():
            print >>sys.stderr, "    %s (%d, %d)" % (c, caps[c][MAX_SIZE],
                                                     cap_sizes[c])
    return res

def find_tapdisk_logs():
    return glob.glob('/var/log/blktap/*.log*')

def find_tapback_logs():
    return glob.glob('/var/log/tapback/tapback*')

def generate_tapdisk_logs():
    for pid in pidof('tapback'):
        try:
            os.kill(pid, SIGHUP)
        except:
            pass
    for pid in pidof('tapdisk2'):
	try:
	    os.kill(pid, SIGUSR1)
            output_ts("Including logs for tapdisk process %d" % pid)
        except :
            pass
    # give processes a second to write their logs
    time.sleep(1)
    file_output(CAP_TAPDISK_LOGS, find_tapdisk_logs() + find_tapback_logs())

def clean_tapdisk_logs():
    for filename in find_tapdisk_logs():
        try:
            os.remove(filename)
        except :
            pass

def dump_xapi_subprocess_info(cap):
    """Check which fds are open by xapi and its subprocesses to diagnose faults like CA-10543.
       Returns a string containing a pretty-printed pstree-like structure. """
    pids = filter(lambda x: x.isdigit(), os.listdir("/proc"))
    def readlines(filename):
        lines = ''
	try:
            f = open(filename, "r")
            lines = f.readlines()
            f.close()
        except:
            pass
        return lines
    def cmdline(pid):
    	all = readlines("/proc/" + pid + "/cmdline")
	if all == []:
	   return ""
	else:
	   return all[0].replace('\x00', ' ')
    def parent(pid):
    	for i in readlines("/proc/" + pid + "/status"):
	    if i.startswith("PPid:"):
	       return i.split()[-1]
	return None
    def pstree(pid):
    	result = { "cmdline": cmdline(pid) }
	child_pids = filter(lambda x:parent(x) == pid, pids)
	children = { }
	for child in child_pids:
	    children[child] = pstree(child)
	result['children'] = children
	fds = { }
	for fd in os.listdir("/proc/" + pid + "/fd"):
            try:
                fds[fd] = os.readlink("/proc/" + pid + "/fd/" + fd)
            except:
                pass
	result['fds'] = fds
	return result
    xapis = filter(lambda x: cmdline(x).startswith("/opt/xensource/bin/xapi"), pids)
    xapis = filter(lambda x: parent(x) == "1", xapis)
    result = {}
    for xapi in xapis:
    	result[xapi] = pstree(xapi)
    pp = pprint.PrettyPrinter(indent=4)
    return pp.pformat(result)

def dump_xapi_rrds(cap):
    socket.setdefaulttimeout(5)
    session = XenAPI.xapi_local()
    session.xenapi.login_with_password('', '')
    this_host = session.xenapi.session.get_this_host(session._session)
    # better way to find pool master?
    pool = session.xenapi.pool.get_all_records().values()[0]
    i_am_master = (this_host == pool['master'])

    for vm in session.xenapi.VM.get_all_records().values():
        if vm['is_a_template']:
            continue
        if vm['resident_on'] == this_host or (i_am_master and vm['power_state'] in ['Suspended', 'Halted']):
            rrd = urllib.urlopen('http://localhost/vm_rrd?session_id=%s&uuid=%s' % (session._session, vm['uuid']))
            try:
                (i, o, x) = select([rrd], [], [], 5.0)
                if len(i) == 1:
                    data['xapi_rrd-%s' % vm['uuid']] = {'cap': cap,
                                                        'output': StringIOmtime(rrd.read())}
            finally:
                rrd.close()

    output = ''
    rrd = urllib.urlopen('http://localhost/host_rrd?session_id=%s' % session._session)
    try:
        output = rrd.read()
    finally:
        rrd.close()

    session.xenapi.session.logout()
    return output

'''Filter a Xapi XML database.

    There is one important assumption made in this class:
    - the XML document does not contain any characters between the end of one
      tag and the beginning of the next, ie every > is immediately followed by
      a <
'''
class DBFilter:
    def __init__(self):
        self.result = ''
        self.rest = ''
        self.state = {}

    def filter_secrets(self, s):
        if 'in_secret_table' not in self.state:
            self.state['in_secret_table'] = False

        # this logic doesn't deal with <table name="secret" /> properly!!!
        if s.startswith('<table ') and 'name="secret"' in s:
            self.state['in_secret_table'] = True
        elif s.startswith('</table>'):
            self.state['in_secret_table'] = False

        if self.state['in_secret_table'] and s.startswith("<row"): # match only on DB rows
            s = re.sub(r'(value=")[^"]+(")', r'\1REMOVED\2', s)
        return s

    def feed(self, s):
        rem = self.rest + s
        p = rem.find('>')
        while p != -1:
            s = rem[:p+1]
            rem = rem[p+1:]
            self.result  += self.filter_secrets(s)
            p = rem.find('>')
        self.rest = rem

    def output(self):
        r = self.result + self.filter_secrets(self.rest)
        self.result, self.rest = '', ''
        self.state = {}
        return r

def filter_db_pii(s, state):
    dbfilter = DBFilter()
    dbfilter.feed(s)
    return dbfilter.output()

clipboard_match = re.compile(r'^/local/domain/(\d+)/data/((set)|(report))_clipboard')
def filter_xenstore_secrets(s, state):
    match = clipboard_match.search(s)
    if match:
        return '/local/domain/%s/data/%s_clipboard = <filtered for security>\n' % (match.group(1), match.group(2))
    else:
        return s

def dump_filtered_xapi_db(cap):
    db_file = None

    ih = None
    output = ''

    # determine DB file name
    c = open(DB_CONF, 'r')
    try:
        for line in c:
            l = line.rstrip('\n')
            if (l[0], l[-1]) == ('[', ']'):
                db_file = l[1:-1]
                break
    finally:
        c.close()


    ih = open(db_file, 'r')
    if not ih:
        return ''

    dbfilter = DBFilter()
    rec = ih.read(2048)
    while rec != '':
        dbfilter.feed(rec)
        rec = ih.read(2048)
    output = dbfilter.output()

    ih.close()
    return output

def dump_scsi_hosts(cap):
    output = ''
    l = os.listdir('/sys/class/scsi_host')
    l.sort()

    for h in l:
        procname = ''
        try:
                f = open('/sys/class/scsi_host/%s/proc_name' % h)
                procname = f.readline().strip("\n")
                f.close()
        except:
                pass
        modelname = None
        try:
                f = open('/sys/class/scsi_host/%s/model_name' % h)
                modelname = f.readline().strip("\n")
                f.close()
        except:
                pass

        output += "%s:\n" %h
        output += "    %s%s\n" % (procname, modelname and (" -> %s" % modelname) or '')

    return output

def module_info(cap):
    output = StringIO.StringIO()
    modules = open(PROC_MODULES, 'r')
    procs = []

    for line in modules:
        module = line.split()[0]
        procs.append(ProcOutput([MODINFO, module], caps[cap][MAX_TIME], output))
    modules.close()

    run_procs([procs])

    return output.getvalue()

def csl_logs(cap):
    socket.setdefaulttimeout(5)
    session = XenAPI.xapi_local()
    session.xenapi.login_with_password('', '')
    this_host = session.xenapi.session.get_this_host(session._session)
    # better way to find pool master?
    pool = session.xenapi.pool.get_all_records().values()[0]
    i_am_master = (this_host == pool['master'])

    output = StringIO.StringIO()
    procs = []
    csl_targets_fetched = []

    for pbd in session.xenapi.PBD.get_all_records().values():
        if pbd.has_key('device_config') and pbd['device_config'].has_key('target'):
            if pbd['device_config']['target'] in csl_targets_fetched:
                continue
            sr = session.xenapi.SR.get_record(pbd['SR'])
            if sr.has_key('type') and sr['type'] == 'cslg':
                if sr['shared'] and pbd['host'] != this_host and not i_am_master:
                    continue

                dev_cfg = pbd['device_config']
                server = "server=%s" % socket.gethostbyname(dev_cfg['target'])
                if dev_cfg.has_key('port'):
                    server += ':' + dev_cfg['port']
                if dev_cfg.has_key('username'):
                    server += ',' + dev_cfg['username']
                if dev_cfg.has_key('password_secret'):
                    sec_ref = session.xenapi.secret.get_by_uuid(dev_cfg['password_secret'])
                    server += ',' + session.xenapi.secret.get_value(sec_ref)
                procs.append(ProcOutput([CSL, server, 'srv-log-get'], caps[cap][MAX_TIME], output))
                csl_targets_fetched.append(dev_cfg['target'])

    session.xenapi.session.logout()

    run_procs([procs])

    return output.getvalue()

def multipathd_topology(cap):
    pipe = Popen([MULTIPATHD, '-k'], bufsize=1, stdin=PIPE,
                     stdout=PIPE, stderr=dev_null)
    stdout, stderr = pipe.communicate('show topology')

    return stdout

def dp_list():
    output = StringIO.StringIO()
    procs = [ProcOutput([OVS_DPCTL, 'dump-dps'], caps[CAP_NETWORK_STATUS][MAX_TIME], output)]

    run_procs([procs])

    if not procs[0].timed_out:
        return output.getvalue().splitlines()
    return []

def br_list():
    output = StringIO.StringIO()
    procs = [ProcOutput([OVS_VSCTL, 'list-br'], caps[CAP_NETWORK_STATUS][MAX_TIME], output)]

    run_procs([procs])

    if not procs[0].timed_out:
        return output.getvalue().splitlines()
    return []

def bond_list():
    output = StringIO.StringIO()
    procs = [ProcOutput([OVS_APPCTL, 'bond/list'], caps[CAP_NETWORK_STATUS][MAX_TIME], output)]

    run_procs([procs])

    if not procs[0].timed_out:
        bonds = output.getvalue().splitlines()[1:]
        return [x.split('\t')[0] for x in bonds]
    return []

def fd_usage(cap):
    output = ''
    fd_dict = {}
    for d in [p for p in os.listdir('/proc') if p.isdigit()]:
        try:
            fh = open('/proc/'+d+'/cmdline')
            name = fh.readline()
            num_fds = len(os.listdir(os.path.join('/proc/'+d+'/fd')))
            if num_fds > 0:
                if not num_fds in fd_dict:
                    fd_dict[num_fds] = []
                fd_dict[num_fds].append(name.replace('\0', ' ').strip())
        finally:
            fh.close()
    keys = fd_dict.keys()
    keys.sort(lambda a, b: int(b) - int(a))
    for k in keys:
        output += "%s: %s\n" % (k, str(fd_dict[k]))
    return output

def load_plugins(just_capabilities = False):
    def getText(nodelist):
        rc = ""
        for node in nodelist:
            if node.nodeType == node.TEXT_NODE:
                rc += node.data
        return rc.encode()

    def getBoolAttr(el, attr, default = False):
        ret = default
        val = el.getAttribute(attr).lower()
        if val in ['true', 'false', 'yes', 'no']:
            ret = val in ['true', 'yes']
        return ret

    for dir in [d for d in os.listdir(PLUGIN_DIR) if os.path.isdir(os.path.join(PLUGIN_DIR, d))]:
        if not caps.has_key(dir):
            if not os.path.exists("%s/%s.xml" % (PLUGIN_DIR, dir)):
                continue
            xmldoc = parse("%s/%s.xml" % (PLUGIN_DIR, dir))
            assert xmldoc.documentElement.tagName == "capability"

            pii, min_size, max_size, min_time, max_time, mime = \
                 PII_MAYBE, -1,-1,-1,-1, MIME_TEXT

            if xmldoc.documentElement.getAttribute("pii") in [PII_NO, PII_YES, PII_MAYBE, PII_IF_CUSTOMIZED]:
                pii = xmldoc.documentElement.getAttribute("pii")
            if xmldoc.documentElement.getAttribute("min_size") != '':
                min_size = long(xmldoc.documentElement.getAttribute("min_size"))
            if xmldoc.documentElement.getAttribute("max_size") != '':
                max_size = long(xmldoc.documentElement.getAttribute("max_size"))
            if xmldoc.documentElement.getAttribute("min_time") != '':
                min_time = int(xmldoc.documentElement.getAttribute("min_time"))
            if xmldoc.documentElement.getAttribute("max_time") != '':
                max_time = int(xmldoc.documentElement.getAttribute("max_time"))
            if xmldoc.documentElement.getAttribute("mime") in [MIME_DATA, MIME_TEXT]:
                mime = xmldoc.documentElement.getAttribute("mime")
            checked = getBoolAttr(xmldoc.documentElement, 'checked', True)
            hidden = getBoolAttr(xmldoc.documentElement, 'hidden', False)

            cap(dir, pii, min_size, max_size, min_time, max_time, mime, checked, hidden)

        if just_capabilities:
            continue

        plugdir = os.path.join(PLUGIN_DIR, dir)
        for file in [f for f in os.listdir(plugdir) if f.endswith('.xml')]:
            xmldoc = parse(os.path.join(plugdir, file))
            assert xmldoc.documentElement.tagName == "collect"

            for el in xmldoc.documentElement.getElementsByTagName("*"):
                if el.tagName == "files":
                    file_output(dir, getText(el.childNodes).split())
                elif el.tagName == "list":
                    recursive = getBoolAttr(el, 'recursive')
                    dir_list(dir, getText(el.childNodes).split(), recursive)
                elif el.tagName == "directory":
                    pattern = el.getAttribute("pattern")
                    if pattern == '': pattern = None
                    negate = getBoolAttr(el, 'negate')
                    tree_output(dir, getText(el.childNodes), pattern and re.compile(pattern) or None, negate)
                elif el.tagName == "command":
                    label = el.getAttribute("label")
                    if label == '': label = None
                    cmd_output(dir, getText(el.childNodes), label)

def removeNoError(filename):
    try:
        os.remove(filename)
    except OSError:
        pass

class TarOutput:
    def __init__(self, subdir, suffix, output_fd):
        self.output_fd = output_fd
        self.subdir = subdir
        mode = 'w|'
        if suffix == 'tar.bz2':
            mode = 'w|bz2'
        self.filename = "%s/%s.%s" % (BUG_DIR, subdir, suffix)

        if output_fd == -1:
            self.tf = tarfile.open(self.filename, mode)
        else:
            self.tf = tarfile.open(None, 'w|', os.fdopen(output_fd, 'ab'))

    def _getTi(self, filename):
        ti = tarfile.TarInfo(filename)
        ti.uname = 'root'
        ti.gname = 'root'
        return ti

    def addRealFile(self, name, filename):
        ti = self._getTi(name)
        s = os.stat(filename)
        ti.mtime = s.st_mtime
        ti.size = s.st_size
        self.tf.addfile(ti, file(filename,'rb'))

    def addFile(self, name, data):
        ti = self._getTi(name)
        ti.mtime = data.mtime
        ti.size = len(data.getvalue())
        data.seek(0)
        self.tf.addfile(ti, data)

    def close(self):
        try:
            self.tf.close()
            if self.output_fd == -1:
                output ('Writing tarball %s successful.' % self.filename)
                if SILENT_MODE:
                    print self.filename
            return True
        except Exception, e:
            if output_fd == -1:
                output ("Error closing tar file '%s': '%s'" % (self.filename, e))
            else:
                output ("Error closing tar file descriptor: '%s'" % e)

            output ("Cleaning up incomplete file '%s'" % self.filename)
            removeNoError(self.filename)
            return False

class ZipOutput:
    def __init__(self, subdir):
        self.subdir = subdir
        self.filename = "%s/%s.zip" % (BUG_DIR, subdir)
        self.zf = zipfile.ZipFile(self.filename, 'w', zipfile.ZIP_DEFLATED)

    def addRealFile(self, name, filename):
        if os.stat(filename).st_size < 50:
            compress_type = zipfile.ZIP_STORED
        else:
            compress_type = zipfile.ZIP_DEFLATED
        self.zf.write(filename, name, compress_type)

    def addFile(self, name, data):
        self.zf.writestr(name, data.getvalue())

    def close(self):
        try:
            self.zf.close()
            output ('Writing archive %s successful.' % self.filename)
            if SILENT_MODE:
                print self.filename
            return True
        except Exception, e:
            output ("Error closing zip file '%s': %s" % (self.filename, e))
            output ("Cleaning up incomplete file '%s'" % self.filename)
            removeNoError(self.filename)
            return False


def make_inventory(inventory, subdir):
    document = getDOMImplementation().createDocument(
        None, INVENTORY_XML_ROOT, None)

    # create summary entry
    s = document.createElement(INVENTORY_XML_SUMMARY)
    user = os.getenv('SUDO_USER', os.getenv('USER'))
    if user:
        s.setAttribute('user', user)
    s.setAttribute('date', time.strftime('%c'))
    s.setAttribute('hostname', platform.node())
    s.setAttribute('uname', ' '.join(platform.uname()))
    s.setAttribute('uptime', commands.getoutput(UPTIME))
    document.getElementsByTagName(INVENTORY_XML_ROOT)[0].appendChild(s)

    map(lambda (k, v): inventory_entry(document, subdir, k, v),
        inventory.items())
    return document.toprettyxml()

def inventory_entry(document, subdir, k, v):
    try:
        el = document.createElement(INVENTORY_XML_ELEMENT)
        el.setAttribute('capability', v['cap'])
        el.setAttribute('filename', construct_filename(subdir, k, v))
        el.setAttribute('md5sum', md5sum(v))
        document.getElementsByTagName(INVENTORY_XML_ROOT)[0].appendChild(el)
    except:
        pass


def md5sum_file(filename):
    m = md5_new()
    f = open(filename, 'rb')
    while True:
        data = f.read(4096)
        if not data:
            break
        m.update(data)
    f.close()
    return m.hexdigest()


def md5sum(d):
    if d.has_key('md5'):
        return d['md5']
    elif d.has_key('filename'):
        return md5sum_file(d['filename'])
    elif d.has_key('output'):
        m = md5_new()
        m.update(d['output'].getvalue())
        return m.hexdigest()
    raise Exception('Cannot compute md5 for this entry')


def construct_filename(subdir, k, v):
    s = v.get('filename')
    if s:
        if s.startswith('/'):
            s = s[1:]
    else:
        s = k.replace(' ', '-')
        s = s.replace('--', '-')
        s = s.replace('/', '%')
        if s.find('.') == -1:
            s += '.out'

    return no_unicode(os.path.join(subdir, s))


def update_capabilities():
    update_cap_size(CAP_HOST_CRASHDUMP_LOGS,
                    size_of_dir(HOST_CRASHDUMPS_DIR, HOST_CRASHDUMP_LOGS_EXCLUDES_RE, True))
    update_cap_size(CAP_XAPI_DEBUG, size_of_dir(XAPI_DEBUG_DIR))
    update_cap_size(CAP_XENSERVER_LOGS, size_of_all(XENSERVER_LOGS))

    # compute max time & size based on number of PIFs and VIFs
    netdevs = os.listdir('/sys/class/net')
    num_vifs = len(filter(lambda x: x.startswith('vif'), netdevs))
    num_pifs = len(filter(lambda x: x.startswith('eth'), netdevs))
    max_time = caps[CAP_NETWORK_STATUS][MAX_TIME] * (num_pifs + num_vifs)
    update_cap_time(CAP_NETWORK_STATUS, max_time)
    max_size = caps[CAP_NETWORK_STATUS][MAX_SIZE] * (num_pifs + num_vifs)
    update_cap_size(CAP_NETWORK_STATUS, max_size)
    max_size = caps[CAP_NETWORK_CONFIG][MAX_SIZE] * num_pifs + CAP_NETWORK_CONFIG_OVERHEAD
    update_cap_size(CAP_NETWORK_CONFIG, max_size)

    # compute max time & size based on number of domains, VBDs and VIFs
    num_vbds = len(filter(lambda x: x.startswith('blktap'), os.listdir('/dev/blktap')))
    num_doms = len(xc().domain_getinfo())
    max_time = (caps[CAP_XENSERVER_DATABASES][MAX_TIME] * (num_doms + num_vbds + num_vifs) +
                CAP_XENSERVER_DATABASES_TIME_OVERHEAD)
    update_cap_time(CAP_XENSERVER_DATABASES, max_time)
    max_size = (caps[CAP_XENSERVER_DATABASES][MAX_SIZE] * (num_doms + num_vbds + num_vifs) +
                CAP_XENSERVER_DATABASES_SIZE_OVERHEAD)
    update_cap_size(CAP_XENSERVER_DATABASES, max_size)


def update_cap_size(cap, size):
    update_cap(cap, MIN_SIZE, size)
    update_cap(cap, MAX_SIZE, size)


def update_cap_time(cap, time):
    update_cap(cap, MAX_TIME, time)


def update_cap(cap, k, v):
    global caps
    l = list(caps[cap])
    l[k] = v
    caps[cap] = tuple(l)


def size_of_dir(d, pattern = None, negate = False):
    if os.path.isdir(d):
        return size_of_all([os.path.join(d, fn) for fn in os.listdir(d)],
                           pattern, negate)
    else:
        return 0


def size_of_all(files, pattern = None, negate = False):
    return sum([size_of(f, pattern, negate) for f in files])


def matches(f, pattern, negate):
    if negate:
        return not matches(f, pattern, False)
    else:
        return pattern is None or pattern.match(f)


def size_of(f, pattern, negate):
    if os.path.isfile(f) and matches(f, pattern, negate):
        return os.stat(f)[6]
    else:
        return size_of_dir(f, pattern, negate)


def print_capabilities():
    document = getDOMImplementation().createDocument(
        "ns", CAP_XML_ROOT, None)
    map(lambda key: capability(document, key), [k for k in caps.keys() if not caps[k][HIDDEN]])
    print document.toprettyxml()

def capability(document, key):
    c = caps[key]
    el = document.createElement(CAP_XML_ELEMENT)
    el.setAttribute('key', c[KEY])
    el.setAttribute('pii', c[PII])
    el.setAttribute('min-size', str(c[MIN_SIZE]))
    el.setAttribute('max-size', str(c[MAX_SIZE]))
    el.setAttribute('min-time', str(c[MIN_TIME]))
    el.setAttribute('max-time', str(c[MAX_TIME]))
    el.setAttribute('content-type', c[MIME])
    el.setAttribute('default-checked', c[CHECKED] and 'yes' or 'no')
    document.getElementsByTagName(CAP_XML_ROOT)[0].appendChild(el)


def prettyDict(d):
    format = '%%-%ds: %%s' % max(map(len, [k for k, _ in d.items()]))
    return '\n'.join([format % i for i in d.items()]) + '\n'


def yes(prompt):
    yn = raw_input(prompt)

    return len(yn) == 0 or yn.lower()[0] == 'y'


partition_re = re.compile(r'(.*[0-9]+$)|(^xvd)')

def disk_list():
    disks = []
    try:
        f = open('/proc/partitions')
        f.readline()
        f.readline()
        for line in f.readlines():
            (major, minor, blocks, name) = line.split()
            if int(major) < 254 and not partition_re.match(name):
                disks.append(name)
        f.close()
    except:
        pass
    return disks


class ProcOutput:
    debug = False

    def __init__(self, command, max_time, inst=None, filter=None):
        self.command = command
        self.max_time = max_time
        self.inst = inst
        self.running = False
        self.status = None
        self.timed_out = False
        self.failed = False
        self.filter = filter
        self.filter_state = {}

    def __del__(self):
        self.terminate()

    def cmdAsStr(self):
        return isinstance(self.command, list) and ' '.join(self.command) or self.command

    def run(self):
        self.timed_out = False
        try:
            if ProcOutput.debug:
                output_ts("Starting '%s'" % self.cmdAsStr())
            self.proc = Popen(self.command, bufsize=1, stdin=dev_null, stdout=PIPE, stderr=dev_null, shell=isinstance(self.command, str))
            old = fcntl.fcntl(self.proc.stdout.fileno(), fcntl.F_GETFD)
            fcntl.fcntl(self.proc.stdout.fileno(), fcntl.F_SETFD, old | fcntl.FD_CLOEXEC)
            self.running = True
            self.failed = False
        except:
            output_ts("'%s' failed" % self.cmdAsStr())
            self.running = False
            self.failed = True

    def terminate(self):
        if self.running:
            try:
                self.proc.stdout.close()
                os.kill(self.proc.pid, SIGTERM)
            except:
                pass
            self.proc = None
            self.running = False
            self.status = SIGTERM

    def read_line(self):
        assert self.running
        line = self.proc.stdout.readline()
        if line == '':
            # process exited
            self.status = self.proc.wait()
            self.proc = None
            self.running = False
        else:
            if self.filter:
                line = self.filter(line, self.filter_state)
            if self.inst:
                self.inst.write(line)

class ProcOutputAndArchive(ProcOutput):
    def __init__(self, command, max_time, name, archive, data):
        self.data = data
        self.name = name
        self.archive = archive
        ProcOutput.__init__(self, command, max_time, data['output'], data['filter'])

    def collectData(self):
        self.archive.addFile(self.name, self.data['output'])
        self.data['md5'] = md5sum(self.data)
        self.data['output'].close()
        del self.data['output']

    def terminate(self):
        if self.running:
            ProcOutput.terminate(self)
            if not self.running:
                self.collectData()

    def read_line(self):
        if self.running:
            ProcOutput.read_line(self)
            if not self.running:
                self.collectData()

def run_procs(procs):
    start = int(time.time())

    while True:
        pipes = []
        active_procs = []

        for pp in procs:
            for p in pp:
                if p.running:
                    active_procs.append(p)
                    pipes.append(p.proc.stdout)
                    break
                elif p.status == None and not p.failed and not p.timed_out:
                    p.run()
                    if p.running:
                        active_procs.append(p)
                        pipes.append(p.proc.stdout)
                        break

        if len(pipes) == 0:
            # all finished
            break

        (i, o, x) = select(pipes, [], [], 1.0)
        now = int(time.time())

        # handle process output
        for p in active_procs:
            if p.proc.stdout in i:
                p.read_line()

            # handle timeout
            if not unlimited_time and p.running and now > (start + p.max_time):
                output_ts("'%s' timed out" % p.cmdAsStr())
                if p.inst:
                    p.inst.write("\n** timeout **\n")
                p.timed_out = True
                p.terminate()


def pidof(name):
    pids = []

    for d in [p for p in os.listdir('/proc') if p.isdigit()]:
        try:
            if os.path.basename(os.readlink('/proc/%s/exe' % d)) == name:
                pids.append(int(d))
        except:
            pass

    return pids


def readKeyValueFile(filename, allowed_keys = None, strip_quotes = True, assert_quotes = True):
    """ Reads a KEY=Value style file (e.g. xensource-inventory). Returns a
    dictionary of key/values in the file.  Not designed for use with large files
    as the file is read entirely into memory."""

    f = open(filename, "r")
    lines = [x.strip("\n") for x in f.readlines()]
    f.close()

    # remove lines contain
    if allowed_keys:
        lines = filter(lambda x: True in [x.startswith(y) for y in allowed_keys],
                       lines)

    defs = [ (l[:l.find("=")], l[(l.find("=") + 1):]) for l in lines ]

    if strip_quotes:
        def quotestrip(x):
            if assert_quotes:
                assert x.startswith("'") and x.endswith("'")
            return x.strip("'")
        defs = [ (a, quotestrip(b)) for (a,b) in defs ]

    return dict(defs)


class StringIOmtime(StringIO.StringIO):
    def __init__(self, buf = ''):
        StringIO.StringIO.__init__(self, buf)
        self.mtime = time.time()

    def write(self, s):
        StringIO.StringIO.write(self, s)
        self.mtime = time.time()


if __name__ == "__main__":
    try:
        sys.exit(main())
    except KeyboardInterrupt:
        print "\nInterrupted."
        sys.exit(3)
