You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
5.7 KiB

4 years ago
  1. #!/home/alpcentaur/ProjektA/PrototypeWebApp/venv/bin/python3.5
  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. VERSION="0.1"
  23. usage = """%prog [options]
  24. Options:
  25. -h, --help show help message (including options list) and exit
  26. """
  27. from operator import itemgetter
  28. HEADERS = {
  29. 'ID': {'get': itemgetter('id'), 'length':14},
  30. 'Zone': {'get': itemgetter('zone'), 'length':14},
  31. 'Hostname': {'get': itemgetter('dns'), 'length':20},
  32. 'Code': {'get': itemgetter('code'), 'length':18},
  33. 'Description': {'get': itemgetter('description'), 'length':30},
  34. 'NotBefore': {'get': itemgetter('not_before'), 'length':25},
  35. 'NotAfter': {'get': itemgetter('not_after'), 'length':25},
  36. 'T:': {'length': 30},
  37. }
  38. def get_column(name, event=None):
  39. if name.startswith('T:'):
  40. return event[name]
  41. return HEADERS[name]['get'](event)
  42. def list(region, headers, order, completed):
  43. """List status events for all instances in a given region"""
  44. import re
  45. ec2 = boto.connect_ec2(region=region)
  46. reservations = ec2.get_all_reservations()
  47. instanceinfo = {}
  48. events = {}
  49. displaytags = [ x for x in headers if x.startswith('T:') ]
  50. # Collect the tag for every possible instance
  51. for res in reservations:
  52. for instance in res.instances:
  53. iid = instance.id
  54. instanceinfo[iid] = {}
  55. for tagname in displaytags:
  56. _, tag = tagname.split(':', 1)
  57. instanceinfo[iid][tagname] = instance.tags.get(tag,'')
  58. instanceinfo[iid]['dns'] = instance.public_dns_name
  59. stats = ec2.get_all_instance_status()
  60. for stat in stats:
  61. if stat.events:
  62. for event in stat.events:
  63. events[stat.id] = {}
  64. events[stat.id]['id'] = stat.id
  65. events[stat.id]['dns'] = instanceinfo[stat.id]['dns']
  66. events[stat.id]['zone'] = stat.zone
  67. for tag in displaytags:
  68. events[stat.id][tag] = instanceinfo[stat.id][tag]
  69. events[stat.id]['code'] = event.code
  70. events[stat.id]['description'] = event.description
  71. events[stat.id]['not_before'] = event.not_before
  72. events[stat.id]['not_after'] = event.not_after
  73. if completed and re.match('^\[Completed\]',event.description):
  74. events[stat.id]['not_before'] = 'Completed'
  75. events[stat.id]['not_after'] = 'Completed'
  76. # Create format string
  77. format_string = ""
  78. for h in headers:
  79. if h.startswith('T:'):
  80. format_string += "%%-%ds" % HEADERS['T:']['length']
  81. else:
  82. format_string += "%%-%ds" % HEADERS[h]['length']
  83. print format_string % headers
  84. print "-" * len(format_string % headers)
  85. for instance in sorted(events,
  86. key=lambda ev: get_column(order, events[ev])):
  87. e = events[instance]
  88. print format_string % tuple(get_column(h, e) for h in headers)
  89. if __name__ == "__main__":
  90. import boto
  91. from optparse import OptionParser
  92. from boto.ec2 import regions
  93. parser = OptionParser(version=VERSION, usage=usage)
  94. parser.add_option("-a", "--all", help="check all regions", dest="all", default=False,action="store_true")
  95. parser.add_option("-r", "--region", help="region to check (default us-east-1)", dest="region", default="us-east-1")
  96. 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")
  97. parser.add_option("-S", "--sort", help="Header for sort order", default=None, action="store", dest="order",metavar="HeaderName")
  98. parser.add_option("-c", "--completed", help="List time fields as \"Completed\" for completed events (Default: false)", default=False, action="store_true", dest="completed")
  99. (options, args) = parser.parse_args()
  100. if options.headers:
  101. headers = tuple(options.headers.split(','))
  102. else:
  103. headers = ('ID', 'Zone', 'Hostname', 'Code', 'NotBefore', 'NotAfter')
  104. if options.order:
  105. order = options.order
  106. else:
  107. order = 'ID'
  108. if options.all:
  109. for r in regions():
  110. print "Region %s" % r.name
  111. list(r, headers, order, options.completed)
  112. else:
  113. # Connect the region
  114. for r in regions():
  115. if r.name == options.region:
  116. region = r
  117. break
  118. else:
  119. print "Region %s not found." % options.region
  120. sys.exit(1)
  121. list(r, headers, order, options.completed)