forked from ike-dai/zaws
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathec2.go
More file actions
63 lines (56 loc) · 1.66 KB
/
ec2.go
File metadata and controls
63 lines (56 loc) · 1.66 KB
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
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"os"
)
func get_ec2_list(sess *session.Session) []*ec2.Instance {
var instances []*ec2.Instance
svc := ec2.New(sess)
resp, err := svc.DescribeInstances(nil)
if err != nil {
fmt.Printf("[ERROR] Fail DescribeInstances API call: %s \n", err.Error())
os.Exit(1)
}
for _, reservation := range resp.Reservations {
instances = append(instances, reservation.Instances...)
}
return instances
}
func (z *Zaws) ShowEc2List() {
list := make([]Data, 0)
instances := get_ec2_list(z.AwsSession)
for _, instance := range instances {
data := Data{InstanceType: *instance.InstanceType, InstanceId: *instance.InstanceId}
if instance.PrivateIpAddress != nil {
data.InstancePrivateAddr = *instance.PrivateIpAddress
}
for _, tag := range instance.Tags {
if *tag.Key == "Name" {
data.InstanceName = *tag.Value
}
}
if data.InstanceName == "" {
data.InstanceName = *instance.InstanceId
}
list = append(list, data)
}
fmt.Printf(convert_to_lldjson_string(list))
}
func (z *Zaws) ShowEC2CloudwatchMetricsList() {
list := make([]Data, 0)
metrics := get_metric_list(z.AwsSession, "InstanceId", z.TargetId)
for _, metric := range metrics {
datapoints := get_metric_stats(z.AwsSession, "InstanceId", z.TargetId, *metric.MetricName, *metric.Namespace)
data := Data{MetricName: *metric.MetricName, MetricNamespace: *metric.Namespace}
if len(datapoints) > 0 {
data.MetricUnit = *datapoints[0].Unit
}
list = append(list, data)
}
fmt.Printf(convert_to_lldjson_string(list))
}
func (z *Zaws) SendEc2MetricStats() {
z.SendMetricStats("InstanceId")
}