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 | |
| 20 | import argparse |
| 21 | import datetime |
Chris Dent | 57d7967 | 2016-02-23 15:38:43 +0000 | [diff] [blame] | 22 | from distutils import spawn |
Sean Dague | 737e942 | 2015-05-12 19:51:39 -0400 | [diff] [blame] | 23 | import fnmatch |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 24 | import os |
| 25 | import os.path |
Ian Wienand | 99440f9 | 2015-07-01 06:14:01 +1000 | [diff] [blame] | 26 | import subprocess |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 27 | import sys |
| 28 | |
| 29 | |
| 30 | def get_options(): |
| 31 | parser = argparse.ArgumentParser( |
| 32 | description='Dump world state for debugging') |
| 33 | parser.add_argument('-d', '--dir', |
| 34 | default='.', |
| 35 | help='Output directory for worlddump') |
Sean Dague | ac9313e | 2015-07-27 13:33:30 -0400 | [diff] [blame] | 36 | parser.add_argument('-n', '--name', |
| 37 | default='', |
| 38 | help='Additional name to tag into file') |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 39 | return parser.parse_args() |
| 40 | |
| 41 | |
Sean Dague | ac9313e | 2015-07-27 13:33:30 -0400 | [diff] [blame] | 42 | def filename(dirname, name=""): |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 43 | now = datetime.datetime.utcnow() |
Sean Dague | ac9313e | 2015-07-27 13:33:30 -0400 | [diff] [blame] | 44 | fmt = "worlddump-%Y-%m-%d-%H%M%S" |
| 45 | if name: |
| 46 | fmt += "-" + name |
| 47 | fmt += ".txt" |
| 48 | return os.path.join(dirname, now.strftime(fmt)) |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 49 | |
| 50 | |
| 51 | def warn(msg): |
Eyal | e736177 | 2016-04-05 16:18:56 +0300 | [diff] [blame^] | 52 | print("WARN: %s" % msg) |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 53 | |
| 54 | |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 55 | def _dump_cmd(cmd): |
Eyal | e736177 | 2016-04-05 16:18:56 +0300 | [diff] [blame^] | 56 | print(cmd) |
| 57 | print("-" * len(cmd)) |
| 58 | print() |
Ian Wienand | 99440f9 | 2015-07-01 06:14:01 +1000 | [diff] [blame] | 59 | try: |
| 60 | subprocess.check_call(cmd, shell=True) |
Eyal | e736177 | 2016-04-05 16:18:56 +0300 | [diff] [blame^] | 61 | print() |
Ihar Hrachyshka | 7976aac | 2016-03-03 15:30:49 +0100 | [diff] [blame] | 62 | except subprocess.CalledProcessError as e: |
Eyal | e736177 | 2016-04-05 16:18:56 +0300 | [diff] [blame^] | 63 | print("*** Failed to run '%(cmd)s': %(err)s" % {'cmd': cmd, 'err': e}) |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 64 | |
| 65 | |
Chris Dent | 57d7967 | 2016-02-23 15:38:43 +0000 | [diff] [blame] | 66 | def _find_cmd(cmd): |
| 67 | if not spawn.find_executable(cmd): |
Eyal | e736177 | 2016-04-05 16:18:56 +0300 | [diff] [blame^] | 68 | print("*** %s not found: skipping" % cmd) |
Chris Dent | 57d7967 | 2016-02-23 15:38:43 +0000 | [diff] [blame] | 69 | return False |
| 70 | return True |
| 71 | |
| 72 | |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 73 | def _header(name): |
Eyal | e736177 | 2016-04-05 16:18:56 +0300 | [diff] [blame^] | 74 | print() |
| 75 | print(name) |
| 76 | print("=" * len(name)) |
| 77 | print() |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 78 | |
| 79 | |
fumihiko kakuma | 6099401 | 2016-03-08 20:55:01 +0900 | [diff] [blame] | 80 | # This method gets a max openflow version supported by openvswitch. |
| 81 | # For example 'ovs-ofctl --version' displays the following: |
| 82 | # |
| 83 | # ovs-ofctl (Open vSwitch) 2.0.2 |
| 84 | # Compiled Dec 9 2015 14:08:08 |
| 85 | # OpenFlow versions 0x1:0x4 |
| 86 | # |
| 87 | # The above shows that openvswitch supports from OpenFlow11 to OpenFlow13. |
| 88 | # This method gets max version searching 'OpenFlow versions 0x1:0x'. |
| 89 | # And return a version value converted to an integer type. |
| 90 | def _get_ofp_version(): |
| 91 | process = subprocess.Popen(['ovs-ofctl', '--version'], stdout=subprocess.PIPE) |
| 92 | stdout, _ = process.communicate() |
| 93 | find_str = 'OpenFlow versions 0x1:0x' |
| 94 | offset = stdout.find(find_str) |
| 95 | return int(stdout[offset + len(find_str):-1]) - 1 |
| 96 | |
| 97 | |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 98 | def disk_space(): |
| 99 | # the df output |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 100 | _header("File System Summary") |
| 101 | |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 102 | dfraw = os.popen("df -Ph").read() |
| 103 | df = [s.split() for s in dfraw.splitlines()] |
| 104 | for fs in df: |
| 105 | try: |
| 106 | if int(fs[4][:-1]) > 95: |
| 107 | warn("Device %s (%s) is %s full, might be an issue" % ( |
| 108 | fs[0], fs[5], fs[4])) |
| 109 | except ValueError: |
| 110 | # if it doesn't look like an int, that's fine |
| 111 | pass |
| 112 | |
Eyal | e736177 | 2016-04-05 16:18:56 +0300 | [diff] [blame^] | 113 | print(dfraw) |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 114 | |
| 115 | |
Sean Dague | 2da606d | 2015-08-06 10:02:43 -0400 | [diff] [blame] | 116 | def ebtables_dump(): |
Sean Dague | 5c5e086 | 2015-11-09 14:08:15 -0500 | [diff] [blame] | 117 | tables = ['filter', 'nat', 'broute'] |
Sean Dague | 2da606d | 2015-08-06 10:02:43 -0400 | [diff] [blame] | 118 | _header("EB Tables Dump") |
Chris Dent | 57d7967 | 2016-02-23 15:38:43 +0000 | [diff] [blame] | 119 | if not _find_cmd('ebtables'): |
| 120 | return |
Sean Dague | 5c5e086 | 2015-11-09 14:08:15 -0500 | [diff] [blame] | 121 | for table in tables: |
| 122 | _dump_cmd("sudo ebtables -t %s -L" % table) |
Sean Dague | 2da606d | 2015-08-06 10:02:43 -0400 | [diff] [blame] | 123 | |
| 124 | |
Sean Dague | 168b7c2 | 2015-05-07 08:57:28 -0400 | [diff] [blame] | 125 | def iptables_dump(): |
| 126 | tables = ['filter', 'nat', 'mangle'] |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 127 | _header("IP Tables Dump") |
| 128 | |
Sean Dague | 168b7c2 | 2015-05-07 08:57:28 -0400 | [diff] [blame] | 129 | for table in tables: |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 130 | _dump_cmd("sudo iptables --line-numbers -L -nv -t %s" % table) |
| 131 | |
| 132 | |
Ihar Hrachyshka | 72c34ee | 2016-01-30 16:18:01 +0100 | [diff] [blame] | 133 | def _netns_list(): |
| 134 | process = subprocess.Popen(['ip', 'netns'], stdout=subprocess.PIPE) |
| 135 | stdout, _ = process.communicate() |
| 136 | return stdout.split() |
| 137 | |
| 138 | |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 139 | def network_dump(): |
| 140 | _header("Network Dump") |
| 141 | |
| 142 | _dump_cmd("brctl show") |
| 143 | _dump_cmd("arp -n") |
Ihar Hrachyshka | 72c34ee | 2016-01-30 16:18:01 +0100 | [diff] [blame] | 144 | ip_cmds = ["addr", "link", "route"] |
| 145 | for cmd in ip_cmds + ['netns']: |
| 146 | _dump_cmd("ip %s" % cmd) |
| 147 | for netns_ in _netns_list(): |
| 148 | for cmd in ip_cmds: |
| 149 | args = {'netns': netns_, 'cmd': cmd} |
| 150 | _dump_cmd('sudo ip netns exec %(netns)s ip %(cmd)s' % args) |
Sean Dague | 168b7c2 | 2015-05-07 08:57:28 -0400 | [diff] [blame] | 151 | |
| 152 | |
Ihar Hrachyshka | c1b7cb1 | 2016-02-11 13:50:46 +0100 | [diff] [blame] | 153 | def ovs_dump(): |
| 154 | _header("Open vSwitch Dump") |
| 155 | |
Chris Dent | 57d7967 | 2016-02-23 15:38:43 +0000 | [diff] [blame] | 156 | # NOTE(cdent): If we're not using neutron + ovs these commands |
| 157 | # will not be present so |
| 158 | if not _find_cmd('ovs-vsctl'): |
| 159 | return |
| 160 | |
Ihar Hrachyshka | c1b7cb1 | 2016-02-11 13:50:46 +0100 | [diff] [blame] | 161 | # NOTE(ihrachys): worlddump is used outside of devstack context (f.e. in |
| 162 | # grenade), so there is no single place to determine the bridge names from. |
| 163 | # Hardcode for now. |
| 164 | bridges = ('br-int', 'br-tun', 'br-ex') |
fumihiko kakuma | 6099401 | 2016-03-08 20:55:01 +0900 | [diff] [blame] | 165 | ofctl_cmds = ('show', 'dump-ports-desc', 'dump-ports', 'dump-flows') |
| 166 | ofp_max = _get_ofp_version() |
| 167 | vers = 'OpenFlow10' |
| 168 | for i in range(ofp_max + 1): |
| 169 | vers += ',OpenFlow1' + str(i) |
Ihar Hrachyshka | c1b7cb1 | 2016-02-11 13:50:46 +0100 | [diff] [blame] | 170 | _dump_cmd("sudo ovs-vsctl show") |
fumihiko kakuma | 6099401 | 2016-03-08 20:55:01 +0900 | [diff] [blame] | 171 | for ofctl_cmd in ofctl_cmds: |
| 172 | for bridge in bridges: |
| 173 | args = {'vers': vers, 'cmd': ofctl_cmd, 'bridge': bridge} |
| 174 | _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] | 175 | |
| 176 | |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 177 | def process_list(): |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 178 | _header("Process Listing") |
| 179 | _dump_cmd("ps axo " |
| 180 | "user,ppid,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,args") |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 181 | |
| 182 | |
Sean Dague | 737e942 | 2015-05-12 19:51:39 -0400 | [diff] [blame] | 183 | def compute_consoles(): |
| 184 | _header("Compute consoles") |
| 185 | for root, dirnames, filenames in os.walk('/opt/stack'): |
| 186 | for filename in fnmatch.filter(filenames, 'console.log'): |
| 187 | fullpath = os.path.join(root, filename) |
| 188 | _dump_cmd("sudo cat %s" % fullpath) |
| 189 | |
| 190 | |
Joe Gordon | 2ebe993 | 2015-06-07 16:57:34 +0900 | [diff] [blame] | 191 | def guru_meditation_report(): |
| 192 | _header("nova-compute Guru Meditation Report") |
Ian Wienand | 3a9df1d | 2015-07-01 06:18:47 +1000 | [diff] [blame] | 193 | |
| 194 | try: |
| 195 | subprocess.check_call(["pgrep","nova-compute"]) |
| 196 | except subprocess.CalledProcessError: |
Eyal | e736177 | 2016-04-05 16:18:56 +0300 | [diff] [blame^] | 197 | print("Skipping as nova-compute does not appear to be running") |
Ian Wienand | 3a9df1d | 2015-07-01 06:18:47 +1000 | [diff] [blame] | 198 | return |
| 199 | |
Kashyap Chamarthy | 8872545 | 2015-09-14 13:17:56 +0200 | [diff] [blame] | 200 | _dump_cmd("kill -s USR2 `pgrep nova-compute`") |
Eyal | e736177 | 2016-04-05 16:18:56 +0300 | [diff] [blame^] | 201 | print("guru meditation report in nova-compute log") |
Joe Gordon | 2ebe993 | 2015-06-07 16:57:34 +0900 | [diff] [blame] | 202 | |
| 203 | |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 204 | def main(): |
| 205 | opts = get_options() |
Sean Dague | ac9313e | 2015-07-27 13:33:30 -0400 | [diff] [blame] | 206 | fname = filename(opts.dir, opts.name) |
Eyal | e736177 | 2016-04-05 16:18:56 +0300 | [diff] [blame^] | 207 | print("World dumping... see %s for details" % fname) |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 208 | sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) |
| 209 | with open(fname, 'w') as f: |
| 210 | os.dup2(f.fileno(), sys.stdout.fileno()) |
| 211 | disk_space() |
| 212 | process_list() |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 213 | network_dump() |
Ihar Hrachyshka | c1b7cb1 | 2016-02-11 13:50:46 +0100 | [diff] [blame] | 214 | ovs_dump() |
Sean Dague | 168b7c2 | 2015-05-07 08:57:28 -0400 | [diff] [blame] | 215 | iptables_dump() |
Sean Dague | 2da606d | 2015-08-06 10:02:43 -0400 | [diff] [blame] | 216 | ebtables_dump() |
Sean Dague | 737e942 | 2015-05-12 19:51:39 -0400 | [diff] [blame] | 217 | compute_consoles() |
Joe Gordon | 2ebe993 | 2015-06-07 16:57:34 +0900 | [diff] [blame] | 218 | guru_meditation_report() |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 219 | |
| 220 | |
| 221 | if __name__ == '__main__': |
| 222 | try: |
| 223 | sys.exit(main()) |
| 224 | except KeyboardInterrupt: |
| 225 | sys.exit(1) |