diff --git a/codecov.yml b/codecov.yml index 76ade0fdb0e..a151773528e 100644 --- a/codecov.yml +++ b/codecov.yml @@ -6,7 +6,7 @@ coverage: default: # basic target: auto - threshold: null + threshold: 0.1 base: auto # advanced branches: null diff --git a/filebeat/filebeat.full.yml b/filebeat/filebeat.full.yml index f2a1f217dea..3a13b9fc34d 100644 --- a/filebeat/filebeat.full.yml +++ b/filebeat/filebeat.full.yml @@ -938,6 +938,19 @@ output.elasticsearch: # dashboards and index pattern. Example: testbeat-* #dashboards.index: +#================================ HTTP Endpoint ====================================== +# Each beat can expose internal data points through a http endpoint. For security +# reason the endpoint is disabled by default. This feature is currently in beta. + +# Defines if http endpoint is enabled +#http.enabled: false + +# Host to expose the http endpoint to. It is recommended to use only localhost. +#http.host: localhost + +# Port on which the http endpoint is exposed. Default is 5066 +#http.port: 5066 + #================================ Logging ====================================== # There are three options for the log output: syslog, file, stderr. # Under Windows systems, the log files are per default sent to the file output, diff --git a/heartbeat/heartbeat.full.yml b/heartbeat/heartbeat.full.yml index 23d052be71d..7fe14781283 100644 --- a/heartbeat/heartbeat.full.yml +++ b/heartbeat/heartbeat.full.yml @@ -786,6 +786,19 @@ output.elasticsearch: # dashboards and index pattern. Example: testbeat-* #dashboards.index: +#================================ HTTP Endpoint ====================================== +# Each beat can expose internal data points through a http endpoint. For security +# reason the endpoint is disabled by default. This feature is currently in beta. + +# Defines if http endpoint is enabled +#http.enabled: false + +# Host to expose the http endpoint to. It is recommended to use only localhost. +#http.host: localhost + +# Port on which the http endpoint is exposed. Default is 5066 +#http.port: 5066 + #================================ Logging ====================================== # There are three options for the log output: syslog, file, stderr. # Under Windows systems, the log files are per default sent to the file output, diff --git a/libbeat/_meta/config.full.yml b/libbeat/_meta/config.full.yml index 938fb59aa32..c58a78b3828 100644 --- a/libbeat/_meta/config.full.yml +++ b/libbeat/_meta/config.full.yml @@ -588,6 +588,19 @@ output.elasticsearch: # dashboards and index pattern. Example: testbeat-* #dashboards.index: +#================================ HTTP Endpoint ====================================== +# Each beat can expose internal data points through a http endpoint. For security +# reason the endpoint is disabled by default. This feature is currently in beta. + +# Defines if http endpoint is enabled +#http.enabled: false + +# Host to expose the http endpoint to. It is recommended to use only localhost. +#http.host: localhost + +# Port on which the http endpoint is exposed. Default is 5066 +#http.port: 5066 + #================================ Logging ====================================== # There are three options for the log output: syslog, file, stderr. # Under Windows systems, the log files are per default sent to the file output, diff --git a/libbeat/api/config.go b/libbeat/api/config.go new file mode 100644 index 00000000000..4182a61e869 --- /dev/null +++ b/libbeat/api/config.go @@ -0,0 +1,15 @@ +package api + +type Config struct { + Enabled bool + Host string + Port int +} + +var ( + DefaultConfig = Config{ + Enabled: false, + Host: "localhost", + Port: 5066, + } +) diff --git a/libbeat/api/server.go b/libbeat/api/server.go new file mode 100644 index 00000000000..899bb580bf5 --- /dev/null +++ b/libbeat/api/server.go @@ -0,0 +1,73 @@ +package api + +import ( + "fmt" + "net/http" + "net/url" + "strconv" + + "github.com/elastic/beats/libbeat/common" + "github.com/elastic/beats/libbeat/logp" + "github.com/elastic/beats/libbeat/monitoring" +) + +// Start starts the metrics api endpoint on the configured host and port +func Start(cfg *common.Config, info common.BeatInfo) { + logp.Beta("Metrics endpoint is enabled.") + config := DefaultConfig + cfg.Unpack(&config) + + logp.Info("Starting stats endpoint") + go func() { + mux := http.NewServeMux() + + // register handlers + mux.HandleFunc("/", rootHandler(info)) + mux.HandleFunc("/stats", statsHandler) + + url := config.Host + ":" + strconv.Itoa(config.Port) + logp.Info("URL: %s", url) + endpoint := http.ListenAndServe(url, mux) + logp.Info("finished starting stats endpoint: %v", endpoint) + }() +} + +func rootHandler(info common.BeatInfo) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + // Return error page + if r.URL.Path != "/" { + http.NotFound(w, r) + return + } + + w.Header().Set("Content-Type", "application/json; charset=utf-8") + + data := common.MapStr{ + "version": info.Version, + "beat": info.Beat, + "name": info.Name, + "uuid": info.UUID, + "hostname": info.Hostname, + } + + print(w, data, r.URL) + } +} + +// statsHandler report expvar and all libbeat/monitoring metrics +func statsHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + + data := monitoring.CollectStructSnapshot(nil, monitoring.Full, false) + + print(w, data, r.URL) +} + +func print(w http.ResponseWriter, data common.MapStr, u *url.URL) { + query := u.Query() + if _, ok := query["pretty"]; ok { + fmt.Fprintf(w, data.StringToPrint()) + } else { + fmt.Fprintf(w, data.String()) + } +} diff --git a/libbeat/beat/beat.go b/libbeat/beat/beat.go index fc76727df53..be9289b5fa8 100644 --- a/libbeat/beat/beat.go +++ b/libbeat/beat/beat.go @@ -46,6 +46,7 @@ import ( "github.com/satori/go.uuid" "github.com/elastic/beats/filebeat/input/file" + "github.com/elastic/beats/libbeat/api" "github.com/elastic/beats/libbeat/cfgfile" "github.com/elastic/beats/libbeat/common" "github.com/elastic/beats/libbeat/dashboards" @@ -110,6 +111,7 @@ type BeatConfig struct { Processors processors.PluginConfig `config:"processors"` Path paths.Path `config:"path"` Dashboards *common.Config `config:"dashboards"` + Http *common.Config `config:"http"` } var ( @@ -252,6 +254,10 @@ func (b *Beat) launch(bt Creator) error { defer logp.Info("%s stopped.", b.Info.Beat) defer logp.LogTotalExpvars(&b.Config.Logging) + if b.Config.Http.Enabled() { + api.Start(b.Config.Http, b.Info) + } + return beater.Run(b) } diff --git a/metricbeat/metricbeat.full.yml b/metricbeat/metricbeat.full.yml index e3b5256e9cb..8fb15639841 100644 --- a/metricbeat/metricbeat.full.yml +++ b/metricbeat/metricbeat.full.yml @@ -920,6 +920,19 @@ output.elasticsearch: # dashboards and index pattern. Example: testbeat-* #dashboards.index: +#================================ HTTP Endpoint ====================================== +# Each beat can expose internal data points through a http endpoint. For security +# reason the endpoint is disabled by default. This feature is currently in beta. + +# Defines if http endpoint is enabled +#http.enabled: false + +# Host to expose the http endpoint to. It is recommended to use only localhost. +#http.host: localhost + +# Port on which the http endpoint is exposed. Default is 5066 +#http.port: 5066 + #================================ Logging ====================================== # There are three options for the log output: syslog, file, stderr. # Under Windows systems, the log files are per default sent to the file output, diff --git a/packetbeat/packetbeat.full.yml b/packetbeat/packetbeat.full.yml index 7e65b351a6d..e4077259d47 100644 --- a/packetbeat/packetbeat.full.yml +++ b/packetbeat/packetbeat.full.yml @@ -1043,6 +1043,19 @@ output.elasticsearch: # dashboards and index pattern. Example: testbeat-* #dashboards.index: +#================================ HTTP Endpoint ====================================== +# Each beat can expose internal data points through a http endpoint. For security +# reason the endpoint is disabled by default. This feature is currently in beta. + +# Defines if http endpoint is enabled +#http.enabled: false + +# Host to expose the http endpoint to. It is recommended to use only localhost. +#http.host: localhost + +# Port on which the http endpoint is exposed. Default is 5066 +#http.port: 5066 + #================================ Logging ====================================== # There are three options for the log output: syslog, file, stderr. # Under Windows systems, the log files are per default sent to the file output, diff --git a/winlogbeat/winlogbeat.full.yml b/winlogbeat/winlogbeat.full.yml index 9dcb2e4877f..171a178bfac 100644 --- a/winlogbeat/winlogbeat.full.yml +++ b/winlogbeat/winlogbeat.full.yml @@ -623,6 +623,19 @@ output.elasticsearch: # dashboards and index pattern. Example: testbeat-* #dashboards.index: +#================================ HTTP Endpoint ====================================== +# Each beat can expose internal data points through a http endpoint. For security +# reason the endpoint is disabled by default. This feature is currently in beta. + +# Defines if http endpoint is enabled +#http.enabled: false + +# Host to expose the http endpoint to. It is recommended to use only localhost. +#http.host: localhost + +# Port on which the http endpoint is exposed. Default is 5066 +#http.port: 5066 + #================================ Logging ====================================== # There are three options for the log output: syslog, file, stderr. # Under Windows systems, the log files are per default sent to the file output,