blob: e1ef544a555fc6bcd158fbe667c48120426d4d32 [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
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',
37)
38
39
Sean Dague97fcc7b2014-06-16 17:24:14 -040040def get_options():
41 parser = argparse.ArgumentParser(
42 description='Dump world state for debugging')
43 parser.add_argument('-d', '--dir',
44 default='.',
45 help='Output directory for worlddump')
Sean Dagueac9313e2015-07-27 13:33:30 -040046 parser.add_argument('-n', '--name',
47 default='',
48 help='Additional name to tag into file')
Sean Dague97fcc7b2014-06-16 17:24:14 -040049 return parser.parse_args()
50
51
Sean Dagueac9313e2015-07-27 13:33:30 -040052def filename(dirname, name=""):
Sean Dague97fcc7b2014-06-16 17:24:14 -040053 now = datetime.datetime.utcnow()
Sean Dagueac9313e2015-07-27 13:33:30 -040054 fmt = "worlddump-%Y-%m-%d-%H%M%S"
55 if name:
56 fmt += "-" + name
57 fmt += ".txt"
58 return os.path.join(dirname, now.strftime(fmt))
Sean Dague97fcc7b2014-06-16 17:24:14 -040059
60
61def warn(msg):
Eyale7361772016-04-05 16:18:56 +030062 print("WARN: %s" % msg)
Sean Dague97fcc7b2014-06-16 17:24:14 -040063
64
Sean Dague60a14052015-05-11 14:53:39 -040065def _dump_cmd(cmd):
Eyale7361772016-04-05 16:18:56 +030066 print(cmd)
67 print("-" * len(cmd))
68 print()
Ian Wienand99440f92015-07-01 06:14:01 +100069 try:
70 subprocess.check_call(cmd, shell=True)
Eyale7361772016-04-05 16:18:56 +030071 print()
Ihar Hrachyshka7976aac2016-03-03 15:30:49 +010072 except subprocess.CalledProcessError as e:
Eyale7361772016-04-05 16:18:56 +030073 print("*** Failed to run '%(cmd)s': %(err)s" % {'cmd': cmd, 'err': e})
Sean Dague60a14052015-05-11 14:53:39 -040074
75
Chris Dent57d79672016-02-23 15:38:43 +000076def _find_cmd(cmd):
77 if not spawn.find_executable(cmd):
Eyale7361772016-04-05 16:18:56 +030078 print("*** %s not found: skipping" % cmd)
Chris Dent57d79672016-02-23 15:38:43 +000079 return False
80 return True
81
82
Sean Dague60a14052015-05-11 14:53:39 -040083def _header(name):
Eyale7361772016-04-05 16:18:56 +030084 print()
85 print(name)
86 print("=" * len(name))
87 print()
Sean Dague60a14052015-05-11 14:53:39 -040088
89
fumihiko kakuma578459f2016-04-07 08:15:45 +090090def _bridge_list():
yan.haifeng6ba17f72016-04-29 15:59:56 +080091 process = subprocess.Popen(['sudo', 'ovs-vsctl', 'list-br'],
92 stdout=subprocess.PIPE)
fumihiko kakuma578459f2016-04-07 08:15:45 +090093 stdout, _ = process.communicate()
94 return stdout.split()
95
96
fumihiko kakuma60994012016-03-08 20:55:01 +090097# This method gets a max openflow version supported by openvswitch.
98# For example 'ovs-ofctl --version' displays the following:
99#
100# ovs-ofctl (Open vSwitch) 2.0.2
101# Compiled Dec 9 2015 14:08:08
102# OpenFlow versions 0x1:0x4
103#
fumihiko kakuma2bd25682016-04-05 10:33:50 +0900104# The above shows that openvswitch supports from OpenFlow10 to OpenFlow13.
fumihiko kakuma60994012016-03-08 20:55:01 +0900105# This method gets max version searching 'OpenFlow versions 0x1:0x'.
106# And return a version value converted to an integer type.
107def _get_ofp_version():
108 process = subprocess.Popen(['ovs-ofctl', '--version'], stdout=subprocess.PIPE)
109 stdout, _ = process.communicate()
110 find_str = 'OpenFlow versions 0x1:0x'
111 offset = stdout.find(find_str)
112 return int(stdout[offset + len(find_str):-1]) - 1
113
114
Sean Dague97fcc7b2014-06-16 17:24:14 -0400115def disk_space():
116 # the df output
Sean Dague60a14052015-05-11 14:53:39 -0400117 _header("File System Summary")
118
Sean Dague97fcc7b2014-06-16 17:24:14 -0400119 dfraw = os.popen("df -Ph").read()
120 df = [s.split() for s in dfraw.splitlines()]
121 for fs in df:
122 try:
123 if int(fs[4][:-1]) > 95:
124 warn("Device %s (%s) is %s full, might be an issue" % (
125 fs[0], fs[5], fs[4]))
126 except ValueError:
127 # if it doesn't look like an int, that's fine
128 pass
129
Eyale7361772016-04-05 16:18:56 +0300130 print(dfraw)
Sean Dague97fcc7b2014-06-16 17:24:14 -0400131
132
Sean Dague2da606d2015-08-06 10:02:43 -0400133def ebtables_dump():
Sean Dague5c5e0862015-11-09 14:08:15 -0500134 tables = ['filter', 'nat', 'broute']
Sean Dague2da606d2015-08-06 10:02:43 -0400135 _header("EB Tables Dump")
Chris Dent57d79672016-02-23 15:38:43 +0000136 if not _find_cmd('ebtables'):
137 return
Sean Dague5c5e0862015-11-09 14:08:15 -0500138 for table in tables:
139 _dump_cmd("sudo ebtables -t %s -L" % table)
Sean Dague2da606d2015-08-06 10:02:43 -0400140
141
Sean Dague168b7c22015-05-07 08:57:28 -0400142def iptables_dump():
143 tables = ['filter', 'nat', 'mangle']
Sean Dague60a14052015-05-11 14:53:39 -0400144 _header("IP Tables Dump")
145
Sean Dague168b7c22015-05-07 08:57:28 -0400146 for table in tables:
Sean Dague60a14052015-05-11 14:53:39 -0400147 _dump_cmd("sudo iptables --line-numbers -L -nv -t %s" % table)
148
149
Ihar Hrachyshka72c34ee2016-01-30 16:18:01 +0100150def _netns_list():
151 process = subprocess.Popen(['ip', 'netns'], stdout=subprocess.PIPE)
152 stdout, _ = process.communicate()
153 return stdout.split()
154
155
Sean Dague60a14052015-05-11 14:53:39 -0400156def network_dump():
157 _header("Network Dump")
158
159 _dump_cmd("brctl show")
160 _dump_cmd("arp -n")
Ihar Hrachyshka72c34ee2016-01-30 16:18:01 +0100161 ip_cmds = ["addr", "link", "route"]
162 for cmd in ip_cmds + ['netns']:
163 _dump_cmd("ip %s" % cmd)
164 for netns_ in _netns_list():
165 for cmd in ip_cmds:
166 args = {'netns': netns_, 'cmd': cmd}
167 _dump_cmd('sudo ip netns exec %(netns)s ip %(cmd)s' % args)
Sean Dague168b7c22015-05-07 08:57:28 -0400168
169
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100170def ovs_dump():
171 _header("Open vSwitch Dump")
172
Chris Dent57d79672016-02-23 15:38:43 +0000173 # NOTE(cdent): If we're not using neutron + ovs these commands
174 # will not be present so
175 if not _find_cmd('ovs-vsctl'):
176 return
177
fumihiko kakuma578459f2016-04-07 08:15:45 +0900178 bridges = _bridge_list()
fumihiko kakuma60994012016-03-08 20:55:01 +0900179 ofctl_cmds = ('show', 'dump-ports-desc', 'dump-ports', 'dump-flows')
180 ofp_max = _get_ofp_version()
181 vers = 'OpenFlow10'
fumihiko kakuma578459f2016-04-07 08:15:45 +0900182 for i in range(1, ofp_max + 1):
fumihiko kakuma60994012016-03-08 20:55:01 +0900183 vers += ',OpenFlow1' + str(i)
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100184 _dump_cmd("sudo ovs-vsctl show")
fumihiko kakuma60994012016-03-08 20:55:01 +0900185 for ofctl_cmd in ofctl_cmds:
186 for bridge in bridges:
187 args = {'vers': vers, 'cmd': ofctl_cmd, 'bridge': bridge}
188 _dump_cmd("sudo ovs-ofctl --protocols=%(vers)s %(cmd)s %(bridge)s" % args)
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100189
190
Sean Dague97fcc7b2014-06-16 17:24:14 -0400191def process_list():
Sean Dague60a14052015-05-11 14:53:39 -0400192 _header("Process Listing")
193 _dump_cmd("ps axo "
194 "user,ppid,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,args")
Sean Dague97fcc7b2014-06-16 17:24:14 -0400195
196
Sean Dague737e9422015-05-12 19:51:39 -0400197def compute_consoles():
198 _header("Compute consoles")
199 for root, dirnames, filenames in os.walk('/opt/stack'):
200 for filename in fnmatch.filter(filenames, 'console.log'):
201 fullpath = os.path.join(root, filename)
202 _dump_cmd("sudo cat %s" % fullpath)
203
204
Ihar Hrachyshkaef219bf2016-02-11 13:54:48 +0100205def guru_meditation_reports():
206 for service in GMR_PROCESSES:
207 _header("%s Guru Meditation Report" % service)
Ian Wienand3a9df1d2015-07-01 06:18:47 +1000208
Ihar Hrachyshkaef219bf2016-02-11 13:54:48 +0100209 try:
210 subprocess.check_call(['pgrep', '-f', service])
211 except subprocess.CalledProcessError:
212 print("Skipping as %s does not appear to be running" % service)
213 continue
Ian Wienand3a9df1d2015-07-01 06:18:47 +1000214
Ihar Hrachyshkaef219bf2016-02-11 13:54:48 +0100215 _dump_cmd("killall -e -USR2 %s" % service)
216 print("guru meditation report in %s log" % service)
Joe Gordon2ebe9932015-06-07 16:57:34 +0900217
218
Sean Dague97fcc7b2014-06-16 17:24:14 -0400219def main():
220 opts = get_options()
Sean Dagueac9313e2015-07-27 13:33:30 -0400221 fname = filename(opts.dir, opts.name)
Eyale7361772016-04-05 16:18:56 +0300222 print("World dumping... see %s for details" % fname)
Sean Dague97fcc7b2014-06-16 17:24:14 -0400223 sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
224 with open(fname, 'w') as f:
225 os.dup2(f.fileno(), sys.stdout.fileno())
226 disk_space()
227 process_list()
Sean Dague60a14052015-05-11 14:53:39 -0400228 network_dump()
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100229 ovs_dump()
Sean Dague168b7c22015-05-07 08:57:28 -0400230 iptables_dump()
Sean Dague2da606d2015-08-06 10:02:43 -0400231 ebtables_dump()
Sean Dague737e9422015-05-12 19:51:39 -0400232 compute_consoles()
Ihar Hrachyshkaef219bf2016-02-11 13:54:48 +0100233 guru_meditation_reports()
Sean Dague97fcc7b2014-06-16 17:24:14 -0400234
235
236if __name__ == '__main__':
237 try:
238 sys.exit(main())
239 except KeyboardInterrupt:
240 sys.exit(1)