| 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 | 
 | 21 | import os | 
 | 22 | import os.path | 
 | 23 | import sys | 
 | 24 |  | 
 | 25 |  | 
 | 26 | def 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 |  | 
 | 35 | def 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 |  | 
 | 40 | def warn(msg): | 
 | 41 |     print "WARN: %s" % msg | 
 | 42 |  | 
 | 43 |  | 
 | 44 | def disk_space(): | 
 | 45 |     # the df output | 
 | 46 |     print """ | 
 | 47 | File 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 |  | 
 | 64 | def process_list(): | 
 | 65 |     print """ | 
 | 66 | Process Listing | 
 | 67 | =============== | 
 | 68 | """ | 
 | 69 |     psraw = os.popen("ps auxw").read() | 
 | 70 |     print psraw | 
 | 71 |  | 
 | 72 |  | 
 | 73 | def main(): | 
 | 74 |     opts = get_options() | 
 | 75 |     fname = filename(opts.dir) | 
 | 76 |     print "World dumping... see %s for details" % fname | 
 | 77 |     sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) | 
 | 78 |     with open(fname, 'w') as f: | 
 | 79 |         os.dup2(f.fileno(), sys.stdout.fileno()) | 
 | 80 |         disk_space() | 
 | 81 |         process_list() | 
 | 82 |  | 
 | 83 |  | 
 | 84 | if __name__ == '__main__': | 
 | 85 |     try: | 
 | 86 |         sys.exit(main()) | 
 | 87 |     except KeyboardInterrupt: | 
 | 88 |         sys.exit(1) |