2016-05-05 02:45:51 -07:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/common/log"
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
resolvConf = "/etc/resolv.conf"
|
|
|
|
|
|
|
|
dnsNameLabel = model.MetaLabelPrefix + "dns_name"
|
|
|
|
|
|
|
|
// Constants for instrumentation.
|
|
|
|
namespace = "prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
dnsSDLookupsCount = prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
2016-10-21 01:18:28 -07:00
|
|
|
Name: "sd_dns_lookups_total",
|
2016-05-05 02:45:51 -07:00
|
|
|
Help: "The number of DNS-SD lookups.",
|
|
|
|
})
|
|
|
|
dnsSDLookupFailuresCount = prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
2016-10-21 01:18:28 -07:00
|
|
|
Name: "sd_dns_lookup_failures_total",
|
2016-05-05 02:45:51 -07:00
|
|
|
Help: "The number of DNS-SD lookup failures.",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(dnsSDLookupFailuresCount)
|
|
|
|
prometheus.MustRegister(dnsSDLookupsCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discovery periodically performs DNS-SD requests. It implements
|
|
|
|
// the TargetProvider interface.
|
|
|
|
type Discovery struct {
|
|
|
|
names []string
|
|
|
|
|
|
|
|
interval time.Duration
|
|
|
|
port int
|
|
|
|
qtype uint16
|
2017-04-28 08:12:38 -07:00
|
|
|
logger log.Logger
|
2016-05-05 02:45:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDiscovery returns a new Discovery which periodically refreshes its targets.
|
2017-04-28 08:12:38 -07:00
|
|
|
func NewDiscovery(conf *config.DNSSDConfig, logger log.Logger) *Discovery {
|
2016-05-05 02:45:51 -07:00
|
|
|
qtype := dns.TypeSRV
|
|
|
|
switch strings.ToUpper(conf.Type) {
|
|
|
|
case "A":
|
|
|
|
qtype = dns.TypeA
|
|
|
|
case "AAAA":
|
|
|
|
qtype = dns.TypeAAAA
|
|
|
|
case "SRV":
|
|
|
|
qtype = dns.TypeSRV
|
|
|
|
}
|
|
|
|
return &Discovery{
|
|
|
|
names: conf.Names,
|
|
|
|
interval: time.Duration(conf.RefreshInterval),
|
|
|
|
qtype: qtype,
|
|
|
|
port: conf.Port,
|
2017-04-28 08:12:38 -07:00
|
|
|
logger: logger,
|
2016-05-05 02:45:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run implements the TargetProvider interface.
|
2017-03-16 16:29:47 -07:00
|
|
|
func (d *Discovery) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
|
|
|
|
ticker := time.NewTicker(d.interval)
|
2016-05-05 02:45:51 -07:00
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
// Get an initial set right away.
|
2017-03-16 16:29:47 -07:00
|
|
|
d.refreshAll(ctx, ch)
|
2016-05-05 02:45:51 -07:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2017-03-16 16:29:47 -07:00
|
|
|
d.refreshAll(ctx, ch)
|
2016-05-05 02:45:51 -07:00
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 16:29:47 -07:00
|
|
|
func (d *Discovery) refreshAll(ctx context.Context, ch chan<- []*config.TargetGroup) {
|
2016-05-05 02:45:51 -07:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2017-03-16 16:29:47 -07:00
|
|
|
wg.Add(len(d.names))
|
|
|
|
for _, name := range d.names {
|
2016-05-05 02:45:51 -07:00
|
|
|
go func(n string) {
|
2017-03-16 16:29:47 -07:00
|
|
|
if err := d.refresh(ctx, n, ch); err != nil {
|
2017-04-28 08:12:38 -07:00
|
|
|
d.logger.Errorf("Error refreshing DNS targets: %s", err)
|
2016-05-05 02:45:51 -07:00
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2017-03-16 16:29:47 -07:00
|
|
|
func (d *Discovery) refresh(ctx context.Context, name string, ch chan<- []*config.TargetGroup) error {
|
2017-04-28 08:12:38 -07:00
|
|
|
response, err := lookupAll(name, d.qtype, d.logger)
|
2016-05-05 02:45:51 -07:00
|
|
|
dnsSDLookupsCount.Inc()
|
|
|
|
if err != nil {
|
|
|
|
dnsSDLookupFailuresCount.Inc()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tg := &config.TargetGroup{}
|
2016-09-05 05:40:28 -07:00
|
|
|
hostPort := func(a string, p int) model.LabelValue {
|
|
|
|
return model.LabelValue(net.JoinHostPort(a, fmt.Sprintf("%d", p)))
|
|
|
|
}
|
2016-05-05 02:45:51 -07:00
|
|
|
|
|
|
|
for _, record := range response.Answer {
|
|
|
|
target := model.LabelValue("")
|
|
|
|
switch addr := record.(type) {
|
|
|
|
case *dns.SRV:
|
|
|
|
// Remove the final dot from rooted DNS names to make them look more usual.
|
|
|
|
addr.Target = strings.TrimRight(addr.Target, ".")
|
|
|
|
|
2016-09-05 05:40:28 -07:00
|
|
|
target = hostPort(addr.Target, int(addr.Port))
|
2016-05-05 02:45:51 -07:00
|
|
|
case *dns.A:
|
2017-03-16 16:29:47 -07:00
|
|
|
target = hostPort(addr.A.String(), d.port)
|
2016-05-05 02:45:51 -07:00
|
|
|
case *dns.AAAA:
|
2017-03-16 16:29:47 -07:00
|
|
|
target = hostPort(addr.AAAA.String(), d.port)
|
2016-05-05 02:45:51 -07:00
|
|
|
default:
|
2017-04-28 08:12:38 -07:00
|
|
|
d.logger.Warnf("%q is not a valid SRV record", record)
|
2016-05-05 02:45:51 -07:00
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
tg.Targets = append(tg.Targets, model.LabelSet{
|
|
|
|
model.AddressLabel: target,
|
|
|
|
dnsNameLabel: model.LabelValue(name),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
tg.Source = name
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case ch <- []*config.TargetGroup{tg}:
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-28 08:12:38 -07:00
|
|
|
func lookupAll(name string, qtype uint16, logger log.Logger) (*dns.Msg, error) {
|
2016-05-05 02:45:51 -07:00
|
|
|
conf, err := dns.ClientConfigFromFile(resolvConf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not load resolv.conf: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &dns.Client{}
|
|
|
|
response := &dns.Msg{}
|
|
|
|
|
|
|
|
for _, server := range conf.Servers {
|
|
|
|
servAddr := net.JoinHostPort(server, conf.Port)
|
2017-02-13 12:48:51 -08:00
|
|
|
for _, lname := range conf.NameList(name) {
|
|
|
|
response, err = lookup(lname, qtype, client, servAddr, false)
|
2016-05-05 02:45:51 -07:00
|
|
|
if err != nil {
|
2017-04-28 08:12:38 -07:00
|
|
|
logger.
|
2016-11-03 06:41:04 -07:00
|
|
|
With("server", server).
|
|
|
|
With("name", name).
|
|
|
|
With("reason", err).
|
|
|
|
Warn("DNS resolution failed.")
|
2016-05-05 02:45:51 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(response.Answer) > 0 {
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-03 06:41:04 -07:00
|
|
|
return response, fmt.Errorf("could not resolve %s: no server responded", name)
|
2016-05-05 02:45:51 -07:00
|
|
|
}
|
|
|
|
|
2017-02-13 12:48:51 -08:00
|
|
|
func lookup(lname string, queryType uint16, client *dns.Client, servAddr string, edns bool) (*dns.Msg, error) {
|
2016-05-05 02:45:51 -07:00
|
|
|
msg := &dns.Msg{}
|
|
|
|
msg.SetQuestion(dns.Fqdn(lname), queryType)
|
|
|
|
|
|
|
|
if edns {
|
2016-11-03 06:41:04 -07:00
|
|
|
msg.SetEdns0(dns.DefaultMsgSize, false)
|
2016-05-05 02:45:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
response, _, err := client.Exchange(msg, servAddr)
|
2016-11-03 06:41:04 -07:00
|
|
|
if err == dns.ErrTruncated {
|
2016-05-05 02:45:51 -07:00
|
|
|
if client.Net == "tcp" {
|
2016-11-03 06:41:04 -07:00
|
|
|
return nil, fmt.Errorf("got truncated message on TCP (64kiB limit exceeded?)")
|
2016-05-05 02:45:51 -07:00
|
|
|
}
|
|
|
|
if edns { // Truncated even though EDNS is used
|
|
|
|
client.Net = "tcp"
|
|
|
|
}
|
2017-02-13 12:48:51 -08:00
|
|
|
return lookup(lname, queryType, client, servAddr, !edns)
|
2016-05-05 02:45:51 -07:00
|
|
|
}
|
2016-11-03 06:41:04 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if msg.Id != response.Id {
|
|
|
|
return nil, fmt.Errorf("DNS ID mismatch, request: %d, response: %d", msg.Id, response.Id)
|
|
|
|
}
|
2016-05-05 02:45:51 -07:00
|
|
|
return response, nil
|
|
|
|
}
|