// 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 }
func (cwi *CloudwatchInput) Init(config interface{}) (err error) { conf := config.(*CloudwatchInputConfig) statisticsSet := sets.SSet(conf.Statistics...) switch { case conf.MetricName == "": err = errors.New("No metric name supplied") case conf.Period < 60 || conf.Period%60 != 0: err = errors.New("Period must be divisible by 60") case len(conf.Statistics) < 1: err = errors.New("text·2") case conf.Unit != "" && !validUnits.Member(conf.Unit): err = errors.New("Unit is not a valid value") case len(conf.Statistics) < 1: err = errors.New("No statistics supplied") case validMetricStatistics.Union(statisticsSet).Len() != validMetricStatistics.Len(): err = errors.New("Invalid statistic values supplied") } if err != nil { return } dims := make([]cloudwatch.Dimension, 0) for k, v := range conf.Dimensions { dims = append(dims, cloudwatch.Dimension{k, v}) } auth := aws.Auth{AccessKey: conf.AccessKey, SecretKey: conf.SecretKey} cwi.req = &cloudwatch.GetMetricStatisticsRequest{ MetricName: conf.MetricName, Period: conf.Period, Unit: conf.Unit, Statistics: conf.Statistics, Dimensions: dims, } cwi.pollInterval, err = time.ParseDuration(conf.PollInterval) if err != nil { return } region, ok := aws.Regions[conf.Region] if !ok { err = errors.New("Region of that name not found.") return } cwi.namespace = conf.Namespace cwi.cw, err = cloudwatch.NewCloudWatch(auth, region.CloudWatchServicepoint, conf.Namespace) return }
} var validUnits = sets.SSet( "Seconds", "Microseconds", "Milliseconds", "Bytes", "Kilobytes", "Megabytes", "Gigabytes", "Terabytes", "Bits", "Kilobits", "Megabits", "Gigabits", "Terabits", "Percent", "Count", "Bytes/Second", "Kilobytes/Second", "Megabytes/Second", "Gigabytes/Second", "Terabytes/Second", "Bits/Second", "Kilobits/Second", "Megabits/Second", "Gigabits/Second", "Terabits/Second", "Count/Second", ) var validMetricStatistics = sets.SSet(
var validUnits = sets.SSet( UnitSeconds, UnitMicroseconds, UnitMilliseconds, UnitBytes, UnitKilobytes, UnitMegabytes, UnitGigabytes, UnitTerabytes, UnitBits, UnitKilobits, UnitMegabits, UnitGigabits, UnitTerabits, UnitPercent, UnitCount, UnitBytesPerSecond, UnitKilobytesPerSecond, UnitMegabytesPerSecond, UnitGigabytesPerSecond, UnitTerabytesPerSecond, UnitBitsPerSecond, UnitKilobitsPerSecond, UnitMegabitsPerSecond, UnitGigabitsPerSecond, UnitTerabitsPerSecond, UnitCountPerSecond, UnitNone, )