Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 1 | #!/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 | |
Eyal | e736177 | 2016-04-05 16:18:56 +0300 | [diff] [blame] | 17 | |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 18 | """Dump the state of the world for post mortem.""" |
| 19 | |
John L. Villalovos | 09949e0 | 2017-02-06 13:46:32 -0800 | [diff] [blame] | 20 | from __future__ import print_function |
| 21 | |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 22 | import argparse |
| 23 | import datetime |
Chris Dent | 57d7967 | 2016-02-23 15:38:43 +0000 | [diff] [blame] | 24 | from distutils import spawn |
Sean Dague | 737e942 | 2015-05-12 19:51:39 -0400 | [diff] [blame] | 25 | import fnmatch |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 26 | import os |
| 27 | import os.path |
Jens Harbott | ce396d3 | 2019-09-05 08:51:33 +0000 | [diff] [blame] | 28 | import shutil |
Ian Wienand | 99440f9 | 2015-07-01 06:14:01 +1000 | [diff] [blame] | 29 | import subprocess |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 30 | import sys |
| 31 | |
| 32 | |
Ihar Hrachyshka | ef219bf | 2016-02-11 13:54:48 +0100 | [diff] [blame] | 33 | GMR_PROCESSES = ( |
| 34 | 'nova-compute', |
| 35 | 'neutron-dhcp-agent', |
| 36 | 'neutron-l3-agent', |
| 37 | 'neutron-linuxbridge-agent', |
| 38 | 'neutron-metadata-agent', |
| 39 | 'neutron-openvswitch-agent', |
Eric Harney | d8682db | 2016-10-14 14:36:29 -0400 | [diff] [blame] | 40 | 'cinder-volume', |
Ihar Hrachyshka | ef219bf | 2016-02-11 13:54:48 +0100 | [diff] [blame] | 41 | ) |
| 42 | |
| 43 | |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 44 | def get_options(): |
| 45 | parser = argparse.ArgumentParser( |
| 46 | description='Dump world state for debugging') |
| 47 | parser.add_argument('-d', '--dir', |
| 48 | default='.', |
| 49 | help='Output directory for worlddump') |
Sean Dague | ac9313e | 2015-07-27 13:33:30 -0400 | [diff] [blame] | 50 | parser.add_argument('-n', '--name', |
| 51 | default='', |
| 52 | help='Additional name to tag into file') |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 53 | return parser.parse_args() |
| 54 | |
| 55 | |
Sean Dague | ac9313e | 2015-07-27 13:33:30 -0400 | [diff] [blame] | 56 | def filename(dirname, name=""): |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 57 | now = datetime.datetime.utcnow() |
Sean Dague | ac9313e | 2015-07-27 13:33:30 -0400 | [diff] [blame] | 58 | fmt = "worlddump-%Y-%m-%d-%H%M%S" |
| 59 | if name: |
| 60 | fmt += "-" + name |
| 61 | fmt += ".txt" |
| 62 | return os.path.join(dirname, now.strftime(fmt)) |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 63 | |
| 64 | |
| 65 | def warn(msg): |
Eyal | e736177 | 2016-04-05 16:18:56 +0300 | [diff] [blame] | 66 | print("WARN: %s" % msg) |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 67 | |
| 68 | |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 69 | def _dump_cmd(cmd): |
Eyal | e736177 | 2016-04-05 16:18:56 +0300 | [diff] [blame] | 70 | print(cmd) |
| 71 | print("-" * len(cmd)) |
| 72 | print() |
Ian Wienand | 99440f9 | 2015-07-01 06:14:01 +1000 | [diff] [blame] | 73 | try: |
| 74 | subprocess.check_call(cmd, shell=True) |
Eyal | e736177 | 2016-04-05 16:18:56 +0300 | [diff] [blame] | 75 | print() |
Ihar Hrachyshka | 7976aac | 2016-03-03 15:30:49 +0100 | [diff] [blame] | 76 | except subprocess.CalledProcessError as e: |
Eyal | e736177 | 2016-04-05 16:18:56 +0300 | [diff] [blame] | 77 | print("*** Failed to run '%(cmd)s': %(err)s" % {'cmd': cmd, 'err': e}) |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 78 | |
| 79 | |
Chris Dent | 57d7967 | 2016-02-23 15:38:43 +0000 | [diff] [blame] | 80 | def _find_cmd(cmd): |
| 81 | if not spawn.find_executable(cmd): |
Eyal | e736177 | 2016-04-05 16:18:56 +0300 | [diff] [blame] | 82 | print("*** %s not found: skipping" % cmd) |
Chris Dent | 57d7967 | 2016-02-23 15:38:43 +0000 | [diff] [blame] | 83 | return False |
| 84 | return True |
| 85 | |
| 86 | |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 87 | def _header(name): |
Eyal | e736177 | 2016-04-05 16:18:56 +0300 | [diff] [blame] | 88 | print() |
| 89 | print(name) |
| 90 | print("=" * len(name)) |
| 91 | print() |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 92 | |
| 93 | |
fumihiko kakuma | 578459f | 2016-04-07 08:15:45 +0900 | [diff] [blame] | 94 | def _bridge_list(): |
yan.haifeng | 6ba17f7 | 2016-04-29 15:59:56 +0800 | [diff] [blame] | 95 | process = subprocess.Popen(['sudo', 'ovs-vsctl', 'list-br'], |
| 96 | stdout=subprocess.PIPE) |
fumihiko kakuma | 578459f | 2016-04-07 08:15:45 +0900 | [diff] [blame] | 97 | stdout, _ = process.communicate() |
| 98 | return stdout.split() |
| 99 | |
| 100 | |
fumihiko kakuma | 6099401 | 2016-03-08 20:55:01 +0900 | [diff] [blame] | 101 | # This method gets a max openflow version supported by openvswitch. |
| 102 | # For example 'ovs-ofctl --version' displays the following: |
| 103 | # |
| 104 | # ovs-ofctl (Open vSwitch) 2.0.2 |
| 105 | # Compiled Dec 9 2015 14:08:08 |
| 106 | # OpenFlow versions 0x1:0x4 |
| 107 | # |
fumihiko kakuma | 2bd2568 | 2016-04-05 10:33:50 +0900 | [diff] [blame] | 108 | # The above shows that openvswitch supports from OpenFlow10 to OpenFlow13. |
fumihiko kakuma | 6099401 | 2016-03-08 20:55:01 +0900 | [diff] [blame] | 109 | # This method gets max version searching 'OpenFlow versions 0x1:0x'. |
| 110 | # And return a version value converted to an integer type. |
| 111 | def _get_ofp_version(): |
| 112 | process = subprocess.Popen(['ovs-ofctl', '--version'], stdout=subprocess.PIPE) |
| 113 | stdout, _ = process.communicate() |
| 114 | find_str = 'OpenFlow versions 0x1:0x' |
| 115 | offset = stdout.find(find_str) |
| 116 | return int(stdout[offset + len(find_str):-1]) - 1 |
| 117 | |
| 118 | |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 119 | def disk_space(): |
| 120 | # the df output |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 121 | _header("File System Summary") |
| 122 | |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 123 | dfraw = os.popen("df -Ph").read() |
| 124 | df = [s.split() for s in dfraw.splitlines()] |
| 125 | for fs in df: |
| 126 | try: |
| 127 | if int(fs[4][:-1]) > 95: |
| 128 | warn("Device %s (%s) is %s full, might be an issue" % ( |
| 129 | fs[0], fs[5], fs[4])) |
| 130 | except ValueError: |
| 131 | # if it doesn't look like an int, that's fine |
| 132 | pass |
| 133 | |
Eyal | e736177 | 2016-04-05 16:18:56 +0300 | [diff] [blame] | 134 | print(dfraw) |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 135 | |
| 136 | |
Sean Dague | 2da606d | 2015-08-06 10:02:43 -0400 | [diff] [blame] | 137 | def ebtables_dump(): |
Sean Dague | 5c5e086 | 2015-11-09 14:08:15 -0500 | [diff] [blame] | 138 | tables = ['filter', 'nat', 'broute'] |
Sean Dague | 2da606d | 2015-08-06 10:02:43 -0400 | [diff] [blame] | 139 | _header("EB Tables Dump") |
Chris Dent | 57d7967 | 2016-02-23 15:38:43 +0000 | [diff] [blame] | 140 | if not _find_cmd('ebtables'): |
| 141 | return |
Sean Dague | 5c5e086 | 2015-11-09 14:08:15 -0500 | [diff] [blame] | 142 | for table in tables: |
| 143 | _dump_cmd("sudo ebtables -t %s -L" % table) |
Sean Dague | 2da606d | 2015-08-06 10:02:43 -0400 | [diff] [blame] | 144 | |
| 145 | |
Sean Dague | 168b7c2 | 2015-05-07 08:57:28 -0400 | [diff] [blame] | 146 | def iptables_dump(): |
| 147 | tables = ['filter', 'nat', 'mangle'] |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 148 | _header("IP Tables Dump") |
| 149 | |
Sean Dague | 168b7c2 | 2015-05-07 08:57:28 -0400 | [diff] [blame] | 150 | for table in tables: |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 151 | _dump_cmd("sudo iptables --line-numbers -L -nv -t %s" % table) |
| 152 | |
| 153 | |
Ihar Hrachyshka | 72c34ee | 2016-01-30 16:18:01 +0100 | [diff] [blame] | 154 | def _netns_list(): |
| 155 | process = subprocess.Popen(['ip', 'netns'], stdout=subprocess.PIPE) |
| 156 | stdout, _ = process.communicate() |
John L. Villalovos | c6e6939 | 2017-02-06 14:24:42 -0800 | [diff] [blame] | 157 | # NOTE(jlvillal): Sometimes 'ip netns list' can return output like: |
| 158 | # qrouter-0805fd7d-c493-4fa6-82ca-1c6c9b23cd9e (id: 1) |
| 159 | # qdhcp-bb2cc6ae-2ae8-474f-adda-a94059b872b5 (id: 0) |
| 160 | output = [x.split()[0] for x in stdout.splitlines()] |
| 161 | return output |
Ihar Hrachyshka | 72c34ee | 2016-01-30 16:18:01 +0100 | [diff] [blame] | 162 | |
| 163 | |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 164 | def network_dump(): |
| 165 | _header("Network Dump") |
| 166 | |
Nate Johnston | 56946cf | 2018-11-12 11:17:07 -0500 | [diff] [blame] | 167 | _dump_cmd("bridge link") |
Brian Haley | da18895 | 2019-10-31 14:49:23 -0400 | [diff] [blame^] | 168 | if _find_cmd("brctl"): |
| 169 | _dump_cmd("brctl show") |
Nate Johnston | 56946cf | 2018-11-12 11:17:07 -0500 | [diff] [blame] | 170 | _dump_cmd("ip link show type bridge") |
Hunt Xu | c396355 | 2018-01-08 16:11:33 +0800 | [diff] [blame] | 171 | ip_cmds = ["neigh", "addr", "link", "route"] |
Ihar Hrachyshka | 72c34ee | 2016-01-30 16:18:01 +0100 | [diff] [blame] | 172 | for cmd in ip_cmds + ['netns']: |
| 173 | _dump_cmd("ip %s" % cmd) |
| 174 | for netns_ in _netns_list(): |
| 175 | for cmd in ip_cmds: |
| 176 | args = {'netns': netns_, 'cmd': cmd} |
| 177 | _dump_cmd('sudo ip netns exec %(netns)s ip %(cmd)s' % args) |
Sean Dague | 168b7c2 | 2015-05-07 08:57:28 -0400 | [diff] [blame] | 178 | |
| 179 | |
Ihar Hrachyshka | c1b7cb1 | 2016-02-11 13:50:46 +0100 | [diff] [blame] | 180 | def ovs_dump(): |
| 181 | _header("Open vSwitch Dump") |
| 182 | |
Chris Dent | 57d7967 | 2016-02-23 15:38:43 +0000 | [diff] [blame] | 183 | # NOTE(cdent): If we're not using neutron + ovs these commands |
| 184 | # will not be present so |
| 185 | if not _find_cmd('ovs-vsctl'): |
| 186 | return |
| 187 | |
fumihiko kakuma | 578459f | 2016-04-07 08:15:45 +0900 | [diff] [blame] | 188 | bridges = _bridge_list() |
fumihiko kakuma | 6099401 | 2016-03-08 20:55:01 +0900 | [diff] [blame] | 189 | ofctl_cmds = ('show', 'dump-ports-desc', 'dump-ports', 'dump-flows') |
| 190 | ofp_max = _get_ofp_version() |
| 191 | vers = 'OpenFlow10' |
fumihiko kakuma | 578459f | 2016-04-07 08:15:45 +0900 | [diff] [blame] | 192 | for i in range(1, ofp_max + 1): |
fumihiko kakuma | 6099401 | 2016-03-08 20:55:01 +0900 | [diff] [blame] | 193 | vers += ',OpenFlow1' + str(i) |
Ihar Hrachyshka | c1b7cb1 | 2016-02-11 13:50:46 +0100 | [diff] [blame] | 194 | _dump_cmd("sudo ovs-vsctl show") |
fumihiko kakuma | 6099401 | 2016-03-08 20:55:01 +0900 | [diff] [blame] | 195 | for ofctl_cmd in ofctl_cmds: |
| 196 | for bridge in bridges: |
| 197 | args = {'vers': vers, 'cmd': ofctl_cmd, 'bridge': bridge} |
| 198 | _dump_cmd("sudo ovs-ofctl --protocols=%(vers)s %(cmd)s %(bridge)s" % args) |
Ihar Hrachyshka | c1b7cb1 | 2016-02-11 13:50:46 +0100 | [diff] [blame] | 199 | |
| 200 | |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 201 | def process_list(): |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 202 | _header("Process Listing") |
| 203 | _dump_cmd("ps axo " |
| 204 | "user,ppid,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,args") |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 205 | |
| 206 | |
Sean Dague | 737e942 | 2015-05-12 19:51:39 -0400 | [diff] [blame] | 207 | def compute_consoles(): |
| 208 | _header("Compute consoles") |
| 209 | for root, dirnames, filenames in os.walk('/opt/stack'): |
| 210 | for filename in fnmatch.filter(filenames, 'console.log'): |
| 211 | fullpath = os.path.join(root, filename) |
| 212 | _dump_cmd("sudo cat %s" % fullpath) |
| 213 | |
| 214 | |
Ihar Hrachyshka | ef219bf | 2016-02-11 13:54:48 +0100 | [diff] [blame] | 215 | def guru_meditation_reports(): |
| 216 | for service in GMR_PROCESSES: |
| 217 | _header("%s Guru Meditation Report" % service) |
Ian Wienand | 3a9df1d | 2015-07-01 06:18:47 +1000 | [diff] [blame] | 218 | |
Ihar Hrachyshka | ef219bf | 2016-02-11 13:54:48 +0100 | [diff] [blame] | 219 | try: |
| 220 | subprocess.check_call(['pgrep', '-f', service]) |
| 221 | except subprocess.CalledProcessError: |
| 222 | print("Skipping as %s does not appear to be running" % service) |
| 223 | continue |
Ian Wienand | 3a9df1d | 2015-07-01 06:18:47 +1000 | [diff] [blame] | 224 | |
Ihar Hrachyshka | ef219bf | 2016-02-11 13:54:48 +0100 | [diff] [blame] | 225 | _dump_cmd("killall -e -USR2 %s" % service) |
| 226 | print("guru meditation report in %s log" % service) |
Joe Gordon | 2ebe993 | 2015-06-07 16:57:34 +0900 | [diff] [blame] | 227 | |
| 228 | |
Ian Wienand | bfcc760 | 2017-03-29 11:52:06 +1100 | [diff] [blame] | 229 | def var_core(): |
| 230 | if os.path.exists('/var/core'): |
| 231 | _header("/var/core dumps") |
| 232 | # NOTE(ianw) : see DEBUG_LIBVIRT_COREDUMPS. We could think |
| 233 | # about getting backtraces out of these. There are other |
| 234 | # tools out there that can do that sort of thing though. |
| 235 | _dump_cmd("ls -ltrah /var/core") |
| 236 | |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 237 | def main(): |
| 238 | opts = get_options() |
Sean Dague | ac9313e | 2015-07-27 13:33:30 -0400 | [diff] [blame] | 239 | fname = filename(opts.dir, opts.name) |
Eyal | e736177 | 2016-04-05 16:18:56 +0300 | [diff] [blame] | 240 | print("World dumping... see %s for details" % fname) |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 241 | sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) |
| 242 | with open(fname, 'w') as f: |
| 243 | os.dup2(f.fileno(), sys.stdout.fileno()) |
| 244 | disk_space() |
| 245 | process_list() |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 246 | network_dump() |
Ihar Hrachyshka | c1b7cb1 | 2016-02-11 13:50:46 +0100 | [diff] [blame] | 247 | ovs_dump() |
Sean Dague | 168b7c2 | 2015-05-07 08:57:28 -0400 | [diff] [blame] | 248 | iptables_dump() |
Sean Dague | 2da606d | 2015-08-06 10:02:43 -0400 | [diff] [blame] | 249 | ebtables_dump() |
Sean Dague | 737e942 | 2015-05-12 19:51:39 -0400 | [diff] [blame] | 250 | compute_consoles() |
Ihar Hrachyshka | ef219bf | 2016-02-11 13:54:48 +0100 | [diff] [blame] | 251 | guru_meditation_reports() |
Ian Wienand | bfcc760 | 2017-03-29 11:52:06 +1100 | [diff] [blame] | 252 | var_core() |
Jens Harbott | ce396d3 | 2019-09-05 08:51:33 +0000 | [diff] [blame] | 253 | # Singular name for ease of log retrieval |
| 254 | copyname = os.path.join(opts.dir, 'worlddump') |
| 255 | if opts.name: |
| 256 | copyname += '-' + opts.name |
| 257 | copyname += '-latest.txt' |
| 258 | # We make a full copy to deal with jobs that may or may not |
| 259 | # gzip logs breaking symlinks. |
| 260 | shutil.copyfile(fname, copyname) |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 261 | |
| 262 | |
| 263 | if __name__ == '__main__': |
| 264 | try: |
| 265 | sys.exit(main()) |
| 266 | except KeyboardInterrupt: |
| 267 | sys.exit(1) |