2015-06-20 12:38:01 +00:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
2016-03-17 19:17:48 +00:00
|
|
|
"crypto/tls"
|
2015-06-20 12:38:01 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2016-03-01 16:12:23 +00:00
|
|
|
"io/ioutil"
|
2016-03-17 19:17:48 +00:00
|
|
|
"net"
|
2015-06-20 12:38:01 +00:00
|
|
|
"net/http"
|
|
|
|
"sync"
|
2016-02-29 16:52:58 +00:00
|
|
|
"time"
|
2015-06-20 12:38:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Prometheus struct {
|
|
|
|
Urls []string
|
2016-03-17 19:17:48 +00:00
|
|
|
|
|
|
|
// Use SSL but skip chain & host verification
|
|
|
|
InsecureSkipVerify bool
|
|
|
|
// Bearer Token authorization file path
|
|
|
|
BearerToken string `toml:"bearer_token"`
|
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
|
|
|
|
2016-03-31 23:50:24 +00:00
|
|
|
## Use SSL but skip chain & host verification
|
|
|
|
# insecure_skip_verify = false
|
|
|
|
## Use bearer token for authorization
|
|
|
|
# bearer_token = /path/to/bearer/token
|
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")
|
|
|
|
|
|
|
|
// 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 {
|
2015-06-20 12:38:01 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
var outerr error
|
|
|
|
|
2016-03-17 19:17:48 +00:00
|
|
|
for _, serv := range p.Urls {
|
2015-06-20 12:38:01 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func(serv string) {
|
|
|
|
defer wg.Done()
|
2016-03-17 19:17:48 +00:00
|
|
|
outerr = p.gatherURL(serv, acc)
|
2015-06-20 12:38:01 +00:00
|
|
|
}(serv)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return outerr
|
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
|
2016-03-17 19:17:48 +00:00
|
|
|
func (p *Prometheus) gatherURL(url string, acc telegraf.Accumulator) error {
|
2016-03-01 16:12:23 +00:00
|
|
|
collectDate := time.Now()
|
2016-03-17 19:17:48 +00:00
|
|
|
var req, err = http.NewRequest("GET", url, nil)
|
|
|
|
req.Header = make(http.Header)
|
|
|
|
var token []byte
|
|
|
|
var resp *http.Response
|
|
|
|
|
|
|
|
var rt http.RoundTripper = &http.Transport{
|
|
|
|
Dial: (&net.Dialer{
|
2016-04-04 09:59:28 +00:00
|
|
|
Timeout: 8 * time.Second,
|
2016-03-17 19:17:48 +00:00
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
}).Dial,
|
2016-04-04 09:59:28 +00:00
|
|
|
TLSHandshakeTimeout: 8 * time.Second,
|
2016-03-17 19:17:48 +00:00
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: p.InsecureSkipVerify,
|
|
|
|
},
|
|
|
|
ResponseHeaderTimeout: time.Duration(3 * time.Second),
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.BearerToken != "" {
|
|
|
|
token, err = ioutil.ReadFile(p.BearerToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Set("Authorization", "Bearer "+string(token))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err = rt.RoundTrip(req)
|
2015-06-20 12:38:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error making HTTP request to %s: %s", url, err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("%s returned HTTP status %s", url, resp.Status)
|
|
|
|
}
|
|
|
|
|
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-03-01 16:12:23 +00:00
|
|
|
// Headers
|
|
|
|
headers := make(map[string]string)
|
|
|
|
for key, value := range headers {
|
|
|
|
headers[key] = value
|
2015-06-20 12:38:01 +00:00
|
|
|
}
|
2016-03-01 16:12:23 +00:00
|
|
|
|
|
|
|
// Prepare Prometheus parser config
|
|
|
|
promparser := PrometheusParser{
|
|
|
|
PromFormat: headers,
|
2015-06-20 12:38:01 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 16:12:23 +00:00
|
|
|
metrics, err := promparser.Parse(body)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting processing samples for %s: %s",
|
|
|
|
url, err)
|
|
|
|
}
|
|
|
|
// Add (or not) collected metrics
|
|
|
|
for _, metric := range metrics {
|
|
|
|
tags := metric.Tags()
|
|
|
|
tags["url"] = url
|
|
|
|
acc.AddFields(metric.Name(), metric.Fields(), tags, collectDate)
|
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 {
|
2015-06-20 12:38:01 +00:00
|
|
|
return &Prometheus{}
|
|
|
|
})
|
|
|
|
}
|