Example usage for com.amazonaws.services.cloudwatch.model ComparisonOperator GreaterThanThreshold

List of usage examples for com.amazonaws.services.cloudwatch.model ComparisonOperator GreaterThanThreshold

Introduction

In this page you can find the example usage for com.amazonaws.services.cloudwatch.model ComparisonOperator GreaterThanThreshold.

Prototype

ComparisonOperator GreaterThanThreshold

To view the source code for com.amazonaws.services.cloudwatch.model ComparisonOperator GreaterThanThreshold.

Click Source Link

Usage

From source file:aws.example.cloudwatch.PutMetricAlarm.java

License:Open Source License

public static void main(String[] args) {

    final String USAGE = "To run this example, supply an alarm name and instance id\n"
            + "Ex: DeleteAlarm <alarm-name> <instance-id>\n";

    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);/*from   w  w  w .  j  a va 2 s  .c om*/
    }

    String alarmName = args[0];
    String instanceId = args[1];

    final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient();

    Dimension dimension = new Dimension().withName("InstanceId").withValue(instanceId);

    PutMetricAlarmRequest request = new PutMetricAlarmRequest().withAlarmName(alarmName)
            .withComparisonOperator(ComparisonOperator.GreaterThanThreshold).withEvaluationPeriods(1)
            .withMetricName("CPUUtilization").withNamespace("AWS/EC2").withPeriod(60)
            .withStatistic(Statistic.Average).withThreshold(70.0).withActionsEnabled(false)
            .withAlarmDescription("Alarm when server CPU utilization exceeds 70%")
            .withUnit(StandardUnit.Seconds).withDimensions(dimension);

    PutMetricAlarmResult response = cw.putMetricAlarm(request);

    System.out.printf("Successfully created alarm with name %s", alarmName);

}

From source file:cloudwatch.src.main.java.aws.example.cloudwatch.PutMetricAlarm.java

License:Open Source License

public static void main(String[] args) {

    final String USAGE = "To run this example, supply an alarm name and instance id\n"
            + "Ex: DeleteAlarm <alarm-name> <instance-id>\n";

    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);// w  ww . j  a  v  a2 s .c o m
    }

    String alarmName = args[0];
    String instanceId = args[1];

    final AmazonCloudWatch cloudWatch = AmazonCloudWatchClientBuilder.defaultClient();

    Dimension dimension = new Dimension().withName("InstanceId").withValue(instanceId);

    PutMetricAlarmRequest request = new PutMetricAlarmRequest().withAlarmName(alarmName)
            .withComparisonOperator(ComparisonOperator.GreaterThanThreshold).withEvaluationPeriods(1)
            .withMetricName("CPUUtilization").withNamespace("AWS/EC2").withPeriod(60)
            .withStatistic(Statistic.Average).withThreshold(70.0).withActionsEnabled(false)
            .withAlarmDescription("Alarm when server CPU utilization exceeds 70%")
            .withUnit(StandardUnit.Seconds).withDimensions(dimension);

    PutMetricAlarmResult response = cloudWatch.putMetricAlarm(request);

    System.out.printf("Successfully created alarm with name %s", alarmName);

}

From source file:de.tuhrig.deployman.launch.Launcher.java

/**
 * Starts a auto scaling launch configuration. This method is a little but complicated, since the
 * process to start a auto scaling configuration involves some steps: - create a launch
 * configuration which defines the machines to start - create an auto scaling definition which
 * defines how many machines should be started if something happens. - create an auto scaling
 * policy to which we add a metric - create the metric which is checked by the auto scaling -
 * print the starting instances/*from  w  w w  .  j  a  v  a2 s.  co m*/
 */
private List<Instance> runAutoScalingLaunch(Formation formation) {
    this.console.write("Run auto scaling...");

    Machine machine = formation.getMachine();
    Scaling scaling = machine.getScaling();

    AmazonAutoScalingClient autoScaling = new AutoScaling().getClient();

    try {
        CreateLaunchConfigurationRequest request = createLaunchConfigurationRequest(formation);
        autoScaling.createLaunchConfiguration(request);
        this.console.write("Created launch configuration " + scaling.getName());
    } catch (AlreadyExistsException e) {
        this.console.write("Launch configuration " + scaling.getName() + " already exists"); //$NON-NLS-2$
    }

    //
    //
    //

    try {
        CreateAutoScalingGroupRequest request = createAutoScalingRequest(scaling);
        autoScaling.createAutoScalingGroup(request);
        this.console.write("Created auto scaling group " + scaling.getGroup());
    } catch (AlreadyExistsException e) {
        this.console.write("Auto scaling group " + scaling.getGroup() + " already exists"); //$NON-NLS-2$
    }

    //
    //
    //

    PutScalingPolicyRequest request = new PutScalingPolicyRequest().withAutoScalingGroupName(scaling.getGroup())
            .withPolicyName(scaling.getPolicy()).withScalingAdjustment(1)
            .withAdjustmentType("ChangeInCapacity");

    PutScalingPolicyResult result = autoScaling.putScalingPolicy(request);

    this.console.write("Put scaling policy " + scaling.getPolicy());

    //
    //
    //

    // Scale Up

    Dimension dimension = new Dimension().withName("AutoScalingGroupName").withValue(scaling.getGroup());

    List<String> actions = new ArrayList<>();
    actions.add(result.getPolicyARN());

    PutMetricAlarmRequest upRequest = new PutMetricAlarmRequest().withAlarmName(scaling.getAlarm())
            .withMetricName("CPUUtilization").withDimensions(dimension).withNamespace("AWS/EC2")
            .withComparisonOperator(ComparisonOperator.GreaterThanThreshold).withStatistic(Statistic.Average)
            .withUnit(StandardUnit.Percent).withThreshold(60d).withPeriod(300).withEvaluationPeriods(2)
            .withAlarmActions(actions);

    AmazonCloudWatchClient cloudWatch = new CloudWatch().getClient();
    cloudWatch.putMetricAlarm(upRequest);

    this.console.write("Put alarm " + scaling.getAlarm());
    this.console.newLine();

    List<Instance> instances = new ArrayList<>();

    for (AutoScalingInstanceDetails instance : autoScaling.describeAutoScalingInstances()
            .getAutoScalingInstances()) {

        if (instance.getAutoScalingGroupName().equals(scaling.getGroup())) {

            instances.add(new Ec2().getEC2InstanceById(instance.getInstanceId()));
        }
    }

    return instances;
}