Skip to content

Commit

Permalink
Add monitors.d directory + config to default configuration (#9004)
Browse files Browse the repository at this point in the history
This makes heartbeat much easier to use by default, and less confusing in terms of where/how
to create this directory.
  • Loading branch information
andrewvc committed Dec 21, 2018
1 parent 8bb462b commit 2e92a04
Show file tree
Hide file tree
Showing 13 changed files with 361 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d

*Heartbeat*

- Made monitors.d configuration part of the default config. {pull}9004[9004]

*Journalbeat*

*Metricbeat*
Expand Down
16 changes: 14 additions & 2 deletions dev-tools/mage/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ func (b packageBuilder) Build() error {
}

type testPackagesParams struct {
HasModules bool
HasModulesD bool
HasModules bool
HasMonitorsD bool
HasModulesD bool
}

// TestPackagesOption defines a option to the TestPackages target.
Expand All @@ -111,6 +112,13 @@ func WithModules() func(params *testPackagesParams) {
}
}

// WithMonitorsD enables monitors folder contents testing.
func WithMonitorsD() func(params *testPackagesParams) {
return func(params *testPackagesParams) {
params.HasMonitorsD = true
}
}

// WithModulesD enables modules.d folder contents testing
func WithModulesD() func(params *testPackagesParams) {
return func(params *testPackagesParams) {
Expand Down Expand Up @@ -140,6 +148,10 @@ func TestPackages(options ...TestPackagesOption) error {
args = append(args, "--modules")
}

if params.HasMonitorsD {
args = append(args, "--monitors.d")
}

if params.HasModulesD {
args = append(args, "--modules.d")
}
Expand Down
33 changes: 30 additions & 3 deletions dev-tools/packaging/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ var (
modulesDirPattern = regexp.MustCompile(`module/.+`)
modulesDDirPattern = regexp.MustCompile(`modules.d/$`)
modulesDFilePattern = regexp.MustCompile(`modules.d/.+`)
monitorsDFilePattern = regexp.MustCompile(`monitors.d/.+`)
systemdUnitFilePattern = regexp.MustCompile(`/lib/systemd/system/.*\.service`)
)

var (
files = flag.String("files", "../build/distributions/*/*", "filepath glob containing package files")
modules = flag.Bool("modules", false, "check modules folder contents")
modulesd = flag.Bool("modules.d", false, "check modules.d folder contents")
files = flag.String("files", "../build/distributions/*/*", "filepath glob containing package files")
modules = flag.Bool("modules", false, "check modules folder contents")
modulesd = flag.Bool("modules.d", false, "check modules.d folder contents")
monitorsd = flag.Bool("monitors.d", false, "check monitors.d folder contents")
)

func TestRPM(t *testing.T) {
Expand Down Expand Up @@ -117,6 +119,7 @@ func checkRPM(t *testing.T, file string) {
checkModulesPermissions(t, p)
checkModulesPresent(t, "/usr/share", p)
checkModulesDPresent(t, "/etc/", p)
checkMonitorsDPresent(t, "/etc", p)
checkModulesOwner(t, p)
checkSystemdUnitPermissions(t, p)
}
Expand All @@ -134,6 +137,7 @@ func checkDeb(t *testing.T, file string, buf *bytes.Buffer) {
checkManifestOwner(t, p)
checkModulesPresent(t, "./usr/share", p)
checkModulesDPresent(t, "./etc/", p)
checkMonitorsDPresent(t, "./etc/", p)
checkModulesPermissions(t, p)
checkModulesOwner(t, p)
checkSystemdUnitPermissions(t, p)
Expand Down Expand Up @@ -316,6 +320,12 @@ func checkModulesDPresent(t *testing.T, prefix string, p *packageFile) {
}
}

func checkMonitorsDPresent(t *testing.T, prefix string, p *packageFile) {
if *monitorsd {
checkMonitors(t, "monitors.d", prefix, monitorsDFilePattern, p)
}
}

func checkModules(t *testing.T, name, prefix string, r *regexp.Regexp, p *packageFile) {
t.Run(fmt.Sprintf("%s %s contents", p.Name, name), func(t *testing.T) {
minExpectedModules := 4
Expand All @@ -333,6 +343,23 @@ func checkModules(t *testing.T, name, prefix string, r *regexp.Regexp, p *packag
})
}

func checkMonitors(t *testing.T, name, prefix string, r *regexp.Regexp, p *packageFile) {
t.Run(fmt.Sprintf("%s %s contents", p.Name, name), func(t *testing.T) {
minExpectedModules := 1
total := 0
for _, entry := range p.Contents {
if strings.HasPrefix(entry.File, prefix) && r.MatchString(entry.File) {
total++
}
}

if total < minExpectedModules {
t.Errorf("not enough monitors found under %s: actual=%d, expected>=%d",
name, total, minExpectedModules)
}
})
}

func checkDockerEntryPoint(t *testing.T, p *packageFile, info *dockerInfo) {
expectedMode := os.FileMode(0755)

Expand Down
11 changes: 11 additions & 0 deletions heartbeat/_meta/beat.docker.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# Define a directory to load monitor definitions from. Definitions take the form
# of individual yaml files.
heartbeat.config.monitors:
# Directory + glob pattern to search for configuration files
path: ${path.config}/monitors.d/*.yml
# If enabled, heartbeat will periodically check the config.monitors path for changes
reload.enabled: false
# How often to check for changes
reload.period: 5s


heartbeat.monitors:
- type: http
schedule: '@every 5s'
Expand Down
11 changes: 11 additions & 0 deletions heartbeat/_meta/beat.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@

############################# Heartbeat ######################################


# Define a directory to load monitor definitions from. Definitions take the form
# of individual yaml files.
heartbeat.config.monitors:
# Directory + glob pattern to search for configuration files
path: ${path.config}/monitors.d/*.yml
# If enabled, heartbeat will periodically check the config.monitors path for changes
reload.enabled: false
# How often to check for changes
reload.period: 5s

# Configure monitors
heartbeat.monitors:
- type: icmp # monitor type `icmp` (requires root) uses ICMP Echo Request to ping
Expand Down
12 changes: 11 additions & 1 deletion heartbeat/_meta/beat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@

############################# Heartbeat ######################################

# Configure monitors
# Define a directory to load monitor definitions from. Definitions take the form
# of individual yaml files.
heartbeat.config.monitors:
# Directory + glob pattern to search for configuration files
path: ${path.config}/monitors.d/*.yml
# If enabled, heartbeat will periodically check the config.monitors path for changes
reload.enabled: false
# How often to check for changes
reload.period: 5s

# Configure monitors inline
heartbeat.monitors:
- type: http

Expand Down
11 changes: 11 additions & 0 deletions heartbeat/heartbeat.docker.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# Define a directory to load monitor definitions from. Definitions take the form
# of individual yaml files.
heartbeat.config.monitors:
# Directory + glob pattern to search for configuration files
path: ${path.config}/monitors.d/*.yml
# If enabled, heartbeat will periodically check the config.monitors path for changes
reload.enabled: false
# How often to check for changes
reload.period: 5s


heartbeat.monitors:
- type: http
schedule: '@every 5s'
Expand Down
11 changes: 11 additions & 0 deletions heartbeat/heartbeat.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@

############################# Heartbeat ######################################


# Define a directory to load monitor definitions from. Definitions take the form
# of individual yaml files.
heartbeat.config.monitors:
# Directory + glob pattern to search for configuration files
path: ${path.config}/monitors.d/*.yml
# If enabled, heartbeat will periodically check the config.monitors path for changes
reload.enabled: false
# How often to check for changes
reload.period: 5s

# Configure monitors
heartbeat.monitors:
- type: icmp # monitor type `icmp` (requires root) uses ICMP Echo Request to ping
Expand Down
12 changes: 11 additions & 1 deletion heartbeat/heartbeat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@

############################# Heartbeat ######################################

# Configure monitors
# Define a directory to load monitor definitions from. Definitions take the form
# of individual yaml files.
heartbeat.config.monitors:
# Directory + glob pattern to search for configuration files
path: ${path.config}/monitors.d/*.yml
# If enabled, heartbeat will periodically check the config.monitors path for changes
reload.enabled: false
# How often to check for changes
reload.period: 5s

# Configure monitors inline
heartbeat.monitors:
- type: http

Expand Down
14 changes: 13 additions & 1 deletion heartbeat/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func Package() {

// TestPackages tests the generated packages (i.e. file modes, owners, groups).
func TestPackages() error {
return mage.TestPackages()
return mage.TestPackages(mage.WithMonitorsD())
}

// Update updates the generated files (aka make update).
Expand Down Expand Up @@ -118,11 +118,23 @@ func GoTestIntegration(ctx context.Context) error {
}

func customizePackaging() {
monitorsDTarget := "monitors.d"
unixMonitorsDir := "/etc/{{.BeatName}}/monitors.d"
monitorsD := mage.PackageFile{
Mode: 0644,
Source: "monitors.d",
}

for _, args := range mage.Packages {
pkgType := args.Types[0]
switch pkgType {
case mage.Docker:
args.Spec.ExtraVar("linux_capabilities", "cap_net_raw=eip")
args.Spec.Files[monitorsDTarget] = monitorsD
case mage.TarGz, mage.Zip:
args.Spec.Files[monitorsDTarget] = monitorsD
case mage.Deb, mage.RPM, mage.DMG:
args.Spec.Files[unixMonitorsDir] = monitorsD
}
}
}
91 changes: 91 additions & 0 deletions heartbeat/monitors.d/sample.http.yml.disabled
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# These files contain a list of monitor configurations identical
# to the heartbeat.monitors section in heartbeat.yml
# The .example extension on this file must be removed for it to
# be loaded.

- type: http # monitor type `http`. Connect via HTTP an optionally verify response

# Monitor name used for job name and document type
#name: http

# Enable/Disable monitor
#enabled: true

# Configure task schedule
schedule: '@every 5s' # every 5 seconds from start of beat

# Configure URLs to ping
urls: ["http://localhost:9200"]

# 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

# Optional HTTP proxy url.
#proxy_url: ''

# Total test connection and data exchange timeout
#timeout: 16s

# Optional Authentication Credentials
#username: ''
#password: ''

# TLS/SSL connection settings for use with HTTPS endpoint. If not configured
# system defaults will be used.
#ssl:
# Certificate Authorities
#certificate_authorities: ['']

# Required TLS protocols
#supported_protocols: ["TLSv1.0", "TLSv1.1", "TLSv1.2"]

# Request settings:
#check.request:
# Configure HTTP method to use. Only 'HEAD', 'GET' and 'POST' methods are allowed.
#method: "GET"

# Dictionary of additional HTTP headers to send:
#headers:

# Optional request body content
#body:

# Expected response settings
#check.response:
# Expected status code. If not configured or set to 0 any status code not
# being 404 is accepted.
#status: 0

# Required response headers.
#headers:

# Required response contents.
#body:

# Parses the body as JSON, then checks against the given condition expression
#json:
#- description: Explanation of what the check does
# condition:
# equals:
# myField: expectedValue


# NOTE: THIS FEATURE IS DEPRECATED AND WILL BE REMOVED IN A FUTURE RELEASE
# 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
Loading

0 comments on commit 2e92a04

Please sign in to comment.