-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathec2_instances.py
executable file
·98 lines (78 loc) · 2.89 KB
/
ec2_instances.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python2
import sys
import datetime
import argparse
from ec2_watchdata import WatchData
def main():
global configuration
WatchData.stats_period = 60 # Show just last measure
data = WatchData(configuration.group)
data.connect()
data.get_instances_info()
""" Check if we must change the desired instances """
if configuration.instances > 0:
desired = configuration.instances
if desired > 0 and abs(data.instances - desired) < 3:
data.set_desired(desired)
else:
print "You can specify up to +-2 instances more of currently running (%d)" % (
data.instances, )
exit(0)
if configuration.kill:
if configuration.kill in [x for x in data.instances_info]:
data.kill_instance(configuration.kill, True)
else:
print "Instance", configuration.kill, "doesn't exist"
exit(0)
data.get_CPU_loads(1)
if 'LaunchConfigurationName' in data.group:
launch = data.group['LaunchConfigurationName']
elif 'LaunchTemplate' in data.group and 'LaunchTemplateName' in data.group['LaunchTemplate']:
launch = data.group['LaunchTemplate']['LaunchTemplateName']
else:
launch = 'unknown'
print "Group values: instances: %d min: %d max: %d desired: %d Launch config: %s" % (
data.instances, data.min_size, data.max_size,
data.desired, launch)
for i, d in data.instances_info.iteritems():
if i in data.loads:
load = data.loads[i]
print "%s %5.2f%% %s %s" % (i, load, d['State']['Name'], d['ImageId']),
else:
load = "N/A"
print "%s N/A %s %s" % (i, d['State']['Name'], d['ImageId']),
try:
print "%s %s %-15s" % ( d['InstanceType'], d['Placement']['AvailabilityZone'],
d['NetworkInterfaces'][0]['PrivateIpAddress']),
except:
pass
if configuration.all:
try:
print "%-15s" % (
d['NetworkInterfaces'][0]['PrivateDnsName'])
except:
pass
else:
print
print "Average load: %5.2f" % (data.avg_load, )
if data.instances > 1:
print "Average load with %d instances: %5.2f" % (
data.instances - 1, data.total_load / (data.instances - 1))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"--group", "-g", default="web", help="AutoScaler group")
parser.add_argument(
"--all",
"-a",
action="store_true",
help="Show more info for every instance")
group = parser.add_mutually_exclusive_group()
group.add_argument("--kill", "-k", help="Kill instance")
group.add_argument(
"--instances",
"-i",
type=int,
help="Set the number of desired instances")
configuration = parser.parse_args()
main()