This file is indexed.

/usr/sbin/ovs-bugtool is in openvswitch-common 2.3.0+git20140819-3+deb8u1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
#! /usr/bin/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.
# Copyright (c) 2010, 2011, 2012, 2013 Nicira, Inc.

#
# 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 warnings
warnings.filterwarnings(action="ignore", category=DeprecationWarning)

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
import md5
import platform
import fcntl
import glob
import urllib
import socket
import base64

OS_RELEASE = platform.release()

#
# Files & directories
#

APT_SOURCES_LIST = "/etc/apt/sources.list"
APT_SOURCES_LIST_D = "/etc/apt/sources.list.d"
BUG_DIR = "/var/log/ovs-bugtool"
PLUGIN_DIR = "/usr/share/openvswitch/bugtool-plugins"
GRUB_CONFIG = '/boot/grub/menu.lst'
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'
PROC_CPUINFO = '/proc/cpuinfo'
PROC_MEMINFO = '/proc/meminfo'
PROC_IOPORTS = '/proc/ioports'
PROC_INTERRUPTS = '/proc/interrupts'
PROC_SCSI = '/proc/scsi/scsi'
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_NET_BONDING_DIR = '/proc/net/bonding'
IFCFG_RE = re.compile(r'^.*/ifcfg-.*')
ROUTE_RE = re.compile(r'^.*/route-.*')
SYSCONFIG_HWCONF = '/etc/sysconfig/hwconf'
SYSCONFIG_NETWORK = '/etc/sysconfig/network'
SYSCONFIG_NETWORK_SCRIPTS = '/etc/sysconfig/network-scripts'
PROC_NET_VLAN_DIR = '/proc/net/vlan'
PROC_NET_SOFTNET_STAT = '/proc/net/softnet_stat'
MODPROBE_CONF = '/etc/modprobe.conf'
MODPROBE_DIR = '/etc/modprobe.d'
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', '/var/lib/dhcp3']
OPENVSWITCH_LOG_DIR = '/var/log/openvswitch/'
OPENVSWITCH_DEFAULT_SWITCH = '/etc/default/openvswitch-switch' # Debian
OPENVSWITCH_SYSCONFIG_SWITCH = '/etc/sysconfig/openvswitch'    # RHEL
OPENVSWITCH_CONF_DB = '/etc/openvswitch/conf.db'
OPENVSWITCH_COMPACT_DB = '/etc/openvswitch/bugtool-compact-conf.db'
OPENVSWITCH_VSWITCHD_PID = '/var/run/openvswitch/ovs-vswitchd.pid'
VAR_LOG_DIR = '/var/log/'
VAR_LOG_CORE_DIR = '/var/log/core'
YUM_LOG = '/var/log/yum.log'
YUM_REPOS_DIR = '/etc/yum.repos.d'

#
# External programs
#

os.environ['PATH'] = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/share/openvswitch/scripts'
ARP = 'arp'
CAT = 'cat'
CHKCONFIG = 'chkconfig'
DF = 'df'
DMESG = 'dmesg'
DMIDECODE = 'dmidecode'
DMSETUP = 'dmsetup'
DPKG_QUERY = 'dpkg-query'
ETHTOOL = 'ethtool'
FDISK = 'fdisk'
FIND = 'find'
IFCONFIG = 'ifconfig'
IPTABLES = 'iptables'
ISCSIADM = 'iscsiadm'
LOSETUP = 'losetup'
LS = 'ls'
LSPCI = 'lspci'
MD5SUM = 'md5sum'
MODINFO = 'modinfo'
MPPUTIL = 'mppUtil'
MULTIPATHD = 'multipathd'
NETSTAT = 'netstat'
OVS_DPCTL = 'ovs-dpctl'
OVS_OFCTL = 'ovs-ofctl'
OVS_VSCTL = 'ovs-vsctl'
PS = 'ps'
ROUTE = 'route'
RPM = 'rpm'
SG_MAP = 'sg_map'
SYSCTL = 'sysctl'
TC = 'tc'
UPTIME = 'uptime'
ZCAT = '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_BOOT_LOADER          = 'boot-loader'
CAP_DISK_INFO            = 'disk-info'
CAP_HARDWARE_INFO        = 'hardware-info'
CAP_KERNEL_INFO          = 'kernel-info'
CAP_LOSETUP_A            = 'loopback-devices'
CAP_MULTIPATH            = 'multipath'
CAP_NETWORK_CONFIG       = 'network-config'
CAP_NETWORK_INFO         = 'network-info'
CAP_NETWORK_STATUS       = 'network-status'
CAP_OPENVSWITCH_LOGS     = 'ovs-system-logs'
CAP_PROCESS_LIST         = 'process-list'
CAP_SYSTEM_LOGS          = 'system-logs'
CAP_SYSTEM_SERVICES      = 'system-services'
CAP_YUM                  = 'yum'

