-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add http_response plugin #943
Closed
Closed
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
577134a
add http_response plugin
e922788
add plugin to all
lswith 0cf4b67
update to make a working sample_config
lswith 67b81a6
fmt
lswith b480e2f
add the ability to parse http headers
lswith a47b361
update to allow for following redirects
lswith 8dd1300
take a request body as a param
lswith 6feef3f
added tests and did some refactoring
bf09b4f
update to 5 second default and string map for headers
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# 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" | ||
## Set response_timeout (default 10 seconds) | ||
response_timeout = 10 | ||
## HTTP Request Method | ||
method = "GET" | ||
## HTTP Request Headers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these headers should be specified as a map (see https://github.com/influxdata/telegraf/blob/master/plugins/inputs/httpjson/httpjson.go) |
||
headers = ''' | ||
Host: github.com | ||
''' | ||
## Whether to follow redirects from the server (defaults to false) | ||
follow_redirects = true | ||
## Optional HTTP Request Body | ||
body = ''' | ||
{'fake':'data'} | ||
''' | ||
``` | ||
|
||
### Measurements & Fields: | ||
|
||
- http_response | ||
- response_time (float, seconds) | ||
- http_response_code (int) #The code received | ||
|
||
### Tags: | ||
|
||
- All measurements have the following tags: | ||
- server | ||
- method | ||
|
||
### Example Output: | ||
|
||
``` | ||
$ ./telegraf -config telegraf.conf -input-filter http_response -test | ||
http_response,method=GET,server=http://www.github.com http_response_code=200i,response_time=6.223266528 1459419354977857955 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package http_response | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"io" | ||
"net/http" | ||
"net/textproto" | ||
"net/url" | ||
"strings" | ||
"time" | ||
|
||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/plugins/inputs" | ||
) | ||
|
||
// HTTPResponse struct | ||
type HTTPResponse struct { | ||
Address string | ||
Body 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 Request Method | ||
method = "GET" | ||
## HTTP Request Headers | ||
headers = ''' | ||
Host: github.com | ||
''' | ||
## Whether to follow redirects from the server (defaults to false) | ||
follow_redirects = true | ||
## Optional HTTP Request Body | ||
body = ''' | ||
{'fake':'data'} | ||
''' | ||
` | ||
|
||
// SampleConfig returns the plugin SampleConfig | ||
func (h *HTTPResponse) SampleConfig() string { | ||
return sampleConfig | ||
} | ||
|
||
// ErrRedirectAttempted indicates that a redirect occurred | ||
var ErrRedirectAttempted = errors.New("redirect") | ||
|
||
// CreateHttpClient creates an http client which will timeout at the specified | ||
// timeout period and can follow redirects if specified | ||
func CreateHttpClient(followRedirects bool, ResponseTimeout time.Duration) *http.Client { | ||
client := &http.Client{ | ||
Timeout: time.Second * ResponseTimeout, | ||
} | ||
|
||
if followRedirects == false { | ||
client.CheckRedirect = func(req *http.Request, via []*http.Request) error { | ||
return ErrRedirectAttempted | ||
} | ||
} | ||
return client | ||
} | ||
|
||
// ParseHeaders takes a string of newline seperated http headers and returns a | ||
// http.Header object. An error is returned if the headers cannot be parsed. | ||
func ParseHeaders(headers string) (http.Header, error) { | ||
headers = strings.TrimSpace(headers) + "\n\n" | ||
reader := bufio.NewReader(strings.NewReader(headers)) | ||
tp := textproto.NewReader(reader) | ||
mimeHeader, err := tp.ReadMIMEHeader() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return http.Header(mimeHeader), nil | ||
} | ||
|
||
// 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 := CreateHttpClient(h.FollowRedirects, time.Duration(h.ResponseTimeout)) | ||
|
||
var body io.Reader | ||
if h.Body != "" { | ||
body = strings.NewReader(h.Body) | ||
} | ||
request, err := http.NewRequest(h.Method, h.Address, body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
request.Header, err = ParseHeaders(h.Headers) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Start Timer | ||
start := time.Now() | ||
resp, err := client.Do(request) | ||
if err != nil { | ||
if h.FollowRedirects { | ||
return nil, err | ||
} | ||
if urlError, ok := err.(*url.Error); ok && | ||
urlError.Err == ErrRedirectAttempted { | ||
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{} | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
default this to 5 seconds