telegraf/plugins/inputs/http_response/http_response.go

116 lines
2.6 KiB
Go
Raw Normal View History

2016-03-31 07:33:28 +00:00
package http_response
import (
2016-03-31 09:53:51 +00:00
"bufio"
2016-03-31 07:33:28 +00:00
"errors"
"net/http"
2016-03-31 09:53:51 +00:00
"net/textproto"
2016-03-31 07:33:28 +00:00
"net/url"
2016-03-31 09:53:51 +00:00
"strings"
2016-03-31 07:33:28 +00:00
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)
2016-03-31 09:53:51 +00:00
// HTTPResponse struct
type HTTPResponse struct {
2016-03-31 07:33:28 +00:00
Address string
Method string
ResponseTimeout int
2016-03-31 09:53:51 +00:00
Headers string
2016-03-31 07:33:28 +00:00
}
2016-03-31 09:53:51 +00:00
// Description returns the plugin Description
func (h *HTTPResponse) Description() string {
2016-03-31 07:33:28 +00:00
return "HTTP/HTTPS request given an address a method and a timeout"
}
var sampleConfig = `
## Server address (default http://localhost)
2016-03-31 08:54:08 +00:00
address = "https://github.com"
2016-03-31 07:33:28 +00:00
## Set response_timeout (default 1 seconds)
2016-03-31 08:54:08 +00:00
response_timeout = 10
2016-03-31 07:33:28 +00:00
## HTTP Method
method = "GET"
2016-03-31 09:53:51 +00:00
## HTTP Request Headers
headers = '''
Host: github.com
'''
2016-03-31 07:33:28 +00:00
`
2016-03-31 09:53:51 +00:00
// SampleConfig returns the plugin SampleConfig
func (h *HTTPResponse) SampleConfig() string {
2016-03-31 07:33:28 +00:00
return sampleConfig
}
2016-03-31 09:53:51 +00:00
// HTTPGather gathers all fields and returns any errors it encounters
func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
2016-03-31 07:33:28 +00:00
// Prepare fields
fields := make(map[string]interface{})
client := &http.Client{
Timeout: time.Second * time.Duration(h.ResponseTimeout),
}
request, err := http.NewRequest(h.Method, h.Address, nil)
if err != nil {
return nil, err
}
2016-03-31 09:53:51 +00:00
h.Headers = strings.TrimSpace(h.Headers) + "\n\n"
reader := bufio.NewReader(strings.NewReader(h.Headers))
tp := textproto.NewReader(reader)
mimeHeader, err := tp.ReadMIMEHeader()
if err != nil {
return nil, err
}
request.Header = http.Header(mimeHeader)
2016-03-31 07:33:28 +00:00
// Start Timer
start := time.Now()
resp, err := client.Do(request)
if err != nil {
return nil, err
}
fields["response_time"] = time.Since(start).Seconds()
fields["http_response_code"] = resp.StatusCode
return fields, nil
}
2016-03-31 09:53:51 +00:00
// Gather gets all metric fields and tags and returns any errors it encounters
func (h *HTTPResponse) Gather(acc telegraf.Accumulator) error {
2016-03-31 07:33:28 +00:00
// Set default values
2016-03-31 09:53:51 +00:00
if h.ResponseTimeout < 1 {
h.ResponseTimeout = 1
2016-03-31 07:33:28 +00:00
}
// Check send and expected string
2016-03-31 09:53:51 +00:00
if h.Method == "" {
h.Method = "GET"
2016-03-31 07:33:28 +00:00
}
2016-03-31 09:53:51 +00:00
if h.Address == "" {
h.Address = "http://localhost"
2016-03-31 07:33:28 +00:00
}
2016-03-31 09:53:51 +00:00
addr, err := url.Parse(h.Address)
2016-03-31 07:33:28 +00:00
if err != nil {
return err
}
if addr.Scheme != "http" && addr.Scheme != "https" {
return errors.New("Only http and https are supported")
}
// Prepare data
2016-03-31 09:53:51 +00:00
tags := map[string]string{"server": h.Address, "method": h.Method}
2016-03-31 07:33:28 +00:00
var fields map[string]interface{}
// Gather data
2016-03-31 09:53:51 +00:00
fields, err = h.HTTPGather()
2016-03-31 07:33:28 +00:00
if err != nil {
return err
}
// Add metrics
acc.AddFields("http_response", fields, tags)
return nil
}
func init() {
inputs.Add("http_response", func() telegraf.Input {
2016-03-31 09:53:51 +00:00
return &HTTPResponse{}
2016-03-31 07:33:28 +00:00
})
}