blob: 1ce931efd5681a1bd6f9ac755e2a8ce3aff6592e [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',
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=""):
Sean Dague97fcc7b2014-06-16 17:24:14 -040054 now = datetime.datetime.utcnow()
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):
78 if not spawn.find_executable(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():
109 process = subprocess.Popen(['ovs-ofctl', '--version'], stdout=subprocess.PIPE)
110 stdout, _ = process.communicate()
111 find_str = 'OpenFlow versions 0x1:0x'
112 offset = stdout.find(find_str)
113 return int(stdout[offset + len(find_str):-1]) - 1
114
115
Sean Dague97fcc7b2014-06-16 17:24:14 -0400116def disk_space():
117 # the df output
Sean Dague60a14052015-05-11 14:53:39 -0400118 _header("File System Summary")
119
Sean Dague97fcc7b2014-06-16 17:24:14 -0400120 dfraw = os.popen("df -Ph").read()
121 df = [s.split() for s in dfraw.splitlines()]
122 for fs in df:
123 try:
124 if int(fs[4][:-1]) > 95:
125 warn("Device %s (%s) is %s full, might be an issue" % (
126 fs[0], fs[5], fs[4]))
127 except ValueError:
128 # if it doesn't look like an int, that's fine
129 pass
130
Eyale7361772016-04-05 16:18:56 +0300131 print(dfraw)
Sean Dague97fcc7b2014-06-16 17:24:14 -0400132
133
Sean Dague2da606d2015-08-06 10:02:43 -0400134def ebtables_dump():
Sean Dague5c5e0862015-11-09 14:08:15 -0500135 tables = ['filter', 'nat', 'broute']
Sean Dague2da606d2015-08-06 10:02:43 -0400136 _header("EB Tables Dump")
Chris Dent57d79672016-02-23 15:38:43 +0000137 if not _find_cmd('ebtables'):
138 return
Sean Dague5c5e0862015-11-09 14:08:15 -0500139 for table in tables:
140 _dump_cmd("sudo ebtables -t %s -L" % table)
Sean Dague2da606d2015-08-06 10:02:43 -0400141
142
Sean Dague168b7c22015-05-07 08:57:28 -0400143def iptables_dump():
144 tables = ['filter', 'nat', 'mangle']
Sean Dague60a14052015-05-11 14:53:39 -0400145 _header("IP Tables Dump")
146
Sean Dague168b7c22015-05-07 08:57:28 -0400147 for table in tables:
Sean Dague60a14052015-05-11 14:53:39 -0400148 _dump_cmd("sudo iptables --line-numbers -L -nv -t %s" % table)
149
150
Ihar Hrachyshka72c34ee2016-01-30 16:18:01 +0100151def _netns_list():
152 process = subprocess.Popen(['ip', 'netns'], stdout=subprocess.PIPE)
153 stdout, _ = process.communicate()
154 return stdout.split()
155
156
Sean Dague60a14052015-05-11 14:53:39 -0400157def network_dump():
158 _header("Network Dump")
159
160 _dump_cmd("brctl show")
161 _dump_cmd("arp -n")
Ihar Hrachyshka72c34ee2016-01-30 16:18:01 +0100162 ip_cmds = ["addr", "link", "route"]
163 for cmd in ip_cmds + ['netns']:
164 _dump_cmd("ip %s" % cmd)
165 for netns_ in _netns_list():
166 for cmd in ip_cmds:
167 args = {'netns': netns_, 'cmd': cmd}
168 _dump_cmd('sudo ip netns exec %(netns)s ip %(cmd)s' % args)
Sean Dague168b7c22015-05-07 08:57:28 -0400169
170
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100171def ovs_dump():
172 _header("Open vSwitch Dump")
173
Chris Dent57d79672016-02-23 15:38:43 +0000174 # NOTE(cdent): If we're not using neutron + ovs these commands
175 # will not be present so
176 if not _find_cmd('ovs-vsctl'):
177 return
178
fumihiko kakuma578459f2016-04-07 08:15:45 +0900179 bridges = _bridge_list()
fumihiko kakuma60994012016-03-08 20:55:01 +0900180 ofctl_cmds = ('show', 'dump-ports-desc', 'dump-ports', 'dump-flows')
181 ofp_max = _get_ofp_version()
182 vers = 'OpenFlow10'
fumihiko kakuma578459f2016-04-07 08:15:45 +0900183 for i in range(1, ofp_max + 1):
fumihiko kakuma60994012016-03-08 20:55:01 +0900184 vers += ',OpenFlow1' + str(i)
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100185 _dump_cmd("sudo ovs-vsctl show")
fumihiko kakuma60994012016-03-08 20:55:01 +0900186 for ofctl_cmd in ofctl_cmds:
187 for bridge in bridges:
188 args = {'vers': vers, 'cmd': ofctl_cmd, 'bridge': bridge}
189 _dump_cmd("sudo ovs-ofctl --protocols=%(vers)s %(cmd)s %(bridge)s" % args)
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100190
191
Sean Dague97fcc7b2014-06-16 17:24:14 -0400192def process_list():
Sean Dague60a14052015-05-11 14:53:39 -0400193 _header("Process Listing")
194 _dump_cmd("ps axo "
195 "user,ppid,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,args")
Sean Dague97fcc7b2014-06-16 17:24:14 -0400196
197
Sean Dague737e9422015-05-12 19:51:39 -0400198def compute_consoles():
199 _header("Compute consoles")
200 for root, dirnames, filenames in os.walk('/opt/stack'):
201 for filename in fnmatch.filter(filenames, 'console.log'):
202 fullpath = os.path.join(root, filename)
203 _dump_cmd("sudo cat %s" % fullpath)
204
205
Ihar Hrachyshkaef219bf2016-02-11 13:54:48 +0100206def guru_meditation_reports():
207 for service in GMR_PROCESSES:
208 _header("%s Guru Meditation Report" % service)
Ian Wienand3a9df1d2015-07-01 06:18:47 +1000209
Ihar Hrachyshkaef219bf2016-02-11 13:54:48 +0100210 try:
211 subprocess.check_call(['pgrep', '-f', service])
212 except subprocess.CalledProcessError:
213 print("Skipping as %s does not appear to be running" % service)
214 continue
Ian Wienand3a9df1d2015-07-01 06:18:47 +1000215
Ihar Hrachyshkaef219bf2016-02-11 13:54:48 +0100216 _dump_cmd("killall -e -USR2 %s" % service)
217 print("guru meditation report in %s log" % service)
Joe Gordon2ebe9932015-06-07 16:57:34 +0900218
219
Sean Dague97fcc7b2014-06-16 17:24:14 -0400220def main():
221 opts = get_options()
Sean Dagueac9313e2015-07-27 13:33:30 -0400222 fname = filename(opts.dir, opts.name)
Eyale7361772016-04-05 16:18:56 +0300223 print("World dumping... see %s for details" % fname)
Sean Dague97fcc7b2014-06-16 17:24:14 -0400224 sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
225 with open(fname, 'w') as f:
226 os.dup2(f.fileno(), sys.stdout.fileno())
227 disk_space()
228 process_list()
Sean Dague60a14052015-05-11 14:53:39 -0400229 network_dump()
Ihar Hrachyshkac1b7cb12016-02-11 13:50:46 +0100230 ovs_dump()
Sean Dague168b7c22015-05-07 08:57:28 -0400231 iptables_dump()
Sean Dague2da606d2015-08-06 10:02:43 -0400232 ebtables_dump()
Sean Dague737e9422015-05-12 19:51:39 -0400233 compute_consoles()
Ihar Hrachyshkaef219bf2016-02-11 13:54:48 +0100234 guru_meditation_reports()
Sean Dague97fcc7b2014-06-16 17:24:14 -0400235
236
237if __name__ == '__main__':
238 try:
239 sys.exit(main())
240 except KeyboardInterrupt:
241 sys.exit(1)