add the ability to parse http headers

This commit is contained in:
Luke Swithenbank 2016-03-31 20:53:51 +11:00
parent 67b81a658f
commit b480e2ff20
1 changed files with 36 additions and 16 deletions

View File

@ -1,23 +1,28 @@
package http_response
import (
"bufio"
"errors"
"net/http"
"net/textproto"
"net/url"
"strings"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)
// HttpResponses struct
type HttpResponse struct {
// HTTPResponse struct
type HTTPResponse struct {
Address string
Method string
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"
}
@ -28,13 +33,19 @@ var sampleConfig = `
response_timeout = 10
## HTTP Method
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
}
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
fields := make(map[string]interface{})
@ -45,6 +56,14 @@ func (h *HttpResponse) HttpGather() (map[string]interface{}, error) {
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()
resp, err := client.Do(request)
@ -56,19 +75,20 @@ func (h *HttpResponse) HttpGather() (map[string]interface{}, error) {
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
if c.ResponseTimeout < 1 {
c.ResponseTimeout = 1
if h.ResponseTimeout < 1 {
h.ResponseTimeout = 1
}
// Check send and expected string
if c.Method == "" {
c.Method = "GET"
if h.Method == "" {
h.Method = "GET"
}
if c.Address == "" {
c.Address = "http://localhost"
if h.Address == "" {
h.Address = "http://localhost"
}
addr, err := url.Parse(c.Address)
addr, err := url.Parse(h.Address)
if err != nil {
return err
}
@ -76,10 +96,10 @@ func (c *HttpResponse) Gather(acc telegraf.Accumulator) error {
return errors.New("Only http and https are supported")
}
// 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{}
// Gather data
fields, err = c.HttpGather()
fields, err = h.HTTPGather()
if err != nil {
return err
}
@ -90,6 +110,6 @@ func (c *HttpResponse) Gather(acc telegraf.Accumulator) error {
func init() {
inputs.Add("http_response", func() telegraf.Input {
return &HttpResponse{}
return &HTTPResponse{}
})
}