AutoScalingPolicy createPolicy(String resourceId, String name, String cpu, String last, String adjustment) { AutoScalingPolicy policy = new AutoScalingPolicy(resourceId + "-" + name); Asserts.isTrue(cpu.matches("[>|<]=\\d+%"), "cpu should be like >=80%"); if (cpu.startsWith(">=")) { policy.comparisonOperator = ComparisonOperator.GreaterThanOrEqualToThreshold; } else { policy.comparisonOperator = ComparisonOperator.LessThanOrEqualToThreshold; } policy.cpuUtilizationPercentage = Integer.parseInt(cpu.substring(2, cpu.length() - 1)); Asserts.isTrue(last.endsWith("min"), "last should be like 3min"); policy.lastMinutes = Integer.parseInt(last.substring(0, last.length() - 3)); Asserts.isTrue(adjustment.endsWith("%"), "adjustment should be like 15%"); policy.adjustmentPercentage = Integer.parseInt(adjustment.substring(0, adjustment.length() - 1)); return policy; }
/** * * @param instances * @param email * @param jobId */ public void addWatch(Collection<String> instances, String email, String jobId) { String alarmName = getAlarmName(email, jobId); for (MetricAlarm a : asynchCloudWatchClient.describeAlarms().getMetricAlarms()) { if (a.getAlarmName().equalsIgnoreCase(alarmName)) { logger.info("Alarm for job " + jobId + " and email " + email + " already exists."); return; } } List<Dimension> dimensions = new ArrayList<Dimension>(); for (String instanceId : instances) { Dimension d = new Dimension().withName("InstanceId").withValue(instanceId); dimensions.add(d); } PutMetricAlarmRequest request = new PutMetricAlarmRequest() .withActionsEnabled(true).withAlarmName(alarmName) .withComparisonOperator(ComparisonOperator.GreaterThanOrEqualToThreshold) .withDimensions(dimensions) .withAlarmActions(getOrCreateNotification(email)) .withEvaluationPeriods(1) .withPeriod(60) .withThreshold(60.0D) .withStatistic(Statistic.Average) .withMetricName("CPUUtilization") .withNamespace("AWS/EC2"); asynchCloudWatchClient.putMetricAlarm(request); logger.info("Created alarm " + alarmName); }
@Test void createPolicy() { AutoScalingPolicy policy = loader.createPolicy("as", "scale-out", ">=80%", "3min", "15%"); assertEquals("as-scale-out", policy.id); assertEquals(80d, policy.cpuUtilizationPercentage, 0.0001); assertEquals(ComparisonOperator.GreaterThanOrEqualToThreshold, policy.comparisonOperator); assertEquals(3, policy.lastMinutes); assertEquals(15, policy.adjustmentPercentage); }
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); } 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); }