telegraf/plugins/inputs/http_response/http_response.go

141 lines
3.2 KiB
Go

package http_response
import (
"bufio"
"errors"
"fmt"
"net/http"
"net/textproto"
"net/url"
"os"
"strings"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)
// HTTPResponse struct
type HTTPResponse struct {
Address string
Method string
ResponseTimeout int
Headers string
FollowRedirects bool
}
// Description returns the plugin Description
func (h *HTTPResponse) Description() string {
return "HTTP/HTTPS request given an address a method and a timeout"
}
var sampleConfig = `
## Server address (default http://localhost)
address = "http://github.com"
## Set response_timeout (default 10 seconds)
response_timeout = 10
## HTTP Method
method = "GET"
## HTTP Request Headers
headers = '''
Host: github.com
'''
## Whether to follow redirects from the server (defaults to false)
follow_redirects = true
`
// SampleConfig returns the plugin SampleConfig
func (h *HTTPResponse) SampleConfig() string {
return sampleConfig
}
var ErrRedirectAttempted = errors.New("redirect")
// HTTPGather gathers all fields and returns any errors it encounters
func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
// Prepare fields
fields := make(map[string]interface{})
client := &http.Client{
Timeout: time.Second * time.Duration(h.ResponseTimeout),
}
if h.FollowRedirects == false {
fmt.Println(h.FollowRedirects)
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return ErrRedirectAttempted
}
}
request, err := http.NewRequest(h.Method, h.Address, nil)
if err != nil {
return nil, err
}
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)
// Start Timer
start := time.Now()
request.Write(os.Stdout)
resp, err := client.Do(request)
if err != nil {
if h.FollowRedirects {
return nil, err
}
if urlError, ok := err.(*url.Error); ok &&
urlError.Err == ErrRedirectAttempted {
fmt.Println(err)
err = nil
} else {
return nil, err
}
}
fields["response_time"] = time.Since(start).Seconds()
fields["http_response_code"] = resp.StatusCode
return fields, nil
}
// Gather gets all metric fields and tags and returns any errors it encounters
func (h *HTTPResponse) Gather(acc telegraf.Accumulator) error {
// Set default values
if h.ResponseTimeout < 1 {
h.ResponseTimeout = 10
}
// Check send and expected string
if h.Method == "" {
h.Method = "GET"
}
if h.Address == "" {
h.Address = "http://localhost"
}
addr, err := url.Parse(h.Address)
if err != nil {
return err
}
if addr.Scheme != "http" && addr.Scheme != "https" {
return errors.New("Only http and https are supported")
}
// Prepare data
tags := map[string]string{"server": h.Address, "method": h.Method}
var fields map[string]interface{}
// Gather data
fields, err = h.HTTPGather()
if err != nil {
return err
}
// Add metrics
acc.AddFields("http_response", fields, tags)
return nil
}
func init() {
inputs.Add("http_response", func() telegraf.Input {
return &HTTPResponse{}
})
}