blob: cb325105261d6902b6fd5c96e202a7b0ef9e1ca7 [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
21import os
22import os.path
23import sys
24
25
26def get_options():
27 parser = argparse.ArgumentParser(
28 description='Dump world state for debugging')
29 parser.add_argument('-d', '--dir',
30 default='.',
31 help='Output directory for worlddump')
32 return parser.parse_args()
33
34
35def filename(dirname):
36 now = datetime.datetime.utcnow()
37 return os.path.join(dirname, now.strftime("worlddump-%Y-%m-%d-%H%M%S.txt"))
38
39
40def warn(msg):
41 print "WARN: %s" % msg
42
43
44def disk_space():
45 # the df output
46 print """
47File System Summary
48===================
49"""
50 dfraw = os.popen("df -Ph").read()
51 df = [s.split() for s in dfraw.splitlines()]
52 for fs in df:
53 try:
54 if int(fs[4][:-1]) > 95:
55 warn("Device %s (%s) is %s full, might be an issue" % (
56 fs[0], fs[5], fs[4]))
57 except ValueError:
58 # if it doesn't look like an int, that's fine
59 pass
60
61 print dfraw
62
63
Sean Dague168b7c22015-05-07 08:57:28 -040064def iptables_dump():
65 tables = ['filter', 'nat', 'mangle']
66 print """
67IP Tables Dump
68===============
69"""
70 for table in tables:
71 print os.popen("sudo iptables --line-numbers -L -nv -t %s"
72 % table).read()
73
74
Sean Dague97fcc7b2014-06-16 17:24:14 -040075def process_list():
76 print """
77Process Listing
78===============
79"""
Dean Troyerbba47422015-03-28 13:37:26 -050080 psraw = os.popen("ps axo user,ppid,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,args").read()
Sean Dague97fcc7b2014-06-16 17:24:14 -040081 print psraw
82
83
84def main():
85 opts = get_options()
86 fname = filename(opts.dir)
87 print "World dumping... see %s for details" % fname
88 sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
89 with open(fname, 'w') as f:
90 os.dup2(f.fileno(), sys.stdout.fileno())
91 disk_space()
92 process_list()
Sean Dague168b7c22015-05-07 08:57:28 -040093 iptables_dump()
Sean Dague97fcc7b2014-06-16 17:24:14 -040094
95
96if __name__ == '__main__':
97 try:
98 sys.exit(main())
99 except KeyboardInterrupt:
100 sys.exit(1)