blob: 0a9f810877b855540dfd1a63bdb0303dacd263e1 [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
Sean Dague737e9422015-05-12 19:51:39 -040021import fnmatch
Sean Dague97fcc7b2014-06-16 17:24:14 -040022import os
23import os.path
Ian Wienand99440f92015-07-01 06:14:01 +100024import subprocess
Sean Dague97fcc7b2014-06-16 17:24:14 -040025import sys
26
27
28def get_options():
29 parser = argparse.ArgumentParser(
30 description='Dump world state for debugging')
31 parser.add_argument('-d', '--dir',
32 default='.',
33 help='Output directory for worlddump')
34 return parser.parse_args()
35
36
37def filename(dirname):
38 now = datetime.datetime.utcnow()
39 return os.path.join(dirname, now.strftime("worlddump-%Y-%m-%d-%H%M%S.txt"))
40
41
42def warn(msg):
43 print "WARN: %s" % msg
44
45
Sean Dague60a14052015-05-11 14:53:39 -040046def _dump_cmd(cmd):
47 print cmd
48 print "-" * len(cmd)
49 print
Ian Wienand99440f92015-07-01 06:14:01 +100050 try:
51 subprocess.check_call(cmd, shell=True)
52 except subprocess.CalledProcessError:
53 print "*** Failed to run: %s" % cmd
Sean Dague60a14052015-05-11 14:53:39 -040054
55
56def _header(name):
57 print
58 print name
59 print "=" * len(name)
60 print
61
62
Sean Dague97fcc7b2014-06-16 17:24:14 -040063def disk_space():
64 # the df output
Sean Dague60a14052015-05-11 14:53:39 -040065 _header("File System Summary")
66
Sean Dague97fcc7b2014-06-16 17:24:14 -040067 dfraw = os.popen("df -Ph").read()
68 df = [s.split() for s in dfraw.splitlines()]
69 for fs in df:
70 try:
71 if int(fs[4][:-1]) > 95:
72 warn("Device %s (%s) is %s full, might be an issue" % (
73 fs[0], fs[5], fs[4]))
74 except ValueError:
75 # if it doesn't look like an int, that's fine
76 pass
77
78 print dfraw
79
80
Sean Dague168b7c22015-05-07 08:57:28 -040081def iptables_dump():
82 tables = ['filter', 'nat', 'mangle']
Sean Dague60a14052015-05-11 14:53:39 -040083 _header("IP Tables Dump")
84
Sean Dague168b7c22015-05-07 08:57:28 -040085 for table in tables:
Sean Dague60a14052015-05-11 14:53:39 -040086 _dump_cmd("sudo iptables --line-numbers -L -nv -t %s" % table)
87
88
89def network_dump():
90 _header("Network Dump")
91
92 _dump_cmd("brctl show")
93 _dump_cmd("arp -n")
94 _dump_cmd("ip addr")
95 _dump_cmd("ip link")
96 _dump_cmd("ip route")
Sean Dague168b7c22015-05-07 08:57:28 -040097
98
Sean Dague97fcc7b2014-06-16 17:24:14 -040099def process_list():
Sean Dague60a14052015-05-11 14:53:39 -0400100 _header("Process Listing")
101 _dump_cmd("ps axo "
102 "user,ppid,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,args")
Sean Dague97fcc7b2014-06-16 17:24:14 -0400103
104
Sean Dague737e9422015-05-12 19:51:39 -0400105def compute_consoles():
106 _header("Compute consoles")
107 for root, dirnames, filenames in os.walk('/opt/stack'):
108 for filename in fnmatch.filter(filenames, 'console.log'):
109 fullpath = os.path.join(root, filename)
110 _dump_cmd("sudo cat %s" % fullpath)
111
112
Joe Gordon2ebe9932015-06-07 16:57:34 +0900113def guru_meditation_report():
114 _header("nova-compute Guru Meditation Report")
115 _dump_cmd("kill -s USR1 `pgrep nova-compute`")
116 print "guru meditation report in nova-compute log"
117
118
Sean Dague97fcc7b2014-06-16 17:24:14 -0400119def main():
120 opts = get_options()
121 fname = filename(opts.dir)
122 print "World dumping... see %s for details" % fname
123 sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
124 with open(fname, 'w') as f:
125 os.dup2(f.fileno(), sys.stdout.fileno())
126 disk_space()
127 process_list()
Sean Dague60a14052015-05-11 14:53:39 -0400128 network_dump()
Sean Dague168b7c22015-05-07 08:57:28 -0400129 iptables_dump()
Sean Dague737e9422015-05-12 19:51:39 -0400130 compute_consoles()
Joe Gordon2ebe9932015-06-07 16:57:34 +0900131 guru_meditation_report()
Sean Dague97fcc7b2014-06-16 17:24:14 -0400132
133
134if __name__ == '__main__':
135 try:
136 sys.exit(main())
137 except KeyboardInterrupt:
138 sys.exit(1)