OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2011 Jim Browne http://www.42lines.net |
| 3 # Borrows heavily from boto/bin/list_instances which has no attribution |
| 4 # |
| 5 # Permission is hereby granted, free of charge, to any person obtaining a |
| 6 # copy of this software and associated documentation files (the |
| 7 # "Software"), to deal in the Software without restriction, including |
| 8 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 9 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 10 # persons to whom the Software is furnished to do so, subject to the fol- |
| 11 # lowing conditions: |
| 12 # |
| 13 # The above copyright notice and this permission notice shall be included |
| 14 # in all copies or substantial portions of the Software. |
| 15 # |
| 16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 17 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 18 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 19 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 20 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 22 |
| 23 VERSION="0.1" |
| 24 usage = """%prog [options] |
| 25 Options: |
| 26 -h, --help show help message (including options list) and exit |
| 27 """ |
| 28 |
| 29 from operator import itemgetter |
| 30 |
| 31 HEADERS = { |
| 32 'ID': {'get': itemgetter('id'), 'length':14}, |
| 33 'Zone': {'get': itemgetter('zone'), 'length':14}, |
| 34 'Hostname': {'get': itemgetter('dns'), 'length':20}, |
| 35 'Code': {'get': itemgetter('code'), 'length':18}, |
| 36 'Description': {'get': itemgetter('description'), 'length':30}, |
| 37 'NotBefore': {'get': itemgetter('not_before'), 'length':25}, |
| 38 'NotAfter': {'get': itemgetter('not_after'), 'length':25}, |
| 39 'T:': {'length': 30}, |
| 40 } |
| 41 |
| 42 def get_column(name, event=None): |
| 43 if name.startswith('T:'): |
| 44 return event[name] |
| 45 return HEADERS[name]['get'](event) |
| 46 |
| 47 def list(region, headers, order, completed): |
| 48 """List status events for all instances in a given region""" |
| 49 |
| 50 import re |
| 51 |
| 52 ec2 = boto.connect_ec2(region=region) |
| 53 |
| 54 reservations = ec2.get_all_instances() |
| 55 |
| 56 instanceinfo = {} |
| 57 events = {} |
| 58 |
| 59 displaytags = [ x for x in headers if x.startswith('T:') ] |
| 60 |
| 61 # Collect the tag for every possible instance |
| 62 for res in reservations: |
| 63 for instance in res.instances: |
| 64 iid = instance.id |
| 65 instanceinfo[iid] = {} |
| 66 for tagname in displaytags: |
| 67 _, tag = tagname.split(':', 1) |
| 68 instanceinfo[iid][tagname] = instance.tags.get(tag,'') |
| 69 instanceinfo[iid]['dns'] = instance.public_dns_name |
| 70 |
| 71 stats = ec2.get_all_instance_status() |
| 72 |
| 73 for stat in stats: |
| 74 if stat.events: |
| 75 for event in stat.events: |
| 76 events[stat.id] = {} |
| 77 events[stat.id]['id'] = stat.id |
| 78 events[stat.id]['dns'] = instanceinfo[stat.id]['dns'] |
| 79 events[stat.id]['zone'] = stat.zone |
| 80 for tag in displaytags: |
| 81 events[stat.id][tag] = instanceinfo[stat.id][tag] |
| 82 events[stat.id]['code'] = event.code |
| 83 events[stat.id]['description'] = event.description |
| 84 events[stat.id]['not_before'] = event.not_before |
| 85 events[stat.id]['not_after'] = event.not_after |
| 86 if completed and re.match('^\[Completed\]',event.description): |
| 87 events[stat.id]['not_before'] = 'Completed' |
| 88 events[stat.id]['not_after'] = 'Completed' |
| 89 |
| 90 # Create format string |
| 91 format_string = "" |
| 92 for h in headers: |
| 93 if h.startswith('T:'): |
| 94 format_string += "%%-%ds" % HEADERS['T:']['length'] |
| 95 else: |
| 96 format_string += "%%-%ds" % HEADERS[h]['length'] |
| 97 |
| 98 |
| 99 print format_string % headers |
| 100 print "-" * len(format_string % headers) |
| 101 |
| 102 for instance in sorted(events, |
| 103 key=lambda ev: get_column(order, events[ev])): |
| 104 e = events[instance] |
| 105 print format_string % tuple(get_column(h, e) for h in headers) |
| 106 |
| 107 if __name__ == "__main__": |
| 108 import boto |
| 109 from optparse import OptionParser |
| 110 from boto.ec2 import regions |
| 111 |
| 112 parser = OptionParser(version=VERSION, usage=usage) |
| 113 parser.add_option("-a", "--all", help="check all regions", dest="all", defau
lt=False,action="store_true") |
| 114 parser.add_option("-r", "--region", help="region to check (default us-east-1
)", dest="region", default="us-east-1") |
| 115 parser.add_option("-H", "--headers", help="Set headers (use 'T:tagname' for
including tags)", default=None, action="store", dest="headers", metavar="ID,Zone
,Hostname,Code,Description,NotBefore,NotAfter,T:Name") |
| 116 parser.add_option("-S", "--sort", help="Header for sort order", default=None
, action="store", dest="order",metavar="HeaderName") |
| 117 parser.add_option("-c", "--completed", help="List time fields as \"Completed
\" for completed events (Default: false)", default=False, action="store_true", d
est="completed") |
| 118 |
| 119 (options, args) = parser.parse_args() |
| 120 |
| 121 if options.headers: |
| 122 headers = tuple(options.headers.split(',')) |
| 123 else: |
| 124 headers = ('ID', 'Zone', 'Hostname', 'Code', 'NotBefore', 'NotAfter') |
| 125 |
| 126 if options.order: |
| 127 order = options.order |
| 128 else: |
| 129 order = 'ID' |
| 130 |
| 131 if options.all: |
| 132 for r in regions(): |
| 133 print "Region %s" % r.name |
| 134 list(r, headers, order, options.completed) |
| 135 else: |
| 136 # Connect the region |
| 137 for r in regions(): |
| 138 if r.name == options.region: |
| 139 region = r |
| 140 break |
| 141 else: |
| 142 print "Region %s not found." % options.region |
| 143 sys.exit(1) |
| 144 |
| 145 list(r, headers, order, options.completed) |
OLD | NEW |