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