forked from mackerelio/mackerel-agent-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-ec2-cpucredit.go
150 lines (124 loc) · 3.66 KB
/
aws-ec2-cpucredit.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
package mpawsec2cpucredit
import (
"errors"
"flag"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
mp "github.com/mackerelio/go-mackerel-plugin"
)
var graphdef = map[string]mp.Graphs{
"ec2.cpucredit": {
Label: "EC2 CPU Credit",
Unit: "float",
Metrics: []mp.Metrics{
{Name: "usage", Label: "Usage", Diff: false},
{Name: "balance", Label: "Balance", Diff: false},
},
},
}
// CPUCreditPlugin is a mackerel plugin
type CPUCreditPlugin struct {
Region string
AccessKeyID string
SecretAccessKey string
InstanceID string
}
func getLastPointAverage(cw *cloudwatch.CloudWatch, dimension *cloudwatch.Dimension, metricName string) (float64, error) {
namespace := "AWS/EC2"
now := time.Now()
prev := now.Add(time.Duration(600) * time.Second * -1) // 10 min (to fetch at least 1 data-point)
input := &cloudwatch.GetMetricStatisticsInput{
Dimensions: []*cloudwatch.Dimension{dimension},
EndTime: aws.Time(now),
StartTime: aws.Time(prev),
MetricName: aws.String(metricName),
Period: aws.Int64(60),
Statistics: []*string{aws.String("Average")},
Namespace: aws.String(namespace),
}
response, err := cw.GetMetricStatistics(input)
if err != nil {
return 0, err
}
datapoints := response.Datapoints
if len(datapoints) == 0 {
return 0, errors.New("fetched no datapoints")
}
latest := new(time.Time)
var latestVal float64
for _, dp := range datapoints {
if dp.Timestamp.Before(*latest) {
continue
}
latest = dp.Timestamp
latestVal = *dp.Average
}
return latestVal, nil
}
// FetchMetrics fetch the metrics
func (p CPUCreditPlugin) FetchMetrics() (map[string]float64, error) {
sess, err := session.NewSession()
if err != nil {
return nil, err
}
config := aws.NewConfig()
if p.AccessKeyID != "" && p.SecretAccessKey != "" {
config = config.WithCredentials(credentials.NewStaticCredentials(p.AccessKeyID, p.SecretAccessKey, ""))
}
if p.Region != "" {
config = config.WithRegion(p.Region)
}
cw := cloudwatch.New(sess, config)
dimension := &cloudwatch.Dimension{
Name: aws.String("InstanceId"),
Value: aws.String(p.InstanceID),
}
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
}
// GraphDefinition for plugin
func (p CPUCreditPlugin) GraphDefinition() map[string]mp.Graphs {
return graphdef
}
// Do the plugin
func Do() {
optRegion := flag.String("region", "", "AWS Region")
optInstanceID := flag.String("instance-id", "", "Instance ID")
optAccessKeyID := flag.String("access-key-id", "", "AWS Access Key ID")
optSecretAccessKey := flag.String("secret-access-key", "", "AWS Secret Access Key")
optTempfile := flag.String("tempfile", "", "Temp file name")
flag.Parse()
var cpucredit CPUCreditPlugin
if *optRegion == "" || *optInstanceID == "" {
ec2metadata := ec2metadata.New(session.New())
if ec2metadata.Available() {
cpucredit.Region, _ = ec2metadata.Region()
cpucredit.InstanceID, _ = ec2metadata.GetMetadata("instance-id")
}
} else {
cpucredit.Region = *optRegion
cpucredit.InstanceID = *optInstanceID
}
cpucredit.AccessKeyID = *optAccessKeyID
cpucredit.SecretAccessKey = *optSecretAccessKey
helper := mp.NewMackerelPlugin(cpucredit)
helper.Tempfile = *optTempfile
if os.Getenv("MACKEREL_AGENT_PLUGIN_META") != "" {
helper.OutputDefinitions()
} else {
helper.OutputValues()
}
}