public Datum(Datapoint d, String metric, Statistic statistic) { switch (statistic) { case Sum: value = d.getSum(); break; case Maximum: value = d.getMaximum(); break; case Minimum: value = d.getMinimum(); break; case SampleCount: value = d.getSampleCount(); break; default: value = d.getAverage(); } timestamp = d.getTimestamp().getTime(); this.metric = metric; this.statistic = statistic.name(); this.unit = d.getUnit(); }
@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)); }
/** * * @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 List<Datum> getAllData(List<Datapoint> datapoints, String metric, Statistic statistic) { ArrayList<Datum> data = new ArrayList<Datum>(); for (Datapoint d : datapoints) data.add(new Datum(d, metric, statistic)); Collections.sort(data); return data; }
public static List<Datum> getMetric(String metricName, Dimension dim, String namespace, Date startTime, int period, Statistic statistic, StandardUnit unit) { connect(); GetMetricStatisticsRequest req = new GetMetricStatisticsRequest(); req.setMetricName(metricName); List<Dimension> dimensions = new ArrayList<Dimension>(); dimensions.add(dim); req.setDimensions(dimensions); req.setNamespace(namespace); req.setStartTime(startTime); req.setEndTime(new Date()); if (period % 60 != 0) period = (period / 60) * 60; req.setPeriod(period); if (unit != null) req.setUnit(unit); if (statistic == null) statistic = Statistic.Average; List<String> statistics = new ArrayList<String>(); statistics.add(statistic.name()); req.setStatistics(statistics); GetMetricStatisticsResult res = client.getMetricStatistics(req); return Datum.getAllData(res.getDatapoints(), metricName, statistic); }
public static void retrieveMetrics(List<String> ids, VirtualMachine vm, String[] metricsToBeGet, String localPath, Date date, int period, Statistic statistic, StandardUnit unit) throws Exception { if (ids.size() == 0) return; int count = 1; if (metricsToBeGet != null && metricsToBeGet.length > 0) for (String id : ids) { retrieveMetrics(id, vm, count, metricsToBeGet, localPath, date, period, statistic, unit); ++count; } }
public void retrieveMetrics(String[] metricsToBeGet, String localPath, Date date, int period, Statistic statistic, StandardUnit unit) throws Exception { int count = 1; if (metricsToBeGet != null && metricsToBeGet.length > 0) for (it.cloud.Instance i : instancesSet) { retrieveMetrics(i.id, this, count, metricsToBeGet, localPath, date, period, statistic, unit); ++count; } }
public static void retrieveMetrics(String id, VirtualMachine vm, int count, String[] metricsToBeGet, String localPath, Date date, int period, Statistic statistic, StandardUnit unit) throws Exception { if (metricsToBeGet != null && metricsToBeGet.length > 0) for (String s : metricsToBeGet) { Path file = Paths.get(localPath, vm.name + count, s + ".csv"); file.toFile().getParentFile().mkdirs(); CloudWatch.writeInstanceMetricToFile(file, s, id, date, period, statistic, unit); } }
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 static List<Datum> getInstanceMetric(String metricName, String instanceId, Date startTime, int period, Statistic statistic, StandardUnit unit) { return getMetric(metricName, getInstanceDimension(instanceId), "AWS/EC2", startTime, period, statistic, unit); }
public static void writeInstanceMetricToFile(Path file, String metricName, String instanceId, Date startTime, int period, Statistic statistic, StandardUnit unit) throws Exception { writeMetricToFile(file, metricName, getInstanceDimension(instanceId), "AWS/EC2", startTime, period, statistic, unit); }
public static void writeMetricToFile(Path file, String metricName, Dimension dim, String namespace, Date startTime, int period, Statistic statistic, StandardUnit unit) throws Exception { List<Datum> data = getMetric(metricName, dim, namespace, startTime, period, statistic, unit); Datum.writeAllData(file, data); }
public static void retrieveMetrics(List<String> ids, VirtualMachine vm, String localPath, Date date) throws Exception { retrieveMetrics(ids, vm, localPath, date, getSuggestedPeriod(date), Statistic.Average, null); }
public static void retrieveMetrics(List<String> ids, VirtualMachine vm, String localPath, Date date, int period, Statistic statistic, StandardUnit unit) throws Exception { String metricsToBeGet = vm.getParameter("METRICS"); if (metricsToBeGet != null) retrieveMetrics(ids, vm, metricsToBeGet.split(";"), localPath, date, period, statistic, unit); }
public void retrieveMetrics(String localPath, Date date) throws Exception { retrieveMetrics(localPath, date, getSuggestedPeriod(date), Statistic.Average, null); }
public void retrieveMetrics(String localPath, Date date, int period, Statistic statistic, StandardUnit unit) throws Exception { String metricsToBeGet = getParameter("METRICS"); if (metricsToBeGet != null) retrieveMetrics(metricsToBeGet.split(";"), localPath, date, period, statistic, unit); }
public static void retrieveMetrics(String id, VirtualMachine vm, int count, String localPath, Date date) throws Exception { retrieveMetrics(id, vm, count, localPath, date, getSuggestedPeriod(date), Statistic.Average, null); }
public static void retrieveMetrics(String id, VirtualMachine vm, int count, String localPath, Date date, int period, Statistic statistic, StandardUnit unit) throws Exception { String metricsToBeGet = vm.getParameter("METRICS"); if (metricsToBeGet != null) retrieveMetrics(id, vm, count, metricsToBeGet.split(";"), localPath, date, period, statistic, unit); }