support query endpoint and change default listen port

This commit is contained in:
ncohensm 2016-06-22 15:44:25 -07:00
parent 6b5a9d34bb
commit 6e791357ef
2 changed files with 31 additions and 17 deletions

View File

@ -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. 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). 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: ### Configuration:
@ -15,7 +15,7 @@ This is a sample configuration for the plugin.
# # Influx HTTP write listener # # Influx HTTP write listener
[[inputs.http_listener]] [[inputs.http_listener]]
## Address and port to host HTTP listener on ## Address and port to host HTTP listener on
service_address = ":8086" service_address = ":8186"
## timeouts in seconds ## timeouts in seconds
read_timeout = "10" read_timeout = "10"

View File

@ -31,7 +31,7 @@ type HttpListener struct {
const sampleConfig = ` const sampleConfig = `
## Address and port to host HTTP listener on ## Address and port to host HTTP listener on
service_address = ":8086" service_address = ":8186"
## timeouts in seconds ## timeouts in seconds
read_timeout = "10" read_timeout = "10"
@ -103,7 +103,10 @@ func (t *HttpListener) ServeHTTP(res http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body) body, err := ioutil.ReadAll(req.Body)
if err == nil { if err == nil {
log.Printf("Received request: [%s]\n", string(body)) var path = req.URL.Path[1:]
if path == "write" {
log.Printf("Received write request: [%s]\n", string(body))
var metrics []telegraf.Metric var metrics []telegraf.Metric
if len(body) == 0 { if len(body) == 0 {
@ -121,6 +124,17 @@ func (t *HttpListener) ServeHTTP(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(204) res.WriteHeader(204)
res.Write([]byte("")) 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 { } else {
log.Printf("Problem reading request: [%s], Error: %s\n", string(body), err) log.Printf("Problem reading request: [%s], Error: %s\n", string(body), err)
res.WriteHeader(500) res.WriteHeader(500)