Esempio n. 1
0
func (c *CloudWatch) ListMetrics(req *ListMetricsRequest) (result *ListMetricsResponse, err error) {

	// Serialize all the params
	params := aws.MakeParams("ListMetrics")
	if req.Namespace != "" {
		params["Namespace"] = req.Namespace
	}
	if len(req.Dimensions) > 0 {
		for i, d := range req.Dimensions {
			prefix := "Dimensions.member." + strconv.Itoa(i+1)
			params[prefix+".Name"] = d.Name
			params[prefix+".Value"] = d.Value
		}
	}

	result = new(ListMetricsResponse)
	err = c.query("GET", "/", params, &result)
	metrics := result.ListMetricsResult.Metrics
	if result.ListMetricsResult.NextToken != "" {
		params = aws.MakeParams("ListMetrics")
		params["NextToken"] = result.ListMetricsResult.NextToken
		for result.ListMetricsResult.NextToken != "" && err == nil {
			result = new(ListMetricsResponse)
			err = c.query("GET", "/", params, &result)
			if err == nil {
				newslice := make([]Metric, len(metrics)+len(result.ListMetricsResult.Metrics))
				copy(newslice, metrics)
				copy(newslice[len(metrics):], result.ListMetricsResult.Metrics)
				metrics = newslice
			}
		}
		result.ListMetricsResult.Metrics = metrics
	}
	return
}
Esempio n. 2
0
func (c *CloudWatch) PutMetricAlarm(alarm *MetricAlarm) (result *aws.BaseResponse, err error) {
	// Serialize the params
	params := aws.MakeParams("PutMetricAlarm")

	switch {
	case alarm.AlarmName == "":
		err = errors.New("No AlarmName supplied")
	case !validComparisonOperators.Member(alarm.ComparisonOperator):
		err = errors.New("ComparisonOperator is not valid")
	case alarm.EvaluationPeriods == 0:
		err = errors.New("No number of EvaluationPeriods specified")
	case alarm.MetricName == "":
		err = errors.New("No MetricName specified")
	case alarm.Namespace == "":
		err = errors.New("No Namespace specified")
	case alarm.Period == 0:
		err = errors.New("No Period over which statistic should apply was specified")
	case !validMetricStatistics.Member(alarm.Statistic):
		err = errors.New("Invalid statistic value supplied")
	case alarm.Threshold == 0:
		err = errors.New("No Threshold value specified")
	case alarm.Unit != "" && !validUnits.Member(alarm.Unit):
		err = errors.New("Unit is not a valid value")
	}
	if err != nil {
		return
	}

	for i, action := range alarm.AlarmActions {
		params["AlarmActions.member."+strconv.Itoa(i+1)] = action.ARN
	}
	if alarm.AlarmDescription != "" {
		params["AlarmDescription"] = alarm.AlarmDescription
		return
	}
	params["AlarmDescription"] = alarm.AlarmDescription
	params["AlarmName"] = alarm.AlarmName
	params["ComparisonOperator"] = alarm.ComparisonOperator
	for i, dim := range alarm.Dimensions {
		dimprefix := "Dimensions.member." + strconv.Itoa(i+1)
		params[dimprefix+".Name"] = dim.Name
		params[dimprefix+".Value"] = dim.Value
	}
	params["EvaluationPeriods"] = strconv.Itoa(alarm.EvaluationPeriods)
	params["MetricName"] = alarm.MetricName
	params["Namespace"] = alarm.Namespace
	params["Period"] = strconv.Itoa(alarm.Period)
	params["Statistic"] = alarm.Statistic
	params["Threshold"] = strconv.FormatFloat(alarm.Threshold, 'E', 10, 64)
	if alarm.Unit != "" {
		params["Unit"] = alarm.Unit
	}

	result = new(aws.BaseResponse)
	err = c.query("POST", "/", params, result)
	return
}
Esempio n. 3
0
// Get statistics for specified metric
//
// If the arguments are invalid or the server returns an error, the error will
// be set and the other values undefined.
func (c *CloudWatch) GetMetricStatistics(req *GetMetricStatisticsRequest) (result *GetMetricStatisticsResponse, err error) {
	statisticsSet := sets.SSet(req.Statistics...)
	// Kick out argument errors
	switch {
	case req.EndTime.IsZero():
		err = errors.New("No endTime specified")
	case req.StartTime.IsZero():
		err = errors.New("No startTime specified")
	case req.MetricName == "":
		err = errors.New("No metricName specified")
	case req.Namespace == "":
		err = errors.New("No Namespace specified")
	case req.Period < 60 || req.Period%60 != 0:
		err = errors.New("Period not 60 seconds or a multiple of 60 seconds")
	case len(req.Statistics) < 1:
		err = errors.New("No statistics supplied")
	case validMetricStatistics.Union(statisticsSet).Len() != validMetricStatistics.Len():
		err = errors.New("Invalid statistic values supplied")
	case req.Unit != "" && !validUnits.Member(req.Unit):
		err = errors.New("Unit is not a valid value")
	}
	if err != nil {
		return
	}

	// Serialize all the params
	params := aws.MakeParams("GetMetricStatistics")
	params["EndTime"] = req.EndTime.UTC().Format(time.RFC3339)
	params["StartTime"] = req.StartTime.UTC().Format(time.RFC3339)
	params["MetricName"] = req.MetricName
	params["Namespace"] = req.Namespace
	params["Period"] = strconv.Itoa(req.Period)
	if req.Unit != "" {
		params["Unit"] = req.Unit
	}

	// Serialize the lists of data
	for i, d := range req.Dimensions {
		prefix := "Dimensions.member." + strconv.Itoa(i+1)
		params[prefix+".Name"] = d.Name
		params[prefix+".Value"] = d.Value
	}
	for i, d := range req.Statistics {
		prefix := "Statistics.member." + strconv.Itoa(i+1)
		params[prefix] = d
	}
	result = new(GetMetricStatisticsResponse)
	err = c.query("GET", "/", params, result)
	return
}
Esempio n. 4
0
// DescribeDBInstances - Returns a description of each Database Instance
// Supports pagination by using the "Marker" parameter, and "maxRecords" for subsequent calls
// Unfortunately RDS does not currently support filtering
//
// See http://goo.gl/lzZMyz for more details.
func (rds *RDS) DescribeDBInstances(id string, maxRecords int, marker string) (*DescribeDBInstancesResponse, error) {

	params := aws.MakeParams("DescribeDBInstances")

	if id != "" {
		params["DBInstanceIdentifier"] = id
	}

	if maxRecords != 0 {
		params["MaxRecords"] = strconv.Itoa(maxRecords)
	}
	if marker != "" {
		params["Marker"] = marker
	}

	resp := &DescribeDBInstancesResponse{}
	err := rds.query("POST", "/", params, resp)
	return resp, err
}
Esempio n. 5
0
func (c *CloudWatch) PutMetricDataNamespace(metrics []MetricDatum, namespace string) (result *aws.BaseResponse, err error) {
	// Serialize the params
	params := aws.MakeParams("PutMetricData")
	if namespace != "" {
		params["Namespace"] = namespace
	}
	for i, metric := range metrics {
		prefix := "MetricData.member." + strconv.Itoa(i+1)
		if metric.MetricName == "" {
			err = fmt.Errorf("No metric name supplied for metric: %d", i)
			return
		}
		params[prefix+".MetricName"] = metric.MetricName
		if metric.Unit != "" {
			params[prefix+".Unit"] = metric.Unit
		}
		if metric.Value != 0 {
			params[prefix+".Value"] = strconv.FormatFloat(metric.Value, 'E', 10, 64)
		}
		if !metric.Timestamp.IsZero() {
			params[prefix+".Timestamp"] = metric.Timestamp.UTC().Format(time.RFC3339)
		}
		for j, dim := range metric.Dimensions {
			dimprefix := prefix + ".Dimensions.member." + strconv.Itoa(j+1)
			params[dimprefix+".Name"] = dim.Name
			params[dimprefix+".Value"] = dim.Value
		}
		if metric.StatisticValues != nil {
			statprefix := prefix + ".StatisticValues"
			params[statprefix+".Maximum"] = strconv.FormatFloat(metric.StatisticValues.Maximum, 'E', 10, 64)
			params[statprefix+".Minimum"] = strconv.FormatFloat(metric.StatisticValues.Minimum, 'E', 10, 64)
			params[statprefix+".SampleCount"] = strconv.FormatFloat(metric.StatisticValues.SampleCount, 'E', 10, 64)
			params[statprefix+".Sum"] = strconv.FormatFloat(metric.StatisticValues.Sum, 'E', 10, 64)
		}
	}
	result = new(aws.BaseResponse)
	err = c.query("POST", "/", params, result)
	return
}