Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autodiscover for aws_ec2 #14823

Merged
merged 29 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3506757
Add autodiscover for aws_ec2
kaiyan-sheng Nov 27, 2019
07469cd
Add aws_ec2 autodiscover
kaiyan-sheng Nov 27, 2019
dfc6269
test with logp.Debug
kaiyan-sheng Nov 27, 2019
4d350ea
Merge remote-tracking branch 'upstream/master' into autodiscover_aws_ec2
kaiyan-sheng Dec 6, 2019
e5899f8
Add comment to DefaultConfig function
kaiyan-sheng Dec 6, 2019
fcdba2f
Merge remote-tracking branch 'upstream/master' into autodiscover_aws_ec2
kaiyan-sheng Dec 17, 2019
c042a08
Change ec2 metadata mapping
kaiyan-sheng Dec 17, 2019
5662544
Update changelog
kaiyan-sheng Dec 17, 2019
a4e7f4f
Update changelog
kaiyan-sheng Dec 17, 2019
580c13a
change instanceId to instanceID
kaiyan-sheng Dec 17, 2019
f06521d
Collect from all regions if regions is not specified
kaiyan-sheng Dec 18, 2019
3a5fa25
Add GetRegions into aws_elb provider
kaiyan-sheng Dec 18, 2019
fef6fde
Add aws_ec2 provider into autodiscover doc
kaiyan-sheng Dec 19, 2019
543e18a
update autodiscover doc
kaiyan-sheng Dec 19, 2019
4605f1f
try to fix doc
kaiyan-sheng Dec 19, 2019
556830a
Add unit tests
kaiyan-sheng Dec 19, 2019
0f7afa6
add comments
kaiyan-sheng Dec 19, 2019
6af9ca2
Fix metricbeat doc
kaiyan-sheng Dec 19, 2019
672c42b
Make houndci happy
kaiyan-sheng Dec 19, 2019
cd459b4
Merge remote-tracking branch 'upstream/master' into autodiscover_aws_ec2
kaiyan-sheng Jan 3, 2020
397dd24
Merge remote-tracking branch 'upstream/master' into autodiscover_aws_ec2
kaiyan-sheng Jan 8, 2020
6d13377
Merge remote-tracking branch 'upstream/master' into autodiscover_aws_ec2
kaiyan-sheng Jan 24, 2020
780a34a
Merge remote-tracking branch 'upstream/master' into autodiscover_aws_ec2
kaiyan-sheng Feb 3, 2020
8e70d89
update changelog
kaiyan-sheng Feb 3, 2020
eb0d83f
update changelog
kaiyan-sheng Feb 3, 2020
6505d1a
Merge remote-tracking branch 'upstream/master' into autodiscover_aws_ec2
kaiyan-sheng Feb 5, 2020
48f121c
Add aws prefix to meta ec2
kaiyan-sheng Feb 6, 2020
656379e
update field names from ec2 to aws.ec2
kaiyan-sheng Feb 6, 2020
286d2ad
Add aws.ec2.* to autodiscover template
kaiyan-sheng Feb 6, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions x-pack/libbeat/autodiscover/providers/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,15 @@
// you may not use this file except in compliance with the Elastic License.

package aws

// SafeStrp makes handling AWS *string types easier.
// The AWS lib never returns plain strings, always using pointers, probably for memory efficiency reasons.
// This is a bit odd, because strings are just pointers into byte arrays, however this is the choice they've made.
// This will return the plain version of the given string or an empty string if the pointer is null
func SafeStrp(strp *string) string {
kaiyan-sheng marked this conversation as resolved.
Show resolved Hide resolved
if strp == nil {
return ""
}

return *strp
}
41 changes: 41 additions & 0 deletions x-pack/libbeat/autodiscover/providers/aws/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package aws

import (
"time"

"github.com/elastic/beats/x-pack/libbeat/common/aws"

"github.com/elastic/beats/libbeat/autodiscover/template"
"github.com/elastic/beats/libbeat/common"
)

// Config for the aws_ec2 autodiscover provider.
type Config struct {
Type string `config:"type"`

// Standard autodiscover fields.

// Hints are currently not supported, but may be implemented in a later release
HintsEnabled bool `config:"hints.enabled"`
Builders []*common.Config `config:"builders"`
Appenders []*common.Config `config:"appenders"`
Templates template.MapperSettings `config:"templates"`
kaiyan-sheng marked this conversation as resolved.
Show resolved Hide resolved

// Period defines how often to poll the AWS API.
Period time.Duration `config:"period" validate:"nonzero,required"`

// AWS Specific autodiscover fields

Regions []string `config:"regions" validate:"required"`
AWSConfig aws.ConfigAWS `config:",inline"`
kaiyan-sheng marked this conversation as resolved.
Show resolved Hide resolved
}

