add http_response plugin
This commit is contained in:
parent
51d7724255
commit
577134a6d0
|
@ -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
|
||||
```
|
|
@ -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{}
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue