-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add autodiscover for aws_ec2 (#14823)
* Add autodiscover for aws_ec2 * Add aws.ec2.* to autodiscover template
- Loading branch information
1 parent
e61c4c4
commit adcd962
Showing
23 changed files
with
1,145 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{beatname_uc} supports templates for modules: | ||
|
||
["source","yaml",subs="attributes"] | ||
------------------------------------------------------------------------------------- | ||
metricbeat.autodiscover: | ||
providers: | ||
- type: aws_ec2 | ||
period: 1m | ||
credential_profile_name: elastic-beats | ||
templates: | ||
- condition: | ||
equals: | ||
aws.ec2.tags.service: "mysql" | ||
config: | ||
- module: mysql | ||
metricsets: ["status", "galera_status"] | ||
period: 10s | ||
hosts: ["root:password@tcp(${data.aws.ec2.public.ip}:3306)/"] | ||
username: root | ||
password: password | ||
------------------------------------------------------------------------------------- | ||
|
||
This autodiscover provider takes our standard AWS credentials options. | ||
With this configuration, `mysql` metricbeat module will be launched for all EC2 | ||
instances that have `service: mysql` as a tag. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
x-pack/libbeat/autodiscover/providers/aws/ec2/_meta/fields.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// 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/pkg/errors" | ||
|
||
"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 | ||
} | ||
|
||
// toMap converts this ec2Instance into the form consumed as metadata in the autodiscovery process. | ||
func (i *ec2Instance) toMap() common.MapStr { | ||
architecture, err := i.ec2Instance.Architecture.MarshalValue() | ||
if err != nil { | ||
logp.Error(errors.Wrap(err, "MarshalValue failed for architecture: ")) | ||
} | ||
|
||
m := common.MapStr{ | ||
"image": i.toImage(), | ||
"vpc": i.toVpc(), | ||
"subnet": i.toSubnet(), | ||
"private": i.toPrivate(), | ||
"public": i.toPublic(), | ||
"monitoring": i.toMonitoringState(), | ||
"kernel": i.toKernel(), | ||
"state": i.stateMap(), | ||
"architecture": architecture, | ||
"root_device_name": awsauto.SafeString(i.ec2Instance.RootDeviceName), | ||
} | ||
|
||
for _, tag := range i.ec2Instance.Tags { | ||
m.Put("tags."+awsauto.SafeString(tag.Key), awsauto.SafeString(tag.Value)) | ||
} | ||
return m | ||
} | ||
|
||
func (i *ec2Instance) instanceID() string { | ||
return awsauto.SafeString(i.ec2Instance.InstanceId) | ||
} | ||
|
||
func (i *ec2Instance) toImage() common.MapStr { | ||
m := common.MapStr{} | ||
m["id"] = awsauto.SafeString(i.ec2Instance.ImageId) | ||
return m | ||
} | ||
|
||
func (i *ec2Instance) toMonitoringState() common.MapStr { | ||
monitoringState, err := i.ec2Instance.Monitoring.State.MarshalValue() | ||
if err != nil { | ||
logp.Error(errors.Wrap(err, "MarshalValue failed for monitoring state: ")) | ||
} | ||
|
||
m := common.MapStr{} | ||
m["state"] = monitoringState | ||
return m | ||
} | ||
|
||
func (i *ec2Instance) toPrivate() common.MapStr { | ||
m := common.MapStr{} | ||
m["ip"] = awsauto.SafeString(i.ec2Instance.PrivateIpAddress) | ||
m["dns_name"] = awsauto.SafeString(i.ec2Instance.PrivateDnsName) | ||
return m | ||
} | ||
|
||
func (i *ec2Instance) toPublic() common.MapStr { | ||
m := common.MapStr{} | ||
m["ip"] = awsauto.SafeString(i.ec2Instance.PublicIpAddress) | ||
m["dns_name"] = awsauto.SafeString(i.ec2Instance.PublicDnsName) | ||
return m | ||
} | ||
|
||
func (i *ec2Instance) toVpc() common.MapStr { | ||
m := common.MapStr{} | ||
m["id"] = awsauto.SafeString(i.ec2Instance.VpcId) | ||
return m | ||
} | ||
|
||
func (i *ec2Instance) toSubnet() common.MapStr { | ||
m := common.MapStr{} | ||
m["id"] = awsauto.SafeString(i.ec2Instance.SubnetId) | ||
return m | ||
} | ||
|
||
func (i *ec2Instance) toKernel() common.MapStr { | ||
m := common.MapStr{} | ||
m["id"] = awsauto.SafeString(i.ec2Instance.KernelId) | ||
return m | ||
} | ||
|
||
func (i *ec2Instance) toCloudMap() common.MapStr { | ||
m := common.MapStr{} | ||
availabilityZone := awsauto.SafeString(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)-1] | ||
|
||
instance := common.MapStr{} | ||
instance["id"] = i.instanceID() | ||
m["instance"] = instance | ||
|
||
instanceType, err := i.ec2Instance.InstanceType.MarshalValue() | ||
if err != nil { | ||
logp.Error(errors.Wrap(err, "MarshalValue failed for instance type: ")) | ||
} | ||
machine := common.MapStr{} | ||
machine["type"] = instanceType | ||
m["machine"] = machine | ||
return m | ||
} | ||
|
||
// 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 { | ||
logp.Error(errors.Wrap(err, "MarshalValue failed for instance state name: ")) | ||
} | ||
|
||
stateMap["name"] = nameString | ||
stateMap["code"] = state.Code | ||
return stateMap | ||
} |
Oops, something went wrong.