cluster_instance_monitoring.py 3.77 KB
Newer Older
1 2 3 4
import boto3
import argparse
import sys
import yaml
5
from pprint import pprint
6

7
def find_active_instances(cluster_file, region):
8 9 10 11 12 13 14 15 16
    """
    Determines if a given cluster has at least one ASG and at least one active instance.

    Input: 
    cluster_file: a yaml file containing a dictionary of triples that specify the particular cluster to monitor.
    The keys of each entry in the dictionary are 'env', 'deployment', and 'cluster', specifying the environment, deployment,
        and cluster to find ASG's and active instances for. 

    """
17 18
    with open(cluster_file, 'r') as f:
        cluster_map = yaml.safe_load(f)
19 20 21 22

    asg = boto3.client('autoscaling', region)
    all_groups = asg.describe_auto_scaling_groups()

23 24
    # dictionary that contains the environment/deployment/cluster triple as the key and the value is a list of the asgs that match the triple
    all_matching_asgs = {}
25

26
    # all the triples for which an autoscaling group does not exist 
27 28
    not_matching_triples = []

29
    # check if there exists at least one ASG for each triple
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    for triple in cluster_map:
        #the asgs that match this particular triple
        cluster_asgs = []
        
        for g in all_groups['AutoScalingGroups']:
            match_env = False
            match_deployment = False
            match_cluster = False
            for tag in g['Tags']:
                if tag['Key'] == 'environment' and tag['Value'] == triple['env']:
                    match_env = True
                if tag['Key'] == 'deployment' and tag['Value'] == triple['deployment']:
                    match_deployment = True
                if tag['Key'] == 'cluster' and tag['Value'] == triple['cluster']:
                    match_cluster = True
            if match_env and match_cluster and match_deployment:
                cluster_asgs += [g]
        
        if not cluster_asgs:
            not_matching_triples += [triple]
        else:
51 52
            triple_str = triple['env'] + '-' + triple['deployment'] + '-' + triple['cluster']
            all_matching_asgs[triple_str] = cluster_asgs
53

54 55
    #The triples that have no active instances
    no_active_instances_triples = []
56

57 58 59 60 61 62 63 64 65 66
    #check that each triple has at least one active instance in at least one of its ASG's
    for triple in all_matching_asgs:
        asgs = all_matching_asgs[triple]
        triple_has_active_instances = False
        for asg in asgs:
            for instance in asg['Instances']:
                if instance['LifecycleState'] == 'InService':
                    triple_has_active_instances = True
        if not triple_has_active_instances:
            no_active_instances_triples += [triple]
67 68


69
    if no_active_instances_triples or not_matching_triples:
70 71
        if not_matching_triples:
            print('Fail. There are no autoscaling groups found for the following cluster(s):')
72 73 74 75 76 77 78 79
            pprint(not_matching_triples)
        if no_active_instances_triples:
            print("Fail. There are no active instances for the following cluster(s)")
            for triple in no_active_instances_triples:
                print('environment: ' + triple.split('-')[0])
                print('deployment: ' + triple.split('-')[1])
                print('cluster: ' + triple.split('-')[2])
                print('----')
80 81 82 83 84 85 86 87 88
        sys.exit(1)
    
    print("Success. ASG's with active instances found for all of the cluster triples.")
    sys.exit(0)

    
if __name__=="__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('-f', '--file', help='Yaml file of env/deployment/cluster triples that we want to find active instances for', required=True)
89
    parser.add_argument('-r', '--region', help="Region that we want to find ASG's and active instances in", default='us-east-1', required=True)
90 91
    args = parser.parse_args()

92
    find_active_instances(args.file, args.region)
93