-
Notifications
You must be signed in to change notification settings - Fork 10
/
clb-diagram.py
81 lines (67 loc) · 2.47 KB
/
clb-diagram.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
import boto3
import pprint
import json
import sys
from diagrams import Cluster, Diagram
from diagrams.aws.compute import EC2
from diagrams.aws.network import ELB
def getinstancename(instanceid):
instances=ec2.describe_instances(Filters=[
{
'Name': 'instance-id',
'Values': [
instanceid
]
},
],)
resultset = {}
for instance in instances["Reservations"]:
for inst in instance["Instances"]:
resultset["State"]=inst["State"]["Name"]
for tag in inst["Tags"]:
if tag['Key'] == 'Name':
resultset["Name"]=tag['Value']
# print (resultset)
return resultset
def getinstancehealth(lbname,instanceid):
instancestate=elb.describe_instance_health(
LoadBalancerName=lbname,
Instances = [{
'InstanceId' : instanceid
}]
)
return instancestate['InstanceStates'][0]['State']
def describelbs():
lbs = elb.describe_load_balancers(PageSize=400)
for lb in lbs["LoadBalancerDescriptions"]:
lbjson={}
lbjson['Name']=lb["LoadBalancerName"]
lbjson['HealthCheck']=lb["HealthCheck"]
lbjson['Instances']=[]
if len(lb["Instances"]) > 0:
InstanceList=[]
for instance in lb["Instances"]:
instance.update(getinstancename(instance["InstanceId"]))
instance['Health']=getinstancehealth(lb["LoadBalancerName"], instance["InstanceId"])
InstanceList.append(instance)
lbjson['Instances']=InstanceList
print("\n",json.dumps(lbjson, indent=4, sort_keys=True))
with Diagram(profile+"_CLB_"+lbjson['Name'],show=False):
lb = ELB(lbjson['Name'])
instance_group=[]
for instance in lbjson['Instances']:
instance_group.append(EC2(instance['Name']))
lb >> instance_group
if __name__ == "__main__":
if len(sys.argv) < 3:
print("-- Region Name and the Profile name is mandatory --")
print(" Syntax: python3 ",sys.argv[0]," us-east-1 default")
exit()
region_name = sys.argv[1]
profile = sys.argv[2]
print("profilename selected:",profile)
print("regionname selected: ",region_name)
session = boto3.session.Session(profile_name=profile)
elb = session.client('elb')
ec2 = session.client('ec2')
describelbs()