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 | |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 79 | def disk_space(): |
| 80 | # the df output |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 81 | _header("File System Summary") |
| 82 | |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 83 | dfraw = os.popen("df -Ph").read() |
| 84 | df = [s.split() for s in dfraw.splitlines()] |
| 85 | for fs in df: |
| 86 | try: |
| 87 | if int(fs[4][:-1]) > 95: |
| 88 | warn("Device %s (%s) is %s full, might be an issue" % ( |
| 89 | fs[0], fs[5], fs[4])) |
| 90 | except ValueError: |
| 91 | # if it doesn't look like an int, that's fine |
| 92 | pass |
| 93 | |
| 94 | print dfraw |
| 95 | |
| 96 | |
Sean Dague | 2da606d | 2015-08-06 10:02:43 -0400 | [diff] [blame] | 97 | def ebtables_dump(): |
Sean Dague | 5c5e086 | 2015-11-09 14:08:15 -0500 | [diff] [blame] | 98 | tables = ['filter', 'nat', 'broute'] |
Sean Dague | 2da606d | 2015-08-06 10:02:43 -0400 | [diff] [blame] | 99 | _header("EB Tables Dump") |
Chris Dent | 57d7967 | 2016-02-23 15:38:43 +0000 | [diff] [blame] | 100 | if not _find_cmd('ebtables'): |
| 101 | return |
Sean Dague | 5c5e086 | 2015-11-09 14:08:15 -0500 | [diff] [blame] | 102 | for table in tables: |
| 103 | _dump_cmd("sudo ebtables -t %s -L" % table) |
Sean Dague | 2da606d | 2015-08-06 10:02:43 -0400 | [diff] [blame] | 104 | |
| 105 | |
Sean Dague | 168b7c2 | 2015-05-07 08:57:28 -0400 | [diff] [blame] | 106 | def iptables_dump(): |
| 107 | tables = ['filter', 'nat', 'mangle'] |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 108 | _header("IP Tables Dump") |
| 109 | |
Sean Dague | 168b7c2 | 2015-05-07 08:57:28 -0400 | [diff] [blame] | 110 | for table in tables: |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 111 | _dump_cmd("sudo iptables --line-numbers -L -nv -t %s" % table) |
| 112 | |
| 113 | |
Ihar Hrachyshka | 72c34ee | 2016-01-30 16:18:01 +0100 | [diff] [blame] | 114 | def _netns_list(): |
| 115 | process = subprocess.Popen(['ip', 'netns'], stdout=subprocess.PIPE) |
| 116 | stdout, _ = process.communicate() |
| 117 | return stdout.split() |
| 118 | |
| 119 | |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 120 | def network_dump(): |
| 121 | _header("Network Dump") |
| 122 | |
| 123 | _dump_cmd("brctl show") |
| 124 | _dump_cmd("arp -n") |
Ihar Hrachyshka | 72c34ee | 2016-01-30 16:18:01 +0100 | [diff] [blame] | 125 | ip_cmds = ["addr", "link", "route"] |
| 126 | for cmd in ip_cmds + ['netns']: |
| 127 | _dump_cmd("ip %s" % cmd) |
| 128 | for netns_ in _netns_list(): |
| 129 | for cmd in ip_cmds: |
| 130 | args = {'netns': netns_, 'cmd': cmd} |
| 131 | _dump_cmd('sudo ip netns exec %(netns)s ip %(cmd)s' % args) |
Sean Dague | 168b7c2 | 2015-05-07 08:57:28 -0400 | [diff] [blame] | 132 | |
| 133 | |
Ihar Hrachyshka | c1b7cb1 | 2016-02-11 13:50:46 +0100 | [diff] [blame] | 134 | def ovs_dump(): |
| 135 | _header("Open vSwitch Dump") |
| 136 | |
Chris Dent | 57d7967 | 2016-02-23 15:38:43 +0000 | [diff] [blame] | 137 | # NOTE(cdent): If we're not using neutron + ovs these commands |
| 138 | # will not be present so |
| 139 | if not _find_cmd('ovs-vsctl'): |
| 140 | return |
| 141 | |
Ihar Hrachyshka | c1b7cb1 | 2016-02-11 13:50:46 +0100 | [diff] [blame] | 142 | # NOTE(ihrachys): worlddump is used outside of devstack context (f.e. in |
| 143 | # grenade), so there is no single place to determine the bridge names from. |
| 144 | # Hardcode for now. |
| 145 | bridges = ('br-int', 'br-tun', 'br-ex') |
| 146 | _dump_cmd("sudo ovs-vsctl show") |
| 147 | for bridge in bridges: |
| 148 | _dump_cmd("sudo ovs-ofctl show %s" % bridge) |
| 149 | for bridge in bridges: |
| 150 | _dump_cmd("sudo ovs-ofctl dump-flows %s" % bridge) |
| 151 | |
| 152 | |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 153 | def process_list(): |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 154 | _header("Process Listing") |
| 155 | _dump_cmd("ps axo " |
| 156 | "user,ppid,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,args") |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 157 | |
| 158 | |
Sean Dague | 737e942 | 2015-05-12 19:51:39 -0400 | [diff] [blame] | 159 | def compute_consoles(): |
| 160 | _header("Compute consoles") |
| 161 | for root, dirnames, filenames in os.walk('/opt/stack'): |
| 162 | for filename in fnmatch.filter(filenames, 'console.log'): |
| 163 | fullpath = os.path.join(root, filename) |
| 164 | _dump_cmd("sudo cat %s" % fullpath) |
| 165 | |
| 166 | |
Joe Gordon | 2ebe993 | 2015-06-07 16:57:34 +0900 | [diff] [blame] | 167 | def guru_meditation_report(): |
| 168 | _header("nova-compute Guru Meditation Report") |
Ian Wienand | 3a9df1d | 2015-07-01 06:18:47 +1000 | [diff] [blame] | 169 | |
| 170 | try: |
| 171 | subprocess.check_call(["pgrep","nova-compute"]) |
| 172 | except subprocess.CalledProcessError: |
| 173 | print "Skipping as nova-compute does not appear to be running" |
| 174 | return |
| 175 | |
Kashyap Chamarthy | 8872545 | 2015-09-14 13:17:56 +0200 | [diff] [blame] | 176 | _dump_cmd("kill -s USR2 `pgrep nova-compute`") |
Joe Gordon | 2ebe993 | 2015-06-07 16:57:34 +0900 | [diff] [blame] | 177 | print "guru meditation report in nova-compute log" |
| 178 | |
| 179 | |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 180 | def main(): |
| 181 | opts = get_options() |
Sean Dague | ac9313e | 2015-07-27 13:33:30 -0400 | [diff] [blame] | 182 | fname = filename(opts.dir, opts.name) |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 183 | print "World dumping... see %s for details" % fname |
| 184 | sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) |
| 185 | with open(fname, 'w') as f: |
| 186 | os.dup2(f.fileno(), sys.stdout.fileno()) |
| 187 | disk_space() |
| 188 | process_list() |
Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 189 | network_dump() |
Ihar Hrachyshka | c1b7cb1 | 2016-02-11 13:50:46 +0100 | [diff] [blame] | 190 | ovs_dump() |
Sean Dague | 168b7c2 | 2015-05-07 08:57:28 -0400 | [diff] [blame] | 191 | iptables_dump() |
Sean Dague | 2da606d | 2015-08-06 10:02:43 -0400 | [diff] [blame] | 192 | ebtables_dump() |
Sean Dague | 737e942 | 2015-05-12 19:51:39 -0400 | [diff] [blame] | 193 | compute_consoles() |
Joe Gordon | 2ebe993 | 2015-06-07 16:57:34 +0900 | [diff] [blame] | 194 | guru_meditation_report() |
Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 195 | |
| 196 | |
| 197 | if __name__ == '__main__': |
| 198 | try: |
| 199 | sys.exit(main()) |
| 200 | except KeyboardInterrupt: |
| 201 | sys.exit(1) |