func DefaultConfig() *Config {
kaiyan-sheng marked this conversation as resolved.
Show resolved Hide resolved
kaiyan-sheng marked this conversation as resolved.
Show resolved Hide resolved
kaiyan-sheng marked this conversation as resolved.
Show resolved Hide resolved
kaiyan-sheng marked this conversation as resolved.
Show resolved Hide resolved
return &Config{
Period: time.Minute,
}
}
11 changes: 11 additions & 0 deletions x-pack/libbeat/autodiscover/providers/aws/ec2/_meta/fields.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- key: ec2_listener
title: "EC2 Listener"
description: >
AWS EC2 Listeners
short_config: false
release: experimental
fields:
- name: ec2_listener
type: group
description: >
Represents an AWS EC2 Listener, e.g. state of an EC2.
116 changes: 116 additions & 0 deletions x-pack/libbeat/autodiscover/providers/aws/ec2/ec2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package ec2

import (
"github.com/aws/aws-sdk-go-v2/service/ec2"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
awsauto "github.com/elastic/beats/x-pack/libbeat/autodiscover/providers/aws"
)

type ec2Instance struct {
ec2Instance ec2.Instance
logger *logp.Logger
}

// toMap converts this ec2Instance into the form consumed as metadata in the autodiscovery process.
func (i *ec2Instance) toMap() common.MapStr {
instanceType, err := i.ec2Instance.InstanceType.MarshalValue()
if err != nil {
i.logger.Error("MarshalValue failed for instance type: ", err)
}

monitoringState, err := i.ec2Instance.Monitoring.State.MarshalValue()
if err != nil {
i.logger.Error("MarshalValue failed for monitoring state: ", err)
}

architecture, err := i.ec2Instance.Architecture.MarshalValue()
if err != nil {
i.logger.Error("MarshalValue failed for architecture: ", err)
}

m := common.MapStr{
"image_id": awsauto.SafeStrp(i.ec2Instance.ImageId),
kaiyan-sheng marked this conversation as resolved.
Show resolved Hide resolved
"vpc_id": awsauto.SafeStrp(i.ec2Instance.VpcId),
"subnet_id": awsauto.SafeStrp(i.ec2Instance.SubnetId),
"host_id": awsauto.SafeStrp(i.ec2Instance.Placement.HostId),
"group_name": awsauto.SafeStrp(i.ec2Instance.Placement.GroupName),
"arn": awsauto.SafeStrp(i.ec2Instance.IamInstanceProfile.Arn),
"instance_id": awsauto.SafeStrp(i.ec2Instance.IamInstanceProfile.Id),
"type": instanceType,
"private_ip": awsauto.SafeStrp(i.ec2Instance.PrivateIpAddress),
"private_dns_name": awsauto.SafeStrp(i.ec2Instance.PrivateDnsName),
"public_ip": awsauto.SafeStrp(i.ec2Instance.PublicIpAddress),
"public_dns_name": awsauto.SafeStrp(i.ec2Instance.PublicDnsName),
"monitoring_state": monitoringState,
"architecture": architecture,
"root_device_name": awsauto.SafeStrp(i.ec2Instance.RootDeviceName),
"kernel_id": awsauto.SafeStrp(i.ec2Instance.KernelId),
"state": i.stateMap(),
"state_reason": i.stateReasonMap(),
"tags": i.tagMap(),
}
return m
}

func (i *ec2Instance) toCloudMap() common.MapStr {
m := common.MapStr{}
availabilityZone := awsauto.SafeStrp(i.ec2Instance.Placement.AvailabilityZone)
m["availability_zone"] = availabilityZone
m["provider"] = "aws"

// The region is just an AZ with the last character removed
m["region"] = availabilityZone[:len(availabilityZone)-2]
return m
}

// arn returns a globally unique ID. In the case of an ec2Instance, that would be its listenerArn.
func (i *ec2Instance) arn() string {
return awsauto.SafeStrp(i.ec2Instance.IamInstanceProfile.Arn)
}

// stateMap converts the State part of the ec2 struct into a friendlier map with 'reason' and 'code' fields.
func (i *ec2Instance) stateMap() (stateMap common.MapStr) {
state := i.ec2Instance.State
stateMap = common.MapStr{}
nameString, err := state.Name.MarshalValue()
if err != nil {
i.logger.Error("MarshalValue failed for instance state name: ", err)
}

stateMap["name"] = nameString
stateMap["code"] = state.Code

stateReason := i.ec2Instance.StateReason
stateMap["state_reason"] = awsauto.SafeStrp(stateReason.Code)
stateMap[""] = awsauto.SafeStrp(stateReason.Message)
return stateMap
}

// stateReasonMap converts the State Reason part of the ec2 struct into a friendlier map with 'reason' and 'code' fields.
func (i *ec2Instance) stateReasonMap() (stateReasonMap common.MapStr) {
stateReasonMap = common.MapStr{}
stateReason := i.ec2Instance.StateReason
stateReasonMap["code"] = awsauto.SafeStrp(stateReason.Code)
stateReasonMap["message"] = awsauto.SafeStrp(stateReason.Message)
return stateReasonMap
}

// stateMap converts the State part of the ec2 struct into a friendlier map with 'reason' and 'code' fields.
func (i *ec2Instance) tagMap() (tagsMap []common.MapStr) {
tags := i.ec2Instance.Tags
tagsMap = []common.MapStr{}
tagPair := common.MapStr{}
for _, tag := range tags {
tagPair["key"] = awsauto.SafeStrp(tag.Key)
tagPair["value"] = awsauto.SafeStrp(tag.Value)
tagsMap = append(tagsMap, tagPair)
}

return tagsMap
}
Loading