|
| 1 | +package url_monitor |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/influxdata/telegraf" |
| 12 | + "github.com/influxdata/telegraf/internal" |
| 13 | + "github.com/influxdata/telegraf/plugins/inputs" |
| 14 | +) |
| 15 | + |
| 16 | +// HTTPResponse struct |
| 17 | +type HTTPResponse struct { |
| 18 | + Address string |
| 19 | + Body string |
| 20 | + Method string |
| 21 | + ResponseTimeout internal.Duration |
| 22 | + Headers map[string]string |
| 23 | + FollowRedirects bool |
| 24 | + |
| 25 | + // Path to CA file |
| 26 | + SSLCA string `toml:"ssl_ca"` |
| 27 | + // Path to host cert file |
| 28 | + SSLCert string `toml:"ssl_cert"` |
| 29 | + // Path to cert key file |
| 30 | + SSLKey string `toml:"ssl_key"` |
| 31 | + // Use SSL but skip chain & host verification |
| 32 | + InsecureSkipVerify bool |
| 33 | +} |
| 34 | + |
| 35 | +// Description returns the plugin Description |
| 36 | +func (h *HTTPResponse) Description() string { |
| 37 | + return "HTTP/HTTPS request given an address a method and a timeout" |
| 38 | +} |
| 39 | + |
| 40 | +var sampleConfig = ` |
| 41 | + ## Server address (default http://localhost) |
| 42 | + address = "http://github.com" |
| 43 | + ## Set response_timeout (default 5 seconds) |
| 44 | + response_timeout = "5s" |
| 45 | + ## HTTP Request Method |
| 46 | + method = "GET" |
| 47 | + ## Whether to follow redirects from the server (defaults to false) |
| 48 | + follow_redirects = true |
| 49 | + ## HTTP Request Headers (all values must be strings) |
| 50 | + # [inputs.url_monitor.headers] |
| 51 | + # Host = "github.com" |
| 52 | + ## Optional HTTP Request Body |
| 53 | + # body = ''' |
| 54 | + # {'fake':'data'} |
| 55 | + # ''' |
| 56 | +
|
| 57 | + ## Optional SSL Config |
| 58 | + # ssl_ca = "/etc/telegraf/ca.pem" |
| 59 | + # ssl_cert = "/etc/telegraf/cert.pem" |
| 60 | + # ssl_key = "/etc/telegraf/key.pem" |
| 61 | + ## Use SSL but skip chain & host verification |
| 62 | + # insecure_skip_verify = false |
| 63 | +` |
| 64 | + |
| 65 | +// SampleConfig returns the plugin SampleConfig |
| 66 | +func (h *HTTPResponse) SampleConfig() string { |
| 67 | + return sampleConfig |
| 68 | +} |
| 69 | + |
| 70 | +// ErrRedirectAttempted indicates that a redirect occurred |
| 71 | +var ErrRedirectAttempted = errors.New("redirect") |
| 72 | + |
| 73 | +// CreateHttpClient creates an http client which will timeout at the specified |
| 74 | +// timeout period and can follow redirects if specified |
| 75 | +func (h *HTTPResponse) createHttpClient() (*http.Client, error) { |
| 76 | + tlsCfg, err := internal.GetTLSConfig( |
| 77 | + h.SSLCert, h.SSLKey, h.SSLCA, h.InsecureSkipVerify) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + tr := &http.Transport{ |
| 82 | + ResponseHeaderTimeout: h.ResponseTimeout.Duration, |
| 83 | + TLSClientConfig: tlsCfg, |
| 84 | + } |
| 85 | + client := &http.Client{ |
| 86 | + Transport: tr, |
| 87 | + Timeout: h.ResponseTimeout.Duration, |
| 88 | + } |
| 89 | + |
| 90 | + if h.FollowRedirects == false { |
| 91 | + client.CheckRedirect = func(req *http.Request, via []*http.Request) error { |
| 92 | + return ErrRedirectAttempted |
| 93 | + } |
| 94 | + } |
| 95 | + return client, nil |
| 96 | +} |
| 97 | + |
| 98 | +// HTTPGather gathers all fields and returns any errors it encounters |
| 99 | +func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) { |
| 100 | + // Prepare fields |
| 101 | + fields := make(map[string]interface{}) |
| 102 | + |
| 103 | + client, err := h.createHttpClient() |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + |
| 108 | + var body io.Reader |
| 109 | + if h.Body != "" { |
| 110 | + body = strings.NewReader(h.Body) |
| 111 | + } |
| 112 | + request, err := http.NewRequest(h.Method, h.Address, body) |
| 113 | + if err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + |
| 117 | + for key, val := range h.Headers { |
| 118 | + request.Header.Add(key, val) |
| 119 | + if key == "Host" { |
| 120 | + request.Host = val |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // Start Timer |
| 125 | + start := time.Now() |
| 126 | + resp, err := client.Do(request) |
| 127 | + if err != nil { |
| 128 | + if h.FollowRedirects { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + if urlError, ok := err.(*url.Error); ok && |
| 132 | + urlError.Err == ErrRedirectAttempted { |
| 133 | + err = nil |
| 134 | + } else { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + } |
| 138 | + fields["response_time"] = time.Since(start).Seconds() |
| 139 | + fields["url_monitor_code"] = resp.StatusCode |
| 140 | + return fields, nil |
| 141 | +} |
| 142 | + |
| 143 | +// Gather gets all metric fields and tags and returns any errors it encounters |
| 144 | +func (h *HTTPResponse) Gather(acc telegraf.Accumulator) error { |
| 145 | + // Set default values |
| 146 | + if h.ResponseTimeout.Duration < time.Second { |
| 147 | + h.ResponseTimeout.Duration = time.Second * 5 |
| 148 | + } |
| 149 | + // Check send and expected string |
| 150 | + if h.Method == "" { |
| 151 | + h.Method = "GET" |
| 152 | + } |
| 153 | + if h.Address == "" { |
| 154 | + h.Address = "http://localhost" |
| 155 | + } |
| 156 | + addr, err := url.Parse(h.Address) |
| 157 | + if err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + if addr.Scheme != "http" && addr.Scheme != "https" { |
| 161 | + return errors.New("Only http and https are supported") |
| 162 | + } |
| 163 | + // Prepare data |
| 164 | + tags := map[string]string{"server": h.Address, "method": h.Method} |
| 165 | + var fields map[string]interface{} |
| 166 | + // Gather data |
| 167 | + fields, err = h.HTTPGather() |
| 168 | + if err != nil { |
| 169 | + return err |
| 170 | + } |
| 171 | + // Add metrics |
| 172 | + acc.AddFields("url_monitor", fields, tags) |
| 173 | + return nil |
| 174 | +} |
| 175 | + |
| 176 | +func init() { |
| 177 | + inputs.Add("url_monitor", func() telegraf.Input { |
| 178 | + return &HTTPResponse{} |
| 179 | + }) |
| 180 | +} |
0 commit comments