Last active
March 20, 2020 09:21
-
-
Save MJ111/6d9204535b75ca506064c9ffc0cf0df4 to your computer and use it in GitHub Desktop.
Analyze clients(aws) when redis connection getting exhausted
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# find which aws instances have consumed connections | |
import boto3 | |
ec2 = boto3.resource('ec2') | |
running_instances = ec2.instances.filter(Filters=[{ | |
'Name': 'instance-state-name', | |
'Values': ['running']}]) | |
ec2info = dict() | |
for instance in running_instances: | |
if instance.tags: | |
for tag in instance.tags: | |
if 'Name' in tag['Key']: | |
name = tag['Value'] | |
# Add instance info to a dictionary | |
ec2info[instance.private_ip_address] = { | |
'Id': instance.id, | |
'Name': name, | |
'Type': instance.instance_type, | |
'State': instance.state['Name'], | |
'Private IP': instance.private_ip_address, | |
'Public IP': instance.public_ip_address, | |
'Launch Time': instance.launch_time | |
} | |
ip_cnt = dict() | |
# redis-cli -h hostname client list >> client_list.txt | |
f = open('./client_list.txt', 'r') | |
lines = f.readlines() | |
for l in lines: | |
ip = l.split()[1][5:].split(':')[0] | |
cnt = ip_cnt.get(ip, 0) | |
ip_cnt.update({ip: cnt+1}) | |
# group ip by instance.tag.Name | |
keys = frozenset(ip_cnt.keys()) | |
for key in keys: | |
if key in ec2info: | |
name = ec2info.get(key).get('Name') | |
cnt = ip_cnt.get(name, 0) | |
ip_cnt[name] = ip_cnt[key] + cnt | |
del ip_cnt[key] | |
ip_cnt = {k: v for k, v in sorted(ip_cnt.items(), key=lambda item: -item[1])} | |
_sum=0 | |
for k, v in ip_cnt.items(): | |
print('service - ' + k + ' : ' + str(v)) | |
_sum += v | |
print('connection sum: ' + str(_sum)) | |
""" | |
output example: | |
service - abc : 15 | |
service - eee : 10 | |
service - cdf : 5 | |
connection sum: 30 | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment