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