blob: 7f2614dcaa16a020f4f1825a6388e2f69758b920 [file] [log] [blame]
Sean Dague97fcc7b2014-06-16 17:24:14 -04001#!/usr/bin/env python
2#
3# Copyright 2014 Hewlett-Packard Development Company, L.P.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
17"""Dump the state of the world for post mortem."""
18
19import argparse
20import datetime
21import os
22import os.path
23import sys
24
25
26def get_options():
27 parser = argparse.ArgumentParser(
28 description='Dump world state for debugging')
29 parser.add_argument('-d', '--dir',
30 default='.',
31 help='Output directory for worlddump')
32 return parser.parse_args()
33
34
35def filename(dirname):
36 now = datetime.datetime.utcnow()
37 return os.path.join(dirname, now.strftime("worlddump-%Y-%m-%d-%H%M%S.txt"))
38
39
40def warn(msg):
41 print "WARN: %s" % msg
42
43
Sean Dague60a14052015-05-11 14:53:39 -040044def _dump_cmd(cmd):
45 print cmd
46 print "-" * len(cmd)
47 print
48 print os.popen(cmd).read()
49
50
51def _header(name):
52 print
53 print name
54 print "=" * len(name)
55 print
56
57
Sean Dague97fcc7b2014-06-16 17:24:14 -040058def disk_space():
59 # the df output
Sean Dague60a14052015-05-11 14:53:39 -040060 _header("File System Summary")
61
Sean Dague97fcc7b2014-06-16 17:24:14 -040062 dfraw = os.popen("df -Ph").read()
63 df = [s.split() for s in dfraw.splitlines()]
64 for fs in df:
65 try:
66 if int(fs[4][:-1]) > 95:
67 warn("Device %s (%s) is %s full, might be an issue" % (
68 fs[0], fs[5], fs[4]))
69 except ValueError:
70 # if it doesn't look like an int, that's fine
71 pass
72
73 print dfraw
74
75
Sean Dague168b7c22015-05-07 08:57:28 -040076def iptables_dump():
77 tables = ['filter', 'nat', 'mangle']
Sean Dague60a14052015-05-11 14:53:39 -040078 _header("IP Tables Dump")
79
Sean Dague168b7c22015-05-07 08:57:28 -040080 for table in tables:
Sean Dague60a14052015-05-11 14:53:39 -040081 _dump_cmd("sudo iptables --line-numbers -L -nv -t %s" % table)
82
83
84def network_dump():
85 _header("Network Dump")
86
87 _dump_cmd("brctl show")
88 _dump_cmd("arp -n")
89 _dump_cmd("ip addr")
90 _dump_cmd("ip link")
91 _dump_cmd("ip route")
Sean Dague168b7c22015-05-07 08:57:28 -040092
93
Sean Dague97fcc7b2014-06-16 17:24:14 -040094def process_list():
Sean Dague60a14052015-05-11 14:53:39 -040095 _header("Process Listing")
96 _dump_cmd("ps axo "
97 "user,ppid,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,args")
Sean Dague97fcc7b2014-06-16 17:24:14 -040098
99
100def main():
101 opts = get_options()
102 fname = filename(opts.dir)
103 print "World dumping... see %s for details" % fname
104 sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
105 with open(fname, 'w') as f:
106 os.dup2(f.fileno(), sys.stdout.fileno())
107 disk_space()
108 process_list()
Sean Dague60a14052015-05-11 14:53:39 -0400109 network_dump()
Sean Dague168b7c22015-05-07 08:57:28 -0400110 iptables_dump()
Sean Dague97fcc7b2014-06-16 17:24:14 -0400111
112
113if __name__ == '__main__':
114 try:
115 sys.exit(main())
116 except KeyboardInterrupt:
117 sys.exit(1)