def list(): elb = connect() lbs = elb.get_all_load_balancers() for lb in lbs: print lb.name, lb.dns_name print " ", ", ".join([str(x) for x in lb.instances]) print " ", lb.get_instance_health()
def list_elbs(region): elb = boto.ec2.elb.connect_to_region(region) elb_list = elb.get_all_load_balancers() elb_names = [] for lb in elb_list: elb_names.append(lb.name) return elb_names
def main(): region = REGIONS[opts.region] try: elb = boto.ec2.elb.connect_to_region(region) balancer = elb.get_all_load_balancers( load_balancer_names=[opts.elb])[0] except: die("Could not connect to elb") # instance-idでinstanceを探す。instance-idが指定されていなければ、Nameでinstanceを探す。 if opts.instance_id: try: instance_id = conn.get_all_instances( filters={'instance-id': opts.instance_id})[0].instances[0].id except: die("Could not find instance-id") if opts.debug: print "instance-id : " + instance_id print "instance-name: " + opts.src_name print "dst name : " + opts.dst_name sys.exit() # Go try: conn.create_image(instance_id, opts.dst_name, description=None, no_reboot=opts.no_reboot) except: die("create_imate() is failed.")
def main(): region = REGIONS[opts.region] try: elb = boto.ec2.elb.connect_to_region(region) balancer = elb.get_all_load_balancers(load_balancer_names=[opts.elb])[0] except: die("Could not connect to elb") # instance-idでinstanceを探す。instance-idが指定されていなければ、Nameでinstanceを探す。 if opts.instance_id: try: instance_id = conn.get_all_instances(filters={'instance-id':opts.instance_id})[0].instances[0].id except: die("Could not find instance-id") if opts.debug: print "instance-id : " + instance_id print "instance-name: " + opts.src_name print "dst name : " + opts.dst_name sys.exit() # Go try: conn.create_image(instance_id, opts.dst_name, description=None, no_reboot=opts.no_reboot) except: die("create_imate() is failed.")
def get_installed_elb(): elb = boto.ec2.elb.connect_to_region('us-east-1') new_elb = None for existed_elb in elb.get_all_load_balancers(): if(existed_elb.name=='ELB_demo'): new_elb = existed_elb if(not new_elb): new_elb = create_elb() return new_elb;
def add_to_balancer(self, balancer_name, instance_id): elb = self._connect_elb() balancers = elb.get_all_load_balancers() for balancer in balancers: if balancer.name == balancer_name: print "[INFO ] Adding instance to: %s" %balancer.name balancer.register_instances([instance_id]) return
def emit_elb_metrics(pool, cw, region_name): try: elb = boto.ec2.elb.connect_to_region(region_name=region_name) lbs = [lb.name for lb in elb.get_all_load_balancers()] for name in lbs: for az in AZS: for metric_name, stat in ELB_METRICS.iteritems(): metric = {'name': metric_name, 'stat': stat} pool.spawn(emit_metric_for_loadbalancer, cw, metric, name, region_name, az) except Exception as e: print(e, file=sys.stderr)
def await_at_least_two_healthy(): print "Waiting for at least two healthy instances" elb = boto.ec2.elb.connect_to_region('us-east-1') balancer = elb.get_all_load_balancers(load_balancer_names=['GccExplorerVpc'])[0] while True: healthy = [s for s in balancer.get_instance_health() if s.state == u'InService'] if len(healthy) >= 2: print "Found {} healthy".format(len(healthy)) break print "Only {} healthy...waiting".format(len(healthy)) time.sleep(5) print "Enough healthy instances"
def _get_instance_lbs(self, ec2_elbs=None): """Returns a list of ELBs attached to self.instance_id ec2_elbs: an optional list of elb names that will be used for elb lookup instead of returning what elbs are attached to self.instance_id""" if not ec2_elbs: ec2_elbs = self._get_auto_scaling_group_lbs() try: elb = connect_to_aws(boto.ec2.elb, self.region, **self.aws_connect_params) except (boto.exception.NoAuthHandlerFound, AnsibleAWSError) as e: self.module.fail_json(msg=str(e)) elbs = [] marker = None while True: try: newelbs = elb.get_all_load_balancers(marker=marker) marker = newelbs.next_marker elbs.extend(newelbs) if not marker: break except TypeError: # Older version of boto do not allow for params elbs = elb.get_all_load_balancers() break if ec2_elbs: lbs = sorted(lb for lb in elbs if lb.name in ec2_elbs) else: lbs = [] for lb in elbs: for info in lb.instances: if self.instance_id == info.id: lbs.append(lb) return lbs
def register_instance(load_balancer_name): (access_key, secret_key, token) = get_credentials() instance_id = urllib2.urlopen("http://169.254.169.254/latest/meta-data/instance-id").read() elb = boto.ec2.elb.connect_to_region( region_name=DEFAULT_REGION, aws_access_key_id=access_key, aws_secret_access_key=secret_key, security_token=token ) lbs = elb.get_all_load_balancers(load_balancer_names=[load_balancer_name]) for lb in lbs: lb.register_instances([instance_id])
def main(): ''' Gather and examine details about this node within ELBs ''' args = parse_args() aws_access, aws_secret = get_aws_creds('/root/.aws/credentials') instance_region = get_instance_region() elb = boto.ec2.elb.connect_to_region(instance_region, aws_access_key_id=aws_access, aws_secret_access_key=aws_secret) instance_name = get_instance_name( '/etc/openshift_tools/metric_sender.yaml') ''' Define what instance type this node is, only master/infra are in ELBs ''' if "master" in instance_name: instance_type = "master" if args.verbose: print "Instance %s type is master." % instance_name elif "infra" in instance_name: instance_type = "infra" if args.verbose: print "Instance %s type is infra." % instance_name else: print "%s is not an infra or master node. Nothing to do." exit() ''' Fetch the load balancers and make sure this instance is within them ''' try: elbs = elb.get_all_load_balancers() except: print "Rate limit reached, skipping." exit() instance_id = get_instance_id() instance_missing = 0 for i in elbs: if instance_type in i.name: if not filter(lambda x: x.id == instance_id, i.instances): instance_missing = 1 if args.verbose: print "Instance %s is missing from ELB %s!" % (instance_id, i.name) ''' Now that we know if this instance is missing, feed zabbix ''' mts = MetricSender(verbose=args.verbose, debug=args.debug) mts.add_metric({'openshift.aws.elb.status': instance_missing}) mts.send_metrics()
def find_load_balancers(self, instance_id): """ Returns a list of ELB that the given instance is behind """ elb = self._get_connection() load_balancers = [ lb for lb in elb.get_all_load_balancers() if instance_id in [i.id for i in lb.instances] ] self._logger.info('Found following load balancers: %s', load_balancers) if len(load_balancers) > 1: self._logger.warning('The instance %s is under multiple load balancers: %s', instance_id, load_balancers) return load_balancers
def main(): parser = optparse.OptionParser('Usage: %prog [options]') parser.add_option('-l', '--load-balancer', dest='lb', help='The name of the load balancer to deregister instances from.') (opts, args) = parser.parse_args() if 0 != len(args): parser.print_help() return 1 try: elb = boto.connect_elb() lb = elb.get_all_load_balancers([opts.lb]) if lb is None: raise Error('could not find \'{0}\''.format(opts.lb)) elb.deregister_instances(opts.lb, [i.id for i in lb.instances]) except Error, err: sys.stderr.write('[ERROR] {0}\n'.format(err)) return 1
def __init__(self): ''' Where the magic begins ''' #set inventory self.lbname = os.environ['LBNAME'] #its all the same to here. elb = boto.ec2.elb.connect_to_region('us-west-2') load_balancer = elb.get_all_load_balancers([self.lbname])[0] instance_ids = [instance.id for instance in load_balancer.instances] ec2 = boto.ec2.connect_to_region("us-west-2") reservations = ec2.get_all_instances(instance_ids) tags = [i.tags['Group'] for r in reservations for i in r.instances] first = tags[0] print first #check to see if the current situation is bad. (mixed between group A and B for whatever reason.) for tag in tags: if tag != first: print "The instnaces have become mixed between A and B, be sure to take care of that through the AWS console before continuing."
def get_elbs_for_account(self, account_name): ''' Returns a list of elastic load balancers for all regions in account_name :param account_name: string of the account name :type account_name: string :returns: list of boto load balancer objects :rtype: list ''' creds = self.get_account_api_creds(account_name) ec2 = boto.connect_ec2(creds['access_key'], creds['secret_key']) regions = ec2.get_all_regions() elb_hosts = [] try: for region in regions: elb = boto.ec2.elb.connect_to_region( region.name, aws_access_key_id=creds['access_key'], aws_secret_access_key=creds['secret_key']) for lb in elb.get_all_load_balancers(): elb_hosts.append(lb) return elb_hosts except: raise ASEException('Cannot get ELBs for named account')
def find_elb(my_tag): n = "" all_elb = elb.get_all_load_balancers() for my_elb in all_elb: if re.search(my_tag, str(my_elb), re.IGNORECASE): result[my_elb] = [] inst_health = my_elb.get_instance_health() for i in inst_health: if re.search("InService", str(i)): n = re.search('[a-z]\-([a-z]|[0-9])*', str(i)) result[my_elb].append(n.group(0)) else: n = re.search('[a-z]\-([a-z]|[0-9])*', str(i)) inst_ko.append(n.group(0)) result[my_elb].append(n.group(0)) if result == {} and inst_ko == []: print bcolors.RED + "Pas d'ELB correspondant" + bcolors.ENDC i = 0 for k, v in result.iteritems(): print bcolors.BLUE + str(k) + bcolors.ENDC for i in v: print bcolors.GREEN + str(find_inst_ip(i)) + bcolors.ENDC, print "\n"
def get_elb_instances(wb_ref): elb = boto.ec2.elb.connect_to_region('eu-west-1') balancers = elb.get_all_load_balancers() sheet3 = wb_ref.create_sheet("ELB") #Add Headers sheet3.cell(row=1, column=1, value="NAME") sheet3.cell(row=1, column=2, value="INSTANCES") sheet3.cell(row=1, column=3, value="SCHEME") sheet3.cell(row=1, column=4, value="CREATED_TIME") sheet3.cell(row=1, column=5, value="$$ PER MONTH") i = 2 for balancer in balancers: sheet3.cell(row=i, column=1, value=balancer.name) inst = '' for instance in balancer.instances: inst = instance.id + ',' + inst sheet3.cell(row=i, column=2, value=inst) sheet3.cell(row=i, column=3, value=balancer.scheme) sheet3.cell(row=i, column=4, value=balancer.created_time) sheet3.cell(row=i, column=5, value="=0.028*24*30") i = i + 1
def get_elb_bucket(region,elb_name): elb = boto.ec2.elb.connect_to_region(region) #get CNAME for ELB elb_object = elb.get_all_load_balancers(load_balancer_names=elb_name) if len(elb_object) != 1: print "more than one ELB name matching name" sys.exit() elb_dns_name = elb_object[0].canonical_hosted_zone_name #obtain enabled AZs for ELB enabled_az_count = len(elb_object[0].availability_zones) #obtain backend instance count backend_instance_count = len(elb_object[0].instances) #get ELB log policy elb_log_policy = elb.get_lb_attribute(elb_name,'accessLog') bucket_name = elb_log_policy.s3_bucket_name bucket_prefix = elb_log_policy.s3_bucket_prefix emit_interval = elb_log_policy.emit_interval if not bucket_name: print "there does not appear to be an access log policy enabled on this ELB" sys.exit() if verbose: print "ELB bucket policy found: %s/%s"% (bucket_name, bucket_prefix) #seperates on : for the main function return bucket_name,bucket_prefix,elb_dns_name,enabled_az_count,backend_instance_count,emit_interval
are attached to self.instance_id""" if not ec2_elbs: ec2_elbs = self._get_auto_scaling_group_lbs() try: elb = connect_to_aws(boto.ec2.elb, self.region, **self.aws_connect_params) except (boto.exception.NoAuthHandlerFound, AnsibleAWSError), e: self.module.fail_json(msg=str(e)) elbs = [] marker = None while True: try: newelbs = elb.get_all_load_balancers(marker=marker) marker = newelbs.next_marker elbs.extend(newelbs) if not marker: break except TypeError: # Older version of boto do not allow for params elbs = elb.get_all_load_balancers() break if ec2_elbs: lbs = sorted(lb for lb in elbs if lb.name in ec2_elbs) else: lbs = [] for lb in elbs: for info in lb.instances:
def show_existing_elb(): elb = boto.ec2.elb.connect_to_region('us-east-1') for existed_elb in elb.get_all_load_balancers(): print existed_elb
for elb lookup instead of returning what elbs are attached to self.instance_id""" if not ec2_elbs: ec2_elbs = self._get_auto_scaling_group_lbs() try: elb = connect_to_aws(boto.ec2.elb, self.region, **self.aws_connect_params) except (boto.exception.NoAuthHandlerFound, StandardError), e: self.module.fail_json(msg=str(e)) elbs = [] marker = None while True: try: newelbs = elb.get_all_load_balancers(marker=marker) marker = newelbs.next_marker elbs.extend(newelbs) if not marker: break except TypeError: # Older version of boto do not allow for params elbs = elb.get_all_load_balancers() break if ec2_elbs: lbs = sorted(lb for lb in elbs if lb.name in ec2_elbs) else: lbs = [] for lb in elbs: for info in lb.instances:
# get a list of active stacks stacks = [] try: for stack in cf.describe_stacks(stack_name_or_id=stack_name_or_id): if stack.stack_status not in ('ROLLBACK_COMPLETE') and stack.stack_name.startswith('tcm-'): stacks.append(stack) except BotoServerError, e: # log return None clusters = [] for stack in stacks: cluster = {'stack': stack} for resource in stack.list_resources(): if resource.resource_type == 'AWS::ElasticLoadBalancing::LoadBalancer': cluster['elb'] = elb.get_all_load_balancers(load_balancer_names=[resource.physical_resource_id])[0] elif resource.resource_type == 'AWS::AutoScaling::LaunchConfiguration': kwargs = {'names': [resource.physical_resource_id]} cluster['launch_config'] = autoscale.get_all_launch_configurations(**kwargs)[0] elif resource.resource_type == 'AWS::AutoScaling::AutoScalingGroup': cluster['group'] = autoscale.get_all_groups(names=[resource.physical_resource_id])[0] else: raise Exception("Unkonw resource type '%s'" % resource.resource_type) clusters.append(cluster) # sort list by stack creation time clusters.sort(key=lambda x: x['stack'].creation_time, reverse=True) return clusters
def tag_load_balancers(): """Get Key/Value tags from Instances and apply them to the Instance's Load Balancers""" print 'Gathering information for Load Balancers...' #connect to EC2 and get load balancers client = boto3.client('elb', region_name = region) elb = boto.ec2.elb.connect_to_region(region) lb_conn = elb.get_all_load_balancers() instance_elb_dict = {} instance_tag_dict = {} #create a dict with instanceid/loadbalancer as key/value for b in lb_conn: lb = elb.get_all_load_balancers([b.name])[0] for instance_info in lb.instances: instance_elb_dict [ instance_info.id ] = b.name #connect to EC2 and get instances inst_conn = boto.ec2.connect_to_region(region) reservations = inst_conn.get_all_instances() #create a dict with instanceid/tag as key/value for res in reservations: for inst in res.instances: if key_tag in inst.tags: instance_tag_dict [ inst.id ] = inst.tags[key_tag] #get the instance id from load balancer metadata for b in lb_conn: lb = elb.get_all_load_balancers([b.name])[0] for instance_info in lb.instances: inst_id = instance_info.id #pull matching tags and load balancer info from dicts try: inst_tag = instance_tag_dict[inst_id] inst_elb = instance_elb_dict[inst_id] print 'Tagging Load Balancer... %s' % inst_elb response = client.add_tags( LoadBalancerNames=[ inst_elb, ], Tags=[ { 'Key': key_tag, 'Value': inst_tag }, ] ) except: pass
def register_node(self, instance_id): elb = elb.connect_to_region(self.region) lb = elb.get_all_load_balancers(load_balancer_names=self.app_name) lb.register_instances([instance_id])
def register(instIds, loadBalancerName): lb = elb.get_all_load_balancers(load_balancer_names=[loadBalancerName]) lb[0].register_instances(instIds)
def delete(lbname): "Delete a load balancer. use with caution." elb = connect() lb = elb.get_all_load_balancers(load_balancer_names=[lbname])[0] return lb.delete()
def list(args, config): instance_type = args['--type'] subnet = args['--subnet'] if not args['<env>']: args['<env>'] = configuration.get_all_envs(config) # Launch Instances per env env_tables = {} for env in args['<env>']: e = configuration.get_env(env, config) if not e: puts(colored.red( '"%s" is not defined in your Nyfile' % env)) else: ec2 = connection.create(config) with distraction(): filters = AttrDict() filters.vpc_id = e.vpc if instance_type: filters['tag:ny_type'] = instance_type try: reservations = ec2.get_all_instances(filters=filters) except: puts(colored.red("Unable to get reservations")) reservations = [] # Get the load balancers elb = boto.ec2.elb.ELBConnection( aws_access_key_id=ec2.aws_access_key_id, aws_secret_access_key=ec2.aws_secret_access_key) # Make an instance dict instances_to_lb = {} for lb in elb.get_all_load_balancers(): for instance in lb.instances: instances_to_lb[instance.id] = lb.name # Start building the table t = PrettyTable(['instance_id', 'ip', 'tags', 'lb']) t.align["tags"] = "l" t.align["lb"] = "l" t.align["ip"] = "l" t.hrules = prettytable.ALL for res in reservations: for instance in res.instances: tags = [] for name,value in instance.tags.items(): tags.append('%s => %s' % (name,value,)) t.add_row([ instance.id, instance.private_ip_address, '\n'.join(tags), (instances_to_lb[instance.id] if instance.id in instances_to_lb else ''), ]) env_tables[env] = t for env,table in env_tables.items(): puts('Instances in %s' % env.upper()) puts(str(table)) # The str is required for puts to work
import boto import boto.ec2 import boto.ec2.elb """ conn=boto.ec2.connect_to_region("us-east-2") #conn.run_instances('ami-0cd3dfa4e37921605') rs=conn.get_all_security_groups() for r in rs: print(r) print(rs[17].rules) """ regions = boto.ec2.elb.regions() #print(regions) elb = boto.ec2.elb.connect_to_region('us-east-2') print(elb.get_all_load_balancers())
trace('Waiting for removal...') time.sleep(1) # Main try: trace('\n\n\n----------------------------------------------------') trace('Script started @ ' + str(time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime()))) instance_list = [] new_instance_list = [] options = {'Image' : getImageStatus, 'Instance' : getInstanceStatus, } # Get old images from load balancer load_balancer = elb.get_all_load_balancers([lb_name])[0] for instance_info in load_balancer.instances: if instance_info != None: print instance_info.id instance_list.append(str(instance_info.id)) try: trace(load_balancer) except Exception,e: print e sys.exit(1) # Create AMI from stager image_id = create_AMI() waitAndReport('Image','available',image_id,0)
security_groups=conn.get_all_security_groups() print security_groups security_group=security_groups[2] print security_group ##################### elb boto.ec2 regions = boto.ec2.elb.regions() print regions elb = boto.ec2.elb.connect_to_region(CONNECT_REGION,aws_access_key_id=AWS_ACCESS_KEY,aws_secret_access_key=AWS_SECRET_KEY) # conn = boto.ec2.connect_to_region("us-east-1") list = elb.get_all_load_balancers() print list hc = HealthCheck( interval=15, target='HTTP:8080/upload' ) zones = [CONNECT_AVAILABILITY_ZONE] ports = [(80, 80, 'http'), (8080, 8080, 'http')] lb = elb.create_load_balancer('my-lb', zones, ports) lb.configure_health_check(hc) print lb.dns_name
raise return status def _get_instance_lbs(self, ec2_elbs=None): """Returns a list of ELBs attached to self.instance_id ec2_elbs: an optional list of elb names that will be used for elb lookup instead of returning what elbs are attached to self.instance_id""" try: elb = connect_to_aws(boto.ec2.elb, self.region, **self.aws_connect_params) except (boto.exception.NoAuthHandlerFound, StandardError), e: self.module.fail_json(msg=str(e)) elbs = elb.get_all_load_balancers() if ec2_elbs: lbs = sorted(lb for lb in elbs if lb.name in ec2_elbs) else: lbs = [] for lb in elbs: for info in lb.instances: if self.instance_id == info.id: lbs.append(lb) return lbs def _get_instance(self): """Returns a boto.ec2.InstanceObject for self.instance_id""" try: ec2 = connect_to_aws(boto.ec2, self.region,
import boto.ec2.elb elb = boto.ec2.elb.connect_to_region('us-east-1') balancers = elb.get_all_load_balancers() print(balancers[0]) instance_health = balancers[0].get_instance_health() instances = [state.instance_id for state in instance_health] for i in instances: print i
def get_elb_metrics(access_key_id, secret_access_key, only_latest=True, start_time=None, end_time=None): ''' Retrieves AWS ELB metrics from CloudWatch. @param access_key_id AWS Access Key ID. @param secret_access_key AWS Secret Access Key. @param only_latest True to return only the single latest sample for each metric; False to return all the metrics returned between start_time and end_time. @param start_time The earliest metric time to retrieve (inclusive); defaults to 20 minutes before end_time. @param end_time The latest metric time to retrieve (exclusive); defaults to now. @return A dictionary, in the following format: {(RegionId, LoadBalancerName, MetricName): [(Timestamp, Value, Statistic), (Timestamp, Value, Statistic), ...], (RegionId, LoadBalancerName, MetricName): [(Timestamp, Value, Statistic), (Timestamp, Value, Statistic), ...], ...} That is, the dictionary keys are tuples of the region, load balancer name and metric name, and the dictionary values are lists of tuples of timestamp, value and statistic (TVS). The TVS lists are guaranteed to be sorted in ascending order (latest timestamp last). If only_latest is True, each TVS list is guaranteed to have at most one value (but may have zero if no data is available for the requested range). @note AWS reports metrics in either 60-second or 5-minute intervals, depending on monitoring service level and metric. Keep in mine that even if end_time is now, the latest datapoint returned may be up to 5 minutes in the past. @note The Timestamp value will be for the *beginning* of each period. For example, for a period of 60 seconds, a metric returned with a timestamp of 11:23 will be for the period of [11:23, 11:24); or the period of 11:23:00 through 11:23:59.999. ''' logger = logging.getLogger('get_elb_metrics') # Note: although we want a 60-second period, not all ELB metrics are provided in 60-second # periods, depending on service level and metric. Instead, query the last 20 minutes, and take # the latest period we can get. period = 60 end_time = end_time or datetime.datetime.utcnow() start_time = start_time or (end_time - datetime.timedelta(minutes=20)) out = dict() for region in boto.ec2.elb.regions(): logger.info("Region: %s", region.name) # Some regions are returned that actually do not support EC2. Skip those. if region.name in ['cn-north-1', 'us-gov-west-1']: continue elb = boto.connect_elb(access_key_id, secret_access_key, region=region) cloud_watch = boto.connect_cloudwatch(access_key_id, secret_access_key,region=region) load_balancers = elb.get_all_load_balancers() for lb in load_balancers: logger.info("\tELB: %s" % lb.name) for metric in ELB_METRICS: # AWS ELB Metric Name metric_name = metric[0] # AWS Statistic metric_statistic = metric[1] # Boundary metric identifier metric_name_id = metric[2] logger.info("\t\tELB Metric: %s %s %s" % (metric_name, metric_statistic, metric_name_id)) region = RegionInfo(name=region.name,endpoint="monitoring." + region.name + ".amazonaws.com") cw = boto.connect_cloudwatch(access_key_id, secret_access_key,region=region) data = cw.get_metric_statistics(period=period, start_time=start_time, end_time=end_time, metric_name=metric_name, namespace='AWS/ELB', statistics=metric_statistic, dimensions=dict(LoadBalancerName=lb.name)) if not data: logger.info("\t\t\tNo data") continue if only_latest: # Pick out the latest sample only data = [max(data, key=lambda d: d['Timestamp'])] else: # Output all retrieved samples as a list, sorted by timestamp data = sorted(data, key=lambda d: d['Timestamp']) out_metric = [] for sample in data: logger.info("\t\t\tELB Value: %s: %s" % (sample['Timestamp'], sample[metric_statistic])) out_metric.append((sample['Timestamp'], sample[metric_statistic], metric_statistic, metric_name_id)) out[(region.name, lb.name, metric_name)] = out_metric return out
def get_entities_for_region(self, region): elb = boto.connect_elb(self.access_key_id, self.secret_access_key, region=region) return elb.get_all_load_balancers()