blob: 8c053e038f569e6f082db18dbbe9e8b9bebf20d8 [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
Eyale7361772016-04-05 16:18:56 +030017
Sean Dague97fcc7b2014-06-16 17:24:14 -040018"""Dump the state of the world for post mortem."""
19
20import argparse
21import datetime
Chris Dent57d79672016-02-23 15:38:43 +000022from distutils import spawn
Sean Dague737e9422015-05-12 19:51:39 -040023import fnmatch
Sean Dague97fcc7b2014-06-16 17:24:14 -040024import os
25import os.path
Ian Wienand99440f92015-07-01 06:14:01 +100026import subprocess
Sean Dague97fcc7b2014-06-16 17:24:14 -040027import sys
28
29
30def get_options():
31 parser = argparse.ArgumentParser(
32 description='Dump world state for debugging')
33 parser.add_argument('-d', '--dir',
34 default='.',
35 help='Output directory for worlddump')
Sean Dagueac9313e2015-07-27 13:33:30 -040036 parser.add_argument('-n', '--name',
37 default='',
38 help='Additional name to tag into file')
Sean Dague97fcc7b2014-06-16 17:24:14 -040039 return parser.parse_args()
40
41
Sean Dagueac9313e2015-07-27 13:33:30 -040042def filename(dirname, name=""):
Sean Dague97fcc7b2014-06-16 17:24:14 -040043 now = datetime.datetime.utcnow()
Sean Dagueac9313e2015-07-27 13:33:30 -040044 fmt = "worlddump-%Y-%m-%d-%H%M%S"
45 if name:
46 fmt += "-" + name
47 fmt += ".txt"
48 return os.path.join(dirname, now.strftime(fmt))
Sean Dague97fcc7b2014-06-16 17:24:14 -040049
50
51def warn(msg):
Eyale7361772016-04-05 16:18:56 +030052 print("WARN: %s" % msg)
Sean Dague97fcc7b2014-06-16 17:24:14 -040053
54
Sean Dague60a14052015-05-11 14:53:39 -040055def _dump_cmd(cmd):
Eyale7361772016-04-05 16:18:56 +030056 print(cmd)
57 print("-" * len(cmd))
58 print()
Ian Wienand99440f92015-07-01 06:14:01 +100059 try:
60 subprocess.check_call(cmd, shell=True)
Eyale7361772016-04-05 16:18:56 +030061 print()
Ihar Hrachyshka7976aac2016-03-03 15:30:49 +010062 except subprocess.CalledProcessError as e:
Eyale7361772016-04-05 16:18:56 +030063 print("*** Failed to run '%(cmd)s': %(err)s" % {'cmd': cmd, 'err': e})
Sean Dague60a14052015-05-11 14:53:39 -040064
65
Chris Dent57d79672016-02-23 15:38:43 +000066def _find_cmd(cmd):
67 if not spawn.find_executable(cmd):
Eyale7361772016-04-05 16:18:56 +030068 print("*** %s not found: skipping" % cmd)
Chris Dent57d79672016-02-23 15:38:43 +000069 return False
70 return True
71
72
Sean Dague60a14052015-05-11 14:53:39 -040073def _header(name):
Eyale7361772016-04-05 16:18:56 +030074 print()
75 print(name)
76 print("=" * len(name))
77 print()
Sean Dague60a14052015-05-11 14:53:39 -040078
79
fumihiko kakuma578459f2016-04-07 08:15:45 +090080def _bridge_list():
81 process = subprocess.Popen(['ovs-vsctl', 'list-br'], stdout=subprocess.PIPE)
82 stdout, _ = process.communicate()
83 return stdout.split()
84
85
fumihiko kakuma60994012016-03-08 20:55:01 +090086# This method gets a max openflow version supported by openvswitch.
87# For example 'ovs-ofctl --version' displays the following:
88#
89# ovs-ofctl (Open vSwitch) 2.0.2
90# Compiled Dec 9 2015 14:08:08
91# OpenFlow versions 0x1:0x4
92#
fumihiko kakuma2bd25682016-04-05 10:33:50 +090093# The above shows that openvswitch supports from OpenFlow10 to OpenFlow13.
fumihiko kakuma60994012016-03-08 20:55:01 +090094# This method gets max version searching 'OpenFlow versions 0x1:0x'.
95# And return a version value converted to an integer type.
96def _get_ofp_version():
97 process = subprocess.Popen(['ovs-ofctl', '--version'], stdout=subprocess.PIPE)
98 stdout, _ = process.communicate()
99 find_str = 'OpenFlow versions 0x1:0x'
100 offset = stdout.find(find_str)
101 return int(stdout[offset + len(find_str):-1]) - 1
102
103
Sean Dague97fcc7b2014-06-16 17:24:14 -0400104def disk_space():
105 # the df output
Sean Dague60a14052015-05-11 14:53:39 -0400106 _header("File System Summary")
107
Sean Dague97fcc7b2014-06-16 17:24:14 -0400108 dfraw = os.popen("df -Ph").read()
109 df = [s.split() for s in dfraw.splitlines()]
110 for fs in df:
111 try:
112 if int(fs[4][:-1]) > 95:
113 warn("Device %s (%s) is %s full, might be an issue" % (
114 fs[0], fs[5], fs[4]))
115 except ValueError:
116 # if it doesn't look like an int, that's fine
117 pass
118
Eyale7361772016-04-05 16:18:56 +0300119 print(dfraw)
Sean Dague97fcc7b2014-06-16 17:24:14 -0400120
121
Sean Dague2da606d2015-08-06 10:02:43 -0400122def ebtables_dump():
Sean Dague5c5e0862015-11-09 14:08:15 -0500123 tables = ['filter', 'nat', 'broute']
Sean Dague2da606d2015-08-06 10:02:43 -0400124 _header("EB Tables Dump")
Chris Dent57d79672016-02-23 15:38:43 +0000125 if not _find_cmd('ebtables'):
126 return
Sean Dague5c5e0862015-11-09 14:08:15 -0500127 for table in tables:
128 _dump_cmd("sudo ebtables -t %s -L" % table)
Sean Dague2da606d2015-08-06 10:02:43 -0400129
130
Sean Dague168b7c22015-05-07 08:57:28 -0400131def iptables_dump():
132 tables = ['filter', 'nat', 'mangle']
Sean Dague60a14052015-05-11 14:53:39 -0400133 _header("IP Tables Dump")
134
Sean Dague168b7c22015-05-07 08:57:28 -0400135 for table in tables:
Sean Dague60a14052015-05-11 14:53:39 -0400136 _dump_cmd("sudo iptables --line-numbers -L -nv -t %s" % table)
137
138
Ihar Hrachyshka72c34ee2016-01-30 16:18:01 +0100139def _netns_list():
140 process = subprocess.Popen(['ip', 'netns'], stdout=subprocess.PIPE)
141 stdout, _ = process.communicate()
142 return stdout.split()
143
144
Sean Dague60a14052015-05-11 14:53:39 -0400145def network_dump():
146 _header("Network Dump")
147
148 _dump_cmd("brctl show")
149 _dump_cmd("arp -n")
Ihar Hrachyshka72c34ee2016-01-30 16:18:01 +0100150 ip_cmds = ["addr", "link", "route"]
151 for cmd in ip_cmds + ['netns']:
152 _dump_cmd("ip %s" % cmd)
153 for netns_ in _netns_list():
154 for cmd in ip_cmds:
155 args = {'netns': netns_, 'cmd': cmd}
156 _dump_cmd('sudo ip netns exec %(netns)s ip %(cmd)s' % args)
Sean Dague168b7c22015-05-07 08:57:28 -0400157
158
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100159def ovs_dump():
160 _header("Open vSwitch Dump")
161
Chris Dent57d79672016-02-23 15:38:43 +0000162 # NOTE(cdent): If we're not using neutron + ovs these commands
163 # will not be present so
164 if not _find_cmd('ovs-vsctl'):
165 return
166
fumihiko kakuma578459f2016-04-07 08:15:45 +0900167 bridges = _bridge_list()
fumihiko kakuma60994012016-03-08 20:55:01 +0900168 ofctl_cmds = ('show', 'dump-ports-desc', 'dump-ports', 'dump-flows')
169 ofp_max = _get_ofp_version()
170 vers = 'OpenFlow10'
fumihiko kakuma578459f2016-04-07 08:15:45 +0900171 for i in range(1, ofp_max + 1):
fumihiko kakuma60994012016-03-08 20:55:01 +0900172 vers += ',OpenFlow1' + str(i)
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100173 _dump_cmd("sudo ovs-vsctl show")
fumihiko kakuma60994012016-03-08 20:55:01 +0900174 for ofctl_cmd in ofctl_cmds:
175 for bridge in bridges:
176 args = {'vers': vers, 'cmd': ofctl_cmd, 'bridge': bridge}
177 _dump_cmd("sudo ovs-ofctl --protocols=%(vers)s %(cmd)s %(bridge)s" % args)
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100178
179
Sean Dague97fcc7b2014-06-16 17:24:14 -0400180def process_list():
Sean Dague60a14052015-05-11 14:53:39 -0400181 _header("Process Listing")
182 _dump_cmd("ps axo "
183 "user,ppid,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,args")
Sean Dague97fcc7b2014-06-16 17:24:14 -0400184
185
Sean Dague737e9422015-05-12 19:51:39 -0400186def compute_consoles():
187 _header("Compute consoles")
188 for root, dirnames, filenames in os.walk('/opt/stack'):
189 for filename in fnmatch.filter(filenames, 'console.log'):
190 fullpath = os.path.join(root, filename)
191 _dump_cmd("sudo cat %s" % fullpath)
192
193
Joe Gordon2ebe9932015-06-07 16:57:34 +0900194def guru_meditation_report():
195 _header("nova-compute Guru Meditation Report")
Ian Wienand3a9df1d2015-07-01 06:18:47 +1000196
197 try:
198 subprocess.check_call(["pgrep","nova-compute"])
199 except subprocess.CalledProcessError:
Eyale7361772016-04-05 16:18:56 +0300200 print("Skipping as nova-compute does not appear to be running")
Ian Wienand3a9df1d2015-07-01 06:18:47 +1000201 return
202
Kashyap Chamarthy88725452015-09-14 13:17:56 +0200203 _dump_cmd("kill -s USR2 `pgrep nova-compute`")
Eyale7361772016-04-05 16:18:56 +0300204 print("guru meditation report in nova-compute log")
Joe Gordon2ebe9932015-06-07 16:57:34 +0900205
206
Sean Dague97fcc7b2014-06-16 17:24:14 -0400207def main():
208 opts = get_options()
Sean Dagueac9313e2015-07-27 13:33:30 -0400209 fname = filename(opts.dir, opts.name)
Eyale7361772016-04-05 16:18:56 +0300210 print("World dumping... see %s for details" % fname)
Sean Dague97fcc7b2014-06-16 17:24:14 -0400211 sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
212 with open(fname, 'w') as f:
213 os.dup2(f.fileno(), sys.stdout.fileno())
214 disk_space()
215 process_list()
Sean Dague60a14052015-05-11 14:53:39 -0400216 network_dump()
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100217 ovs_dump()
Sean Dague168b7c22015-05-07 08:57:28 -0400218 iptables_dump()
Sean Dague2da606d2015-08-06 10:02:43 -0400219 ebtables_dump()
Sean Dague737e9422015-05-12 19:51:39 -0400220 compute_consoles()
Joe Gordon2ebe9932015-06-07 16:57:34 +0900221 guru_meditation_report()
Sean Dague97fcc7b2014-06-16 17:24:14 -0400222
223
224if __name__ == '__main__':
225 try:
226 sys.exit(main())
227 except KeyboardInterrupt:
228 sys.exit(1)