-
Notifications
You must be signed in to change notification settings - Fork 2
/
rds_usage.go
69 lines (57 loc) · 2.12 KB
/
rds_usage.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
package stats
import (
"fmt"
"strconv"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/rds"
)
// GetRDSUsage gets RDS usage for given sessions within specified period of time.
func GetRDSUsage(sess *session.Session, startTime, endTime time.Time) (RDSUsage map[string]string) {
RDSUsage = make(map[string]string)
svc := rds.New(sess)
// List clusters
respDescribeDBClusters, err := svc.DescribeDBClusters(&rds.DescribeDBClustersInput{})
if err != nil {
fmt.Println(err.Error())
} else {
count := len(respDescribeDBClusters.DBClusters)
if count > 0 {
RDSUsage["Clusters"] = strconv.Itoa(count)
}
}
// List DB Instances
respDescribeDBInstances, err := svc.DescribeDBInstances(&rds.DescribeDBInstancesInput{})
if err != nil {
fmt.Println(err.Error())
} else {
count := len(respDescribeDBInstances.DBInstances)
if count > 0 {
RDSUsage["Instances"] = strconv.Itoa(count)
}
}
svcCloudWatch := cloudwatch.New(sess)
// Get CPU Usage
cpuUsage := getMetricsStatistics(svcCloudWatch, startTime, endTime, aws.String("AWS/RDS"), aws.String("CPUUtilization"), "Average", []*cloudwatch.Dimension{})[0]
if cpuUsage > 0 {
RDSUsage["CPU"] = fmt.Sprintf("%0.2f%%", cpuUsage)
}
// Get Queries
queries := getMetricsStatistics(svcCloudWatch, startTime, endTime, aws.String("AWS/RDS"), aws.String("Queries"), "Average", []*cloudwatch.Dimension{})[0]
if queries > 0 {
RDSUsage["Queries"] = fmt.Sprintf("%0.2f/Second", queries)
}
// Get NetworkThroughput
throughput := getMetricsStatistics(svcCloudWatch, startTime, endTime, aws.String("AWS/RDS"), aws.String("NetworkThroughput"), "Average", []*cloudwatch.Dimension{})[0]
if throughput > 0 {
RDSUsage["NetworkThroughput"] = fmt.Sprintf("%s/Second", formatStorage(throughput))
}
// Get Deadlocks
deadlocks := getMetricsStatistics(svcCloudWatch, startTime, endTime, aws.String("AWS/RDS"), aws.String("Deadlocks"), "Average", []*cloudwatch.Dimension{})[0]
if deadlocks > 0 {
RDSUsage["Deadlocks"] = fmt.Sprintf("%0.2f/Second", deadlocks)
}
return RDSUsage
}