From 6e791357ef11e6bc332c644e0db7df1dd19d1d8f Mon Sep 17 00:00:00 2001 From: ncohensm Date: Wed, 22 Jun 2016 15:44:25 -0700 Subject: [PATCH] support query endpoint and change default listen port --- plugins/inputs/http_listener/README.md | 4 +- plugins/inputs/http_listener/http_listener.go | 44 ++++++++++++------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/plugins/inputs/http_listener/README.md b/plugins/inputs/http_listener/README.md index 9bc458c16..8066fa00e 100644 --- a/plugins/inputs/http_listener/README.md +++ b/plugins/inputs/http_listener/README.md @@ -5,7 +5,7 @@ The plugin expects messages in the InfluxDB line-protocol ONLY, other Telegraf i The intent of the plugin is to allow Telegraf to serve as a proxy/router for the /write endpoint of the InfluxDB HTTP API. See: [Telegraf Input Data Formats](https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md#influx). -Example: curl -i -XPOST 'http://localhost:8086/write' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000' +Example: curl -i -XPOST 'http://localhost:8186/write' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000' ### Configuration: @@ -15,7 +15,7 @@ This is a sample configuration for the plugin. # # Influx HTTP write listener [[inputs.http_listener]] ## Address and port to host HTTP listener on - service_address = ":8086" + service_address = ":8186" ## timeouts in seconds read_timeout = "10" diff --git a/plugins/inputs/http_listener/http_listener.go b/plugins/inputs/http_listener/http_listener.go index 032cf16cf..a789cf304 100644 --- a/plugins/inputs/http_listener/http_listener.go +++ b/plugins/inputs/http_listener/http_listener.go @@ -31,7 +31,7 @@ type HttpListener struct { const sampleConfig = ` ## Address and port to host HTTP listener on - service_address = ":8086" + service_address = ":8186" ## timeouts in seconds read_timeout = "10" @@ -103,23 +103,37 @@ func (t *HttpListener) ServeHTTP(res http.ResponseWriter, req *http.Request) { body, err := ioutil.ReadAll(req.Body) if err == nil { - log.Printf("Received request: [%s]\n", string(body)) + var path = req.URL.Path[1:] - var metrics []telegraf.Metric - if len(body) == 0 { - log.Printf("No metrics to parse\n") - } else { - metrics, err = t.parser.Parse(body) - if err == nil { - t.storeMetrics(metrics) - log.Printf("Persisted %d metrics\n", len(metrics)) + if path == "write" { + log.Printf("Received write request: [%s]\n", string(body)) + + var metrics []telegraf.Metric + if len(body) == 0 { + log.Printf("No metrics to parse\n") } else { - log.Printf("Problem parsing body: [%s], Error: %s\n", string(body), err) - res.WriteHeader(500) - res.Write([]byte("ERROR parsing metrics")) + metrics, err = t.parser.Parse(body) + if err == nil { + t.storeMetrics(metrics) + log.Printf("Persisted %d metrics\n", len(metrics)) + } else { + log.Printf("Problem parsing body: [%s], Error: %s\n", string(body), err) + res.WriteHeader(500) + res.Write([]byte("ERROR parsing metrics")) + } + res.WriteHeader(204) + res.Write([]byte("")) } - res.WriteHeader(204) - res.Write([]byte("")) + } else if path == "query" { + // Deliver a dummy response to the query endpoint, as some InfluxDB clients test endpoint availability with a query + log.Printf("Received query request: [%s]\n", string(body)) + res.WriteHeader(200) + res.Write([]byte("{\"results\":[]}")) + } else { + // Don't know how to respond to calls to other endpoints + log.Printf("Received unknown %s request: [%s]\n", path, string(body)) + res.WriteHeader(404) + res.Write([]byte("Not Found")) } } else { log.Printf("Problem reading request: [%s], Error: %s\n", string(body), err)