Exemplo n.º 1
0
def create_autoscaling_policy(autoscale, cluster_name, opts):
    scale_up_policy = ScalingPolicy(
        name='scale_up', adjustment_type='ChangeInCapacity',
        as_name=cluster_name + "-ag", scaling_adjustment=5, cooldown=60)
    scale_down_policy = ScalingPolicy(
        name='scale_down', adjustment_type='ChangeInCapacity',
        as_name=cluster_name + "-ag", scaling_adjustment=-1, cooldown=60)
    autoscale.create_scaling_policy(scale_up_policy)
    autoscale.create_scaling_policy(scale_down_policy)
    scale_up_policy = autoscale.get_all_policies(
        as_group=cluster_name + "-ag", policy_names=['scale_up'])[0]
    scale_down_policy = autoscale.get_all_policies(
        as_group=cluster_name + "-ag", policy_names=['scale_down'])[0]
    alarm_dimensions = {"AutoScalingGroupName": cluster_name + "-ag"}
    cloudwatch = boto.ec2.cloudwatch.connect_to_region(opts.region)
    scale_up_alarm = MetricAlarm(
        name='scale_up_on_cpu', namespace='AWS/EC2',
        metric='CPUUtilization', statistic='Average',
        comparison='>', threshold='50',
        period='60', evaluation_periods=1,
        alarm_actions=[scale_up_policy.policy_arn],
        dimensions=alarm_dimensions)
    cloudwatch.create_alarm(scale_up_alarm)
    scale_down_alarm = MetricAlarm(
        name='scale_down_on_cpu', namespace='AWS/EC2',
        metric='CPUUtilization', statistic='Average',
        comparison='<', threshold='40',
        period='60', evaluation_periods=1,
        alarm_actions=[scale_down_policy.policy_arn],
        dimensions=alarm_dimensions)
    cloudwatch.create_alarm(scale_down_alarm)
Exemplo n.º 2
0
def create_autoscaling_policy(autoscale, cluster_name, opts):
    scale_up_policy = ScalingPolicy(
        name='scale_up',
        adjustment_type='ChangeInCapacity',
        as_name=cluster_name + "-ag",
        scaling_adjustment=opts.scale_up_nodes_amount,
        cooldown=opts.scale_up_cooldown)
    scale_down_policy = ScalingPolicy(
        name='scale_down',
        adjustment_type='ChangeInCapacity',
        as_name=cluster_name + "-ag",
        scaling_adjustment=-opts.scale_down_nodes_amount,
        cooldown=opts.scale_down_cooldown)
    autoscale.create_scaling_policy(scale_up_policy)
    autoscale.create_scaling_policy(scale_down_policy)
    scale_up_policy = autoscale.get_all_policies(as_group=cluster_name + "-ag",
                                                 policy_names=['scale_up'])[0]
    scale_down_policy = autoscale.get_all_policies(
        as_group=cluster_name + "-ag", policy_names=['scale_down'])[0]
    alarm_dimensions = {"AutoScalingGroupName": cluster_name + "-ag"}
    cloudwatch = boto.ec2.cloudwatch.connect_to_region(opts.region)
    scale_up_alarm = MetricAlarm(name='scale_up_on_cpu',
                                 namespace='AWS/EC2',
                                 metric='CPUUtilization',
                                 statistic='Average',
                                 comparison='>',
                                 threshold='50',
                                 period='60',
                                 evaluation_periods=1,
                                 alarm_actions=[scale_up_policy.policy_arn],
                                 dimensions=alarm_dimensions)
    cloudwatch.create_alarm(scale_up_alarm)
    scale_down_alarm = MetricAlarm(
        name='scale_down_on_cpu',
        namespace='AWS/EC2',
        metric='CPUUtilization',
        statistic='Average',
        comparison='<',
        threshold='40',
        period='60',
        evaluation_periods=1,
        alarm_actions=[scale_down_policy.policy_arn],
        dimensions=alarm_dimensions)
    cloudwatch.create_alarm(scale_down_alarm)
Exemplo n.º 3
0
autoscale=boto.ec2.autoscale.connect_to_region('us-east-1') #the client
#lc
lc=LaunchConfiguration(name='jianLaunchConfig',image_id='ami-3c8f3a54',key_name='jj',security_groups=['http'],instance_type='m3.medium',instance_monitoring=True)
autoscale.create_launch_configuration(lc)
print 'launch cofig created'
#ag
ag=AutoScalingGroup(group_name='jianGroup',load_balancers=['jianLb'],availability_zones=['us-east-1a'],launch_config=lc,min_size=2,max_size=4,connection=autoscale)
autoscale.create_auto_scaling_group(ag)
ag.put_notification_configuration(topic="arn:aws:sns:us-east-1:683895670525:launch",notification_types=['autoscaling:EC2_INSTANCE_LAUNCH', 'autoscaling:EC2_INSTANCE_LAUNCH_ERROR'])
ag.put_notification_configuration(topic="arn:aws:sns:us-east-1:683895670525:terminate",notification_types=['autoscaling:EC2_INSTANCE_TERMINATE','autoscaling:EC2_INSTANCE_TERMINATE_ERROR'])
print 'aotuscaling group createc'
#scaling policy
scale_up_policy=ScalingPolicy(name='jianScaleUp',adjustment_type='ChangeInCapacity',as_name='jianGroup',scaling_adjustment=2,cooldown=60)
scale_down_policy=ScalingPolicy(name='jianScaleDown',adjustment_type='ChangeInCapacity',as_name='jianGroup',scaling_adjustment=-2,cooldown=120)
#submit to aws
autoscale.create_scaling_policy(scale_up_policy)
autoscale.create_scaling_policy(scale_down_policy)
#request back again
scale_up_policy=autoscale.get_all_policies(as_group='jianGroup',policy_names=['jianScaleUp'])[0]
scale_down_policy=autoscale.get_all_policies(as_group='jianGroup',policy_names=['jianScaleDown'])[0]
print 'scaling policy created'

