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