-
Notifications
You must be signed in to change notification settings - Fork 10
/
alb-diagram.py
134 lines (112 loc) · 4.09 KB
/
alb-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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import boto3
import pprint
import sys
import json
from diagrams import Cluster, Diagram
from diagrams.aws.compute import EC2
from diagrams.aws.network import ELB
global alblist
alblist=[]
def gettargetgroups(arn):
tgs=elb.describe_target_groups(LoadBalancerArn=arn)
tgstring=[]
for tg in tgs["TargetGroups"]:
tgstring.append(tg["TargetGroupName"])
return tgstring
def gettargetgrouparns(arn):
tgs=elb.describe_target_groups(LoadBalancerArn=arn)
tgarns=[]
for tg in tgs["TargetGroups"]:
tgarns.append(tg["TargetGroupArn"])
return tgarns
def getinstancename(instanceid):
instances=ec2.describe_instances(Filters=[
{
'Name': 'instance-id',
'Values': [
instanceid
]
},
],)
for instance in instances["Reservations"]:
for inst in instance["Instances"]:
for tag in inst["Tags"]:
if tag['Key'] == 'Name':
return (tag['Value'])
def gettargethealth(arn):
# print("arn",arn)
inss=elb.describe_target_health(TargetGroupArn=arn)
instanceids=[]
result=[]
for ins in inss["TargetHealthDescriptions"]:
ins["Name"]=getinstancename(ins['Target']['Id'])
instanceids.append(ins['Target']['Id'])
result.append(ins)
# print(result)
return result
def describelbs():
lbs = elb.describe_load_balancers(PageSize=400)
for lb in lbs["LoadBalancers"]:
# print("\n"*2)
# print ("-"*6)
lbjson={}
lbjson['Name']=lb["LoadBalancerName"]
lbjson['Type']=lb["Type"]
lbjson['TG']=gettargetgrouparns(lb["LoadBalancerArn"])
lbjson['TGData']=[]
# print("Name:",lb["LoadBalancerName"])
# print("Type:",lb["Type"])
# print("TargetGroups:",gettargetgroups(lb["LoadBalancerArn"]))
# instancelist=[]
TGLIST=[]
for tgs in lbjson['TG']:
TGD={}
TGD['Name']=tgs.split("/")[1]
tgh=gettargethealth(tgs)
# print("tgh",tgh)
if len(tgh) > 0:
# instancelist.append(tgh)
TGD['Instances']=tgh
# lbjson['TG'].append=tgh
else:
TGD['Instances']=""
print("TGD here",TGD)
TGLIST.append(TGD)
print("TGLIST",TGLIST)
lbjson['TGData'] = TGLIST
print("result")
print(json.dumps(lbjson))
with Diagram(profile+"_ALB_"+lbjson['Name'],show=False):
if lbjson['Type'] == "application":
lb = ELB(lbjson['Name'])
for targetgroup in lbjson['TGData']:
print("TGName",targetgroup['Name'])
for instance in targetgroup['Instances']:
instance_group=[]
with Cluster(targetgroup['Name']):
instance_group.append(EC2(instance['Name']))
lb >> instance_group
print(instance_group)
if lbjson['Type'] == "network":
lb = ELB(lbjson['Name'])
for targetgroup in lbjson['TGData']:
print("TGName",targetgroup['Name'])
for instance in targetgroup['Instances']:
instance_group=[]
with Cluster(targetgroup['Name']):
instance_group.append(ELB(instance['Target']['Id'].split("/")[2]))
lb >> instance_group
print(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('elbv2')
ec2 = session.client('ec2')
describelbs()