|
| 1 | +package http |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/influxdata/telegraf" |
| 13 | + "github.com/influxdata/telegraf/internal" |
| 14 | + "github.com/influxdata/telegraf/plugins/inputs" |
| 15 | + "github.com/influxdata/telegraf/plugins/parsers" |
| 16 | +) |
| 17 | + |
| 18 | +type HTTP struct { |
| 19 | + URLs []string `toml:"urls"` |
| 20 | + |
| 21 | + Headers map[string]string |
| 22 | + |
| 23 | + // HTTP Basic Auth Credentials |
| 24 | + Username string |
| 25 | + Password string |
| 26 | + |
| 27 | + // Option to add "url" tag to each metric |
| 28 | + TagURL bool `toml:"tag_url"` |
| 29 | + |
| 30 | + // Path to CA file |
| 31 | + SSLCA string `toml:"ssl_ca"` |
| 32 | + // Path to host cert file |
| 33 | + SSLCert string `toml:"ssl_cert"` |
| 34 | + // Path to cert key file |
| 35 | + SSLKey string `toml:"ssl_key"` |
| 36 | + // Use SSL but skip chain & host verification |
| 37 | + InsecureSkipVerify bool |
| 38 | + |
| 39 | + Timeout internal.Duration |
| 40 | + |
| 41 | + client *http.Client |
| 42 | + |
| 43 | + // The parser will automatically be set by Telegraf core code because |
| 44 | + // this plugin implements the ParserInput interface (i.e. the SetParser method) |
| 45 | + parser parsers.Parser |
| 46 | +} |
| 47 | + |
| 48 | +var sampleConfig = ` |
| 49 | + ## One or more URLs from which to read formatted metrics |
| 50 | + urls = [ |
| 51 | + "http://localhost/metrics" |
| 52 | + ] |
| 53 | +
|
| 54 | + ## Optional HTTP headers |
| 55 | + # headers = {"X-Special-Header" = "Special-Value"} |
| 56 | +
|
| 57 | + ## Optional HTTP Basic Auth Credentials |
| 58 | + # username = "username" |
| 59 | + # password = "pa$$word" |
| 60 | +
|
| 61 | + ## Tag all metrics with the url |
| 62 | + # tag_url = true |
| 63 | +
|
| 64 | + ## Optional SSL Config |
| 65 | + # ssl_ca = "/etc/telegraf/ca.pem" |
| 66 | + # ssl_cert = "/etc/telegraf/cert.pem" |
| 67 | + # ssl_key = "/etc/telegraf/key.pem" |
| 68 | + ## Use SSL but skip chain & host verification |
| 69 | + # insecure_skip_verify = false |
| 70 | +
|
| 71 | + # timeout = "5s" |
| 72 | +
|
| 73 | + ## Data format to consume. |
| 74 | + ## Each data format has its own unique set of configuration options, read |
| 75 | + ## more about them here: |
| 76 | + ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md |
| 77 | + # data_format = "influx" |
| 78 | +` |
| 79 | + |
| 80 | +// SampleConfig returns the default configuration of the Input |
| 81 | +func (*HTTP) SampleConfig() string { |
| 82 | + return sampleConfig |
| 83 | +} |
| 84 | + |
| 85 | +// Description returns a one-sentence description on the Input |
| 86 | +func (*HTTP) Description() string { |
| 87 | + return "Read formatted metrics from one or more HTTP endpoints" |
| 88 | +} |
| 89 | + |
| 90 | +// Gather takes in an accumulator and adds the metrics that the Input |
| 91 | +// gathers. This is called every "interval" |
| 92 | +func (h *HTTP) Gather(acc telegraf.Accumulator) error { |
| 93 | + if h.client == nil { |
| 94 | + tlsCfg, err := internal.GetTLSConfig( |
| 95 | + h.SSLCert, h.SSLKey, h.SSLCA, h.InsecureSkipVerify) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + h.client = &http.Client{ |
| 100 | + Transport: &http.Transport{ |
| 101 | + TLSClientConfig: tlsCfg, |
| 102 | + }, |
| 103 | + Timeout: h.Timeout.Duration, |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + var wg sync.WaitGroup |
| 108 | + for _, u := range h.URLs { |
| 109 | + wg.Add(1) |
| 110 | + go func(url string) { |
| 111 | + defer wg.Done() |
| 112 | + if err := h.gatherURL(acc, url); err != nil { |
| 113 | + acc.AddError(fmt.Errorf("[url=%s]: %s", url, err)) |
| 114 | + } |
| 115 | + }(u) |
| 116 | + } |
| 117 | + |
| 118 | + wg.Wait() |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +// SetParser takes the data_format from the config and finds the right parser for that format |
| 124 | +func (h *HTTP) SetParser(parser parsers.Parser) { |
| 125 | + h.parser = parser |
| 126 | +} |
| 127 | + |
| 128 | +// Gathers data from a particular URL |
| 129 | +// Parameters: |
| 130 | +// acc : The telegraf Accumulator to use |
| 131 | +// url : endpoint to send request to |
| 132 | +// |
| 133 | +// Returns: |
| 134 | +// error: Any error that may have occurred |
| 135 | +func (h *HTTP) gatherURL( |
| 136 | + acc telegraf.Accumulator, |
| 137 | + url string, |
| 138 | +) error { |
| 139 | + request, err := http.NewRequest("GET", url, nil) |
| 140 | + if err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + for k, v := range h.Headers { |
| 145 | + if strings.ToLower(k) == "host" { |
| 146 | + request.Host = v |
| 147 | + } else { |
| 148 | + request.Header.Add(k, v) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + if h.Username != "" { |
| 153 | + request.SetBasicAuth(h.Username, h.Password) |
| 154 | + } |
| 155 | + |
| 156 | + resp, err := h.client.Do(request) |
| 157 | + if err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + defer resp.Body.Close() |
| 161 | + |
| 162 | + if resp.StatusCode != http.StatusOK { |
| 163 | + return fmt.Errorf("Received status code %d (%s), expected %d (%s)", |
| 164 | + resp.StatusCode, |
| 165 | + http.StatusText(resp.StatusCode), |
| 166 | + http.StatusOK, |
| 167 | + http.StatusText(http.StatusOK)) |
| 168 | + } |
| 169 | + |
| 170 | + b, err := ioutil.ReadAll(resp.Body) |
| 171 | + if err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + |
| 175 | + if h.parser == nil { |
| 176 | + return errors.New("Parser is not set") |
| 177 | + } |
| 178 | + |
| 179 | + metrics, err := h.parser.Parse(b) |
| 180 | + if err != nil { |
| 181 | + return err |
| 182 | + } |
| 183 | + |
| 184 | + for _, metric := range metrics { |
| 185 | + if h.TagURL { |
| 186 | + metric.AddTag("url", url) |
| 187 | + } |
| 188 | + acc.AddFields(metric.Name(), metric.Fields(), metric.Tags(), metric.Time()) |
| 189 | + } |
| 190 | + |
| 191 | + return nil |
| 192 | +} |
| 193 | + |
| 194 | +func init() { |
| 195 | + inputs.Add("http", func() telegraf.Input { |
| 196 | + return &HTTP{ |
| 197 | + Timeout: internal.Duration{Duration: time.Second * 5}, |
| 198 | + } |
| 199 | + }) |
| 200 | +} |
0 commit comments