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
|
#GoLang AWS Cloudwatch
## Installation
Please refer to the project's main page at [https://github.com/goamz/goamz](https://github.com/goamz/goamz) for instructions about how to install.
## Available methods
<table>
<tr>
<td>GetMetricStatistics</td>
<td>Gets statistics for the specified metric.</td>
</tr>
<tr>
<td>ListMetrics</td>
<td>Returns a list of valid metrics stored for the AWS account.</td>
</tr>
<tr>
<td>PutMetricData</td>
<td>Publishes metric data points to Amazon CloudWatch.</td>
</tr>
<tr>
<td>PutMetricAlarm</td>
<td>Creates or updates an alarm and associates it with the specified Amazon CloudWatch metric.</td>
</tr>
</table>
[Please refer to AWS Cloudwatch's documentation for more info](http://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_Operations.html)
##Examples
####Get Metric Statistics
```
import (
"fmt"
"time"
"os"
"github.com/goamz/goamz/aws"
"github.com/goamz/goamz/cloudwatch"
)
func test_get_metric_statistics() {
region := aws.Regions["a_region"]
namespace:= "AWS/ELB"
dimension := &cloudwatch.Dimension{
Name: "LoadBalancerName",
Value: "your_value",
}
metricName := "RequestCount"
now := time.Now()
prev := now.Add(time.Duration(600)*time.Second*-1) // 600 secs = 10 minutes
auth, err := aws.GetAuth("your_AccessKeyId", "your_SecretAccessKey", "", now)
if err != nil {
fmt.Printf("Error: %+v\n", err)
os.Exit(1)
}
cw, err := cloudwatch.NewCloudWatch(auth, region.CloudWatchServicepoint)
request := &cloudwatch.GetMetricStatisticsRequest {
Dimensions: []cloudwatch.Dimension{*dimension},
EndTime: now,
StartTime: prev,
MetricName: metricName,
Unit: "Count", // Not mandatory
Period: 60,
Statistics: []string{"Sum"},
Namespace: namespace,
}
response, err := cw.GetMetricStatistics(request)
if err == nil {
fmt.Printf("%+v\n", response)
} else {
fmt.Printf("Error: %+v\n", err)
}
}
```
####List Metrics
```
import (
"fmt"
"time"
"os"
"github.com/goamz/goamz/aws"
"github.com/goamz/goamz/cloudwatch"
)
func test_list_metrics() {
region := aws.Regions["us-east-1"] // Any region here
now := time.Now()
auth, err := aws.GetAuth("an AccessKeyId", "a SecretAccessKey", "", now)
if err != nil {
fmt.Printf("Error: %+v\n", err)
os.Exit(1)
}
cw, err := cloudwatch.NewCloudWatch(auth, region.CloudWatchServicepoint)
request := &cloudwatch.ListMetricsRequest{Namespace: "AWS/EC2"}
response, err := cw.ListMetrics(request)
if err == nil {
fmt.Printf("%+v\n", response)
} else {
fmt.Printf("Error: %+v\n", err)
}
}
```
|