-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstance.go
565 lines (480 loc) · 15.7 KB
/
instance.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
package window
import (
"fmt"
"io/ioutil"
"net"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/emptyinterface/window/pricing"
"github.com/emptyinterface/window/sysinfo"
"golang.org/x/crypto/ssh"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
)
type (
Instance struct {
// The AMI launch index, which can be used to find this instance in the launch
// group.
AmiLaunchIndex int64
// The architecture of the image.
Architecture string
// Any block device mapping entries for the instance.
BlockDeviceMappings []*ec2.InstanceBlockDeviceMapping
// The idempotency token you provided when you launched the instance.
ClientToken string
// Indicates whether the instance is optimized for EBS I/O. This optimization
// provides dedicated throughput to Amazon EBS and an optimized configuration
// stack to provide optimal I/O performance. This optimization isn't available
// with all instance types. Additional usage charges apply when using an EBS
// Optimized instance.
EbsOptimized bool
// The hypervisor type of the instance.
Hypervisor string
// The IAM instance profile associated with the instance.
IamInstanceProfile *ec2.IamInstanceProfile
// The ID of the AMI used to launch the instance.
ImageId string
// The ID of the instance.
InstanceId string
// Indicates whether this is a Spot Instance.
InstanceLifecycle string
// The instance type.
InstanceType string
// The kernel associated with this instance.
KernelId string
// The name of the key pair, if this instance was launched with an associated
// key pair.
KeyName string
// The time the instance was launched.
LaunchTime time.Time
// The monitoring information for the instance.
Monitoring *ec2.Monitoring
// [EC2-VPC] One or more network interfaces for the instance.
NetworkInterfaces []*ec2.InstanceNetworkInterface
// The location where the instance launched.
Placement *ec2.Placement
// The value is Windows for Windows instances; otherwise blank.
Platform string
// The private DNS name assigned to the instance. This DNS name can only be
// used inside the Amazon EC2 network. This name is not available until the
// instance enters the running state.
PrivateDnsName string
// The private IP address assigned to the instance.
PrivateIpAddress string
// The product codes attached to this instance.
ProductCodes []*ec2.ProductCode
// The public DNS name assigned to the instance. This name is not available
// until the instance enters the running state.
PublicDnsName string
// The public IP address assigned to the instance.
PublicIpAddress string
// The RAM disk associated with this instance.
RamdiskId string
// The root device name (for example, /dev/sda1 or /dev/xvda).
RootDeviceName string
// The root device type used by the AMI. The AMI can use an EBS volume or an
// instance store volume.
RootDeviceType string
// One or more security groups for the instance.
SecurityGroupNames []*ec2.GroupIdentifier
// Specifies whether to enable an instance launched in a VPC to perform NAT.
// This controls whether source/destination checking is enabled on the instance.
// A value of true means checking is enabled, and false means checking is disabled.
// The value must be false for the instance to perform NAT. For more information,
// see NAT Instances (http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_NAT_Instance.html)
// in the Amazon Virtual Private Cloud User Guide.
SourceDestCheck bool
// The ID of the Spot Instance request.
SpotInstanceRequestId string
// Specifies whether enhanced networking is enabled.
SriovNetSupport string
// The current state of the instance.
InstanceState *ec2.InstanceState
// The reason for the most recent state transition.
StateReason *ec2.StateReason
// The reason for the most recent state transition. This might be an empty string.
StateTransitionReason string
// The ID of the subnet in which the instance is running.
SubnetId string
// Any tags assigned to the instance.
Tags []*ec2.Tag
// The virtualization type of the instance.
VirtualizationType string
// The ID of the VPC in which the instance is running.
VpcId string
Name string
Id string
State string
Region *Region
VPC *VPC
Classic *Classic
AvailabilityZone *AvailabilityZone
Subnet *Subnet
ELB *ELB
SecurityGroups []*SecurityGroup
AMI *AMI
AutoScalingGroup *AutoScalingGroup
ENIs []*ENI
CloudWatchAlarms []*CloudWatchAlarm
// true if server cannot be ssh polled by usual means
Unreachable bool
UnreachableReason string
SysInfo *sysinfo.SystemInfoCollector
Stats *sysinfo.SystemInfoSummary
sysInfo_me sync.RWMutex
}
InstanceByNameAsc []*Instance
NetworkInterfaceByNameAsc []*ec2.InstanceNetworkInterface
)
const DialTimeout = 3 * time.Second
func (a InstanceByNameAsc) Len() int { return len(a) }
func (a InstanceByNameAsc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a InstanceByNameAsc) Less(i, j int) bool {
return string_less_than(a[i].Name, a[j].Name)
}
func (a NetworkInterfaceByNameAsc) Len() int { return len(a) }
func (a NetworkInterfaceByNameAsc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a NetworkInterfaceByNameAsc) Less(i, j int) bool {
return string_less_than(*a[i].NetworkInterfaceId, *a[j].NetworkInterfaceId)
}
func (inst *Instance) CPU() int {
if inst.Stats != nil && inst.Stats.CPU.PercentInUse > 0 {
return int(inst.Stats.CPU.PercentInUse * 100)
}
return 0
}
func (inst *Instance) CPUs() []int {
var cpus []int
if inst.Stats != nil {
for _, cpu := range inst.Stats.CPU.CPUs {
if cpu.PercentInUse < 0 {
cpus = append(cpus, 0)
} else {
cpus = append(cpus, int(cpu.PercentInUse*100))
}
}
}
return cpus
}
func (inst *Instance) MemoryUser() int {
if inst.Stats != nil {
return int(inst.Stats.Memory.PercentUser * 100)
}
return 0
}
func (inst *Instance) MemorySystem() int {
if inst.Stats != nil {
return int(inst.Stats.Memory.PercentSystem * 100)
}
return 0
}
func (inst *Instance) Disk() int {
if inst.Stats != nil {
return int(inst.Stats.Disk.PercentInUse * 100)
}
return 0
}
func (inst *Instance) NetworkIn() int {
if inst.Stats != nil {
return int(inst.Stats.Network.BytesPerSecondIn)
}
return 0
}
func (inst *Instance) NetworkOut() int {
if inst.Stats != nil {
return int(inst.Stats.Network.BytesPerSecondOut)
}
return 0
}
func (inst *Instance) NetworkInString() string {
if inst.Stats != nil {
return humanBytes(inst.Stats.Network.BytesPerSecondIn, 0)
}
return "0B"
}
func (inst *Instance) NetworkOutString() string {
if inst.Stats != nil {
return humanBytes(inst.Stats.Network.BytesPerSecondOut, 0)
}
return "0B"
}
func (inst *Instance) NetworkIO() string {
if inst.Stats != nil {
return fmt.Sprintf("%s / %s",
humanBytes(inst.Stats.Network.BytesPerSecondIn, 0),
humanBytes(inst.Stats.Network.BytesPerSecondOut, 0),
)
}
return ""
}
func (inst *Instance) NetworkInNormal() float64 {
const ceiling float64 = (125 << 20) / 10
if inst.Stats != nil {
return (float64(inst.Stats.Network.BytesPerSecondIn) / ceiling) * 100
}
return 0
}
func (inst *Instance) NetworkOutNormal() float64 {
const ceiling float64 = (125 << 20) / 10
if inst.Stats != nil {
return (float64(inst.Stats.Network.BytesPerSecondOut) / ceiling) * 100
}
return 0
}
func LoadInstances(input *ec2.DescribeInstancesInput) (map[string]*Instance, error) {
if input == nil {
input = &ec2.DescribeInstancesInput{MaxResults: aws.Int64(1000)}
} else {
input.MaxResults = aws.Int64(1000) // max results
}
instances := map[string]*Instance{}
if err := EC2Client.DescribeInstancesPages(input, func(page *ec2.DescribeInstancesOutput, _ bool) bool {
for _, res := range page.Reservations {
for _, ec2inst := range res.Instances {
instance := &Instance{
AmiLaunchIndex: aws.Int64Value(ec2inst.AmiLaunchIndex),
Architecture: aws.StringValue(ec2inst.Architecture),
BlockDeviceMappings: ec2inst.BlockDeviceMappings,
ClientToken: aws.StringValue(ec2inst.ClientToken),
EbsOptimized: aws.BoolValue(ec2inst.EbsOptimized),
Hypervisor: aws.StringValue(ec2inst.Hypervisor),
IamInstanceProfile: ec2inst.IamInstanceProfile,
ImageId: aws.StringValue(ec2inst.ImageId),
InstanceId: aws.StringValue(ec2inst.InstanceId),
InstanceLifecycle: aws.StringValue(ec2inst.InstanceLifecycle),
InstanceType: aws.StringValue(ec2inst.InstanceType),
KernelId: aws.StringValue(ec2inst.KernelId),
KeyName: aws.StringValue(ec2inst.KeyName),
LaunchTime: aws.TimeValue(ec2inst.LaunchTime),
Monitoring: ec2inst.Monitoring,
NetworkInterfaces: ec2inst.NetworkInterfaces,
Placement: ec2inst.Placement,
Platform: aws.StringValue(ec2inst.Platform),
PrivateDnsName: aws.StringValue(ec2inst.PrivateDnsName),
PrivateIpAddress: aws.StringValue(ec2inst.PrivateIpAddress),
ProductCodes: ec2inst.ProductCodes,
PublicDnsName: aws.StringValue(ec2inst.PublicDnsName),
PublicIpAddress: aws.StringValue(ec2inst.PublicIpAddress),
RamdiskId: aws.StringValue(ec2inst.RamdiskId),
RootDeviceName: aws.StringValue(ec2inst.RootDeviceName),
RootDeviceType: aws.StringValue(ec2inst.RootDeviceType),
SecurityGroupNames: ec2inst.SecurityGroups,
SourceDestCheck: aws.BoolValue(ec2inst.SourceDestCheck),
SpotInstanceRequestId: aws.StringValue(ec2inst.SpotInstanceRequestId),
SriovNetSupport: aws.StringValue(ec2inst.SriovNetSupport),
InstanceState: ec2inst.State,
StateReason: ec2inst.StateReason,
StateTransitionReason: aws.StringValue(ec2inst.StateTransitionReason),
SubnetId: aws.StringValue(ec2inst.SubnetId),
Tags: ec2inst.Tags,
VirtualizationType: aws.StringValue(ec2inst.VirtualizationType),
VpcId: aws.StringValue(ec2inst.VpcId),
sysInfo_me: sync.RWMutex{},
}
instance.Name = TagOrDefault(instance.Tags, "Name", instance.InstanceId)
instance.Id = "inst:" + instance.InstanceId
instance.State = aws.StringValue(instance.InstanceState.Name)
instances[instance.InstanceId] = instance
}
}
return true
}); err != nil {
return nil, err
}
return instances, nil
}
func (inst *Instance) priceKey() string {
var tenancy string
if inst.Placement != nil {
if inst.Placement.Tenancy != nil {
switch *inst.Placement.Tenancy {
case "dedicated":
tenancy = pricing.DedicatedTenancy
case "default":
tenancy = pricing.SharedTenancy
case "host":
tenancy = pricing.HostTenancy
}
}
}
var platform string
if len(inst.Platform) > 0 {
platform = inst.Platform
} else if inst.AMI != nil {
switch {
case strings.Contains(inst.AMI.Platform, "Windows"):
platform = pricing.WindowsPlatform
case strings.Contains(inst.AMI.Platform, "RHEL"):
platform = pricing.RHELPlatform
case strings.Contains(inst.AMI.Platform, "SUSE"):
platform = pricing.SUSEPlatform
default:
platform = pricing.LinuxPlatform
}
} else {
platform = pricing.LinuxPlatform
}
key := fmt.Sprintf("%s:%s:%s:%s:%s",
pricing.AmazonEC2OfferCode,
pricing.OnDemandTermType,
tenancy,
inst.InstanceType,
platform,
)
return key
}
func (inst *Instance) HourlyCost() float64 {
if offer, exists := inst.Region.Prices[inst.priceKey()]; exists {
return offer.PricePerUnit
}
return 0
}
func (inst *Instance) MonthlyCost() float64 {
if offer, exists := inst.Region.Prices[inst.priceKey()]; exists {
return offer.PricePerUnit * 24 * 30
}
return 0
}
func (inst *Instance) Inactive() bool {
return false
}
func (inst *Instance) Poll() []chan error {
var errs []chan error
if inst.SysInfo != nil {
errs = append(errs, inst.Region.Throttle.do(inst.Name+" POLL", func() error {
err := inst.SysInfo.Poll()
inst.sysInfo_me.Lock()
defer inst.sysInfo_me.Unlock()
inst.Stats = inst.SysInfo.GetSummary()
return err
}))
return errs
}
switch {
case inst.Unreachable:
return nil
case aws.StringValue(inst.InstanceState.Name) != ec2.InstanceStateNameRunning:
inst.UnreachableReason = "Instance not running"
return nil
case len(inst.KeyName) == 0:
inst.Unreachable = true
inst.UnreachableReason = "Key not specified"
return nil
}
hosts := hostsForPort(inst, 22)
if len(hosts) == 0 {
inst.UnreachableReason = "No ports open"
return nil
}
data, err := ioutil.ReadFile(filepath.Join(inst.Region.sshKeyPath, inst.KeyName+".pem"))
if err != nil {
inst.UnreachableReason = "Key file not found"
return nil
}
signer, err := ssh.ParsePrivateKey(data)
if err != nil {
inst.UnreachableReason = "Key file not valid"
return nil
}
// default to this
inst.Unreachable = true
inst.UnreachableReason = "No valid user found"
users := []string{"ubuntu", "centos", "ec2-user", "admin"}
try := func(user, host string) error {
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
}
conn, err := net.DialTimeout("tcp", host, DialTimeout)
if err != nil {
return err
}
defer conn.Close()
c, chans, reqs, err := ssh.NewClientConn(conn, host, config)
if err != nil {
return err
}
client := ssh.NewClient(c, chans, reqs)
defer client.Close()
sess, err := client.NewSession()
if err != nil {
return err
}
defer sess.Close()
inst.sysInfo_me.Lock()
defer inst.sysInfo_me.Unlock()
if inst.SysInfo == nil {
inst.SysInfo = sysinfo.NewSystemInfoCollector(host, config, 2)
err := inst.SysInfo.Poll()
inst.Stats = inst.SysInfo.GetSummary()
inst.Unreachable = false
inst.UnreachableReason = ""
return err
}
return nil
}
for _, user := range users {
for _, host := range hosts {
user, host := user, host
errs = append(errs, inst.Region.Throttle.do(inst.Name+" POLL", func() error {
if err := try(user, host); err != nil && !strings.Contains(err.Error(), "ssh: handshake failed:") {
if neterr, ok := err.(net.Error); !ok || (!neterr.Timeout() && !neterr.Temporary()) {
return err
}
}
return nil
}))
}
}
return errs
}
func (inst *Instance) PortsInvolved() []int {
return SecurityGroupSet(inst.SecurityGroups).PortsInvolved()
}
func hostsForPort(inst *Instance, port int64) []string {
pt := strconv.FormatInt(port, 10)
var hosts []string
if len(inst.NetworkInterfaces) > 0 {
sgs := map[string]*SecurityGroup{}
for _, sg := range inst.SecurityGroups {
sgs[sg.GroupId] = sg
}
for _, ni := range inst.NetworkInterfaces {
for _, group := range ni.Groups {
if sg, exists := sgs[*group.GroupId]; exists {
for _, perm := range sg.IpPermissions {
if perm.ToPort != nil && *perm.ToPort == port {
for _, priv := range ni.PrivateIpAddresses {
if priv.PrivateIpAddress != nil {
hosts = append(hosts, *priv.PrivateIpAddress+":"+pt)
}
if priv.Association != nil && priv.Association.PublicIp != nil {
hosts = append(hosts, *priv.Association.PublicIp+":"+pt)
}
}
goto NEXT_NETWORK_INTERFACE
}
}
}
}
NEXT_NETWORK_INTERFACE:
}
} else {
for _, sg := range inst.SecurityGroups {
for _, perm := range sg.IpPermissions {
if perm.ToPort != nil && *perm.ToPort == port {
if len(inst.PrivateIpAddress) > 0 {
hosts = append(hosts, inst.PrivateIpAddress+":"+pt)
}
if len(inst.PublicIpAddress) > 0 {
hosts = append(hosts, inst.PublicIpAddress+":"+pt)
}
}
}
}
}
return hosts
}