##cloud watch
cloudwatch=boto.ec2.cloudwatch.connect_to_region('us-east-1')
alarm_dimensions={"AutoScalingGroupName":'jianGroup'}
#scale up
scale_up_alarm=MetricAlarm(name='scale_up_on_cpu',namespace='AWS/EC2',metric='CPUUtilization',statistic='Average',comparison='>',threshold='80',period='60',evaluation_periods=1,alarm_actions=[scale_up_policy.policy_arn],dimensions=alarm_dimensions)
cloudwatch.create_alarm(scale_up_alarm)
#scale down
scale_down_alarm = MetricAlarm(name='scale_down_on_cpu', namespace='AWS/EC2',metric='CPUUtilization', statistic='Average',comparison='<', threshold='60',period='60', evaluation_periods=1,alarm_actions=[scale_down_policy.policy_arn],dimensions=alarm_dimensions)
cloudwatch.create_alarm(scale_down_alarm)
Exemplo n.º 4
0
def main():
    parser = optparse.OptionParser('Usage: %prog [options]')
    parser.add_option('-n', '--name', dest='name',
        help='The name of this configuration (e.g., TEST).')
    parser.add_option('-i', '--image', dest='image', default=Defaults.IMAGE,
        help='The Amazon  Machine  Image (AMI) ID that will be used to launch '
             'EC2 instances. The most recent Amazon Linux AMI 2013.09.2 (ami-'
             'a43909e1) is used by default.')
    parser.add_option('-t', '--type', dest='type', default=Defaults.TYPE,
        help='The type of the Amazon EC2 instance. If not specified, micro '
             'instance (t1.micro) type will be used.')
    parser.add_option('-k', '--key', dest='key',
        help='The name of the key pair to use when creating EC2 instances. '
             'This options is required.')
    parser.add_option('-g', '--group', dest='group',
        help='Security group that will be used when creating EC2 instances. '
             'This option is required.')
    parser.add_option('-m', '--min', dest='min', default=Defaults.MIN_INSTANCES,
        help='The minimum number of EC2 instances in the auto scaling group. '
             'By default it is set to 2.')
    parser.add_option('-M', '--max', dest='max', default=Defaults.MAX_INSTANCES,
        help='The maximum size of the auto scaling group. By default it is '
             'set to 4.')
    parser.add_option('-z', '--zone', dest='zones', action='append',
        help='The availability zone for the auto scaling group. This option '
             'is required.')
    parser.add_option('-l', '--load-balancer', dest='lbs', action='append',
        help='The name of an existing AWS load balancer to use, if any.')
    parser.add_option('--min-threshold', dest='min_threshold',
        default=Defaults.MIN_THRESHOLD, help='The minimum CPU utilization '
        'threshold that triggers an alarm. This option is not required and '
        'is set to 40% by default.')
    parser.add_option('--max-threshold', dest='max_threshold',
        default=Defaults.MAX_THRESHOLD, help='The maximum CPU utilization '
        'threshold that triggers an alarm. This option is not required and '
        'is set to 60% by default.')
    parser.add_option('-a', '--adjustment', dest='adjustment',
        default=Defaults.ADJUSTMENT, help='The number of EC2 instances by '
        'which to scale up or down. This is set to 1 by default.')
    parser.add_option('-p', '--period', dest='period', default=Defaults.PERIOD,
        help='The evaluation period in seconds. This is optional and is set '
             'to 300 seconds by default.')
    (opts, args) = parser.parse_args()

    if (0 != len(args) or
        opts.name is None or
        opts.key is None or
        opts.group is None or
        opts.zones is None):
        parser.print_help()
        return 1

    try:
        c = boto.connect_autoscale()

        lc = _create_launch_configuration(c, opts)
        g = _create_autoscaling_group(c, lc, opts)

        policy_up = ScalingPolicy(name=opts.name + s.POLICY_UP_SUFFIX,
            as_name=g.name,
            scaling_adjustment=opts.adjustment,
            adjustment_type='ChangeInCapacity')
        c.create_scaling_policy(policy_up)

        cloudwatch = boto.connect_cloudwatch()

        alarm_high = MetricAlarm(name=opts.name + s.ALARM_HIGH_SUFFIX,
            alarm_actions=[policy_up],
            metric='CPUUtilization',
            namespace='AWS/EC2',
            statistic='Average',
            dimensions={'AutoScalingGroupName': g.name},
            period=opts.period,
            evaluation_periods=1,
            threshold=int(opts.max_threshold),
            comparison='>')
        cloudwatch.create_alarm(alarm_high)

        policy_down = ScalingPolicy(name=opts.name + s.POLICY_DOWN_SUFFIX,
            as_name=g.name,
            scaling_adjustment=-opts.adjustment,
            adjustment_type='ChangeInCapacity')
        autoscale.create_scaling_policy(policy_down)

        alarm_low = MetricAlarm(name=opts.name + s.ALARM_LOW_SUFFIX,
            alarm_actions=[policy_down],
            metric='CPUUtilization',
            namespace='AWS/EC2',
            statistic='Average',
            dimensions={'AutoScalingGroupName': g.name},
            period=opts.period,
            evaluation_periods=1,
            threshold=int(opts.min_threshold),
            comparison='<')
        cloudwatch.create_alarm(alarm_low)
    except Error, err:
        sys.stderr.write('[ERROR] {0}\n'.format(err))
        return 1