2016-03-25 22:16:23 +00:00
|
|
|
package cloudwatch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2016-06-02 11:34:03 +00:00
|
|
|
"sync"
|
2016-03-25 22:16:23 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/cloudwatch"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-05-25 11:30:39 +00:00
|
|
|
internalaws "github.com/influxdata/telegraf/internal/config/aws"
|
2016-05-24 13:50:01 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/limiter"
|
2016-03-25 22:16:23 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
CloudWatch struct {
|
2016-05-25 11:30:39 +00:00
|
|
|
Region string `toml:"region"`
|
|
|
|
AccessKey string `toml:"access_key"`
|
|
|
|
SecretKey string `toml:"secret_key"`
|
|
|
|
RoleARN string `toml:"role_arn"`
|
|
|
|
Profile string `toml:"profile"`
|
|
|
|
Filename string `toml:"shared_credential_file"`
|
|
|
|
Token string `toml:"token"`
|
|
|
|
|
2016-08-26 00:53:46 +00:00
|
|
|
Period internal.Duration `toml:"period"`
|
|
|
|
Delay internal.Duration `toml:"delay"`
|
|
|
|
Namespace string `toml:"namespace"`
|
|
|
|
Metrics []*Metric `toml:"metrics"`
|
|
|
|
CacheTTL internal.Duration `toml:"cache_ttl"`
|
2016-08-26 00:56:33 +00:00
|
|
|
RateLimit int `toml:"ratelimit"`
|
2016-03-25 22:16:23 +00:00
|
|
|
client cloudwatchClient
|
|
|
|
metricCache *MetricCache
|
|
|
|
}
|
|
|
|
|
|
|
|
Metric struct {
|
|
|
|
MetricNames []string `toml:"names"`
|
|
|
|
Dimensions []*Dimension `toml:"dimensions"`
|
|
|
|
}
|
|
|
|
|
|
|
|
Dimension struct {
|
|
|
|
Name string `toml:"name"`
|
|
|
|
Value string `toml:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
MetricCache struct {
|
|
|
|
TTL time.Duration
|
|
|
|
Fetched time.Time
|
|
|
|
Metrics []*cloudwatch.Metric
|
|
|
|
}
|
|
|
|
|
|
|
|
cloudwatchClient interface {
|
|
|
|
ListMetrics(*cloudwatch.ListMetricsInput) (*cloudwatch.ListMetricsOutput, error)
|
|
|
|
GetMetricStatistics(*cloudwatch.GetMetricStatisticsInput) (*cloudwatch.GetMetricStatisticsOutput, error)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *CloudWatch) SampleConfig() string {
|
|
|
|
return `
|
|
|
|
## Amazon Region
|
2016-11-04 13:16:41 +00:00
|
|
|
region = "us-east-1"
|
2016-03-25 22:16:23 +00:00
|
|
|
|
2016-04-23 18:19:04 +00:00
|
|
|
## Amazon Credentials
|
|
|
|
## Credentials are loaded in the following order
|
2016-05-25 11:30:39 +00:00
|
|
|
## 1) Assumed credentials via STS if role_arn is specified
|
|
|
|
## 2) explicit credentials from 'access_key' and 'secret_key'
|
|
|
|
## 3) shared profile from 'profile'
|
|
|
|
## 4) environment variables
|
|
|
|
## 5) shared credentials file
|
|
|
|
## 6) EC2 Instance Profile
|
2016-04-23 18:19:04 +00:00
|
|
|
#access_key = ""
|
|
|
|
#secret_key = ""
|
2016-05-25 11:30:39 +00:00
|
|
|
#token = ""
|
|
|
|
#role_arn = ""
|
|
|
|
#profile = ""
|
|
|
|
#shared_credential_file = ""
|
2016-04-23 18:19:04 +00:00
|
|
|
|
2016-11-07 12:14:04 +00:00
|
|
|
# The minimum period for Cloudwatch metrics is 1 minute (60s). However not all
|
|
|
|
# metrics are made available to the 1 minute period. Some are collected at
|
2017-05-08 18:29:26 +00:00
|
|
|
# 3 minute, 5 minute, or larger intervals. See https://aws.amazon.com/cloudwatch/faqs/#monitoring.
|
2016-11-07 12:14:04 +00:00
|
|
|
# Note that if a period is configured that is smaller than the minimum for a
|
|
|
|
# particular metric, that metric will not be returned by the Cloudwatch API
|
|
|
|
# and will not be collected by Telegraf.
|
|
|
|
#
|
2016-03-25 22:16:23 +00:00
|
|
|
## Requested CloudWatch aggregation Period (required - must be a multiple of 60s)
|
2016-11-07 12:14:04 +00:00
|
|
|
period = "5m"
|
2016-03-25 22:16:23 +00:00
|
|
|
|
|
|
|
## Collection Delay (required - must account for metrics availability via CloudWatch API)
|
2016-11-07 12:14:04 +00:00
|
|
|
delay = "5m"
|
2016-03-25 22:16:23 +00:00
|
|
|
|
2016-04-22 16:23:00 +00:00
|
|
|
## Recomended: use metric 'interval' that is a multiple of 'period' to avoid
|
2016-03-25 22:16:23 +00:00
|
|
|
## gaps or overlap in pulled data
|
2016-11-07 12:14:04 +00:00
|
|
|
interval = "5m"
|
2016-03-25 22:16:23 +00:00
|
|
|
|
2016-05-25 11:30:39 +00:00
|
|
|
## Configure the TTL for the internal cache of metrics.
|
|
|
|
## Defaults to 1 hr if not specified
|
2016-11-04 13:16:41 +00:00
|
|
|
#cache_ttl = "10m"
|
2016-05-25 11:30:39 +00:00
|
|
|
|
2016-03-25 22:16:23 +00:00
|
|
|
## Metric Statistic Namespace (required)
|
2016-11-04 13:16:41 +00:00
|
|
|
namespace = "AWS/ELB"
|
2016-03-25 22:16:23 +00:00
|
|
|
|
2016-08-26 01:04:29 +00:00
|
|
|
## Maximum requests per second. Note that the global default AWS rate limit is
|
2017-03-15 22:20:18 +00:00
|
|
|
## 400 reqs/sec, so if you define multiple namespaces, these should add up to a
|
|
|
|
## maximum of 400. Optional - default value is 200.
|
|
|
|
## See http://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_limits.html
|
|
|
|
ratelimit = 200
|
2016-08-26 01:04:29 +00:00
|
|
|
|
2016-03-25 22:16:23 +00:00
|
|
|
## Metrics to Pull (optional)
|
|
|
|
## Defaults to all Metrics in Namespace if nothing is provided
|
|
|
|
## Refreshes Namespace available metrics every 1h
|
|
|
|
#[[inputs.cloudwatch.metrics]]
|
2016-11-04 13:16:41 +00:00
|
|
|
# names = ["Latency", "RequestCount"]
|
2016-04-22 16:23:00 +00:00
|
|
|
#
|
2016-03-25 22:16:23 +00:00
|
|
|
# ## Dimension filters for Metric (optional)
|
|
|
|
# [[inputs.cloudwatch.metrics.dimensions]]
|
2016-11-04 13:16:41 +00:00
|
|
|
# name = "LoadBalancerName"
|
|
|
|
# value = "p-example"
|
2016-03-25 22:16:23 +00:00
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CloudWatch) Description() string {
|
|
|
|
return "Pull Metric Statistics from Amazon CloudWatch"
|
|
|
|
}
|
|
|
|
|
2016-12-13 14:13:53 +00:00
|
|
|
func SelectMetrics(c *CloudWatch) ([]*cloudwatch.Metric, error) {
|
2016-03-25 22:16:23 +00:00
|
|
|
var metrics []*cloudwatch.Metric
|
|
|
|
|
|
|
|
// check for provided metric filter
|
|
|
|
if c.Metrics != nil {
|
|
|
|
metrics = []*cloudwatch.Metric{}
|
|
|
|
for _, m := range c.Metrics {
|
2016-05-25 11:30:39 +00:00
|
|
|
if !hasWilcard(m.Dimensions) {
|
|
|
|
dimensions := make([]*cloudwatch.Dimension, len(m.Dimensions))
|
|
|
|
for k, d := range m.Dimensions {
|
|
|
|
dimensions[k] = &cloudwatch.Dimension{
|
|
|
|
Name: aws.String(d.Name),
|
|
|
|
Value: aws.String(d.Value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, name := range m.MetricNames {
|
|
|
|
metrics = append(metrics, &cloudwatch.Metric{
|
|
|
|
Namespace: aws.String(c.Namespace),
|
|
|
|
MetricName: aws.String(name),
|
|
|
|
Dimensions: dimensions,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
allMetrics, err := c.fetchNamespaceMetrics()
|
|
|
|
if err != nil {
|
2016-12-13 14:13:53 +00:00
|
|
|
return nil, err
|
2016-05-25 11:30:39 +00:00
|
|
|
}
|
|
|
|
for _, name := range m.MetricNames {
|
|
|
|
for _, metric := range allMetrics {
|
2016-12-13 14:13:53 +00:00
|
|
|
if isSelected(name, metric, m.Dimensions) {
|
2016-05-25 11:30:39 +00:00
|
|
|
metrics = append(metrics, &cloudwatch.Metric{
|
|
|
|
Namespace: aws.String(c.Namespace),
|
|
|
|
MetricName: aws.String(name),
|
|
|
|
Dimensions: metric.Dimensions,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-03-25 22:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
metrics, err = c.fetchNamespaceMetrics()
|
|
|
|
if err != nil {
|
2016-12-13 14:13:53 +00:00
|
|
|
return nil, err
|
2016-03-25 22:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-13 14:13:53 +00:00
|
|
|
return metrics, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CloudWatch) Gather(acc telegraf.Accumulator) error {
|
|
|
|
if c.client == nil {
|
|
|
|
c.initializeCloudWatch()
|
|
|
|
}
|
2016-03-25 22:16:23 +00:00
|
|
|
|
2016-12-13 14:13:53 +00:00
|
|
|
metrics, err := SelectMetrics(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-25 22:16:23 +00:00
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
// limit concurrency or we can easily exhaust user connection limit
|
2016-05-24 13:50:01 +00:00
|
|
|
// see cloudwatch API request limits:
|
|
|
|
// http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/cloudwatch_limits.html
|
2016-08-26 00:46:38 +00:00
|
|
|
lmtr := limiter.NewRateLimiter(c.RateLimit, time.Second)
|
2016-05-24 13:50:01 +00:00
|
|
|
defer lmtr.Stop()
|
2016-06-02 11:34:03 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(metrics))
|
2016-03-25 22:16:23 +00:00
|
|
|
for _, m := range metrics {
|
2016-05-24 13:50:01 +00:00
|
|
|
<-lmtr.C
|
2016-06-02 11:34:03 +00:00
|
|
|
go func(inm *cloudwatch.Metric) {
|
|
|
|
defer wg.Done()
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.AddError(c.gatherMetric(acc, inm, now))
|
2016-06-02 11:34:03 +00:00
|
|
|
}(m)
|
2016-03-25 22:16:23 +00:00
|
|
|
}
|
2016-06-02 11:34:03 +00:00
|
|
|
wg.Wait()
|
2016-03-25 22:16:23 +00:00
|
|
|
|
2017-04-24 18:13:26 +00:00
|
|
|
return nil
|
2016-03-25 22:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
inputs.Add("cloudwatch", func() telegraf.Input {
|
2016-05-25 11:30:39 +00:00
|
|
|
ttl, _ := time.ParseDuration("1hr")
|
|
|
|
return &CloudWatch{
|
2016-08-30 21:33:51 +00:00
|
|
|
CacheTTL: internal.Duration{Duration: ttl},
|
2017-03-15 22:20:18 +00:00
|
|
|
RateLimit: 200,
|
2016-05-25 11:30:39 +00:00
|
|
|
}
|
2016-03-25 22:16:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize CloudWatch client
|
|
|
|
*/
|
|
|
|
func (c *CloudWatch) initializeCloudWatch() error {
|
2016-05-25 11:30:39 +00:00
|
|
|
credentialConfig := &internalaws.CredentialConfig{
|
|
|
|
Region: c.Region,
|
|
|
|
AccessKey: c.AccessKey,
|
|
|
|
SecretKey: c.SecretKey,
|
|
|
|
RoleARN: c.RoleARN,
|
|
|
|
Profile: c.Profile,
|
|
|
|
Filename: c.Filename,
|
|
|
|
Token: c.Token,
|
2016-04-23 18:19:04 +00:00
|
|
|
}
|
2016-05-25 11:30:39 +00:00
|
|
|
configProvider := credentialConfig.Credentials()
|
2016-03-25 22:16:23 +00:00
|
|
|
|
2016-05-25 11:30:39 +00:00
|
|
|
c.client = cloudwatch.New(configProvider)
|
2016-03-25 22:16:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fetch available metrics for given CloudWatch Namespace
|
|
|
|
*/
|
2016-11-04 13:16:41 +00:00
|
|
|
func (c *CloudWatch) fetchNamespaceMetrics() ([]*cloudwatch.Metric, error) {
|
2016-03-25 22:16:23 +00:00
|
|
|
if c.metricCache != nil && c.metricCache.IsValid() {
|
2016-11-04 13:16:41 +00:00
|
|
|
return c.metricCache.Metrics, nil
|
2016-03-25 22:16:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-04 13:16:41 +00:00
|
|
|
metrics := []*cloudwatch.Metric{}
|
2016-03-25 22:16:23 +00:00
|
|
|
|
|
|
|
var token *string
|
|
|
|
for more := true; more; {
|
|
|
|
params := &cloudwatch.ListMetricsInput{
|
|
|
|
Namespace: aws.String(c.Namespace),
|
|
|
|
Dimensions: []*cloudwatch.DimensionFilter{},
|
|
|
|
NextToken: token,
|
|
|
|
MetricName: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.client.ListMetrics(params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics = append(metrics, resp.Metrics...)
|
|
|
|
|
|
|
|
token = resp.NextToken
|
|
|
|
more = token != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c.metricCache = &MetricCache{
|
|
|
|
Metrics: metrics,
|
|
|
|
Fetched: time.Now(),
|
2016-05-25 11:30:39 +00:00
|
|
|
TTL: c.CacheTTL.Duration,
|
2016-03-25 22:16:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-04 13:16:41 +00:00
|
|
|
return metrics, nil
|
2016-03-25 22:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Gather given Metric and emit any error
|
|
|
|
*/
|
2016-05-24 13:50:01 +00:00
|
|
|
func (c *CloudWatch) gatherMetric(
|
|
|
|
acc telegraf.Accumulator,
|
|
|
|
metric *cloudwatch.Metric,
|
|
|
|
now time.Time,
|
2017-04-24 18:13:26 +00:00
|
|
|
) error {
|
2016-03-25 22:16:23 +00:00
|
|
|
params := c.getStatisticsInput(metric, now)
|
|
|
|
resp, err := c.client.GetMetricStatistics(params)
|
|
|
|
if err != nil {
|
2017-04-24 18:13:26 +00:00
|
|
|
return err
|
2016-03-25 22:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, point := range resp.Datapoints {
|
|
|
|
tags := map[string]string{
|
|
|
|
"region": c.Region,
|
|
|
|
"unit": snakeCase(*point.Unit),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range metric.Dimensions {
|
|
|
|
tags[snakeCase(*d.Name)] = *d.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// record field for each statistic
|
|
|
|
fields := map[string]interface{}{}
|
|
|
|
|
|
|
|
if point.Average != nil {
|
|
|
|
fields[formatField(*metric.MetricName, cloudwatch.StatisticAverage)] = *point.Average
|
|
|
|
}
|
|
|
|
if point.Maximum != nil {
|
|
|
|
fields[formatField(*metric.MetricName, cloudwatch.StatisticMaximum)] = *point.Maximum
|
|
|
|
}
|
|
|
|
if point.Minimum != nil {
|
|
|
|
fields[formatField(*metric.MetricName, cloudwatch.StatisticMinimum)] = *point.Minimum
|
|
|
|
}
|
|
|
|
if point.SampleCount != nil {
|
|
|
|
fields[formatField(*metric.MetricName, cloudwatch.StatisticSampleCount)] = *point.SampleCount
|
|
|
|
}
|
|
|
|
if point.Sum != nil {
|
|
|
|
fields[formatField(*metric.MetricName, cloudwatch.StatisticSum)] = *point.Sum
|
|
|
|
}
|
|
|
|
|
|
|
|
acc.AddFields(formatMeasurement(c.Namespace), fields, tags, *point.Timestamp)
|
|
|
|
}
|
|
|
|
|
2017-04-24 18:13:26 +00:00
|
|
|
return nil
|
2016-03-25 22:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Formatting helpers
|
|
|
|
*/
|
|
|
|
func formatField(metricName string, statistic string) string {
|
|
|
|
return fmt.Sprintf("%s_%s", snakeCase(metricName), snakeCase(statistic))
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatMeasurement(namespace string) string {
|
|
|
|
namespace = strings.Replace(namespace, "/", "_", -1)
|
|
|
|
namespace = snakeCase(namespace)
|
|
|
|
return fmt.Sprintf("cloudwatch_%s", namespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
func snakeCase(s string) string {
|
|
|
|
s = internal.SnakeCase(s)
|
|
|
|
s = strings.Replace(s, "__", "_", -1)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Map Metric to *cloudwatch.GetMetricStatisticsInput for given timeframe
|
|
|
|
*/
|
|
|
|
func (c *CloudWatch) getStatisticsInput(metric *cloudwatch.Metric, now time.Time) *cloudwatch.GetMetricStatisticsInput {
|
|
|
|
end := now.Add(-c.Delay.Duration)
|
|
|
|
|
|
|
|
input := &cloudwatch.GetMetricStatisticsInput{
|
|
|
|
StartTime: aws.Time(end.Add(-c.Period.Duration)),
|
|
|
|
EndTime: aws.Time(end),
|
|
|
|
MetricName: metric.MetricName,
|
|
|
|
Namespace: metric.Namespace,
|
|
|
|
Period: aws.Int64(int64(c.Period.Duration.Seconds())),
|
|
|
|
Dimensions: metric.Dimensions,
|
|
|
|
Statistics: []*string{
|
|
|
|
aws.String(cloudwatch.StatisticAverage),
|
|
|
|
aws.String(cloudwatch.StatisticMaximum),
|
|
|
|
aws.String(cloudwatch.StatisticMinimum),
|
|
|
|
aws.String(cloudwatch.StatisticSum),
|
|
|
|
aws.String(cloudwatch.StatisticSampleCount)},
|
|
|
|
}
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check Metric Cache validity
|
|
|
|
*/
|
|
|
|
func (c *MetricCache) IsValid() bool {
|
|
|
|
return c.Metrics != nil && time.Since(c.Fetched) < c.TTL
|
|
|
|
}
|
2016-05-25 11:30:39 +00:00
|
|
|
|
|
|
|
func hasWilcard(dimensions []*Dimension) bool {
|
|
|
|
for _, d := range dimensions {
|
|
|
|
if d.Value == "" || d.Value == "*" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-12-13 14:13:53 +00:00
|
|
|
func isSelected(name string, metric *cloudwatch.Metric, dimensions []*Dimension) bool {
|
|
|
|
if name != *metric.MetricName {
|
|
|
|
return false
|
|
|
|
}
|
2016-05-25 11:30:39 +00:00
|
|
|
if len(metric.Dimensions) != len(dimensions) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, d := range dimensions {
|
|
|
|
selected := false
|
|
|
|
for _, d2 := range metric.Dimensions {
|
|
|
|
if d.Name == *d2.Name {
|
|
|
|
if d.Value == "" || d.Value == "*" || d.Value == *d2.Value {
|
|
|
|
selected = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !selected {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|