blob: 926b4a1873b1806a41b82c16a91013067d9005c5 [file] [log] [blame]
Sean Dague97fcc7b2014-06-16 17:24:14 -04001#!/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
19import argparse
20import datetime
Sean Dague737e9422015-05-12 19:51:39 -040021import fnmatch
Sean Dague97fcc7b2014-06-16 17:24:14 -040022import os
23import os.path
Ian Wienand99440f92015-07-01 06:14:01 +100024import subprocess
Sean Dague97fcc7b2014-06-16 17:24:14 -040025import sys
26
27
28def 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 Dagueac9313e2015-07-27 13:33:30 -040034 parser.add_argument('-n', '--name',
35 default='',
36 help='Additional name to tag into file')
Sean Dague97fcc7b2014-06-16 17:24:14 -040037 return parser.parse_args()
38
39
Sean Dagueac9313e2015-07-27 13:33:30 -040040def filename(dirname, name=""):
Sean Dague97fcc7b2014-06-16 17:24:14 -040041 now = datetime.datetime.utcnow()
Sean Dagueac9313e2015-07-27 13:33:30 -040042 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 Dague97fcc7b2014-06-16 17:24:14 -040047
48
49def warn(msg):
50 print "WARN: %s" % msg
51
52
Sean Dague60a14052015-05-11 14:53:39 -040053def _dump_cmd(cmd):
54 print cmd
55 print "-" * len(cmd)
56 print
Ian Wienand99440f92015-07-01 06:14:01 +100057 try:
58 subprocess.check_call(cmd, shell=True)
59 except subprocess.CalledProcessError:
60 print "*** Failed to run: %s" % cmd
Sean Dague60a14052015-05-11 14:53:39 -040061
62
63def _header(name):
64 print
65 print name
66 print "=" * len(name)
67 print
68
69
Sean Dague97fcc7b2014-06-16 17:24:14 -040070def disk_space():
71 # the df output
Sean Dague60a14052015-05-11 14:53:39 -040072 _header("File System Summary")
73
Sean Dague97fcc7b2014-06-16 17:24:14 -040074 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 Dague168b7c22015-05-07 08:57:28 -040088def iptables_dump():
89 tables = ['filter', 'nat', 'mangle']
Sean Dague60a14052015-05-11 14:53:39 -040090 _header("IP Tables Dump")
91
Sean Dague168b7c22015-05-07 08:57:28 -040092 for table in tables:
Sean Dague60a14052015-05-11 14:53:39 -040093 _dump_cmd("sudo iptables --line-numbers -L -nv -t %s" % table)
94
95
96def network_dump():
97 _header("Network Dump")
98
99 _dump_cmd("brctl show")
100 _dump_cmd("arp -n")
101 _dump_cmd("ip addr")
102 _dump_cmd("ip link")
103 _dump_cmd("ip route")
Sean Dague168b7c22015-05-07 08:57:28 -0400104
105
Sean Dague97fcc7b2014-06-16 17:24:14 -0400106def process_list():
Sean Dague60a14052015-05-11 14:53:39 -0400107 _header("Process Listing")
108 _dump_cmd("ps axo "
109 "user,ppid,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,args")
Sean Dague97fcc7b2014-06-16 17:24:14 -0400110
111
Sean Dague737e9422015-05-12 19:51:39 -0400112def compute_consoles():
113 _header("Compute consoles")
114 for root, dirnames, filenames in os.walk('/opt/stack'):
115 for filename in fnmatch.filter(filenames, 'console.log'):
116 fullpath = os.path.join(root, filename)
117 _dump_cmd("sudo cat %s" % fullpath)
118
119
Joe Gordon2ebe9932015-06-07 16:57:34 +0900120def guru_meditation_report():
121 _header("nova-compute Guru Meditation Report")
Ian Wienand3a9df1d2015-07-01 06:18:47 +1000122
123 try:
124 subprocess.check_call(["pgrep","nova-compute"])
125 except subprocess.CalledProcessError:
126 print "Skipping as nova-compute does not appear to be running"
127 return
128
Joe Gordon2ebe9932015-06-07 16:57:34 +0900129 _dump_cmd("kill -s USR1 `pgrep nova-compute`")
130 print "guru meditation report in nova-compute log"
131
132
Sean Dague97fcc7b2014-06-16 17:24:14 -0400133def main():
134 opts = get_options()
Sean Dagueac9313e2015-07-27 13:33:30 -0400135 fname = filename(opts.dir, opts.name)
Sean Dague97fcc7b2014-06-16 17:24:14 -0400136 print "World dumping... see %s for details" % fname
137 sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
138 with open(fname, 'w') as f:
139 os.dup2(f.fileno(), sys.stdout.fileno())
140 disk_space()
141 process_list()
Sean Dague60a14052015-05-11 14:53:39 -0400142 network_dump()
Sean Dague168b7c22015-05-07 08:57:28 -0400143 iptables_dump()
Sean Dague737e9422015-05-12 19:51:39 -0400144 compute_consoles()
Joe Gordon2ebe9932015-06-07 16:57:34 +0900145 guru_meditation_report()
Sean Dague97fcc7b2014-06-16 17:24:14 -0400146
147
148if __name__ == '__main__':
149 try:
150 sys.exit(main())
151 except KeyboardInterrupt:
152 sys.exit(1)