blob: edbfa268dbfacf33c5cd8455b600f59dfb102cff [file] [log] [blame]
Federico Ressi21a10d32020-01-31 07:43:30 +01001#!/usr/bin/env python3
Sean Dague97fcc7b2014-06-16 17:24:14 -04002#
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
Sean Dague737e9422015-05-12 19:51:39 -040022import fnmatch
Federico Ressi21a10d32020-01-31 07:43:30 +010023import io
Sean Dague97fcc7b2014-06-16 17:24:14 -040024import os
Jens Harbottce396d32019-09-05 08:51:33 +000025import shutil
Ian Wienand99440f92015-07-01 06:14:01 +100026import subprocess
Sean Dague97fcc7b2014-06-16 17:24:14 -040027import sys
28
29
Ihar Hrachyshkaef219bf2016-02-11 13:54:48 +010030GMR_PROCESSES = (
31 'nova-compute',
32 'neutron-dhcp-agent',
33 'neutron-l3-agent',
34 'neutron-linuxbridge-agent',
35 'neutron-metadata-agent',
36 'neutron-openvswitch-agent',
Eric Harneyd8682db2016-10-14 14:36:29 -040037 'cinder-volume',
Ihar Hrachyshkaef219bf2016-02-11 13:54:48 +010038)
39
40
Sean Dague97fcc7b2014-06-16 17:24:14 -040041def get_options():
42 parser = argparse.ArgumentParser(
43 description='Dump world state for debugging')
44 parser.add_argument('-d', '--dir',
45 default='.',
46 help='Output directory for worlddump')
Sean Dagueac9313e2015-07-27 13:33:30 -040047 parser.add_argument('-n', '--name',
48 default='',
49 help='Additional name to tag into file')
Sean Dague97fcc7b2014-06-16 17:24:14 -040050 return parser.parse_args()
51
52
Sean Dagueac9313e2015-07-27 13:33:30 -040053def filename(dirname, name=""):
Brian Haley9be4cee2024-04-23 15:37:37 -040054 now = datetime.datetime.now(datetime.timezone.utc)
Sean Dagueac9313e2015-07-27 13:33:30 -040055 fmt = "worlddump-%Y-%m-%d-%H%M%S"
56 if name:
57 fmt += "-" + name
58 fmt += ".txt"
59 return os.path.join(dirname, now.strftime(fmt))
Sean Dague97fcc7b2014-06-16 17:24:14 -040060
61
62def warn(msg):
Eyale7361772016-04-05 16:18:56 +030063 print("WARN: %s" % msg)
Sean Dague97fcc7b2014-06-16 17:24:14 -040064
65
Sean Dague60a14052015-05-11 14:53:39 -040066def _dump_cmd(cmd):
Eyale7361772016-04-05 16:18:56 +030067 print(cmd)
68 print("-" * len(cmd))
69 print()
Ian Wienand99440f92015-07-01 06:14:01 +100070 try:
71 subprocess.check_call(cmd, shell=True)
Eyale7361772016-04-05 16:18:56 +030072 print()
Ihar Hrachyshka7976aac2016-03-03 15:30:49 +010073 except subprocess.CalledProcessError as e:
Eyale7361772016-04-05 16:18:56 +030074 print("*** Failed to run '%(cmd)s': %(err)s" % {'cmd': cmd, 'err': e})
Sean Dague60a14052015-05-11 14:53:39 -040075
76
Chris Dent57d79672016-02-23 15:38:43 +000077def _find_cmd(cmd):
Martin Kopeca37b6ab2023-05-26 13:46:42 +020078 if not shutil.which(cmd):
Eyale7361772016-04-05 16:18:56 +030079 print("*** %s not found: skipping" % cmd)
Chris Dent57d79672016-02-23 15:38:43 +000080 return False
81 return True
82
83
Sean Dague60a14052015-05-11 14:53:39 -040084def _header(name):
Eyale7361772016-04-05 16:18:56 +030085 print()
86 print(name)
87 print("=" * len(name))
88 print()
Sean Dague60a14052015-05-11 14:53:39 -040089
90
fumihiko kakuma578459f2016-04-07 08:15:45 +090091def _bridge_list():
yan.haifeng6ba17f72016-04-29 15:59:56 +080092 process = subprocess.Popen(['sudo', 'ovs-vsctl', 'list-br'],
93 stdout=subprocess.PIPE)
fumihiko kakuma578459f2016-04-07 08:15:45 +090094 stdout, _ = process.communicate()
95 return stdout.split()
96
97
fumihiko kakuma60994012016-03-08 20:55:01 +090098# This method gets a max openflow version supported by openvswitch.
99# For example 'ovs-ofctl --version' displays the following:
100#
101# ovs-ofctl (Open vSwitch) 2.0.2
102# Compiled Dec 9 2015 14:08:08
103# OpenFlow versions 0x1:0x4
104#
fumihiko kakuma2bd25682016-04-05 10:33:50 +0900105# The above shows that openvswitch supports from OpenFlow10 to OpenFlow13.
fumihiko kakuma60994012016-03-08 20:55:01 +0900106# This method gets max version searching 'OpenFlow versions 0x1:0x'.
107# And return a version value converted to an integer type.
108def _get_ofp_version():
Federico Ressi21a10d32020-01-31 07:43:30 +0100109 process = subprocess.Popen(['ovs-ofctl', '--version'],
110 stdout=subprocess.PIPE)
fumihiko kakuma60994012016-03-08 20:55:01 +0900111 stdout, _ = process.communicate()
Federico Ressi21a10d32020-01-31 07:43:30 +0100112 find_str = b'OpenFlow versions 0x1:0x'
fumihiko kakuma60994012016-03-08 20:55:01 +0900113 offset = stdout.find(find_str)
114 return int(stdout[offset + len(find_str):-1]) - 1
115
116
Sean Dague97fcc7b2014-06-16 17:24:14 -0400117def disk_space():
118 # the df output
Sean Dague60a14052015-05-11 14:53:39 -0400119 _header("File System Summary")
120
Sean Dague97fcc7b2014-06-16 17:24:14 -0400121 dfraw = os.popen("df -Ph").read()
122 df = [s.split() for s in dfraw.splitlines()]
123 for fs in df:
124 try:
125 if int(fs[4][:-1]) > 95:
126 warn("Device %s (%s) is %s full, might be an issue" % (
127 fs[0], fs[5], fs[4]))
128 except ValueError:
129 # if it doesn't look like an int, that's fine
130 pass
131
Eyale7361772016-04-05 16:18:56 +0300132 print(dfraw)
Sean Dague97fcc7b2014-06-16 17:24:14 -0400133
134
Sean Dague2da606d2015-08-06 10:02:43 -0400135def ebtables_dump():
Jens Harbott5a684eb2021-06-09 09:37:34 +0200136 tables = ['filter', 'nat']
Sean Dague2da606d2015-08-06 10:02:43 -0400137 _header("EB Tables Dump")
Chris Dent57d79672016-02-23 15:38:43 +0000138 if not _find_cmd('ebtables'):
139 return
Sean Dague5c5e0862015-11-09 14:08:15 -0500140 for table in tables:
141 _dump_cmd("sudo ebtables -t %s -L" % table)
Sean Dague2da606d2015-08-06 10:02:43 -0400142
143
Sean Dague168b7c22015-05-07 08:57:28 -0400144def iptables_dump():
145 tables = ['filter', 'nat', 'mangle']
Sean Dague60a14052015-05-11 14:53:39 -0400146 _header("IP Tables Dump")
147
Sean Dague168b7c22015-05-07 08:57:28 -0400148 for table in tables:
Sean Dague60a14052015-05-11 14:53:39 -0400149 _dump_cmd("sudo iptables --line-numbers -L -nv -t %s" % table)
150
151
Ihar Hrachyshka72c34ee2016-01-30 16:18:01 +0100152def _netns_list():
153 process = subprocess.Popen(['ip', 'netns'], stdout=subprocess.PIPE)
154 stdout, _ = process.communicate()
John L. Villalovosc6e69392017-02-06 14:24:42 -0800155 # NOTE(jlvillal): Sometimes 'ip netns list' can return output like:
156 # qrouter-0805fd7d-c493-4fa6-82ca-1c6c9b23cd9e (id: 1)
157 # qdhcp-bb2cc6ae-2ae8-474f-adda-a94059b872b5 (id: 0)
158 output = [x.split()[0] for x in stdout.splitlines()]
159 return output
Ihar Hrachyshka72c34ee2016-01-30 16:18:01 +0100160
161
Sean Dague60a14052015-05-11 14:53:39 -0400162def network_dump():
163 _header("Network Dump")
164
Nate Johnston56946cf2018-11-12 11:17:07 -0500165 _dump_cmd("bridge link")
Nate Johnston56946cf2018-11-12 11:17:07 -0500166 _dump_cmd("ip link show type bridge")
Sean Mooney7de6e0b2020-10-21 13:59:50 +0100167 ip_cmds = ["neigh", "addr", "route", "-6 route"]
Ihar Hrachyshka72c34ee2016-01-30 16:18:01 +0100168 for cmd in ip_cmds + ['netns']:
169 _dump_cmd("ip %s" % cmd)
170 for netns_ in _netns_list():
171 for cmd in ip_cmds:
LuyaoZhong8d4ae4f2020-02-19 08:16:03 +0000172 args = {'netns': bytes.decode(netns_), 'cmd': cmd}
Ihar Hrachyshka72c34ee2016-01-30 16:18:01 +0100173 _dump_cmd('sudo ip netns exec %(netns)s ip %(cmd)s' % args)
Sean Dague168b7c22015-05-07 08:57:28 -0400174
175
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100176def ovs_dump():
177 _header("Open vSwitch Dump")
178
Chris Dent57d79672016-02-23 15:38:43 +0000179 # NOTE(cdent): If we're not using neutron + ovs these commands
180 # will not be present so
181 if not _find_cmd('ovs-vsctl'):
182 return
183
fumihiko kakuma578459f2016-04-07 08:15:45 +0900184 bridges = _bridge_list()
fumihiko kakuma60994012016-03-08 20:55:01 +0900185 ofctl_cmds = ('show', 'dump-ports-desc', 'dump-ports', 'dump-flows')
186 ofp_max = _get_ofp_version()
187 vers = 'OpenFlow10'
fumihiko kakuma578459f2016-04-07 08:15:45 +0900188 for i in range(1, ofp_max + 1):
fumihiko kakuma60994012016-03-08 20:55:01 +0900189 vers += ',OpenFlow1' + str(i)
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100190 _dump_cmd("sudo ovs-vsctl show")
fumihiko kakuma60994012016-03-08 20:55:01 +0900191 for ofctl_cmd in ofctl_cmds:
192 for bridge in bridges:
LuyaoZhong8d4ae4f2020-02-19 08:16:03 +0000193 args = {'vers': vers, 'cmd': ofctl_cmd, 'bridge': bytes.decode(bridge)}
fumihiko kakuma60994012016-03-08 20:55:01 +0900194 _dump_cmd("sudo ovs-ofctl --protocols=%(vers)s %(cmd)s %(bridge)s" % args)
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100195
196
Sean Dague97fcc7b2014-06-16 17:24:14 -0400197def process_list():
Sean Dague60a14052015-05-11 14:53:39 -0400198 _header("Process Listing")
199 _dump_cmd("ps axo "
200 "user,ppid,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,args")
Sean Dague97fcc7b2014-06-16 17:24:14 -0400201
202
Sean Dague737e9422015-05-12 19:51:39 -0400203def compute_consoles():
204 _header("Compute consoles")
Federico Ressi21a10d32020-01-31 07:43:30 +0100205 for root, _, filenames in os.walk('/opt/stack'):
Sean Dague737e9422015-05-12 19:51:39 -0400206 for filename in fnmatch.filter(filenames, 'console.log'):
207 fullpath = os.path.join(root, filename)
208 _dump_cmd("sudo cat %s" % fullpath)
209
210
Ihar Hrachyshkaef219bf2016-02-11 13:54:48 +0100211def guru_meditation_reports():
212 for service in GMR_PROCESSES:
213 _header("%s Guru Meditation Report" % service)
Ian Wienand3a9df1d2015-07-01 06:18:47 +1000214
Ihar Hrachyshkaef219bf2016-02-11 13:54:48 +0100215 try:
216 subprocess.check_call(['pgrep', '-f', service])
217 except subprocess.CalledProcessError:
218 print("Skipping as %s does not appear to be running" % service)
219 continue
Ian Wienand3a9df1d2015-07-01 06:18:47 +1000220
Ihar Hrachyshkaef219bf2016-02-11 13:54:48 +0100221 _dump_cmd("killall -e -USR2 %s" % service)
222 print("guru meditation report in %s log" % service)
Joe Gordon2ebe9932015-06-07 16:57:34 +0900223
224
Ian Wienandbfcc7602017-03-29 11:52:06 +1100225def var_core():
226 if os.path.exists('/var/core'):
227 _header("/var/core dumps")
228 # NOTE(ianw) : see DEBUG_LIBVIRT_COREDUMPS. We could think
229 # about getting backtraces out of these. There are other
230 # tools out there that can do that sort of thing though.
231 _dump_cmd("ls -ltrah /var/core")
232
Federico Ressi21a10d32020-01-31 07:43:30 +0100233
234def disable_stdio_buffering():
235 # re-open STDOUT as binary, then wrap it in a
236 # TextIOWrapper, and write through everything.
237 binary_stdout = io.open(sys.stdout.fileno(), 'wb', 0)
238 sys.stdout = io.TextIOWrapper(binary_stdout, write_through=True)
239
240
Sean Dague97fcc7b2014-06-16 17:24:14 -0400241def main():
242 opts = get_options()
Sean Dagueac9313e2015-07-27 13:33:30 -0400243 fname = filename(opts.dir, opts.name)
Eyale7361772016-04-05 16:18:56 +0300244 print("World dumping... see %s for details" % fname)
Federico Ressi21a10d32020-01-31 07:43:30 +0100245
246 disable_stdio_buffering()
247
248 with io.open(fname, 'w') as f:
Sean Dague97fcc7b2014-06-16 17:24:14 -0400249 os.dup2(f.fileno(), sys.stdout.fileno())
250 disk_space()
251 process_list()
Sean Dague60a14052015-05-11 14:53:39 -0400252 network_dump()
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100253 ovs_dump()
Sean Dague168b7c22015-05-07 08:57:28 -0400254 iptables_dump()
Sean Dague2da606d2015-08-06 10:02:43 -0400255 ebtables_dump()
Sean Dague737e9422015-05-12 19:51:39 -0400256 compute_consoles()
Ihar Hrachyshkaef219bf2016-02-11 13:54:48 +0100257 guru_meditation_reports()
Ian Wienandbfcc7602017-03-29 11:52:06 +1100258 var_core()
Jens Harbottce396d32019-09-05 08:51:33 +0000259 # Singular name for ease of log retrieval
260 copyname = os.path.join(opts.dir, 'worlddump')
261 if opts.name:
262 copyname += '-' + opts.name
263 copyname += '-latest.txt'
264 # We make a full copy to deal with jobs that may or may not
265 # gzip logs breaking symlinks.
266 shutil.copyfile(fname, copyname)
Sean Dague97fcc7b2014-06-16 17:24:14 -0400267
268
269if __name__ == '__main__':
270 try:
271 sys.exit(main())
272 except KeyboardInterrupt:
273 sys.exit(1)