Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #!/usr/bin/python
  2. import sys
  3. import os
  4. import boto3
  5. from pprint import pprint
  6. from functools import reduce
  7.  
  8. # export AWS_ACCESS_KEY_ID=
  9. # export AWS_SECRET_ACCESS_KEY=
  10. # export AWS_DEFAULT_REGION=
  11.  
  12. try:
  13. AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
  14. AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY']
  15. except KeyError:
  16. print("Please set env variable")
  17. sys.exit(1)
  18.  
  19.  
  20. ec2 = boto3.client('ec2')
  21. result = ec2.describe_instances()
  22. reservations = result["Reservations"]
  23.  
  24. running_instances = {}
  25. spot_instances = {}
  26. state_name = []
  27. for reservation in reservations:
  28. for instance in reservation["Instances"]:
  29. if instance["State"]["Name"] not in state_name:
  30. state_name.append(instance["State"]["Name"])
  31. if instance["State"]["Name"] != "running":
  32. sys.stderr.write(
  33. "Disqualifying instance %s: not running\n" % (instance["InstanceId"]))
  34. elif "SpotInstanceRequestId" in instance:
  35. instance_type = instance["InstanceType"]
  36. spot_instances[(instance_type)] = spot_instances.get(
  37. (instance_type), 0) + 1
  38. # sys.stderr.write(
  39. # "Disqualifying instance %s: spot\n" % (instance["InstanceId"]))
  40. else:
  41. instance_type = instance["InstanceType"]
  42. running_instances[(instance_type)] = running_instances.get(
  43. (instance_type), 0) + 1
  44.  
  45.  
  46. # pprint(state_name)
  47. # pprint(running_instances)
  48.  
  49.  
  50. reserved_instances = {}
  51. result = ec2.describe_reserved_instances()
  52. for reserved_instance in result["ReservedInstances"]:
  53. if reserved_instance["State"] != "active":
  54. sys.stderr.write(
  55. "Excluding reserved instances %s: no longer active\n" % (reserved_instance["ReservedInstancesId"]))
  56. else:
  57. instance_type = reserved_instance["InstanceType"]
  58. reserved_instances[(instance_type)] = reserved_instances.get(
  59. (instance_type), 0) + reserved_instance["InstanceCount"]
  60.  
  61. # pprint(reserved_instances)
  62.  
  63.  
  64. # this dict will have a positive number if there are unused reservations
  65. # and negative number if an instance is on demand
  66. instance_diff = dict([(x, reserved_instances[x] -
  67. running_instances.get(x, 0)) for x in reserved_instances])
  68.  
  69. # instance_diff only has the keys that were present in reserved_instances. There's probably a cooler way to add a filtered dict here
  70. for placement_key in running_instances:
  71. if not placement_key in reserved_instances:
  72. instance_diff[placement_key] = -running_instances[placement_key]
  73.  
  74. # pprint(instance_diff)
  75.  
  76. print("")
  77.  
  78. unused_reservations = dict((key, value)
  79. for key, value in instance_diff.items() if value > 0)
  80. if unused_reservations == {}:
  81. print("Congratulations, you have no unused reservations")
  82. else:
  83. for unused_reservation in unused_reservations:
  84. print("UNUSED RESERVATION!\t(%s)\t%s" % (
  85. unused_reservations[unused_reservation], unused_reservation))
  86.  
  87. print("")
  88.  
  89. unreserved_instances = dict((key, -value)
  90. for key, value in instance_diff.items() if value < 0)
  91. if unreserved_instances == {}:
  92. print("Congratulations, you have no unreserved instances")
  93. else:
  94. for unreserved_instance in unreserved_instances:
  95. print("Instance not reserved:\t(%s)\t%s" % (
  96. unreserved_instances[unreserved_instance], unreserved_instance))
  97.  
  98. qty_running_instances = reduce(
  99. lambda x, y: x+y, running_instances.values()) if len(running_instances) > 0 else 0
  100. qty_reserved_instances = reduce(
  101. lambda x, y: x+y, reserved_instances.values()) if len(reserved_instances) > 0 else 0
  102. qty_spot_instances = reduce(
  103. lambda x, y: x+y, spot_instances.values()) if len(spot_instances) > 0 else 0
  104.  
  105. print("")
  106. print("(%s) running non-spot instances" % (qty_running_instances))
  107. print("(%s) reservations" % (qty_reserved_instances))
  108. print("(%s) spots" % (qty_spot_instances))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement