public void createAlarmIfNotAlreadyExist(String topicArn) { List<Dimension> dimensions = new ArrayList<>(); dimensions.add(new Dimension().withName("Currency").withValue("USD")); PutMetricAlarmRequest request = new PutMetricAlarmRequest(); request.setAlarmName("s3video Billing Alarm"); request.setAlarmActions(Arrays.asList(topicArn)); request.setMetricName("EstimatedCharges"); request.setNamespace("AWS/Billing"); request.setDimensions(dimensions); request.setThreshold(alarmThreshold); request.setPeriod(SIX_HOUR_IN_SECONDS); request.setStatistic("Maximum"); request.setComparisonOperator("GreaterThanThreshold"); request.setEvaluationPeriods(1); cloudWatchClient.putMetricAlarm(request); }
@Override public void execute(Context context) throws Exception { String asGroupName = resource.asGroup.remoteASGroup.getAutoScalingGroupName(); String policyARN = AWS.as.createPolicy(new PutScalingPolicyRequest() .withPolicyName(resource.id) .withAutoScalingGroupName(asGroupName) .withScalingAdjustment(resource.adjustmentPercentage) .withAdjustmentType("PercentChangeInCapacity") .withMinAdjustmentStep(1) .withCooldown(300)); AWS.cloudWatch.createAlarm(new PutMetricAlarmRequest() .withAlarmName(context.env.name + ":" + resource.id + "-alarm") .withMetricName("CPUUtilization") .withComparisonOperator(resource.comparisonOperator) .withThreshold(resource.cpuUtilizationPercentage) .withPeriod(60) .withEvaluationPeriods(resource.lastMinutes) .withStatistic(Statistic.Average) .withNamespace("AWS/EC2") .withDimensions(new Dimension().withName("AutoScalingGroupName").withValue(asGroupName)) .withAlarmActions(policyARN)); }
@Test public void testCreateContentHealthCheckAlarm() { final String alarmName = "testAlarmName"; final String alarmDescription = "testAlarmDescription"; final String publishInstanceId = "testPublishInstanceId"; final String namespace = "testNamespace"; final String topicArn = "testTopicArn"; awsHelperService.createContentHealthCheckAlarm(alarmName, alarmDescription, publishInstanceId, namespace, topicArn); final ArgumentCaptor<PutMetricAlarmRequest> argumentCaptor = ArgumentCaptor.forClass(PutMetricAlarmRequest.class); verify(amazonCloudWatch).putMetricAlarm(argumentCaptor.capture()); assertThat(argumentCaptor.getValue().getAlarmName(), equalTo(alarmName)); assertThat(argumentCaptor.getValue().getAlarmDescription(), equalTo(alarmDescription)); assertThat(argumentCaptor.getValue().getDimensions().get(0).getValue(), equalTo(publishInstanceId)); assertThat(argumentCaptor.getValue().getNamespace(), equalTo(namespace)); assertThat(argumentCaptor.getValue().getAlarmActions().get(0), equalTo(topicArn)); }
/** * * @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); }
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); }
public void createAlarm(PutMetricAlarmRequest request) { logger.info("create cloud watch alarm, request={}", request); cloudWatch.putMetricAlarm(request); }