From 005bb608b010fa777db527738ad27b5f40086986 Mon Sep 17 00:00:00 2001 From: Andrew Cholakian Date: Sat, 16 Mar 2019 08:22:30 -0700 Subject: [PATCH] [Heartbeat] Remove extra monitor template files (#11216) In https://github.com/elastic/beats/pull/9648 the monitor generator script was removed, but its supporting files were not. This commit removes the remaining legacy files. --- heartbeat/scripts/generator/README.md | 33 ----- .../_meta/config.reference.yml.tmpl | 62 --------- .../{{monitor}}/_meta/config.yml.tmpl | 9 -- .../{{monitor}}/_meta/fields.yml.tmpl | 22 ---- .../generator/{{monitor}}/check.go.tmpl | 15 --- .../generator/{{monitor}}/config.go.tmpl | 76 ----------- .../scripts/generator/{{monitor}}/job.go.tmpl | 70 ---------- .../generator/{{monitor}}/{{monitor}}.go.tmpl | 121 ------------------ 8 files changed, 408 deletions(-) delete mode 100644 heartbeat/scripts/generator/README.md delete mode 100644 heartbeat/scripts/generator/{{monitor}}/_meta/config.reference.yml.tmpl delete mode 100644 heartbeat/scripts/generator/{{monitor}}/_meta/config.yml.tmpl delete mode 100644 heartbeat/scripts/generator/{{monitor}}/_meta/fields.yml.tmpl delete mode 100644 heartbeat/scripts/generator/{{monitor}}/check.go.tmpl delete mode 100644 heartbeat/scripts/generator/{{monitor}}/config.go.tmpl delete mode 100644 heartbeat/scripts/generator/{{monitor}}/job.go.tmpl delete mode 100644 heartbeat/scripts/generator/{{monitor}}/{{monitor}}.go.tmpl diff --git a/heartbeat/scripts/generator/README.md b/heartbeat/scripts/generator/README.md deleted file mode 100644 index f1b0b9aaec1..00000000000 --- a/heartbeat/scripts/generator/README.md +++ /dev/null @@ -1,33 +0,0 @@ -## Readme - -Code generator for Heartbeat monitors. - -In order to add a new monitor type to Heartbeat, run `make create-monitor -MONITOR=` from the Heartbeat directory. - -``` -$ cd $GOPATH/src/github.com/elastic/beats/heartbeat -$ make create-monitor MONITOR= -$ make update -$ make # build heartbeat -``` - -`make update` is required to update the import list, such that the new monitor -is compiled into Heartbeat. - -The new monitor will be added to the `monitors/active/` sub directory. - -Monitor structure: -- `config.go`: The monitor configuration options and configuration validation. -- `check.go`: The monitor validation support. -- `job.go`: Implements the ping function for connecting and validating an - endpoint. This file generates the monitor specific fields to an event. -- `.go`: The monitor entrypoint registering the factory for setting up - monitoring jobs. -- `_meta/fields.yml`: Document the monitors event field. -- `_meta/config.yml`: Minimal sample configuration file -- `_meta/config.reference.yml`: Reference configuration file. All available - settings are documented in the reference file. - -Code comments tagged with `IMPLEMENT_ME` in the go and meta files give details -on changes required to implement a new monitor type. diff --git a/heartbeat/scripts/generator/{{monitor}}/_meta/config.reference.yml.tmpl b/heartbeat/scripts/generator/{{monitor}}/_meta/config.reference.yml.tmpl deleted file mode 100644 index 1e827850fd7..00000000000 --- a/heartbeat/scripts/generator/{{monitor}}/_meta/config.reference.yml.tmpl +++ /dev/null @@ -1,62 +0,0 @@ -- type: {{monitor}} - - # Monitor name used for job name and document type - #name: {{monitor}} - - # Enable/Disable monitor - #enabled: true - - # Configure task schedule - schedule: '@every 30s' - - # list of hosts to monitor - hosts: ["localhost"] - - # list of ports to ping (used if host is given without portname) - ports: [12345] - - # SOCKS5 proxy url - #proxy_url: '' - - # Resolve hostnames locally instead on SOCKS5 server: - #proxy_use_local_resolver: false - - # Configure file json file to be watched for changes to the monitor: - #watch.poll_file: - # Path to check for updates. - #path: - - # Interval between file file changed checks. - #interval: 5s - - - # Configure IP protocol types to ping on if hostnames are configured. Ping - # all resolvable IPs if `mode` is `all`, or only one IP if `mode` is `any`. - ipv4: true - ipv6: true - mode: any - - # Configure file json file to be watched for changes to the monitor: - #watch.poll_file: - # Path to check for updates. - #path: - - # Interval between file file changed checks. - #interval: 5s - - # Total test connection and data exchange timeout - #timeout: 16s - - - # TLS/SSL connection settings: - #ssl: - # Certificate Authorities - #certificate_authorities: [''] - - # Required TLS protocols - #supported_protocols: ["TLSv1.0", "TLSv1.1", "TLSv1.2"] - - # IMPLEMENT_ME: document check/validation settings - #check: - #request: - #response: diff --git a/heartbeat/scripts/generator/{{monitor}}/_meta/config.yml.tmpl b/heartbeat/scripts/generator/{{monitor}}/_meta/config.yml.tmpl deleted file mode 100644 index 53133e52508..00000000000 --- a/heartbeat/scripts/generator/{{monitor}}/_meta/config.yml.tmpl +++ /dev/null @@ -1,9 +0,0 @@ -- type: {{monitor}} - # list of hosts to monitor - hosts: ["localhost:12345"] - - # Configure task schedule - schedule: '@every 30s' - - # Total test connection and data exchange timeout - #timeout: 16s diff --git a/heartbeat/scripts/generator/{{monitor}}/_meta/fields.yml.tmpl b/heartbeat/scripts/generator/{{monitor}}/_meta/fields.yml.tmpl deleted file mode 100644 index 4afa42447e3..00000000000 --- a/heartbeat/scripts/generator/{{monitor}}/_meta/fields.yml.tmpl +++ /dev/null @@ -1,22 +0,0 @@ -- key: {{monitor}} - title: "{{monitor|upper}} monitor" - description: - fields: - - name: {{monitor}} - type: group - description: > - {{monitor|upper}} related fields. - fields: - - name: rtt - type: group - description: > - {{monitor|upper}} layer round trip times. - fields: - - name: validate - type: group - description: > - Duration of validation step based on existing {{monitor|upper}} connection. - fields: - - name: us - type: long - description: Duration in microseconds diff --git a/heartbeat/scripts/generator/{{monitor}}/check.go.tmpl b/heartbeat/scripts/generator/{{monitor}}/check.go.tmpl deleted file mode 100644 index a992286b458..00000000000 --- a/heartbeat/scripts/generator/{{monitor}}/check.go.tmpl +++ /dev/null @@ -1,15 +0,0 @@ -package {{monitor}} - -import "net" - -// IMPLEMENT_ME: implement validator -type validator struct { -} - -func makeValidator(config *config) (*validator, error) { - return &validator{}, nil -} - -func (v *validator) Check(conn net.Conn) error { - return nil -} diff --git a/heartbeat/scripts/generator/{{monitor}}/config.go.tmpl b/heartbeat/scripts/generator/{{monitor}}/config.go.tmpl deleted file mode 100644 index 46b36a06cc4..00000000000 --- a/heartbeat/scripts/generator/{{monitor}}/config.go.tmpl +++ /dev/null @@ -1,76 +0,0 @@ -package {{monitor}} - -import ( - "time" - - "github.com/elastic/beats/libbeat/outputs/transport" - - "github.com/elastic/beats/libbeat/common/transport/tlscommon" - "github.com/elastic/beats/heartbeat/monitors" -) - -type config struct { - Name string `config:"name"` - - // connection settings - Hosts []string `config:"hosts" validate:"required"` - Ports []uint16 `config:"ports"` - Socks5 transport.ProxyConfig `config:",inline"` - Mode monitors.IPSettings `config:",inline"` - - // configure tls - TLS *tlscommon.Config `config:"ssl"` - - // configure validation - Check checkConfig `config:"check"` - Timeout time.Duration `config:"timeout"` -} - -type checkConfig struct { - Request requestConfig `config:"request"` - Response responseConfig `config:"response"` -} - -type requestConfig struct { - // IMPLEMENT_ME: configure request parameters -} - -type responseConfig struct { - // IMPLEMENT_ME: configure response parameters for validation -} - -// defaultConfig creates a new copy of the monitors default configuration. -func defaultConfig() config { - return config{ - Name: "echo", - Hosts: []string{"localhost"}, - Ports: nil, - Mode: monitors.DefaultIPSettings, - TLS: nil, - Check: checkConfig{ - Request: requestConfig{}, - Response: responseConfig{}, - }, - Timeout: 16 * time.Second, - } -} - -// IMPLEMENT_ME: implement optional config validation or remove method -func (c *config) Validate() error { - return nil -} - -// IMPLEMENT_ME: implement optional config validation or remove method -func (c *checkConfig) Validate() error { - return nil -} - -// IMPLEMENT_ME: implement optional config validation or remove method -func (c *requestConfig) Validate() error { - return nil -} - -// IMPLEMENT_ME: implement optional config validation or remove method -func (c *responseConfig) Validate() error { - return nil -} diff --git a/heartbeat/scripts/generator/{{monitor}}/job.go.tmpl b/heartbeat/scripts/generator/{{monitor}}/job.go.tmpl deleted file mode 100644 index 099b3119f56..00000000000 --- a/heartbeat/scripts/generator/{{monitor}}/job.go.tmpl +++ /dev/null @@ -1,70 +0,0 @@ -package {{monitor}} - -import ( - "net" - "time" - - "github.com/elastic/beats/libbeat/common" - "github.com/elastic/beats/libbeat/outputs/transport" - - "github.com/elastic/beats/heartbeat/look" - "github.com/elastic/beats/heartbeat/reason" -) - -func pingHost( - dialer transport.Dialer, - host string, - timeout time.Duration, - validator *validator, -) (common.MapStr, reason.Reason) { - start := time.Now() - deadline := start.Add(timeout) - - // IMPLEMENT_ME: dial endpoint. Sample code uses plain TCP (optional TLS) - // connection, handling all communication in the validator. - // Libraries for other services might require the dialer or connection - // object, which is then passed to the validator. - - // connect to endpoint - conn, err := dialer.Dial("tcp", host) - if err != nil { - debugf("dial failed with: %v", err) - return nil, reason.IOFailed(err) - } - defer conn.Close() - - // no additional validation step => ping success - if validator == nil { - return common.MapStr{}, nil - } - - // set validation deadline - if err := conn.SetDeadline(deadline); err != nil { - debugf("setting connection deadline failed with: %v", err) - return nil, reason.IOFailed(err) - } - - // validate endpoint status - validateStart := time.Now() - err = validator.Check(conn) - if _, ok := err.(net.Error); ok { - // IMPLEMENT_ME: handle protocol specific IO/communication error types - - return nil, reason.IOFailed(err) - } - - // create validation event - end := time.Now() - event := common.MapStr{ - namespace: common.MapStr{ - "rtt": common.MapStr{ - "validate": look.RTT(end.Sub(validateStart)), - }, - }, - } - if err != nil { - event["error"] = reason.FailValidate(err) - } - - return event, nil -} diff --git a/heartbeat/scripts/generator/{{monitor}}/{{monitor}}.go.tmpl b/heartbeat/scripts/generator/{{monitor}}/{{monitor}}.go.tmpl deleted file mode 100644 index b0aca11956f..00000000000 --- a/heartbeat/scripts/generator/{{monitor}}/{{monitor}}.go.tmpl +++ /dev/null @@ -1,121 +0,0 @@ -package {{monitor}} - -import ( - "fmt" - "net" - "strconv" - - "github.com/elastic/beats/libbeat/common" - "github.com/elastic/beats/libbeat/logp" - "github.com/elastic/beats/libbeat/outputs" - "github.com/elastic/beats/libbeat/outputs/transport" - - "github.com/elastic/beats/heartbeat/monitors" - "github.com/elastic/beats/heartbeat/monitors/active/dialchain" -) - -const ( - // internal monitor name (registry/config name) - monitorName = "{{monitor}}" - - // monitor event namespace - namespace = "{{monitor}}" - - // scheme names for `monitor.scheme` - plainScheme = "{{monitor}}" - tlsScheme = "{{monitor}}s" -) - -var debugf = logp.MakeDebug(monitorName) - -func init() { - // Register active monitor type - monitors.RegisterActive(monitorName, create) -} - -// create builds multiple monitoring jobs based on the endpoint configuration. -// For each configured endpoint, exactly one job will be generated. -func create( - info monitors.Info, - cfg *common.Config, -) ([]monitors.Job, error) { - // unpack the monitors configuration - config := defaultConfig() - if err := cfg.Unpack(&config); err != nil { - return nil, err - } - - // collect list of endpoints from configuration. - endpoints, err := collectEndpoints(&config) - if err != nil { - return nil, err - } - - validator, err := makeValidator(&config) - if err != nil { - return nil, err - } - - // load optional TLS settings - tls, err := outputs.LoadTLSConfig(config.TLS) - if err != nil { - return nil, err - } - - typ := config.Name - timeout := config.Timeout - scheme := plainScheme - if tls != nil { - scheme = tlsScheme - } - - db, err := dialchain.NewBuilder(dialchain.BuilderSettings{ - Timeout: timeout, - Socks5: config.Socks5, - TLS: tls, - }) - if err != nil { - return nil, err - } - - return dialchain.MakeDialerJobs(db, typ, scheme, endpoints, config.Mode, - func(dialer transport.Dialer, addr string) (common.MapStr, error) { - return pingHost(dialer, addr, timeout, validator) - - }) -} - -// collectEndpoints creates an array of all endpoints to be monitored. -// Each address returned matches the ":" scheme. -func collectEndpoints(config *config) ([]dialchain.Endpoint, error) { - var addrs []dialchain.Endpoint - for _, h := range config.Hosts { - ports := config.Ports - host, port, err := splitHostnamePort(h) - if err != nil { - host = h - } else { - ports = []uint16{port} - } - - if len(ports) == 0 { - return nil, fmt.Errorf("host '%v' without port configuration", host) - } - - addrs = append(addrs, dialchain.Endpoint{Host: host, Ports: ports}) - } - - return addrs, nil -} - -func splitHostnamePort(addr string) (string, uint16, error) { - host, port, err := net.SplitHostPort(addr) - if err != nil { - return "", 0, err - } - p, err := strconv.ParseUint(port, 10, 16) - if err != nil { - return "", 0, fmt.Errorf("'%v' is no valid port number in '%v'", port, addr) - } - return host, uint16(p), nil -}