blob: d1293742535cfb835d9757facdb19260141006a8 [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')
Sean Dagueac9313e2015-07-27 13:33:30 -040034 parser.add_argument('-n', '--name',
35 default='',
36 help='Additional name to tag into file')
Sean Dague97fcc7b2014-06-16 17:24:14 -040037 return parser.parse_args()
38
39
Sean Dagueac9313e2015-07-27 13:33:30 -040040def filename(dirname, name=""):
Sean Dague97fcc7b2014-06-16 17:24:14 -040041 now = datetime.datetime.utcnow()
Sean Dagueac9313e2015-07-27 13:33:30 -040042 fmt = "worlddump-%Y-%m-%d-%H%M%S"
43 if name:
44 fmt += "-" + name
45 fmt += ".txt"
46 return os.path.join(dirname, now.strftime(fmt))
Sean Dague97fcc7b2014-06-16 17:24:14 -040047
48
49def warn(msg):
50 print "WARN: %s" % msg
51
52
Sean Dague60a14052015-05-11 14:53:39 -040053def _dump_cmd(cmd):
54 print cmd
55 print "-" * len(cmd)
56 print
Ian Wienand99440f92015-07-01 06:14:01 +100057 try:
58 subprocess.check_call(cmd, shell=True)
Ihar Hrachyshka190b29d2016-02-11 13:42:21 +010059 print
Ian Wienand99440f92015-07-01 06:14:01 +100060 except subprocess.CalledProcessError:
61 print "*** Failed to run: %s" % cmd
Sean Dague60a14052015-05-11 14:53:39 -040062
63
64def _header(name):
65 print
66 print name
67 print "=" * len(name)
68 print
69
70
Sean Dague97fcc7b2014-06-16 17:24:14 -040071def disk_space():
72 # the df output
Sean Dague60a14052015-05-11 14:53:39 -040073 _header("File System Summary")
74
Sean Dague97fcc7b2014-06-16 17:24:14 -040075 dfraw = os.popen("df -Ph").read()
76 df = [s.split() for s in dfraw.splitlines()]
77 for fs in df:
78 try:
79 if int(fs[4][:-1]) > 95:
80 warn("Device %s (%s) is %s full, might be an issue" % (
81 fs[0], fs[5], fs[4]))
82 except ValueError:
83 # if it doesn't look like an int, that's fine
84 pass
85
86 print dfraw
87
88
Sean Dague2da606d2015-08-06 10:02:43 -040089def ebtables_dump():
Sean Dague5c5e0862015-11-09 14:08:15 -050090 tables = ['filter', 'nat', 'broute']
Sean Dague2da606d2015-08-06 10:02:43 -040091 _header("EB Tables Dump")
Sean Dague5c5e0862015-11-09 14:08:15 -050092 for table in tables:
93 _dump_cmd("sudo ebtables -t %s -L" % table)
Sean Dague2da606d2015-08-06 10:02:43 -040094
95
Sean Dague168b7c22015-05-07 08:57:28 -040096def iptables_dump():
97 tables = ['filter', 'nat', 'mangle']
Sean Dague60a14052015-05-11 14:53:39 -040098 _header("IP Tables Dump")
99
Sean Dague168b7c22015-05-07 08:57:28 -0400100 for table in tables:
Sean Dague60a14052015-05-11 14:53:39 -0400101 _dump_cmd("sudo iptables --line-numbers -L -nv -t %s" % table)
102
103
Ihar Hrachyshka72c34ee2016-01-30 16:18:01 +0100104def _netns_list():
105 process = subprocess.Popen(['ip', 'netns'], stdout=subprocess.PIPE)
106 stdout, _ = process.communicate()
107 return stdout.split()
108
109
Sean Dague60a14052015-05-11 14:53:39 -0400110def network_dump():
111 _header("Network Dump")
112
113 _dump_cmd("brctl show")
114 _dump_cmd("arp -n")
Ihar Hrachyshka72c34ee2016-01-30 16:18:01 +0100115 ip_cmds = ["addr", "link", "route"]
116 for cmd in ip_cmds + ['netns']:
117 _dump_cmd("ip %s" % cmd)
118 for netns_ in _netns_list():
119 for cmd in ip_cmds:
120 args = {'netns': netns_, 'cmd': cmd}
121 _dump_cmd('sudo ip netns exec %(netns)s ip %(cmd)s' % args)
Sean Dague168b7c22015-05-07 08:57:28 -0400122
123
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100124def ovs_dump():
125 _header("Open vSwitch Dump")
126
127 # NOTE(ihrachys): worlddump is used outside of devstack context (f.e. in
128 # grenade), so there is no single place to determine the bridge names from.
129 # Hardcode for now.
130 bridges = ('br-int', 'br-tun', 'br-ex')
131 _dump_cmd("sudo ovs-vsctl show")
132 for bridge in bridges:
133 _dump_cmd("sudo ovs-ofctl show %s" % bridge)
134 for bridge in bridges:
135 _dump_cmd("sudo ovs-ofctl dump-flows %s" % bridge)
136
137
Sean Dague97fcc7b2014-06-16 17:24:14 -0400138def process_list():
Sean Dague60a14052015-05-11 14:53:39 -0400139 _header("Process Listing")
140 _dump_cmd("ps axo "
141 "user,ppid,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,args")
Sean Dague97fcc7b2014-06-16 17:24:14 -0400142
143
Sean Dague737e9422015-05-12 19:51:39 -0400144def compute_consoles():
145 _header("Compute consoles")
146 for root, dirnames, filenames in os.walk('/opt/stack'):
147 for filename in fnmatch.filter(filenames, 'console.log'):
148 fullpath = os.path.join(root, filename)
149 _dump_cmd("sudo cat %s" % fullpath)
150
151
Joe Gordon2ebe9932015-06-07 16:57:34 +0900152def guru_meditation_report():
153 _header("nova-compute Guru Meditation Report")
Ian Wienand3a9df1d2015-07-01 06:18:47 +1000154
155 try:
156 subprocess.check_call(["pgrep","nova-compute"])
157 except subprocess.CalledProcessError:
158 print "Skipping as nova-compute does not appear to be running"
159 return
160
Kashyap Chamarthy88725452015-09-14 13:17:56 +0200161 _dump_cmd("kill -s USR2 `pgrep nova-compute`")
Joe Gordon2ebe9932015-06-07 16:57:34 +0900162 print "guru meditation report in nova-compute log"
163
164
Sean Dague97fcc7b2014-06-16 17:24:14 -0400165def main():
166 opts = get_options()
Sean Dagueac9313e2015-07-27 13:33:30 -0400167 fname = filename(opts.dir, opts.name)
Sean Dague97fcc7b2014-06-16 17:24:14 -0400168 print "World dumping... see %s for details" % fname
169 sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
170 with open(fname, 'w') as f:
171 os.dup2(f.fileno(), sys.stdout.fileno())
172 disk_space()
173 process_list()
Sean Dague60a14052015-05-11 14:53:39 -0400174 network_dump()
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100175 ovs_dump()
Sean Dague168b7c22015-05-07 08:57:28 -0400176 iptables_dump()
Sean Dague2da606d2015-08-06 10:02:43 -0400177 ebtables_dump()
Sean Dague737e9422015-05-12 19:51:39 -0400178 compute_consoles()
Joe Gordon2ebe9932015-06-07 16:57:34 +0900179 guru_meditation_report()
Sean Dague97fcc7b2014-06-16 17:24:14 -0400180
181
182if __name__ == '__main__':
183 try:
184 sys.exit(main())
185 except KeyboardInterrupt:
186 sys.exit(1)