KB = 1024
MB = 1024 * 1024

caps = {}
cap_sizes = {}
unlimited_data = False
dbg = False
# Default value for the number of days to collect logs.
log_days = 20
log_last_mod_time = None
free_disk_space = None

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):
    caps[key] = (key, pii, min_size, max_size, min_time, max_time, mime,
                 checked, hidden)
    cap_sizes[key] = 0


cap(CAP_BOOT_LOADER,         PII_NO,                    max_size=3*KB,
    max_time=5)
cap(CAP_DISK_INFO,           PII_MAYBE,                 max_size=50*KB,
    max_time=20)
cap(CAP_HARDWARE_INFO,       PII_MAYBE,                 max_size=2*MB,
    max_time=20)
cap(CAP_KERNEL_INFO,         PII_MAYBE,                 max_size=120*KB,
    max_time=5)
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=5*MB)
cap(CAP_NETWORK_INFO,        PII_YES,                   max_size=50*MB,
    max_time=30)
cap(CAP_NETWORK_STATUS,      PII_YES,                   max_size=-1,
    max_time=30)
cap(CAP_OPENVSWITCH_LOGS,    PII_MAYBE,                 max_size=-1,
    max_time=5)
cap(CAP_PROCESS_LIST,        PII_YES,                   max_size=30*KB,
    max_time=20)
cap(CAP_SYSTEM_LOGS,         PII_MAYBE,                 max_size=200*MB,
    max_time=5)
cap(CAP_SYSTEM_SERVICES,     PII_NO,                    max_size=5*KB,
    max_time=20)
cap(CAP_YUM,                 PII_IF_CUSTOMIZED,         max_size=10*KB,
    max_time=30)

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

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, binary=False):
    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,
                       'binary': binary}


def file_output(cap, path_list, newest_first=False, last_mod_time=None):
    """
    If newest_first is True, the list of files in path_list is sorted
    by file modification time in descending order, else its sorted
    in ascending order.
    """
    if cap in entries:
        path_entries = []
        for path in path_list:
            try:
                s = os.stat(path)
            except OSError, e:
                continue
            if last_mod_time is None or s.st_mtime >= last_mod_time:
                path_entries.append((path, s))

        mtime = lambda(path, stat): stat.st_mtime
        path_entries.sort(key=mtime, reverse=newest_first)
        for p in path_entries:
            if check_space(cap, p[0], p[1].st_size):
                data[p] = {'cap': cap, 'filename': p[0]}


def tree_output(cap, path, pattern=None, negate=False, newest_first=False,
                last_mod_time=None):
    """
    Walks the directory tree rooted at path. Files in current dir are processed
    before files in sub-dirs.
    """
    if cap in entries:
        if os.path.exists(path):
            for root, dirs, files in os.walk(path):
                fns = [fn for fn in [os.path.join(root, f) for f in files]
                       if os.path.isfile(fn) and matches(fn, pattern, negate)]
                file_output(cap, fns, newest_first=newest_first,
                            last_mod_time=last_mod_time)


def prefix_output(cap, prefix, newest_first=False, last_mod_time=None):
    """
    Output files with the same prefix.
    """
    fns = []
    for root, dirs, files in os.walk(os.path.dirname(prefix)):
        fns += [fn for fn in [os.path.join(root, f) for f in files]
                if fn.startswith(prefix)]
    file_output(cap, fns, newest_first=newest_first,
                last_mod_time=last_mod_time)


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

