forked from mackerelio/mackerel-agent-plugins
/
aws-cloudfront.go
192 lines (165 loc) · 4.61 KB
/
aws-cloudfront.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package mpawscloudfront
import (
"errors"
"flag"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
mp "github.com/mackerelio/go-mackerel-plugin"
)
const (
namespace = "AWS/CloudFront"
region = "us-east-1"
metricsTypeAverage = "Average"
metricsTypeSum = "Sum"
)
type metrics struct {
Name string
Type string
}
// CloudFrontPlugin mackerel plugin for cloudfront
type CloudFrontPlugin struct {
AccessKeyID string
SecretAccessKey string
CloudWatch *cloudwatch.CloudWatch
Name string
Prefix string
}
// MetricKeyPrefix interface for PluginWithPrefix
func (p CloudFrontPlugin) MetricKeyPrefix() string {
if p.Prefix == "" {
p.Prefix = "cloudfront"
}
return p.Prefix
}
func (p *CloudFrontPlugin) prepare() error {
sess, err := session.NewSession()
if err != nil {
return err
}
config := aws.NewConfig()
if p.AccessKeyID != "" && p.SecretAccessKey != "" {
config = config.WithCredentials(credentials.NewStaticCredentials(p.AccessKeyID, p.SecretAccessKey, ""))
}
config = config.WithRegion(region)
p.CloudWatch = cloudwatch.New(sess, config)
return nil
}
func (p CloudFrontPlugin) getLastPoint(metric metrics) (float64, error) {
now := time.Now()
dimensions := []*cloudwatch.Dimension{
{
Name: aws.String("DistributionId"),
Value: aws.String(p.Name),
},
{
Name: aws.String("Region"),
Value: aws.String("Global"),
},
}
response, err := p.CloudWatch.GetMetricStatistics(&cloudwatch.GetMetricStatisticsInput{
Dimensions: dimensions,
StartTime: aws.Time(now.Add(time.Duration(180) * time.Second * -1)), // 3 min (to fetch at least 1 data-point)
EndTime: aws.Time(now),
MetricName: aws.String(metric.Name),
Period: aws.Int64(60),
Statistics: []*string{aws.String(metric.Type)},
Namespace: aws.String(namespace),
})
if err != nil {
return 0, err
}
datapoints := response.Datapoints
if len(datapoints) == 0 {
return 0, errors.New("fetched no datapoints")
}
// get a least recently datapoint
// because a most recently datapoint is not stable.
least := time.Now()
var latestVal float64
for _, dp := range datapoints {
if dp.Timestamp.Before(least) {
least = *dp.Timestamp
if metric.Type == metricsTypeAverage {
latestVal = *dp.Average
} else if metric.Type == metricsTypeSum {
latestVal = *dp.Sum
}
}
}
return latestVal, nil
}
// FetchMetrics fetch the metrics
func (p CloudFrontPlugin) FetchMetrics() (map[string]float64, error) {
stat := make(map[string]float64)
for _, met := range [...]metrics{
{Name: "Requests", Type: metricsTypeSum},
{Name: "BytesDownloaded", Type: metricsTypeSum},
{Name: "BytesUploaded", Type: metricsTypeSum},
{Name: "4xxErrorRate", Type: metricsTypeAverage},
{Name: "5xxErrorRate", Type: metricsTypeAverage},
} {
v, err := p.getLastPoint(met)
if err == nil {
stat[met.Name] = v
} else {
log.Printf("%s: %s", met, err)
}
}
return stat, nil
}
// GraphDefinition of CloudFrontPlugin
func (p CloudFrontPlugin) GraphDefinition() map[string]mp.Graphs {
labelPrefix := strings.Title(p.Prefix)
labelPrefix = strings.Replace(labelPrefix, "-", " ", -1)
return map[string]mp.Graphs{
"Requests": {
Label: labelPrefix + " Requests",
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "Requests", Label: "Requests"},
},
},
"Transfer": {
Label: labelPrefix + " Transfer",
Unit: "bytes",
Metrics: []mp.Metrics{
{Name: "BytesDownloaded", Label: "Download", Stacked: true},
{Name: "BytesUploaded", Label: "Upload", Stacked: true},
},
},
"ErrorRate": {
Label: labelPrefix + " ErrorRate",
Unit: "percentage",
Metrics: []mp.Metrics{
{Name: "4xxErrorRate", Label: "4xx", Stacked: true},
{Name: "5xxErrorRate", Label: "5xx", Stacked: true},
},
},
}
}
// Do the plugin
func Do() {
optAccessKeyID := flag.String("access-key-id", "", "AWS Access Key ID")
optSecretAccessKey := flag.String("secret-access-key", "", "AWS Secret Access Key")
optIdentifier := flag.String("identifier", "", "Distribution ID")
optTempfile := flag.String("tempfile", "", "Temp file name")
optPrefix := flag.String("metric-key-prefix", "cloudfront", "Metric key prefix")
flag.Parse()
var plugin CloudFrontPlugin
plugin.AccessKeyID = *optAccessKeyID
plugin.SecretAccessKey = *optSecretAccessKey
plugin.Name = *optIdentifier
plugin.Prefix = *optPrefix
err := plugin.prepare()
if err != nil {
log.Fatalln(err)
}
helper := mp.NewMackerelPlugin(plugin)
helper.Tempfile = *optTempfile
helper.Run()
}