blob: 238a23dab82e443d233c40b020e7f917032838e3 [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():
91 process = subprocess.Popen(['ovs-vsctl', 'list-br'], stdout=subprocess.PIPE)
92 stdout, _ = process.communicate()
93 return stdout.split()
94
95
fumihiko kakuma60994012016-03-08 20:55:01 +090096# This method gets a max openflow version supported by openvswitch.
97# For example 'ovs-ofctl --version' displays the following:
98#
99# ovs-ofctl (Open vSwitch) 2.0.2
100# Compiled Dec 9 2015 14:08:08
101# OpenFlow versions 0x1:0x4
102#
fumihiko kakuma2bd25682016-04-05 10:33:50 +0900103# The above shows that openvswitch supports from OpenFlow10 to OpenFlow13.
fumihiko kakuma60994012016-03-08 20:55:01 +0900104# This method gets max version searching 'OpenFlow versions 0x1:0x'.
105# And return a version value converted to an integer type.
106def _get_ofp_version():
107 process = subprocess.Popen(['ovs-ofctl', '--version'], stdout=subprocess.PIPE)
108 stdout, _ = process.communicate()
109 find_str = 'OpenFlow versions 0x1:0x'
110 offset = stdout.find(find_str)
111 return int(stdout[offset + len(find_str):-1]) - 1
112
113
Sean Dague97fcc7b2014-06-16 17:24:14 -0400114def disk_space():
115 # the df output
Sean Dague60a14052015-05-11 14:53:39 -0400116 _header("File System Summary")
117
Sean Dague97fcc7b2014-06-16 17:24:14 -0400118 dfraw = os.popen("df -Ph").read()
119 df = [s.split() for s in dfraw.splitlines()]
120 for fs in df:
121 try:
122 if int(fs[4][:-1]) > 95:
123 warn("Device %s (%s) is %s full, might be an issue" % (
124 fs[0], fs[5], fs[4]))
125 except ValueError:
126 # if it doesn't look like an int, that's fine
127 pass
128
Eyale7361772016-04-05 16:18:56 +0300129 print(dfraw)
Sean Dague97fcc7b2014-06-16 17:24:14 -0400130
131
Sean Dague2da606d2015-08-06 10:02:43 -0400132def ebtables_dump():
Sean Dague5c5e0862015-11-09 14:08:15 -0500133 tables = ['filter', 'nat', 'broute']
Sean Dague2da606d2015-08-06 10:02:43 -0400134 _header("EB Tables Dump")
Chris Dent57d79672016-02-23 15:38:43 +0000135 if not _find_cmd('ebtables'):
136 return
Sean Dague5c5e0862015-11-09 14:08:15 -0500137 for table in tables:
138 _dump_cmd("sudo ebtables -t %s -L" % table)
Sean Dague2da606d2015-08-06 10:02:43 -0400139
140
Sean Dague168b7c22015-05-07 08:57:28 -0400141def iptables_dump():
142 tables = ['filter', 'nat', 'mangle']
Sean Dague60a14052015-05-11 14:53:39 -0400143 _header("IP Tables Dump")
144
Sean Dague168b7c22015-05-07 08:57:28 -0400145 for table in tables:
Sean Dague60a14052015-05-11 14:53:39 -0400146 _dump_cmd("sudo iptables --line-numbers -L -nv -t %s" % table)
147
148
Ihar Hrachyshka72c34ee2016-01-30 16:18:01 +0100149def _netns_list():
150 process = subprocess.Popen(['ip', 'netns'], stdout=subprocess.PIPE)
151 stdout, _ = process.communicate()
152 return stdout.split()
153
154
Sean Dague60a14052015-05-11 14:53:39 -0400155def network_dump():
156 _header("Network Dump")
157
158 _dump_cmd("brctl show")
159 _dump_cmd("arp -n")
Ihar Hrachyshka72c34ee2016-01-30 16:18:01 +0100160 ip_cmds = ["addr", "link", "route"]
161 for cmd in ip_cmds + ['netns']:
162 _dump_cmd("ip %s" % cmd)
163 for netns_ in _netns_list():
164 for cmd in ip_cmds:
165 args = {'netns': netns_, 'cmd': cmd}
166 _dump_cmd('sudo ip netns exec %(netns)s ip %(cmd)s' % args)
Sean Dague168b7c22015-05-07 08:57:28 -0400167
168
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100169def ovs_dump():
170 _header("Open vSwitch Dump")
171
Chris Dent57d79672016-02-23 15:38:43 +0000172 # NOTE(cdent): If we're not using neutron + ovs these commands
173 # will not be present so
174 if not _find_cmd('ovs-vsctl'):
175 return
176
fumihiko kakuma578459f2016-04-07 08:15:45 +0900177 bridges = _bridge_list()
fumihiko kakuma60994012016-03-08 20:55:01 +0900178 ofctl_cmds = ('show', 'dump-ports-desc', 'dump-ports', 'dump-flows')
179 ofp_max = _get_ofp_version()
180 vers = 'OpenFlow10'
fumihiko kakuma578459f2016-04-07 08:15:45 +0900181 for i in range(1, ofp_max + 1):
fumihiko kakuma60994012016-03-08 20:55:01 +0900182 vers += ',OpenFlow1' + str(i)
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100183 _dump_cmd("sudo ovs-vsctl show")
fumihiko kakuma60994012016-03-08 20:55:01 +0900184 for ofctl_cmd in ofctl_cmds:
185 for bridge in bridges:
186 args = {'vers': vers, 'cmd': ofctl_cmd, 'bridge': bridge}
187 _dump_cmd("sudo ovs-ofctl --protocols=%(vers)s %(cmd)s %(bridge)s" % args)
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100188
189
Sean Dague97fcc7b2014-06-16 17:24:14 -0400190def process_list():
Sean Dague60a14052015-05-11 14:53:39 -0400191 _header("Process Listing")
192 _dump_cmd("ps axo "
193 "user,ppid,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,args")
Sean Dague97fcc7b2014-06-16 17:24:14 -0400194
195
Sean Dague737e9422015-05-12 19:51:39 -0400196def compute_consoles():
197 _header("Compute consoles")
198 for root, dirnames, filenames in os.walk('/opt/stack'):
199 for filename in fnmatch.filter(filenames, 'console.log'):
200 fullpath = os.path.join(root, filename)
201 _dump_cmd("sudo cat %s" % fullpath)
202
203
Ihar Hrachyshkaef219bf2016-02-11 13:54:48 +0100204def guru_meditation_reports():
205 for service in GMR_PROCESSES:
206 _header("%s Guru Meditation Report" % service)
Ian Wienand3a9df1d2015-07-01 06:18:47 +1000207
Ihar Hrachyshkaef219bf2016-02-11 13:54:48 +0100208 try:
209 subprocess.check_call(['pgrep', '-f', service])
210 except subprocess.CalledProcessError:
211 print("Skipping as %s does not appear to be running" % service)
212 continue
Ian Wienand3a9df1d2015-07-01 06:18:47 +1000213
Ihar Hrachyshkaef219bf2016-02-11 13:54:48 +0100214 _dump_cmd("killall -e -USR2 %s" % service)
215 print("guru meditation report in %s log" % service)
Joe Gordon2ebe9932015-06-07 16:57:34 +0900216
217
Sean Dague97fcc7b2014-06-16 17:24:14 -0400218def main():
219 opts = get_options()
Sean Dagueac9313e2015-07-27 13:33:30 -0400220 fname = filename(opts.dir, opts.name)
Eyale7361772016-04-05 16:18:56 +0300221 print("World dumping... see %s for details" % fname)
Sean Dague97fcc7b2014-06-16 17:24:14 -0400222 sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
223 with open(fname, 'w') as f:
224 os.dup2(f.fileno(), sys.stdout.fileno())
225 disk_space()
226 process_list()
Sean Dague60a14052015-05-11 14:53:39 -0400227 network_dump()
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100228 ovs_dump()
Sean Dague168b7c22015-05-07 08:57:28 -0400229 iptables_dump()
Sean Dague2da606d2015-08-06 10:02:43 -0400230 ebtables_dump()
Sean Dague737e9422015-05-12 19:51:39 -0400231 compute_consoles()
Ihar Hrachyshkaef219bf2016-02-11 13:54:48 +0100232 guru_meditation_reports()
Sean Dague97fcc7b2014-06-16 17:24:14 -0400233
234
235if __name__ == '__main__':
236 try:
237 sys.exit(main())
238 except KeyboardInterrupt:
239 sys.exit(1)