def get_notraffic_elb(elb_id, session, warning, delt): no_traffic = "False" Now = datetime.datetime.now() warningdays = Now - datetime.timedelta(days=warning) deletedays = Now - datetime.timedelta(days=delt) aws_zone = config["aws"]["awszone"] our_zone = config["system"]["ourzone"] NowUtc = convertTime(Now, our_zone, aws_zone) warningUtc = convertTime(warningdays, our_zone, aws_zone) deletedaysUtc = convertTime(deletedays, our_zone, aws_zone) NowUtcTime = NowUtc.strftime("%Y-%m-%dT%H:%M:%S") warningUtcTime = deletedaysUtc.strftime("%Y-%m-%dT%H:%M:%S") deletedaysUtcTime = deletedaysUtc.strftime("%Y-%m-%dT%H:%M:%S") cloudwatch = session.client("cloudwatch") deltmetric = cloudwatch.get_metric_statistics( Namespace=config["elb"]["namespace"], MetricName=config["elb"]["metericname"], Dimensions=[{"Name": config["elb"]["dimensions"], "Value": elb_id}], StartTime=deletedaysUtcTime, EndTime=NowUtcTime, Period=int(config["elb"]["period"]), Statistics=config["elb"]["statistics"], ) if not deltmetric["Datapoints"]: no_traffic = "True" else: warningmetric = cloudwatch.get_metric_statistics( Namespace=config["elb"]["namespace"], MetricName=config["elb"]["metericname"], Dimensions=[{"Name": config["elb"]["dimensions"], "Value": elb_id}], StartTime=warningUtcTime, EndTime=NowUtcTime, Period=int(config["elb"]["period"]), Statistics=config["elb"]["statistics"], ) if not warningmetric: no_traffic = "Warning" return no_traffic
import datetime from boto3.session import Session from lib.assumerole import getkey with open('conf/config.json') as config_file: config = json.load(config_file) fmt = '%Y-%m-%d %H:%M:%S' Now = datetime.datetime.now() deletedays =Now - datetime.timedelta(days=config["rds"]["deletedays"]) aws_zone = config['aws']['awszone'] our_zone = config['system']['ourzone'] NowUtc=convertTime(Now,our_zone,aws_zone) deletedaysUtc=convertTime(deletedays,our_zone,aws_zone) NowUtcTime=NowUtc.strftime("%Y-%m-%dT%H:%M:%S") deletedaysUtcTime=deletedaysUtc.strftime("%Y-%m-%dT%H:%M:%S") ##get keys accounts_IDs = config['aws']['accounts'] for accountid in accounts_IDs: acct_module = accounts() account_info=acct_module.getaccount(accountid) print "Account Name : {0} Env: {1}".format(account_info["name"],account_info["env"]) if accountid == config["aws"]["mainaccount"]:
last_starttime_us=instance["LaunchTime"] if instance.get("Tags"): for tag in instance["Tags"]: if tag["Key"] == "Name": InstanceInfo["Name"] = tag["Value"] if tag["Key"] == "Preserve": InstanceInfo["Preserve"] = tag["Value"] matchdate=re.search(r'\d{4}-\d{2}-\d{2}.\d{2}:\d{2}:\d{2}',instance["StateTransitionReason"]) if matchdate: date_shutdown_str = matchdate.group() date_shutdowndate_utc=datetime.datetime.strptime(date_shutdown_str, fmt) date_shutdown=convertTime(date_shutdowndate_utc,config["aws"]["awszone"],config["system"]["ourzone"]) if InstanceInfo["Preserve"] in PreventValue: InstanceInfo["Time"]=date_shutdown preserveList.append(InstanceInfo) elif date_shutdown < daletedays_ourzone: InstanceInfo["Time"]=date_shutdown termiList.append(InstanceInfo) elif date_shutdown < warningdays_ourzone: InstanceInfo["Time"]=date_shutdown warningList.append(InstanceInfo) SortedPreserveList=sorted(preserveList, key=lambda k: k['Name']) SortedtermiList=sorted(termiList, key=lambda k: k['Name']) SortedwarningList=sorted(warningList, key=lambda k: k['Name']) if SortedwarningList: print "=============Warming List================"