2015-09-21 18:49:19 +00:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-11-21 14:51:36 +00:00
|
|
|
package ec2
|
2015-09-21 18:49:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-09-05 12:40:28 +00:00
|
|
|
"net"
|
2015-10-20 12:36:53 +00:00
|
|
|
"strings"
|
2015-09-21 18:49:19 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
2016-11-03 14:54:27 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
2016-10-19 09:20:00 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-09-21 18:49:19 +00:00
|
|
|
"github.com/prometheus/common/log"
|
|
|
|
"github.com/prometheus/common/model"
|
2016-02-18 16:26:27 +00:00
|
|
|
"golang.org/x/net/context"
|
2015-09-21 18:49:19 +00:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
|
|
|
"github.com/prometheus/prometheus/config"
|
|
|
|
"github.com/prometheus/prometheus/util/strutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-09-22 13:01:23 +00:00
|
|
|
ec2Label = model.MetaLabelPrefix + "ec2_"
|
|
|
|
ec2LabelAZ = ec2Label + "availability_zone"
|
|
|
|
ec2LabelInstanceID = ec2Label + "instance_id"
|
|
|
|
ec2LabelInstanceState = ec2Label + "instance_state"
|
2016-10-21 10:13:47 +00:00
|
|
|
ec2LabelInstanceType = ec2Label + "instance_type"
|
2016-09-22 13:01:23 +00:00
|
|
|
ec2LabelPublicDNS = ec2Label + "public_dns_name"
|
|
|
|
ec2LabelPublicIP = ec2Label + "public_ip"
|
|
|
|
ec2LabelPrivateIP = ec2Label + "private_ip"
|
|
|
|
ec2LabelSubnetID = ec2Label + "subnet_id"
|
|
|
|
ec2LabelTag = ec2Label + "tag_"
|
|
|
|
ec2LabelVPCID = ec2Label + "vpc_id"
|
|
|
|
subnetSeparator = ","
|
2015-09-21 18:49:19 +00:00
|
|
|
)
|
|
|
|
|
2016-10-19 09:20:00 +00:00
|
|
|
var (
|
2016-10-26 09:03:35 +00:00
|
|
|
ec2SDRefreshFailuresCount = prometheus.NewCounter(
|
2016-10-19 09:20:00 +00:00
|
|
|
prometheus.CounterOpts{
|
2016-11-21 14:51:36 +00:00
|
|
|
Name: "prometheus_sd_ec2_refresh_failures_total",
|
|
|
|
Help: "The number of EC2-SD scrape failures.",
|
2016-10-19 09:20:00 +00:00
|
|
|
})
|
2016-10-26 09:03:35 +00:00
|
|
|
ec2SDRefreshDuration = prometheus.NewSummary(
|
2016-10-19 09:20:00 +00:00
|
|
|
prometheus.SummaryOpts{
|
2016-11-21 14:51:36 +00:00
|
|
|
Name: "prometheus_sd_ec2_refresh_duration_seconds",
|
|
|
|
Help: "The duration of a EC2-SD refresh in seconds.",
|
2016-10-19 09:20:00 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-10-26 09:03:35 +00:00
|
|
|
prometheus.MustRegister(ec2SDRefreshFailuresCount)
|
|
|
|
prometheus.MustRegister(ec2SDRefreshDuration)
|
2016-10-19 09:20:00 +00:00
|
|
|
}
|
|
|
|
|
2015-09-21 18:49:19 +00:00
|
|
|
// EC2Discovery periodically performs EC2-SD requests. It implements
|
|
|
|
// the TargetProvider interface.
|
|
|
|
type EC2Discovery struct {
|
|
|
|
aws *aws.Config
|
|
|
|
interval time.Duration
|
2016-11-03 14:54:27 +00:00
|
|
|
profile string
|
2015-09-21 18:49:19 +00:00
|
|
|
port int
|
|
|
|
}
|
|
|
|
|
2016-11-21 14:51:36 +00:00
|
|
|
// NewDiscovery returns a new EC2Discovery which periodically refreshes its targets.
|
|
|
|
func NewDiscovery(conf *config.EC2SDConfig) *EC2Discovery {
|
2015-09-21 18:49:19 +00:00
|
|
|
creds := credentials.NewStaticCredentials(conf.AccessKey, conf.SecretKey, "")
|
|
|
|
if conf.AccessKey == "" && conf.SecretKey == "" {
|
2016-11-03 14:54:27 +00:00
|
|
|
creds = nil
|
2015-09-21 18:49:19 +00:00
|
|
|
}
|
|
|
|
return &EC2Discovery{
|
|
|
|
aws: &aws.Config{
|
|
|
|
Region: &conf.Region,
|
|
|
|
Credentials: creds,
|
|
|
|
},
|
2016-11-03 14:54:27 +00:00
|
|
|
profile: conf.Profile,
|
2015-09-21 18:49:19 +00:00
|
|
|
interval: time.Duration(conf.RefreshInterval),
|
|
|
|
port: conf.Port,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run implements the TargetProvider interface.
|
2016-02-18 16:26:27 +00:00
|
|
|
func (ed *EC2Discovery) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
|
2015-09-21 18:49:19 +00:00
|
|
|
ticker := time.NewTicker(ed.interval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
// Get an initial set right away.
|
|
|
|
tg, err := ed.refresh()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
} else {
|
2016-11-18 09:55:29 +00:00
|
|
|
select {
|
|
|
|
case ch <- []*config.TargetGroup{tg}:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
2015-09-21 18:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
tg, err := ed.refresh()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2016-11-18 09:55:29 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ch <- []*config.TargetGroup{tg}:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2015-09-21 18:49:19 +00:00
|
|
|
}
|
2016-02-18 16:26:27 +00:00
|
|
|
case <-ctx.Done():
|
2015-09-21 18:49:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-19 09:20:00 +00:00
|
|
|
func (ed *EC2Discovery) refresh() (tg *config.TargetGroup, err error) {
|
|
|
|
t0 := time.Now()
|
|
|
|
defer func() {
|
2016-10-26 09:03:35 +00:00
|
|
|
ec2SDRefreshDuration.Observe(time.Since(t0).Seconds())
|
2016-10-19 09:20:00 +00:00
|
|
|
if err != nil {
|
2016-10-26 09:03:35 +00:00
|
|
|
ec2SDRefreshFailuresCount.Inc()
|
2016-10-19 09:20:00 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-11-03 14:54:27 +00:00
|
|
|
sess, err := session.NewSessionWithOptions(session.Options{
|
|
|
|
Config: *ed.aws,
|
|
|
|
Profile: ed.profile,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not create aws session: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ec2s := ec2.New(sess)
|
2016-10-19 09:20:00 +00:00
|
|
|
tg = &config.TargetGroup{
|
2015-09-21 18:49:19 +00:00
|
|
|
Source: *ed.aws.Region,
|
|
|
|
}
|
2016-10-19 09:20:00 +00:00
|
|
|
if err = ec2s.DescribeInstancesPages(nil, func(p *ec2.DescribeInstancesOutput, lastPage bool) bool {
|
2015-09-21 18:49:19 +00:00
|
|
|
for _, r := range p.Reservations {
|
|
|
|
for _, inst := range r.Instances {
|
|
|
|
if inst.PrivateIpAddress == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
labels := model.LabelSet{
|
|
|
|
ec2LabelInstanceID: model.LabelValue(*inst.InstanceId),
|
|
|
|
}
|
|
|
|
labels[ec2LabelPrivateIP] = model.LabelValue(*inst.PrivateIpAddress)
|
2016-09-05 12:40:28 +00:00
|
|
|
addr := net.JoinHostPort(*inst.PrivateIpAddress, fmt.Sprintf("%d", ed.port))
|
2015-09-21 18:49:19 +00:00
|
|
|
labels[model.AddressLabel] = model.LabelValue(addr)
|
2015-10-20 12:36:53 +00:00
|
|
|
|
|
|
|
if inst.PublicIpAddress != nil {
|
|
|
|
labels[ec2LabelPublicIP] = model.LabelValue(*inst.PublicIpAddress)
|
|
|
|
labels[ec2LabelPublicDNS] = model.LabelValue(*inst.PublicDnsName)
|
|
|
|
}
|
|
|
|
|
|
|
|
labels[ec2LabelAZ] = model.LabelValue(*inst.Placement.AvailabilityZone)
|
2016-09-22 13:01:23 +00:00
|
|
|
labels[ec2LabelInstanceState] = model.LabelValue(*inst.State.Name)
|
2016-10-21 10:13:47 +00:00
|
|
|
labels[ec2LabelInstanceType] = model.LabelValue(*inst.InstanceType)
|
2015-10-20 12:36:53 +00:00
|
|
|
|
|
|
|
if inst.VpcId != nil {
|
|
|
|
labels[ec2LabelVPCID] = model.LabelValue(*inst.VpcId)
|
|
|
|
|
|
|
|
subnetsMap := make(map[string]struct{})
|
|
|
|
for _, eni := range inst.NetworkInterfaces {
|
|
|
|
subnetsMap[*eni.SubnetId] = struct{}{}
|
|
|
|
}
|
|
|
|
subnets := []string{}
|
|
|
|
for k := range subnetsMap {
|
|
|
|
subnets = append(subnets, k)
|
|
|
|
}
|
|
|
|
labels[ec2LabelSubnetID] = model.LabelValue(
|
|
|
|
subnetSeparator +
|
|
|
|
strings.Join(subnets, subnetSeparator) +
|
|
|
|
subnetSeparator)
|
|
|
|
}
|
|
|
|
|
2015-09-21 18:49:19 +00:00
|
|
|
for _, t := range inst.Tags {
|
|
|
|
name := strutil.SanitizeLabelName(*t.Key)
|
|
|
|
labels[ec2LabelTag+model.LabelName(name)] = model.LabelValue(*t.Value)
|
|
|
|
}
|
|
|
|
tg.Targets = append(tg.Targets, labels)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not describe instances: %s", err)
|
|
|
|
}
|
|
|
|
return tg, nil
|
|
|
|
}
|