// FetchMetrics fetch elasticache values
func (p ECachePlugin) FetchMetrics() (map[string]float64, error) {
	auth, err := aws.GetAuth(p.AccessKeyID, p.SecretAccessKey, "", time.Now())
	if err != nil {
		return nil, err
	}

	cloudWatch, err := cloudwatch.NewCloudWatch(auth, aws.Regions[p.Region].CloudWatchServicepoint)
	if err != nil {
		return nil, err
	}

	stat := make(map[string]float64)

	perInstances := &[]cloudwatch.Dimension{
		cloudwatch.Dimension{
			Name:  "CacheClusterId",
			Value: p.CacheClusterID,
		},
		cloudwatch.Dimension{
			Name:  "CacheNodeId",
			Value: p.CacheNodeID,
		},
	}

	for _, met := range p.CacheMetrics {
		v, err := getLastPoint(cloudWatch, perInstances, met)
		if err == nil {
			stat[met] = v
		} else {
			log.Printf("%s: %s", met, err)
		}
	}

	return stat, nil
}
// FetchMetrics fetch the metrics
func (p CPUCreditPlugin) FetchMetrics() (map[string]float64, error) {
	region := aws.Regions[p.Region]
	dimension := &cloudwatch.Dimension{
		Name:  "InstanceId",
		Value: p.InstanceID,
	}

	auth, err := aws.GetAuth(p.AccessKeyID, p.SecretAccessKey, "", time.Now())
	if err != nil {
		return nil, err
	}
	cw, err := cloudwatch.NewCloudWatch(auth, region.CloudWatchServicepoint)

	stat := make(map[string]float64)

	stat["usage"], err = getLastPointAverage(cw, dimension, "CPUCreditUsage")
	if err != nil {
		return nil, err
	}

	stat["balance"], err = getLastPointAverage(cw, dimension, "CPUCreditBalance")
	if err != nil {
		return nil, err
	}

	return stat, nil
}
func (p RDSPlugin) FetchMetrics() (map[string]float64, error) {
	auth, err := aws.GetAuth(p.AccessKeyId, p.SecretAccessKey, "", time.Now())
	if err != nil {
		return nil, err
	}

	cloudWatch, err := cloudwatch.NewCloudWatch(auth, aws.Regions[p.Region].CloudWatchServicepoint)
	if err != nil {
		return nil, err
	}

	stat := make(map[string]float64)

	perInstance := &cloudwatch.Dimension{
		Name:  "DBInstanceIdentifier",
		Value: p.Identifier,
	}

	for _, met := range [...]string{
		"BinLogDiskUsage", "CPUUtilization", "DatabaseConnections", "DiskQueueDepth", "FreeableMemory",
		"FreeStorageSpace", "ReplicaLag", "SwapUsage", "ReadIOPS", "WriteIOPS", "ReadLatency",
		"WriteLatency", "ReadThroughput", "WriteThroughput", "NetworkTransmitThroughput", "NetworkReceiveThroughput",
	} {
		v, err := GetLastPoint(cloudWatch, perInstance, met)
		if err == nil {
			stat[met] = v
		} else {
			log.Printf("%s: %s", met, err)
		}
	}

	return stat, nil
}
// FetchMetrics interface for mackerel-plugin
func (p RDSPlugin) FetchMetrics() (map[string]float64, error) {
	auth, err := aws.GetAuth(p.AccessKeyID, p.SecretAccessKey, "", time.Now())
	if err != nil {
		return nil, err
	}

	cloudWatch, err := cloudwatch.NewCloudWatch(auth, aws.Regions[p.Region].CloudWatchServicepoint)
	if err != nil {
		return nil, err
	}

	stat := make(map[string]float64)

	perInstance := &cloudwatch.Dimension{
		Name:  "DBInstanceIdentifier",
		Value: p.Identifier,
	}

	for _, met := range p.rdsMetrics() {
		v, err := getLastPoint(cloudWatch, perInstance, met)
		if err == nil {
			stat[met] = v
		} else {
			log.Printf("%s: %s", met, err)
		}
	}

	return stat, nil
}
func main() {
	options := parseCommandLine()

	auth, err := auth()
	if err != nil {
		unknown(options.metricName, err.Error())
	}

	client, err := cloudwatch.NewCloudWatch(auth, options.region.CloudWatchServicepoint)
	if err != nil {
		unknown(options.metricName, err.Error())
	}

	endTime := time.Now()
	startTime := endTime.Add(-1 * time.Duration(options.period) * time.Second)
	request := &cloudwatch.GetMetricStatisticsRequest{
		Namespace:  options.namespace,
		MetricName: options.metricName,
		Dimensions: options.dimensions,
		StartTime:  startTime,
		EndTime:    endTime,
		Period:     options.period,
		Statistics: []string{options.statistic},
	}
	response, err := client.GetMetricStatistics(request)
	if err != nil {
		unknown(options.metricName, err.Error())
	}

	datapoints := response.GetMetricStatisticsResult.Datapoints
	if len(datapoints) == 0 {
		unknown(options.metricName, "No datapoints")
	}
	datapoint := datapoints[0]
	data, err := getData(datapoint, options.statistic)
	if err != nil {
		unknown(options.metricName, err.Error())
	}
	unit := datapoint.Unit

	message := fmt.Sprintf("%f %s", data, unit)
	if options.criticalThreshold > options.warningThreshold {
		if data > options.criticalThreshold {
			critical(options.metricName, message)
		} else if data > options.warningThreshold {
			warning(options.metricName, message)
		}
	} else {
		if data < options.criticalThreshold {
			critical(options.metricName, message)
		} else if data < options.warningThreshold {
			warning(options.metricName, message)
		}
	}
	ok(options.metricName, message)
}
func (p *ESPlugin) prepare() error {
	auth, err := aws.GetAuth(p.AccessKeyID, p.SecretAccessKey, "", time.Now())
	if err != nil {
		return err
	}

	p.CloudWatch, err = cloudwatch.NewCloudWatch(auth, aws.Regions[p.Region].CloudWatchServicepoint)
	if err != nil {
		return err
	}
	return nil
}
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
}
func (cwo *CloudwatchOutput) Init(config interface{}) (err error) {
	conf := config.(*CloudwatchOutputConfig)
	auth := aws.Auth{AccessKey: conf.AccessKey, SecretKey: conf.SecretKey}
	cwo.stopChan = make(chan bool)
	region, ok := aws.Regions[conf.Region]
	if !ok {
		err = errors.New("Region of that name not found.")
		return
	}
	cwo.backlog = conf.Backlog
	cwo.retries = conf.Retries
	if cwo.cw, err = cloudwatch.NewCloudWatch(auth, region.CloudWatchServicepoint,
		conf.Namespace); err != nil {
		return
	}
	if cwo.tzLocation, err = time.LoadLocation(conf.TimestampLocation); err != nil {
		err = fmt.Errorf("CloudwatchOutput unknown timestamp_location '%s': %s",
			conf.TimestampLocation, err)
	}
	return
}
Example #9
0
func NewCloudWatchHandler(region string) Handler {
	auth, err := aws.EnvAuth()
	if err != nil {
		log.Fatalln(err)
	}
	c, err := cloudwatch.NewCloudWatch(auth, aws.Regions[region].CloudWatchServicepoint)
	if err != nil {
		log.Fatalln(err)
	}
	return func(host Host, status bool, t time.Time) error {
		value := CloudWatchFailValue
		if status == true {
			value = CloudWatchOkValue
		}
		metric := cloudwatch.MetricDatum{
			MetricName: host.Address(),
			Value:      value,
		}
		_, err := c.PutMetricDataNamespace([]cloudwatch.MetricDatum{metric}, CloudWatchNamespace)
		return err
	}
}
func (p *ELBPlugin) prepare() error {
	auth, err := aws.GetAuth(p.AccessKeyID, p.SecretAccessKey, "", time.Now())
	if err != nil {
		return err
	}

	p.CloudWatch, err = cloudwatch.NewCloudWatch(auth, aws.Regions[p.Region].CloudWatchServicepoint)
	if err != nil {
		return err
	}

	ret, err := p.CloudWatch.ListMetrics(&cloudwatch.ListMetricsRequest{
		Namespace: "AWS/ELB",
		Dimensions: []cloudwatch.Dimension{
			{
				Name: "AvailabilityZone",
			},
		},
		MetricName: "HealthyHostCount",
	})

	if err != nil {
		return err
	}

	p.AZs = make([]string, 0, len(ret.ListMetricsResult.Metrics))
	for _, met := range ret.ListMetricsResult.Metrics {
		if len(met.Dimensions) > 1 {
			continue
		} else if met.Dimensions[0].Name != "AvailabilityZone" {
			continue
		}

		p.AZs = append(p.AZs, met.Dimensions[0].Value)
	}

	return nil
}
Example #11
0
func (s *S) SetUpSuite(c *gocheck.C) {
	testServer.Start()
	auth := aws.Auth{AccessKey: "abc", SecretKey: "123"}
	s.cw, _ = cloudwatch.NewCloudWatch(auth, aws.ServiceInfo{testServer.URL, aws.V2Signature})
}