-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathec2reporter.go
More file actions
94 lines (86 loc) · 2.31 KB
/
Copy pathec2reporter.go
File metadata and controls
94 lines (86 loc) · 2.31 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
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
package main
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
// Flags for output, name, state
var flagFormat = flag.String("out", "table", "table, json or block for Outputformat")
var flagTNames = flag.String("name", "*", "Search for Tag")
var flagStatus = flag.String("state", "*", "Search for state: running, pending, stop")
//var flagInstID = flag.String("instid", "", "search for InstanceID")
func main() {
flag.Parse()
fmt.Println("ec2reporter v.02")
result := getInstances(*flagTNames, *flagStatus)
resultWorker(result)
// switch for output
switch *flagFormat {
case "json":
outputjson()
case "block":
outputrepot()
case "table":
outouttable()
default:
outouttable()
}
}
// getInstances : connect to aws and get results
func getInstances(iname, istatus string) *ec2.DescribeInstancesOutput {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// Create EC2 client
ec2sess := ec2.New(sess)
params := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("tag:Name"),
Values: []*string{
aws.String("*" + iname + "*"),
},
},
{
Name: aws.String("instance-state-name"),
Values: []*string{aws.String("*" + istatus + "*")},
},
},
// not possible to search with Wildcard
//InstanceIds: []*string{
// aws.String(""), // Required
//},
}
result, err := ec2sess.DescribeInstances(params)
checkError(err)
return result
}
func resultWorker(result *ec2.DescribeInstancesOutput) {
var myInst Ec2Instance
var name string
for _, reservation := range result.Reservations {
for _, instance := range reservation.Instances {
name = "None"
for _, t := range instance.Tags {
if *t.Key == "Name" {
name = *t.Value
}
}
myInst = Ec2Instance{
Name: name,
InstID: *instance.InstanceId,
ImageID: *instance.ImageId,
InstType: *instance.InstanceType,
Launch: *instance.LaunchTime,
State: *instance.State.Name,
PublicIP: *instance.PublicIpAddress,
PrivateIP: *instance.PrivateIpAddress,
Monitoring: *instance.Monitoring.State,
Region: *instance.Placement.AvailabilityZone,
}
Ec2List = append(Ec2List, myInst)
}
}
}