def cli(config):
    """Compare instance reservations and running instances for AWS services.

    Args:
        config (str): The path to the configuration file.

    """
    current_config = parse_config(config)
    # global results for all accounts
    results = {
        'ec2_classic_running_instances': {},
        'ec2_classic_reserved_instances': {},
        'ec2_vpc_running_instances': {},
        'ec2_vpc_reserved_instances': {},
        'elc_running_instances': {},
        'elc_reserved_instances': {},
        'rds_running_instances': {},
        'rds_reserved_instances': {},
    }
    aws_accounts = current_config['Accounts']

    for aws_account in aws_accounts:
        session = create_boto_session(aws_account)
        results = calculate_ec2_ris(session, results)

        if aws_account['rds'] is True:
            results = calculate_rds_ris(session, results)
        if aws_account['elasticache'] is True:
            results = calculate_elc_ris(session, results)

    report = {}
    report['EC2 Classic'] = report_diffs(
        results['ec2_classic_running_instances'],
        results['ec2_classic_reserved_instances'])
    report['EC2 VPC'] = report_diffs(results['ec2_vpc_running_instances'],
                                     results['ec2_vpc_reserved_instances'])
    report['ElastiCache'] = report_diffs(results['elc_running_instances'],
                                         results['elc_reserved_instances'])
    report['RDS'] = report_diffs(results['rds_running_instances'],
                                 results['rds_reserved_instances'])
    for instance, expirations in reserve_expiry.items():
        for expiration in expirations:
            if int(expiration) <= 7:
                expiring_soon = True
    if report['EC2 VPC']['unused_reservations'] and report['EC2 Classic'][
            'unused_reservations'] and report['ElastiCache'][
                'unused_reservations'] and report['RDS']['unused_reservations']:
        print("No Unused Reservations")
        if expiring_soon:
            print("Sending Report about Expiring Reservations")
            report_results(current_config, report)
    else:
        print("Sending Report about Unused Reservations")
        report_results(current_config, report)
def cli(config):
    """Compare instance reservations and running instances for AWS services.

    Args:
        config (str): The path to the configuration file.

    """
    current_config = parse_config(config)
    # global results for all accounts
    results = {
        'ec2_classic_running_instances': {},
        'ec2_classic_reserved_instances': {},
        'ec2_vpc_running_instances': {},
        'ec2_vpc_reserved_instances': {},
        'elc_running_instances': {},
        'elc_reserved_instances': {},
        'rds_running_instances': {},
        'rds_reserved_instances': {}
    }
    aws_accounts = current_config['Accounts']

    for aws_account in aws_accounts:
        session = create_boto_session(aws_account)
        results = calculate_ec2_ris(session, results)

        if aws_account['rds'] is True:
            results = calculate_rds_ris(session, results)
        if aws_account['elasticache'] is True:
            results = calculate_elc_ris(session, results)

    report = {}
    report['EC2 Classic'] = report_diffs(
        results['ec2_classic_running_instances'],
        results['ec2_classic_reserved_instances'])
    report['EC2 VPC'] = report_diffs(
        results['ec2_vpc_running_instances'],
        results['ec2_vpc_reserved_instances'])
    report['ElastiCache'] = report_diffs(
        results['elc_running_instances'],
        results['elc_reserved_instances'])
    report['RDS'] = report_diffs(
        results['rds_running_instances'],
        results['rds_reserved_instances'])
    report_results(current_config, report)
def cli(config):
    """Compare instance reservations and running instances for AWS services.

    Args:
        config (str): The path to the configuration file.
    """
    current_config = parse_config(config)
    results = {}
    aws_accounts = current_config['Accounts']

    for aws_account in aws_accounts:
        name = aws_account['name']
        results[name] = calculate_ec2_ris(account=aws_account)

        if aws_account['rds'] is True:
            results[name].append(calculate_rds_ris(account=aws_account))
        if aws_account['elasticache'] is True:
            results[name].append(calculate_elc_ris(account=aws_account))
    report_results(current_config, results)
Example #4
0
def cli(config):
    """Compare instance reservations and running instances for AWS services.

    Args:
        config (str): The path to the configuration file.

    """
    current_config = parse_config(config)
    # global results for all accounts
    results = {
        'ec2_classic_running_instances': {},
        'ec2_classic_reserved_instances': {},
        'ec2_vpc_running_instances': {},
        'ec2_vpc_reserved_instances': {},
        'elc_running_instances': {},
        'elc_reserved_instances': {},
        'rds_running_instances': {},
        'rds_reserved_instances': {},
    }
    aws_accounts = current_config['Accounts']

    for aws_account in aws_accounts:
        session = create_boto_session(aws_account)
        results = calculate_ec2_ris(session, results)

        if aws_account['rds'] is True:
            results = calculate_rds_ris(session, results)
        if aws_account['elasticache'] is True:
            results = calculate_elc_ris(session, results)

    report = {}
    report['EC2 Classic'] = report_diffs(
        results['ec2_classic_running_instances'],
        results['ec2_classic_reserved_instances'])
    report['EC2 VPC'] = report_diffs(results['ec2_vpc_running_instances'],
                                     results['ec2_vpc_reserved_instances'])
    report['ElastiCache'] = report_diffs(results['elc_running_instances'],
                                         results['elc_reserved_instances'])
    report['RDS'] = report_diffs(results['rds_running_instances'],
                                 results['rds_reserved_instances'])
    report_results(current_config, report)

    stat_map = {
        'EC2 Classic': 'ec2_classic',
        'EC2 VPC': 'ec2_vpc',
        'ElastiCache': 'elc',
        'RDS': 'rds'
    }

    timestamp = datetime.datetime.utcnow().isoformat()
    stats = {"instances": [], "timestamp": timestamp}

    for k, v in stat_map.items():
        diffs = report_diffs(results["{}_running_instances".format(v)],
                             results["{}_reserved_instances".format(v)])
        stats['instances'].append({
            'timestamp': timestamp,
            'service': "running_{}".format(v),
            'qty': diffs['qty_running_instances']
        })
        stats['instances'].append({
            'timestamp': timestamp,
            'service': "reserved_{}".format(v),
            'qty': diffs['qty_reserved_instances']
        })
        stats['instances'].append({
            'timestamp': timestamp,
            'service': "unreserved_{}".format(v),
            'qty': diffs['qty_unreserved_instances']
        })

    es_node = 'esdata01.use1.tools.ddc.io'
    es_index = 'aws_reservation_stats'
    url = "http://{0}:9200/{1}/stat/".format(es_node, es_index)
    for instance in stats['instances']:
        stat_put = requests.post(url, json=instance)
        if stat_put.status_code != 201:
            print(stat_put.json())