From 502bd7f203d9ee2b9076d4a0afd9b05ab0a47682 Mon Sep 17 00:00:00 2001 From: Luke Swithenbank Date: Thu, 31 Mar 2016 18:33:28 +1100 Subject: [PATCH] add http_response plugin --- plugins/inputs/http_response/README.md | 36 +++++++ plugins/inputs/http_response/http_response.go | 95 +++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 plugins/inputs/http_response/README.md create mode 100644 plugins/inputs/http_response/http_response.go diff --git a/plugins/inputs/http_response/README.md b/plugins/inputs/http_response/README.md new file mode 100644 index 000000000..13da76097 --- /dev/null +++ b/plugins/inputs/http_response/README.md @@ -0,0 +1,36 @@ +# Example Input Plugin + +This input plugin will test HTTP/HTTPS connections. + +### Configuration: + +``` +# List of UDP/TCP connections you want to check +[[inputs.http_response]] + # Server address (default http://localhost) + address = "http://github.com:80" + # Set http response timeout (default 1.0) + response_timeout = 1.0 + # HTTP Method (default "GET") + method = "GET" +``` + +### Measurements & Fields: + +- http_response + - response_time (float, seconds) + - http_response_code (int) #The code received + +### Tags: + +- All measurements have the following tags: + - server + - port + - protocol + +### Example Output: + +``` +$ ./telegraf -config telegraf.conf -input-filter http_response -test +http_response,server=http://192.168.2.2:2000,method=GET response_time=0.18070360500000002,http_response_code=200 1454785464182527094 +``` diff --git a/plugins/inputs/http_response/http_response.go b/plugins/inputs/http_response/http_response.go new file mode 100644 index 000000000..e19c698a8 --- /dev/null +++ b/plugins/inputs/http_response/http_response.go @@ -0,0 +1,95 @@ +package http_response + +import ( + "errors" + "net/http" + "net/url" + "time" + + "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/plugins/inputs" +) + +// HttpResponses struct +type HttpResponse struct { + Address string + Method string + ResponseTimeout int +} + +func (_ *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:80" + ## Set response_timeout (default 1 seconds) + response_timeout = 1 + ## HTTP Method + method = "GET" +` + +func (_ *HttpResponse) SampleConfig() string { + return sampleConfig +} + +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), + } + request, err := http.NewRequest(h.Method, h.Address, nil) + if err != nil { + return nil, err + } + // 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 +} + +func (c *HttpResponse) Gather(acc telegraf.Accumulator) error { + // Set default values + if c.ResponseTimeout < 1 { + c.ResponseTimeout = 1 + } + // Check send and expected string + if c.Method == "" { + c.Method = "GET" + } + if c.Address == "" { + c.Address = "http://localhost" + } + addr, err := url.Parse(c.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": c.Address, "method": c.Method} + var fields map[string]interface{} + // Gather data + fields, err = c.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{} + }) +}