def collect_data():
    process_lists = {}

    for (k, v) in data.items():
        cap = v['cap']
        if v.has_key('cmd_args'):
            v['output'] = StringIOmtime()
            if not process_lists.has_key(cap):
                process_lists[cap] = []
            process_lists[cap].append(
                ProcOutput(v['cmd_args'], caps[cap][MAX_TIME], v['output'],
                           v['filter'], v['binary']))
        elif v.has_key('filename') and v['filename'].startswith('/proc/'):
            # proc files must be read into memory
            try:
                f = open(v['filename'], 'r')
                s = f.read()
                f.close()
                if check_space(cap, v['filename'], len(s)):
                    v['output'] = StringIOmtime(s)
            except:
                pass
        elif v.has_key('func'):
            try:
                s = v['func'](cap)
            except Exception, e:
                s = str(e)
            if check_space(cap, k, len(s)):
                v['output'] = StringIOmtime(s)

    run_procs(process_lists.values())


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

    # Filter flags
    only_ovs_info = False
    collect_all_info = True

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

    output_file = None
    output_type = 'tar.gz'
    output_fd = -1

    if argv is None:
        argv = sys.argv

    try:
        (options, params) = getopt.gnu_getopt(
            argv, 'sy', ['capabilities', 'silent', 'yestoall', 'entries=',
                         'output=', 'outfd=', 'outfile=', 'all', 'unlimited',
                         'debug', 'ovs', 'log-days='])
    except getopt.GetoptError, opterr:
        print >>sys.stderr, opterr
        return 2

    try:
        load_plugins(True)
    except:
        pass

    entries = [e for e in caps.keys() if caps[e][CHECKED]]

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

        if k == '--output':
            if  v in ['tar', 'tar.bz2', 'tar.gz', '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

        if k == '--outfile':
            output_file = v

        elif k == '--all':
            entries = caps.keys()
        elif k == '--unlimited':
            unlimited_data = True
        elif k == '--debug':
            dbg = True
            ProcOutput.debug = True

        if k == '--ovs':
            only_ovs_info = True
            collect_all_info = False

        if k == '--log-days':
            log_days = int(v)


    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 output_fd != -1 and output_file is not None:
        print >>sys.stderr, "Cannot set both '--outfd' and '--outfile'"
        return 2

    if output_file is not None and not unlimited_data:
        free_disk_space = get_free_disk_space(output_file) * 90 / 100

    log_last_mod_time = int(time.time()) - log_days * 86400

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

    output('''
This application will collate dmesg output, details of the
hardware configuration of your machine, information about the build of
openvswitch 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

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

    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'])
    if len(pidof('iscsid')) != 0:
        cmd_output(CAP_DISK_INFO, [ISCSIADM, '-m', 'node'])
    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)

    file_output(CAP_HARDWARE_INFO, [PROC_CPUINFO, PROC_MEMINFO, PROC_IOPORTS, PROC_INTERRUPTS])
    cmd_output(CAP_HARDWARE_INFO, [DMIDECODE])
    cmd_output(CAP_HARDWARE_INFO, [LSPCI, '-n'])
    cmd_output(CAP_HARDWARE_INFO, [LSPCI, '-vv'])
    file_output(CAP_HARDWARE_INFO, [PROC_USB_DEV, PROC_SCSI])
    file_output(CAP_HARDWARE_INFO, [SYSCONFIG_HWCONF])
    cmd_output(CAP_HARDWARE_INFO, [LS, '-lR', '/dev'])


    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_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)
    cmd_output(CAP_MULTIPATH, [MPPUTIL, '-a'])
    if CAP_MULTIPATH in entries and collect_all_info:
        dump_rdac_groups(CAP_MULTIPATH)

    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_DEFAULT_SWITCH,
                OPENVSWITCH_SYSCONFIG_SWITCH])

    cmd_output(CAP_NETWORK_INFO, [IFCONFIG, '-a'])
    cmd_output(CAP_NETWORK_INFO, [ROUTE, '-n'])
    cmd_output(CAP_NETWORK_INFO, [ARP, '-n'])
    cmd_output(CAP_NETWORK_INFO, [NETSTAT, '-an'])
    for dir in DHCP_LEASE_DIR:
        tree_output(CAP_NETWORK_INFO, dir)
    for table in ['filter', 'nat', 'mangle', 'raw', 'security']:
        cmd_output(CAP_NETWORK_INFO, [IPTABLES, '-t', table, '-nL'])
    for p in os.listdir('/sys/class/net/'):
        try:
            f = open('/sys/class/net/%s/type' % p, 'r')
            t = f.readline()
            f.close()
            if os.path.islink('/sys/class/net/%s/device' % p) and int(t) == 1:
                # ARPHRD_ETHER
                cmd_output(CAP_NETWORK_INFO, [ETHTOOL, '-S', p])
                if not p.startswith('vif') and not p.startswith('tap'):
                    cmd_output(CAP_NETWORK_INFO, [ETHTOOL, p])
                    cmd_output(CAP_NETWORK_INFO, [ETHTOOL, '-k', p])
                    cmd_output(CAP_NETWORK_INFO, [ETHTOOL, '-i', p])
                    cmd_output(CAP_NETWORK_INFO, [ETHTOOL, '-c', p])
            if int(t) == 1:
                cmd_output(CAP_NETWORK_INFO,
                           [TC, '-s', '-d', 'class', 'show', 'dev', p])
        except:
            pass
    tree_output(CAP_NETWORK_INFO, PROC_NET_BONDING_DIR)
    tree_output(CAP_NETWORK_INFO, PROC_NET_VLAN_DIR)
    cmd_output(CAP_NETWORK_INFO, [TC, '-s', 'qdisc'])
    file_output(CAP_NETWORK_INFO, [PROC_NET_SOFTNET_STAT])

    collect_ovsdb()
    if os.path.exists(OPENVSWITCH_VSWITCHD_PID):
        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_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)

    system_logs = ([ VAR_LOG_DIR + x for x in
        ['crit.log', 'kern.log', 'daemon.log', 'user.log',
        'syslog', 'messages', 'secure', 'debug', 'dmesg', 'boot']])
    for log in system_logs:
        prefix_output(CAP_SYSTEM_LOGS, log, last_mod_time=log_last_mod_time)

    ovs_logs = ([ OPENVSWITCH_LOG_DIR + x for x in
        ['ovs-vswitchd.log', 'ovsdb-server.log',
        'ovs-xapi-sync.log', 'ovs-monitor-ipsec.log', 'ovs-ctl.log']])
    for log in ovs_logs:
        prefix_output(CAP_OPENVSWITCH_LOGS, log, last_mod_time=log_last_mod_time)

    if not os.path.exists('/var/log/dmesg') and not os.path.exists('/var/log/boot'):
        cmd_output(CAP_SYSTEM_LOGS, [DMESG])

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

    tree_output(CAP_SYSTEM_LOGS, VAR_LOG_CORE_DIR)

    file_output(CAP_YUM, [YUM_LOG])
    tree_output(CAP_YUM, YUM_REPOS_DIR)
    cmd_output(CAP_YUM, [RPM, '-qa'])
    file_output(CAP_YUM, [APT_SOURCES_LIST])
    tree_output(CAP_YUM, APT_SOURCES_LIST_D)
    cmd_output(CAP_YUM, [DPKG_QUERY, '-W', '-f=${Package} ${Version} ${Status}\n'], 'dpkg-packages')

    # Filter out ovs relevant information if --ovs option passed
    # else collect all information
    filters = set()
    if only_ovs_info:
        filters.add('ovs')
        ovs_info_caps = [CAP_NETWORK_STATUS, CAP_SYSTEM_LOGS,
                         CAP_NETWORK_CONFIG]
        ovs_info_list = ['process-tree']
        # We cannot use iteritems, since we modify 'data' as we pass through
        for (k, v) in data.items():
            cap = v['cap']
            if 'filename' in v:
                info = k[0]
            else:
                info = k
            if info not in ovs_info_list and cap not in ovs_info_caps:
                del data[k]

    if filters:
        filter = ",".join(filters)
    else:
        filter = None

    try:
        load_plugins(filter=filter)
    except:
        pass

    # permit the user to filter out data
    # We cannot use iteritems, since we modify 'data' as we pass through
    for (k, v) in sorted(data.items()):
       cap = v['cap']
       if 'filename' in v:
           key = k[0]
       else:
           key = k
       if not ANSWER_YES_TO_ALL and not yes("Include '%s'? [Y/n]: " % key):
           del data[k]

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

    subdir = "bug-report-%s" % time.strftime("%Y%m%d%H%M%S")

    # include inventory
    data['inventory.xml'] = {'cap': None, 'output': StringIOmtime(make_inventory(data, subdir))}

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

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

    if output_type.startswith('tar'):
        make_tar(subdir, output_type, output_fd, output_file)
    else:
        make_zip(subdir, output_file)

    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])

    cleanup_ovsdb()
    return 0

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 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 collect_ovsdb():
    if not os.path.isfile(OPENVSWITCH_CONF_DB):
        return

    max_size = 10*MB

    try:
        if os.path.getsize(OPENVSWITCH_CONF_DB) > max_size:
            if os.path.isfile(OPENVSWITCH_COMPACT_DB):
                os.unlink(OPENVSWITCH_COMPACT_DB)

            output = StringIO.StringIO()
            max_time = 5
            procs = [ProcOutput(['ovsdb-tool', 'compact',
                                OPENVSWITCH_CONF_DB, OPENVSWITCH_COMPACT_DB],
                                max_time, output)]
            run_procs([procs])
            file_output(CAP_NETWORK_STATUS, [OPENVSWITCH_COMPACT_DB])
        else:
            file_output(CAP_NETWORK_STATUS, [OPENVSWITCH_CONF_DB])
    except OSError, e:
        return

def cleanup_ovsdb():
    try:
        if os.path.isfile(OPENVSWITCH_COMPACT_DB):
            os.unlink(OPENVSWITCH_COMPACT_DB)
    except:
        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 dump_rdac_groups(cap):
    output = StringIO.StringIO()
    procs = [ProcOutput([MPPUTIL, '-a'], caps[cap][MAX_TIME], output)]

    run_procs([procs])

    if not procs[0].timed_out:
        proc_line = 0
        for line in output.getvalue().splitlines():
            if line.startswith('ID'):
                proc_line = 2
            elif line.startswith('----'):
                proc_line -= 1
            elif proc_line > 0:
                group, _ = line.split(None, 1)
                cmd_output(cap, [MPPUTIL, '-g', group])

def load_plugins(just_capabilities=False, filter=None):
    global log_last_mod_time
    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("*"):
                filters_tmp = el.getAttribute("filters")
                if filters_tmp == '':
                    filters = []
                else:
                    filters = filters_tmp.split(',')
                if not(filter is None or filter in filters):
                    continue
                if el.tagName == "files":
                    newest_first = getBoolAttr(el, 'newest_first')
                    if el.getAttribute("type") == "logs":
                        for fn in getText(el.childNodes).split():
                            prefix_output(dir, fn, newest_first=newest_first,
                                          last_mod_time=log_last_mod_time)
                    else:
                        file_output(dir, getText(el.childNodes).split(),
                                    newest_first=newest_first)
                elif el.tagName == "directory":
                    pattern = el.getAttribute("pattern")
                    if pattern == '': pattern = None
                    negate = getBoolAttr(el, 'negate')
                    newest_first = getBoolAttr(el, 'newest_first')
                    if el.getAttribute("type") == "logs":
                        tree_output(dir, getText(el.childNodes),
                                    pattern and re.compile(pattern) or None,
                                    negate=negate, newest_first=newest_first,
                                    last_mod_time=log_last_mod_time)
                    else:
                        tree_output(dir, getText(el.childNodes),
                                    pattern and re.compile(pattern) or None,
                                    negate=negate, newest_first=newest_first)
                elif el.tagName == "command":
                    label = el.getAttribute("label")
                    if label == '': label = None
                    binary = getBoolAttr(el, 'binary')
                    cmd_output(dir, getText(el.childNodes), label, binary=binary)

def make_tar(subdir, suffix, output_fd, output_file):
    global SILENT_MODE, data

    mode = 'w'
    if suffix == 'tar.bz2':
        mode = 'w:bz2'
    elif suffix == 'tar.gz':
        mode = 'w:gz'

    if output_fd == -1:
        if output_file is None:
            filename = "%s/%s.%s" % (BUG_DIR, subdir, suffix)
        else:
            filename = output_file
        old_umask = os.umask(0077)
        tf = tarfile.open(filename, mode)
        os.umask(old_umask)
    else:
        tf = tarfile.open(None, 'w', os.fdopen(output_fd, 'a'))

    try:
        for (k, v) in data.items():
            try:
                tar_filename = os.path.join(subdir, construct_filename(k, v))
                ti = tarfile.TarInfo(tar_filename)

                ti.uname = 'root'
                ti.gname = 'root'

                if v.has_key('output'):
                    ti.mtime = v['output'].mtime
                    ti.size = len(v['output'].getvalue())
                    v['output'].seek(0)
                    tf.addfile(ti, v['output'])
                elif v.has_key('filename'):
                    s = os.stat(v['filename'])
                    ti.mtime = s.st_mtime
                    ti.size = s.st_size
                    tf.addfile(ti, file(v['filename']))
            except:
                pass
    finally:
        tf.close()

    if output_fd == -1:
        output ('Writing tarball %s successful.' % filename)
        if SILENT_MODE:
            print filename


def make_zip(subdir, output_file):
    global SILENT_MODE, data

    if output_file is None:
        filename = "%s/%s.zip" % (BUG_DIR, subdir)
    else:
        filename = output_file
    old_umask = os.umask(0077)
    zf = zipfile.ZipFile(filename, 'w', zipfile.ZIP_DEFLATED)
    os.umask(old_umask)

    try:
        for (k, v) in data.items():
            try:
                dest = os.path.join(subdir, construct_filename(k, v))

                if v.has_key('output'):
                    zf.writestr(dest, v['output'].getvalue())
                else:
                    if os.stat(v['filename']).st_size < 50:
                        compress_type = zipfile.ZIP_STORED
                    else:
                        compress_type = zipfile.ZIP_DEFLATED
                    zf.write(v['filename'], dest, compress_type)
            except:
                pass
    finally:
        zf.close()

    output ('Writing archive %s successful.' % filename)
    if SILENT_MODE:
        print filename


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', os.path.join(subdir, construct_filename(k, v)))
        el.setAttribute('md5sum', md5sum(v))
        document.getElementsByTagName(INVENTORY_XML_ROOT)[0].appendChild(el)
    except:
        pass


def md5sum(d):
    m = md5.new()
    if d.has_key('filename'):
        f = open(d['filename'])
        data = f.read(1024)
        while len(data) > 0:
            m.update(data)
            data = f.read(1024)
        f.close()
    elif d.has_key('output'):
        m.update(d['output'].getvalue())
    return m.hexdigest()


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

    return s

def update_capabilities():
    pass

def update_cap_size(cap, size):
    update_cap(cap, MIN_SIZE, size)
    update_cap(cap, MAX_SIZE, size)
    update_cap(cap, CHECKED, size > 0)


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, binary=False):
        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.timeout = int(time.time()) + self.max_time
        self.filter = filter
        self.filter_state = {}
        if binary:
            self.bufsize = 1048576  # 1MB buffer
        else:
            self.bufsize = 1        # line buffered

    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=self.bufsize,
                              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
        if self.bufsize == 1:
            line = self.proc.stdout.readline()
        else:
            line = self.proc.stdout.read(self.bufsize)
        if line == '':
            # process exited
            self.proc.stdout.close()
            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)

def run_procs(procs):
    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 p.running and now > p.timeout:
                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 check_space(cap, name, size):
    global free_disk_space
    if free_disk_space is not None and size > free_disk_space:
        output("Omitting %s, out of disk space (requested: %u, allowed: %u)" %
               (name, size, free_disk_space))
        return False
    elif unlimited_data or caps[cap][MAX_SIZE] == -1 or \
             cap_sizes[cap] < caps[cap][MAX_SIZE]:
        cap_sizes[cap] += size
        if free_disk_space is not None:
            free_disk_space -= size
        return True
    else:
        output("Omitting %s, size constraint of %s exceeded" % (name, cap))
        return False


def get_free_disk_space(path):
    path = os.path.abspath(path)
    while not os.path.exists(path):
        path = os.path.dirname(path)
    s = os.statvfs(path)
    return s.f_frsize * s.f_bfree


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)