add the ability to parse http headers

This commit is contained in:
Luke Swithenbank 2016-03-31 20:53:51 +11:00 committed by Cameron Sparr
parent b7435b9cd1
commit 7219efbdb7
1 changed files with 36 additions and 16 deletions

View File

@ -1,23 +1,28 @@
package http_response package http_response
import ( import (
"bufio"
"errors" "errors"
"net/http" "net/http"
"net/textproto"
"net/url" "net/url"
"strings"
"time" "time"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
) )
// HttpResponses struct // HTTPResponse struct
type HttpResponse struct { type HTTPResponse struct {
Address string Address string
Method string Method string
ResponseTimeout int ResponseTimeout int
Headers string
} }
func (_ *HttpResponse) Description() string { // Description returns the plugin Description
func (h *HTTPResponse) Description() string {
return "HTTP/HTTPS request given an address a method and a timeout" return "HTTP/HTTPS request given an address a method and a timeout"
} }
@ -28,13 +33,19 @@ var sampleConfig = `
response_timeout = 10 response_timeout = 10
## HTTP Method ## HTTP Method
method = "GET" method = "GET"
## HTTP Request Headers
headers = '''
Host: github.com
'''
` `
func (_ *HttpResponse) SampleConfig() string { // SampleConfig returns the plugin SampleConfig
func (h *HTTPResponse) SampleConfig() string {
return sampleConfig return sampleConfig
} }
func (h *HttpResponse) HttpGather() (map[string]interface{}, error) { // HTTPGather gathers all fields and returns any errors it encounters
func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
// Prepare fields // Prepare fields
fields := make(map[string]interface{}) fields := make(map[string]interface{})
@ -45,6 +56,14 @@ func (h *HttpResponse) HttpGather() (map[string]interface{}, error) {
if err != nil { if err != nil {
return nil, err 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 Timer
start := time.Now() start := time.Now()
resp, err := client.Do(request) resp, err := client.Do(request)
@ -56,19 +75,20 @@ func (h *HttpResponse) HttpGather() (map[string]interface{}, error) {
return fields, nil return fields, nil
} }
func (c *HttpResponse) Gather(acc telegraf.Accumulator) error { // Gather gets all metric fields and tags and returns any errors it encounters
func (h *HTTPResponse) Gather(acc telegraf.Accumulator) error {
// Set default values // Set default values
if c.ResponseTimeout < 1 { if h.ResponseTimeout < 1 {
c.ResponseTimeout = 1 h.ResponseTimeout = 1
} }
// Check send and expected string // Check send and expected string
if c.Method == "" { if h.Method == "" {
c.Method = "GET" h.Method = "GET"
} }
if c.Address == "" { if h.Address == "" {
c.Address = "http://localhost" h.Address = "http://localhost"
} }
addr, err := url.Parse(c.Address) addr, err := url.Parse(h.Address)
if err != nil { if err != nil {
return err return err
} }
@ -76,10 +96,10 @@ func (c *HttpResponse) Gather(acc telegraf.Accumulator) error {
return errors.New("Only http and https are supported") return errors.New("Only http and https are supported")
} }
// Prepare data // Prepare data
tags := map[string]string{"server": c.Address, "method": c.Method} tags := map[string]string{"server": h.Address, "method": h.Method}
var fields map[string]interface{} var fields map[string]interface{}
// Gather data // Gather data
fields, err = c.HttpGather() fields, err = h.HTTPGather()
if err != nil { if err != nil {
return err return err
} }
@ -90,6 +110,6 @@ func (c *HttpResponse) Gather(acc telegraf.Accumulator) error {
func init() { func init() {
inputs.Add("http_response", func() telegraf.Input { inputs.Add("http_response", func() telegraf.Input {
return &HttpResponse{} return &HTTPResponse{}
}) })
} }