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