2015-06-20 12:38:01 +00:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-03-01 16:12:23 +00:00
|
|
|
"io/ioutil"
|
2017-09-18 22:06:11 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
2015-06-20 12:38:01 +00:00
|
|
|
"net/http"
|
2017-09-18 22:06:11 +00:00
|
|
|
"net/url"
|
2015-06-20 12:38:01 +00:00
|
|
|
"sync"
|
2016-02-29 16:52:58 +00:00
|
|
|
"time"
|
2017-03-29 22:04:29 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/internal"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-06-20 12:38:01 +00:00
|
|
|
)
|
|
|
|
|
2016-07-07 10:15:47 +00:00
|
|
|
const acceptHeader = `application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited;q=0.7,text/plain;version=0.0.4;q=0.3`
|
|
|
|
|
2015-06-20 12:38:01 +00:00
|
|
|
type Prometheus struct {
|
2017-09-18 22:06:11 +00:00
|
|
|
// An array of urls to scrape metrics from.
|
2015-06-20 12:38:01 +00:00
|
|
|
Urls []string
|
2016-03-17 19:17:48 +00:00
|
|
|
|
2017-09-18 22:06:11 +00:00
|
|
|
// An array of Kubernetes services to scrape metrics from.
|
|
|
|
KubernetesServices []string
|
|
|
|
|
2016-03-17 19:17:48 +00:00
|
|
|
// Bearer Token authorization file path
|
|
|
|
BearerToken string `toml:"bearer_token"`
|
2016-06-23 07:59:44 +00:00
|
|
|
|
2016-11-07 16:34:02 +00:00
|
|
|
ResponseTimeout internal.Duration `toml:"response_timeout"`
|
|
|
|
|
2016-06-23 07:59:44 +00:00
|
|
|
// Path to CA file
|
|
|
|
SSLCA string `toml:"ssl_ca"`
|
|
|
|
// Path to host cert file
|
|
|
|
SSLCert string `toml:"ssl_cert"`
|
|
|
|
// Path to cert key file
|
|
|
|
SSLKey string `toml:"ssl_key"`
|
|
|
|
// Use SSL but skip chain & host verification
|
|
|
|
InsecureSkipVerify bool
|
2017-05-09 23:20:43 +00:00
|
|
|
|
|
|
|
client *http.Client
|
2015-06-20 12:38:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## An array of urls to scrape metrics from.
|
2015-10-15 21:53:29 +00:00
|
|
|
urls = ["http://localhost:9100/metrics"]
|
2016-03-17 19:17:48 +00:00
|
|
|
|
2017-09-18 22:06:11 +00:00
|
|
|
## An array of Kubernetes services to scrape metrics from.
|
2017-09-18 23:21:45 +00:00
|
|
|
# kubernetes_services = ["http://my-service-dns.my-namespace:9100/metrics"]
|
2017-09-18 22:06:11 +00:00
|
|
|
|
2016-03-31 23:50:24 +00:00
|
|
|
## Use bearer token for authorization
|
|
|
|
# bearer_token = /path/to/bearer/token
|
2016-06-23 07:59:44 +00:00
|
|
|
|
2016-11-07 16:34:02 +00:00
|
|
|
## Specify timeout duration for slower prometheus clients (default is 3s)
|
|
|
|
# response_timeout = "3s"
|
|
|
|
|
2016-06-23 07:59:44 +00:00
|
|
|
## Optional SSL Config
|
|
|
|
# ssl_ca = /path/to/cafile
|
|
|
|
# ssl_cert = /path/to/certfile
|
|
|
|
# ssl_key = /path/to/keyfile
|
|
|
|
## Use SSL but skip chain & host verification
|
|
|
|
# insecure_skip_verify = false
|
2015-08-26 15:21:39 +00:00
|
|
|
`
|
2015-06-20 12:38:01 +00:00
|
|
|
|
2016-03-17 19:17:48 +00:00
|
|
|
func (p *Prometheus) SampleConfig() string {
|
2015-06-20 12:38:01 +00:00
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
2016-03-17 19:17:48 +00:00
|
|
|
func (p *Prometheus) Description() string {
|
2015-06-20 12:38:01 +00:00
|
|
|
return "Read metrics from one or many prometheus clients"
|
|
|
|
}
|
|
|
|
|
|
|
|
var ErrProtocolError = errors.New("prometheus protocol error")
|
|
|
|
|
2017-09-18 22:06:11 +00:00
|
|
|
func (p *Prometheus) AddressToURL(u *url.URL, address string) string {
|
|
|
|
host := address
|
|
|
|
if u.Port() != "" {
|
|
|
|
host = address + ":" + u.Port()
|
|
|
|
}
|
|
|
|
reconstructedUrl := url.URL{
|
|
|
|
Scheme: u.Scheme,
|
|
|
|
Opaque: u.Opaque,
|
|
|
|
User: u.User,
|
|
|
|
Path: u.Path,
|
|
|
|
RawPath: u.RawPath,
|
|
|
|
ForceQuery: u.ForceQuery,
|
|
|
|
RawQuery: u.RawQuery,
|
|
|
|
Fragment: u.Fragment,
|
|
|
|
Host: host,
|
|
|
|
}
|
|
|
|
return reconstructedUrl.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
type UrlAndAddress struct {
|
2017-09-23 00:26:19 +00:00
|
|
|
OriginalUrl string
|
|
|
|
Url string
|
|
|
|
Address string
|
2017-09-18 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Prometheus) GetAllURLs() ([]UrlAndAddress, error) {
|
|
|
|
allUrls := make([]UrlAndAddress, 0)
|
|
|
|
for _, url := range p.Urls {
|
2017-09-23 00:26:19 +00:00
|
|
|
allUrls = append(allUrls, UrlAndAddress{Url: url, OriginalUrl: url})
|
2017-09-18 22:06:11 +00:00
|
|
|
}
|
|
|
|
for _, service := range p.KubernetesServices {
|
|
|
|
u, err := url.Parse(service)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resolvedAddresses, err := net.LookupHost(u.Hostname())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("prometheus: Could not resolve %s, skipping it. Error: %s", u.Host, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, resolved := range resolvedAddresses {
|
|
|
|
serviceUrl := p.AddressToURL(u, resolved)
|
2017-09-23 00:26:19 +00:00
|
|
|
allUrls = append(allUrls, UrlAndAddress{Url: serviceUrl, Address: resolved, OriginalUrl: service})
|
2017-09-18 22:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return allUrls, nil
|
|
|
|
}
|
|
|
|
|
2015-06-20 12:38:01 +00:00
|
|
|
// Reads stats from all configured servers accumulates stats.
|
|
|
|
// Returns one of the errors encountered while gather stats (if any).
|
2016-03-17 19:17:48 +00:00
|
|
|
func (p *Prometheus) Gather(acc telegraf.Accumulator) error {
|
2017-05-09 23:20:43 +00:00
|
|
|
if p.client == nil {
|
|
|
|
client, err := p.createHttpClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.client = client
|
|
|
|
}
|
|
|
|
|
2015-06-20 12:38:01 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2017-09-18 22:06:11 +00:00
|
|
|
allUrls, err := p.GetAllURLs()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, url := range allUrls {
|
2015-06-20 12:38:01 +00:00
|
|
|
wg.Add(1)
|
2017-09-18 22:06:11 +00:00
|
|
|
go func(serviceUrl UrlAndAddress) {
|
2015-06-20 12:38:01 +00:00
|
|
|
defer wg.Done()
|
2017-09-18 22:06:11 +00:00
|
|
|
acc.AddError(p.gatherURL(serviceUrl, acc))
|
|
|
|
}(url)
|
2015-06-20 12:38:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
2017-04-24 18:13:26 +00:00
|
|
|
return nil
|
2015-06-20 12:38:01 +00:00
|
|
|
}
|
|
|
|
|
2016-02-29 16:52:58 +00:00
|
|
|
var tr = &http.Transport{
|
|
|
|
ResponseHeaderTimeout: time.Duration(3 * time.Second),
|
|
|
|
}
|
|
|
|
|
|
|
|
var client = &http.Client{
|
|
|
|
Transport: tr,
|
|
|
|
Timeout: time.Duration(4 * time.Second),
|
|
|
|
}
|
|
|
|
|
2017-05-09 23:20:43 +00:00
|
|
|
func (p *Prometheus) createHttpClient() (*http.Client, error) {
|
2016-06-23 07:59:44 +00:00
|
|
|
tlsCfg, err := internal.GetTLSConfig(
|
|
|
|
p.SSLCert, p.SSLKey, p.SSLCA, p.InsecureSkipVerify)
|
|
|
|
if err != nil {
|
2017-05-09 23:20:43 +00:00
|
|
|
return nil, err
|
2016-06-23 07:59:44 +00:00
|
|
|
}
|
|
|
|
|
2017-05-09 23:20:43 +00:00
|
|
|
client := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: tlsCfg,
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
},
|
|
|
|
Timeout: p.ResponseTimeout.Duration,
|
2016-03-17 19:17:48 +00:00
|
|
|
}
|
|
|
|
|
2017-05-09 23:20:43 +00:00
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2017-09-18 22:06:11 +00:00
|
|
|
func (p *Prometheus) gatherURL(url UrlAndAddress, acc telegraf.Accumulator) error {
|
|
|
|
var req, err = http.NewRequest("GET", url.Url, nil)
|
2017-05-09 23:20:43 +00:00
|
|
|
req.Header.Add("Accept", acceptHeader)
|
|
|
|
var token []byte
|
|
|
|
var resp *http.Response
|
|
|
|
|
2016-03-17 19:17:48 +00:00
|
|
|
if p.BearerToken != "" {
|
|
|
|
token, err = ioutil.ReadFile(p.BearerToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Set("Authorization", "Bearer "+string(token))
|
|
|
|
}
|
|
|
|
|
2017-05-09 23:20:43 +00:00
|
|
|
resp, err = p.client.Do(req)
|
2015-06-20 12:38:01 +00:00
|
|
|
if err != nil {
|
2017-09-18 22:06:11 +00:00
|
|
|
return fmt.Errorf("error making HTTP request to %s: %s", url.Url, err)
|
2015-06-20 12:38:01 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2017-09-18 22:06:11 +00:00
|
|
|
return fmt.Errorf("%s returned HTTP status %s", url.Url, resp.Status)
|
2015-06-20 12:38:01 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 16:12:23 +00:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error reading body: %s", err)
|
|
|
|
}
|
2015-06-20 12:38:01 +00:00
|
|
|
|
2016-07-07 10:15:47 +00:00
|
|
|
metrics, err := Parse(body, resp.Header)
|
2016-03-01 16:12:23 +00:00
|
|
|
if err != nil {
|
2016-07-07 10:15:47 +00:00
|
|
|
return fmt.Errorf("error reading metrics for %s: %s",
|
2017-09-18 22:06:11 +00:00
|
|
|
url.Url, err)
|
2016-03-01 16:12:23 +00:00
|
|
|
}
|
|
|
|
// Add (or not) collected metrics
|
|
|
|
for _, metric := range metrics {
|
|
|
|
tags := metric.Tags()
|
2017-09-23 00:26:19 +00:00
|
|
|
tags["url"] = url.OriginalUrl
|
2017-09-18 22:06:11 +00:00
|
|
|
if url.Address != "" {
|
|
|
|
tags["address"] = url.Address
|
|
|
|
}
|
2017-10-18 21:51:08 +00:00
|
|
|
|
|
|
|
switch metric.Type() {
|
|
|
|
case telegraf.Counter:
|
|
|
|
acc.AddCounter(metric.Name(), metric.Fields(), tags, metric.Time())
|
|
|
|
case telegraf.Gauge:
|
|
|
|
acc.AddGauge(metric.Name(), metric.Fields(), tags, metric.Time())
|
|
|
|
default:
|
|
|
|
acc.AddFields(metric.Name(), metric.Fields(), tags, metric.Time())
|
|
|
|
}
|
2015-06-20 12:38:01 +00:00
|
|
|
}
|
2015-10-22 16:17:57 +00:00
|
|
|
|
2015-06-20 12:38:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("prometheus", func() telegraf.Input {
|
2016-12-17 13:10:33 +00:00
|
|
|
return &Prometheus{ResponseTimeout: internal.Duration{Duration: time.Second * 3}}
|
2015-06-20 12:38:01 +00:00
|
|
|
})
|
|
|
|
}
|