forked from mackerelio/mackerel-agent-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-rds.go
257 lines (233 loc) · 6.62 KB
/
aws-rds.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package mpawsrds
import (
"errors"
"flag"
"log"
"os"
"strings"
"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"
)
// RDSPlugin mackerel plugin for amazon RDS
type RDSPlugin struct {
Region string
AccessKeyID string
SecretAccessKey string
Identifier string
Engine string
Prefix string
LabelPrefix string
}
func getLastPoint(cloudWatch *cloudwatch.CloudWatch, dimension *cloudwatch.Dimension, metricName string) (float64, error) {
now := time.Now()
response, err := cloudWatch.GetMetricStatistics(&cloudwatch.GetMetricStatisticsInput{
Dimensions: []*cloudwatch.Dimension{dimension},
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(metricName),
Period: aws.Int64(60),
Statistics: []*string{aws.String("Average")},
Namespace: aws.String("AWS/RDS"),
})
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 interface for mackerel-plugin
func (p RDSPlugin) 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)
}
cloudWatch := cloudwatch.New(sess, config)
stat := make(map[string]float64)
perInstance := &cloudwatch.Dimension{
Name: aws.String("DBInstanceIdentifier"),
Value: aws.String(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 (p RDSPlugin) baseGraphDefs() map[string]mp.Graphs {
return map[string]mp.Graphs{
p.Prefix + ".DiskQueueDepth": {
Label: p.LabelPrefix + " BinLogDiskUsage",
Unit: "bytes",
Metrics: []mp.Metrics{
{Name: "DiskQueueDepth", Label: "Depth"},
},
},
p.Prefix + ".CPUUtilization": {
Label: p.LabelPrefix + " CPU Utilization",
Unit: "percentage",
Metrics: []mp.Metrics{
{Name: "CPUUtilization", Label: "CPUUtilization"},
},
},
// .CPUCreditBalance ...Only valid for T2 instances
p.Prefix + ".CPUCreditBalance": {
Label: p.LabelPrefix + " CPU CreditBalance",
Unit: "float",
Metrics: []mp.Metrics{
{Name: "CPUCreditBalance", Label: "CPUCreditBalance"},
},
},
// .CPUCreditUsage ...Only valid for T2 instances
p.Prefix + ".CPUCreditUsage": {
Label: p.LabelPrefix + " CPU CreditUsage",
Unit: "float",
Metrics: []mp.Metrics{
{Name: "CPUCreditUsage", Label: "CPUCreditUsage"},
},
},
p.Prefix + ".DatabaseConnections": {
Label: p.LabelPrefix + " Database Connections",
Unit: "float",
Metrics: []mp.Metrics{
{Name: "DatabaseConnections", Label: "DatabaseConnections"},
},
},
p.Prefix + ".FreeableMemory": {
Label: p.LabelPrefix + " Freeable Memory",
Unit: "bytes",
Metrics: []mp.Metrics{
{Name: "FreeableMemory", Label: "FreeableMemory"},
},
},
p.Prefix + ".FreeStorageSpace": {
Label: p.LabelPrefix + " Free Storage Space",
Unit: "bytes",
Metrics: []mp.Metrics{
{Name: "FreeStorageSpace", Label: "FreeStorageSpace"},
},
},
p.Prefix + ".SwapUsage": {
Label: p.LabelPrefix + " Swap Usage",
Unit: "bytes",
Metrics: []mp.Metrics{
{Name: "SwapUsage", Label: "SwapUsage"},
},
},
p.Prefix + ".IOPS": {
Label: p.LabelPrefix + " IOPS",
Unit: "iops",
Metrics: []mp.Metrics{
{Name: "ReadIOPS", Label: "Read"},
{Name: "WriteIOPS", Label: "Write"},
},
},
p.Prefix + ".Latency": {
Label: p.LabelPrefix + " Latency in second",
Unit: "float",
Metrics: []mp.Metrics{
{Name: "ReadLatency", Label: "Read"},
{Name: "WriteLatency", Label: "Write"},
},
},
p.Prefix + ".Throughput": {
Label: p.LabelPrefix + " Throughput",
Unit: "bytes/sec",
Metrics: []mp.Metrics{
{Name: "ReadThroughput", Label: "Read"},
{Name: "WriteThroughput", Label: "Write"},
},
},
p.Prefix + ".NetworkThroughput": {
Label: p.LabelPrefix + " Network Throughput",
Unit: "bytes/sec",
Metrics: []mp.Metrics{
{Name: "NetworkTransmitThroughput", Label: "Transmit"},
{Name: "NetworkReceiveThroughput", Label: "Receive"},
},
},
}
}
// GraphDefinition interface for mackerel plugin
func (p RDSPlugin) GraphDefinition() map[string]mp.Graphs {
graphdef := p.baseGraphDefs()
switch p.Engine {
case "mysql", "mariadb":
graphdef = mergeGraphDefs(graphdef, p.mySQLGraphDefinition())
case "postgresql":
graphdef = mergeGraphDefs(graphdef, p.postgreSQLGraphDefinition())
case "aurora":
graphdef = p.auroraGraphDefinition()
}
return graphdef
}
// Do the plugin
func Do() {
optRegion := flag.String("region", "", "AWS Region")
optAccessKeyID := flag.String("access-key-id", "", "AWS Access Key ID")
optSecretAccessKey := flag.String("secret-access-key", "", "AWS Secret Access Key")
optIdentifier := flag.String("identifier", "", "DB Instance Identifier")
optEngine := flag.String("engine", "", "RDS Engine")
optPrefix := flag.String("metric-key-prefix", "rds", "Metric key prefix")
optLabelPrefix := flag.String("metric-label-prefix", "", "Metric Label prefix")
optTempfile := flag.String("tempfile", "", "Temp file name")
flag.Parse()
rds := RDSPlugin{
Prefix: *optPrefix,
}
if *optLabelPrefix == "" {
if *optPrefix == "rds" {
rds.LabelPrefix = "RDS"
} else {
rds.LabelPrefix = strings.Title(*optPrefix)
}
} else {
rds.LabelPrefix = *optLabelPrefix
}
if *optRegion == "" {
ec2metadata := ec2metadata.New(session.New())
if ec2metadata.Available() {
rds.Region, _ = ec2metadata.Region()
}
} else {
rds.Region = *optRegion
}
rds.Identifier = *optIdentifier
rds.AccessKeyID = *optAccessKeyID
rds.SecretAccessKey = *optSecretAccessKey
rds.Engine = *optEngine
helper := mp.NewMackerelPlugin(rds)
helper.Tempfile = *optTempfile
if os.Getenv("MACKEREL_AGENT_PLUGIN_META") != "" {
helper.OutputDefinitions()
} else {
helper.OutputValues()
}
}