| 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 | 
| Sean Dague | 737e942 | 2015-05-12 19:51:39 -0400 | [diff] [blame] | 21 | import fnmatch | 
| Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 22 | import os | 
|  | 23 | import os.path | 
| Ian Wienand | 99440f9 | 2015-07-01 06:14:01 +1000 | [diff] [blame] | 24 | import subprocess | 
| Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 25 | import sys | 
|  | 26 |  | 
|  | 27 |  | 
|  | 28 | def get_options(): | 
|  | 29 | parser = argparse.ArgumentParser( | 
|  | 30 | description='Dump world state for debugging') | 
|  | 31 | parser.add_argument('-d', '--dir', | 
|  | 32 | default='.', | 
|  | 33 | help='Output directory for worlddump') | 
| Sean Dague | ac9313e | 2015-07-27 13:33:30 -0400 | [diff] [blame] | 34 | parser.add_argument('-n', '--name', | 
|  | 35 | default='', | 
|  | 36 | help='Additional name to tag into file') | 
| Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 37 | return parser.parse_args() | 
|  | 38 |  | 
|  | 39 |  | 
| Sean Dague | ac9313e | 2015-07-27 13:33:30 -0400 | [diff] [blame] | 40 | def filename(dirname, name=""): | 
| Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 41 | now = datetime.datetime.utcnow() | 
| Sean Dague | ac9313e | 2015-07-27 13:33:30 -0400 | [diff] [blame] | 42 | fmt = "worlddump-%Y-%m-%d-%H%M%S" | 
|  | 43 | if name: | 
|  | 44 | fmt += "-" + name | 
|  | 45 | fmt += ".txt" | 
|  | 46 | return os.path.join(dirname, now.strftime(fmt)) | 
| Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 47 |  | 
|  | 48 |  | 
|  | 49 | def warn(msg): | 
|  | 50 | print "WARN: %s" % msg | 
|  | 51 |  | 
|  | 52 |  | 
| Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 53 | def _dump_cmd(cmd): | 
|  | 54 | print cmd | 
|  | 55 | print "-" * len(cmd) | 
|  | 56 | print | 
| Ian Wienand | 99440f9 | 2015-07-01 06:14:01 +1000 | [diff] [blame] | 57 | try: | 
|  | 58 | subprocess.check_call(cmd, shell=True) | 
|  | 59 | except subprocess.CalledProcessError: | 
|  | 60 | print "*** Failed to run: %s" % cmd | 
| Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 61 |  | 
|  | 62 |  | 
|  | 63 | def _header(name): | 
|  | 64 | print | 
|  | 65 | print name | 
|  | 66 | print "=" * len(name) | 
|  | 67 | print | 
|  | 68 |  | 
|  | 69 |  | 
| Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 70 | def disk_space(): | 
|  | 71 | # the df output | 
| Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 72 | _header("File System Summary") | 
|  | 73 |  | 
| Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 74 | dfraw = os.popen("df -Ph").read() | 
|  | 75 | df = [s.split() for s in dfraw.splitlines()] | 
|  | 76 | for fs in df: | 
|  | 77 | try: | 
|  | 78 | if int(fs[4][:-1]) > 95: | 
|  | 79 | warn("Device %s (%s) is %s full, might be an issue" % ( | 
|  | 80 | fs[0], fs[5], fs[4])) | 
|  | 81 | except ValueError: | 
|  | 82 | # if it doesn't look like an int, that's fine | 
|  | 83 | pass | 
|  | 84 |  | 
|  | 85 | print dfraw | 
|  | 86 |  | 
|  | 87 |  | 
| Sean Dague | 2da606d | 2015-08-06 10:02:43 -0400 | [diff] [blame] | 88 | def ebtables_dump(): | 
| Sean Dague | 5c5e086 | 2015-11-09 14:08:15 -0500 | [diff] [blame] | 89 | tables = ['filter', 'nat', 'broute'] | 
| Sean Dague | 2da606d | 2015-08-06 10:02:43 -0400 | [diff] [blame] | 90 | _header("EB Tables Dump") | 
| Sean Dague | 5c5e086 | 2015-11-09 14:08:15 -0500 | [diff] [blame] | 91 | for table in tables: | 
|  | 92 | _dump_cmd("sudo ebtables -t %s -L" % table) | 
| Sean Dague | 2da606d | 2015-08-06 10:02:43 -0400 | [diff] [blame] | 93 |  | 
|  | 94 |  | 
| Sean Dague | 168b7c2 | 2015-05-07 08:57:28 -0400 | [diff] [blame] | 95 | def iptables_dump(): | 
|  | 96 | tables = ['filter', 'nat', 'mangle'] | 
| Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 97 | _header("IP Tables Dump") | 
|  | 98 |  | 
| Sean Dague | 168b7c2 | 2015-05-07 08:57:28 -0400 | [diff] [blame] | 99 | for table in tables: | 
| Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 100 | _dump_cmd("sudo iptables --line-numbers -L -nv -t %s" % table) | 
|  | 101 |  | 
|  | 102 |  | 
|  | 103 | def network_dump(): | 
|  | 104 | _header("Network Dump") | 
|  | 105 |  | 
|  | 106 | _dump_cmd("brctl show") | 
|  | 107 | _dump_cmd("arp -n") | 
|  | 108 | _dump_cmd("ip addr") | 
|  | 109 | _dump_cmd("ip link") | 
|  | 110 | _dump_cmd("ip route") | 
| Sean Dague | 168b7c2 | 2015-05-07 08:57:28 -0400 | [diff] [blame] | 111 |  | 
|  | 112 |  | 
| Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 113 | def process_list(): | 
| Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 114 | _header("Process Listing") | 
|  | 115 | _dump_cmd("ps axo " | 
|  | 116 | "user,ppid,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,args") | 
| Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 117 |  | 
|  | 118 |  | 
| Sean Dague | 737e942 | 2015-05-12 19:51:39 -0400 | [diff] [blame] | 119 | def compute_consoles(): | 
|  | 120 | _header("Compute consoles") | 
|  | 121 | for root, dirnames, filenames in os.walk('/opt/stack'): | 
|  | 122 | for filename in fnmatch.filter(filenames, 'console.log'): | 
|  | 123 | fullpath = os.path.join(root, filename) | 
|  | 124 | _dump_cmd("sudo cat %s" % fullpath) | 
|  | 125 |  | 
|  | 126 |  | 
| Joe Gordon | 2ebe993 | 2015-06-07 16:57:34 +0900 | [diff] [blame] | 127 | def guru_meditation_report(): | 
|  | 128 | _header("nova-compute Guru Meditation Report") | 
| Ian Wienand | 3a9df1d | 2015-07-01 06:18:47 +1000 | [diff] [blame] | 129 |  | 
|  | 130 | try: | 
|  | 131 | subprocess.check_call(["pgrep","nova-compute"]) | 
|  | 132 | except subprocess.CalledProcessError: | 
|  | 133 | print "Skipping as nova-compute does not appear to be running" | 
|  | 134 | return | 
|  | 135 |  | 
| Joe Gordon | 2ebe993 | 2015-06-07 16:57:34 +0900 | [diff] [blame] | 136 | _dump_cmd("kill -s USR1 `pgrep nova-compute`") | 
|  | 137 | print "guru meditation report in nova-compute log" | 
|  | 138 |  | 
|  | 139 |  | 
| Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 140 | def main(): | 
|  | 141 | opts = get_options() | 
| Sean Dague | ac9313e | 2015-07-27 13:33:30 -0400 | [diff] [blame] | 142 | fname = filename(opts.dir, opts.name) | 
| Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 143 | print "World dumping... see %s for details" % fname | 
|  | 144 | sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) | 
|  | 145 | with open(fname, 'w') as f: | 
|  | 146 | os.dup2(f.fileno(), sys.stdout.fileno()) | 
|  | 147 | disk_space() | 
|  | 148 | process_list() | 
| Sean Dague | 60a1405 | 2015-05-11 14:53:39 -0400 | [diff] [blame] | 149 | network_dump() | 
| Sean Dague | 168b7c2 | 2015-05-07 08:57:28 -0400 | [diff] [blame] | 150 | iptables_dump() | 
| Sean Dague | 2da606d | 2015-08-06 10:02:43 -0400 | [diff] [blame] | 151 | ebtables_dump() | 
| Sean Dague | 737e942 | 2015-05-12 19:51:39 -0400 | [diff] [blame] | 152 | compute_consoles() | 
| Joe Gordon | 2ebe993 | 2015-06-07 16:57:34 +0900 | [diff] [blame] | 153 | guru_meditation_report() | 
| Sean Dague | 97fcc7b | 2014-06-16 17:24:14 -0400 | [diff] [blame] | 154 |  | 
|  | 155 |  | 
|  | 156 | if __name__ == '__main__': | 
|  | 157 | try: | 
|  | 158 | sys.exit(main()) | 
|  | 159 | except KeyboardInterrupt: | 
|  | 160 | sys.exit(1) |