Skip to content

Commit f79b479

Browse files
authored
Merge pull request #39 from xibz/master
Creating alarms in cloudwatch. Thanks, Ben!
2 parents a64cd91 + 1a63ef3 commit f79b479

File tree

4 files changed

+189
-0
lines changed

4 files changed

+189
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/aws/aws-sdk-go/aws"
8+
"github.com/aws/aws-sdk-go/aws/session"
9+
"github.com/aws/aws-sdk-go/service/cloudwatch"
10+
)
11+
12+
// Usage:
13+
// go run main.go <customer id> <instance id> <alarm name>
14+
func main() {
15+
// Load session from shared config.
16+
sess := session.Must(session.NewSessionWithOptions(session.Options{
17+
SharedConfigState: session.SharedConfigEnable,
18+
}))
19+
20+
// Create new cloudwatch client.
21+
svc := cloudwatch.New(sess)
22+
23+
// Create a metric alarm that will reboot an instance if its CPU utilization is
24+
// greate than 70.0%.
25+
_, err := svc.PutMetricAlarm(&cloudwatch.PutMetricAlarmInput{
26+
AlarmName: &os.Args[3],
27+
ComparisonOperator: aws.String(cloudwatch.ComparisonOperatorGreaterThanThreshold),
28+
EvaluationPeriods: aws.Int64(1),
29+
MetricName: aws.String("CPUUtilization"),
30+
Namespace: aws.String("AWS/EC2"),
31+
Period: aws.Int64(60),
32+
Statistic: aws.String(cloudwatch.StatisticAverage),
33+
Threshold: aws.Float64(70.0),
34+
ActionsEnabled: aws.Bool(true),
35+
AlarmDescription: aws.String("Alarm when server CPU exceeds 70%"),
36+
Unit: aws.String(cloudwatch.StandardUnitSeconds),
37+
38+
// This is apart of the default workflow actions. This one will reboot the instance, if the
39+
// alarm is triggered.
40+
AlarmActions: []*string{
41+
aws.String(fmt.Sprintf("arn:aws:swf:us-east-1:%s:action/actions/AWS_EC2.InstanceId.Reboot/1.0", os.Args[1])),
42+
},
43+
Dimensions: []*cloudwatch.Dimension{
44+
&cloudwatch.Dimension{
45+
Name: aws.String("InstanceId"),
46+
Value: &os.Args[2],
47+
},
48+
},
49+
})
50+
51+
if err != nil {
52+
fmt.Println("Error", err)
53+
return
54+
}
55+
56+
// This will enable the alarm to our instance.
57+
result, err := svc.EnableAlarmActions(&cloudwatch.EnableAlarmActionsInput{
58+
AlarmNames: []*string{
59+
&os.Args[3],
60+
},
61+
})
62+
63+
if err != nil {
64+
fmt.Println("Error", err)
65+
return
66+
}
67+
68+
fmt.Println("Alarm action enabled", result)
69+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/aws/aws-sdk-go/aws"
7+
"github.com/aws/aws-sdk-go/aws/session"
8+
"github.com/aws/aws-sdk-go/service/cloudwatch"
9+
)
10+
11+
// Usage:
12+
// go run main.go
13+
func main() {
14+
// Load session from shared config.
15+
sess := session.Must(session.NewSessionWithOptions(session.Options{
16+
SharedConfigState: session.SharedConfigEnable,
17+
}))
18+
19+
// Create new cloudwatch client.
20+
svc := cloudwatch.New(sess)
21+
22+
// This will disable the alarm.
23+
result, err := svc.PutMetricData(&cloudwatch.PutMetricDataInput{
24+
MetricData: []*cloudwatch.MetricDatum{
25+
&cloudwatch.MetricDatum{
26+
MetricName: aws.String("PAGES_VISITED"),
27+
Unit: aws.String(cloudwatch.StandardUnitNone),
28+
Value: aws.Float64(1.0),
29+
Dimensions: []*cloudwatch.Dimension{
30+
&cloudwatch.Dimension{
31+
Name: aws.String("UNIQUE_PAGES"),
32+
Value: aws.String("URLS"),
33+
},
34+
},
35+
},
36+
},
37+
Namespace: aws.String("SITE/TRAFFIC"),
38+
})
39+
40+
if err != nil {
41+
fmt.Println("Error", err)
42+
return
43+
}
44+
45+
fmt.Println("Success", result)
46+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/aws/aws-sdk-go/aws/session"
8+
"github.com/aws/aws-sdk-go/service/cloudwatch"
9+
)
10+
11+
// Usage:
12+
// go run main.go <alarm name>
13+
func main() {
14+
// Load session from shared config.
15+
sess := session.Must(session.NewSessionWithOptions(session.Options{
16+
SharedConfigState: session.SharedConfigEnable,
17+
}))
18+
19+
// Create new cloudwatch client.
20+
svc := cloudwatch.New(sess)
21+
22+
// This will disable the alarm.
23+
result, err := svc.DisableAlarmActions(&cloudwatch.DisableAlarmActionsInput{
24+
AlarmNames: []*string{
25+
&os.Args[1],
26+
},
27+
})
28+
29+
if err != nil {
30+
fmt.Println("Error", err)
31+
return
32+
}
33+
34+
fmt.Println("Success", result)
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/aws/aws-sdk-go/aws"
7+
"github.com/aws/aws-sdk-go/aws/session"
8+
"github.com/aws/aws-sdk-go/service/cloudwatch"
9+
)
10+
11+
// Usage:
12+
// go run main.go
13+
func main() {
14+
// Load session from shared config.
15+
sess := session.Must(session.NewSessionWithOptions(session.Options{
16+
SharedConfigState: session.SharedConfigEnable,
17+
}))
18+
19+
// Create new cloudwatch client.
20+
svc := cloudwatch.New(sess)
21+
22+
// This will disable the alarm.
23+
result, err := svc.ListMetrics(&cloudwatch.ListMetricsInput{
24+
MetricName: aws.String("IncomingLogEvents"),
25+
Namespace: aws.String("AWS/Logs"),
26+
Dimensions: []*cloudwatch.DimensionFilter{
27+
&cloudwatch.DimensionFilter{
28+
Name: aws.String("LogGroupName"),
29+
},
30+
},
31+
})
32+
33+
if err != nil {
34+
fmt.Println("Error", err)
35+
return
36+
}
37+
38+
fmt.Println("Metrics", result.Metrics)
39+
}

0 commit comments

Comments
 (0)