blob: 628a69f7e5d11764e74ed41e855a65f9f21ea177 [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
24import sys
25
Anandprakash Dnyaneshwar Tandalebde24cb2015-06-29 15:04:30 +053026from subprocess import Popen
Sean Dague97fcc7b2014-06-16 17:24:14 -040027
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')
34 return parser.parse_args()
35
36
37def 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
42def warn(msg):
43 print "WARN: %s" % msg
44
45
Sean Dague60a14052015-05-11 14:53:39 -040046def _dump_cmd(cmd):
47 print cmd
48 print "-" * len(cmd)
49 print
Amey Bhide432268b2015-06-30 11:39:05 -070050 Popen(cmd, shell=True)
Sean Dague60a14052015-05-11 14:53:39 -040051
52
53def _header(name):
54 print
55 print name
56 print "=" * len(name)
57 print
58
59
Sean Dague97fcc7b2014-06-16 17:24:14 -040060def disk_space():
61 # the df output
Sean Dague60a14052015-05-11 14:53:39 -040062 _header("File System Summary")
63
Sean Dague97fcc7b2014-06-16 17:24:14 -040064 dfraw = os.popen("df -Ph").read()
65 df = [s.split() for s in dfraw.splitlines()]
66 for fs in df:
67 try:
68 if int(fs[4][:-1]) > 95:
69 warn("Device %s (%s) is %s full, might be an issue" % (
70 fs[0], fs[5], fs[4]))
71 except ValueError:
72 # if it doesn't look like an int, that's fine
73 pass
74
75 print dfraw
76
77
Sean Dague168b7c22015-05-07 08:57:28 -040078def iptables_dump():
79 tables = ['filter', 'nat', 'mangle']
Sean Dague60a14052015-05-11 14:53:39 -040080 _header("IP Tables Dump")
81
Sean Dague168b7c22015-05-07 08:57:28 -040082 for table in tables:
Sean Dague60a14052015-05-11 14:53:39 -040083 _dump_cmd("sudo iptables --line-numbers -L -nv -t %s" % table)
84
85
86def network_dump():
87 _header("Network Dump")
88
89 _dump_cmd("brctl show")
90 _dump_cmd("arp -n")
91 _dump_cmd("ip addr")
92 _dump_cmd("ip link")
93 _dump_cmd("ip route")
Sean Dague168b7c22015-05-07 08:57:28 -040094
95
Sean Dague97fcc7b2014-06-16 17:24:14 -040096def process_list():
Sean Dague60a14052015-05-11 14:53:39 -040097 _header("Process Listing")
98 _dump_cmd("ps axo "
99 "user,ppid,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,args")
Sean Dague97fcc7b2014-06-16 17:24:14 -0400100
101
Sean Dague737e9422015-05-12 19:51:39 -0400102def compute_consoles():
103 _header("Compute consoles")
104 for root, dirnames, filenames in os.walk('/opt/stack'):
105 for filename in fnmatch.filter(filenames, 'console.log'):
106 fullpath = os.path.join(root, filename)
107 _dump_cmd("sudo cat %s" % fullpath)
108
109
Joe Gordon2ebe9932015-06-07 16:57:34 +0900110def guru_meditation_report():
111 _header("nova-compute Guru Meditation Report")
112 _dump_cmd("kill -s USR1 `pgrep nova-compute`")
113 print "guru meditation report in nova-compute log"
114
115
Sean Dague97fcc7b2014-06-16 17:24:14 -0400116def main():
117 opts = get_options()
118 fname = filename(opts.dir)
119 print "World dumping... see %s for details" % fname
120 sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
121 with open(fname, 'w') as f:
122 os.dup2(f.fileno(), sys.stdout.fileno())
123 disk_space()
124 process_list()
Sean Dague60a14052015-05-11 14:53:39 -0400125 network_dump()
Sean Dague168b7c22015-05-07 08:57:28 -0400126 iptables_dump()
Sean Dague737e9422015-05-12 19:51:39 -0400127 compute_consoles()
Joe Gordon2ebe9932015-06-07 16:57:34 +0900128 guru_meditation_report()
Sean Dague97fcc7b2014-06-16 17:24:14 -0400129
130
131if __name__ == '__main__':
132 try:
133 sys.exit(main())
134 except KeyboardInterrupt:
135 sys.exit(1)