Skip to content

Commit fce6ac7

Browse files
mergify[bot]leehinmanpierrehilbert
authored
[8.14](backport #4846) [windows] if elastic-agent run fails, log error to Application EventLog (#4919)
* [windows] if `elastic-agent run` fails, log error to Application EventLog (#4846) (cherry picked from commit 6c20730) --------- Co-authored-by: Lee E Hinman <57081003+leehinman@users.noreply.github.com> Co-authored-by: Pierre HILBERT <pierre.hilbert@elastic.co> Co-authored-by: Lee E. Hinman <lee.e.hinman@elastic.co>
1 parent 9f6413a commit fce6ac7

File tree

6 files changed

+81
-4
lines changed

6 files changed

+81
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: enhancement
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: Capture early errors on Windows in Application eventlog.
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
19+
#description:
20+
21+
# Affected component; a word indicating the component this changeset affects.
22+
component: elastic-agent
23+
24+
# PR URL; optional; the PR number that added the changeset.
25+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
26+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
27+
# Please provide it if you are adding a fragment for a different PR.
28+
pr: https://github.com/elastic/elastic-agent/pull/4846
29+
30+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
31+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
32+
issue: https://github.com/elastic/elastic-agent/issues/4627

internal/pkg/agent/cmd/run.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ const (
6060
fleetInitTimeoutName = "FLEET_SERVER_INIT_TIMEOUT"
6161
)
6262

63-
type cfgOverrider func(cfg *configuration.Configuration)
64-
type awaiters []<-chan struct{}
63+
type (
64+
cfgOverrider func(cfg *configuration.Configuration)
65+
awaiters []<-chan struct{}
66+
)
6567

6668
func newRunCommandWithArgs(_ []string, streams *cli.IOStreams) *cobra.Command {
6769
cmd := &cobra.Command{
@@ -78,6 +80,7 @@ func newRunCommandWithArgs(_ []string, streams *cli.IOStreams) *cobra.Command {
7880
testingMode, _ := cmd.Flags().GetBool("testing-mode")
7981
if err := run(nil, testingMode, fleetInitTimeout); err != nil && !errors.Is(err, context.Canceled) {
8082
fmt.Fprintf(streams.Err, "Error: %v\n%s\n", err, troubleshootMessage())
83+
logExternal(fmt.Sprintf("%s run failed: %s", paths.BinaryName, err))
8184
return err
8285
}
8386
return nil
@@ -132,7 +135,7 @@ func run(override cfgOverrider, testingMode bool, fleetInitTimeout time.Duration
132135
// register as a service
133136
stop := make(chan bool)
134137
ctx, cancel := context.WithCancel(context.Background())
135-
var stopBeat = func() {
138+
stopBeat := func() {
136139
close(stop)
137140
}
138141

internal/pkg/agent/cmd/run_unix.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
//go:build !windows
6+
7+
package cmd
8+
9+
// logExternal logs the error to an external log. On non-windows systems this is a no-op.
10+
func logExternal(msg string) {
11+
}

internal/pkg/agent/cmd/run_windows.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
//go:build windows
6+
7+
package cmd
8+
9+
import (
10+
"golang.org/x/sys/windows/svc/eventlog"
11+
12+
"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
13+
)
14+
15+
// logExternal logs the error to an external log. On Windows this is
16+
// the Application EventLog. This is a best effort logger and no
17+
// errors are returned.
18+
func logExternal(msg string) {
19+
eLog, err2 := eventlog.Open(paths.ServiceName)
20+
if err2 != nil {
21+
return
22+
}
23+
_ = eLog.Error(1, msg)
24+
}

internal/pkg/agent/install/install_windows.go

+8
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import (
1010
"fmt"
1111
"os"
1212
"path/filepath"
13+
"strings"
1314

1415
"golang.org/x/sys/windows"
16+
"golang.org/x/sys/windows/svc/eventlog"
1517

1618
"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
1719
"github.com/elastic/elastic-agent/internal/pkg/agent/perms"
@@ -80,6 +82,12 @@ func withServiceOptions(username string, groupName string) ([]serviceOpt, error)
8082
// gives user the ability to control the service, needed when installed with --unprivileged or
8183
// ReExec is not possible on Windows.
8284
func servicePostInstall(ownership utils.FileOwner) error {
85+
// Modify registry to allow logging to eventlog as "Elastic Agent".
86+
err := eventlog.InstallAsEventCreate(paths.ServiceName, eventlog.Info|eventlog.Warning|eventlog.Error)
87+
if err != nil && !strings.Contains(err.Error(), "registry key already exists") {
88+
return fmt.Errorf("unable to create registry key for logging: %w", err)
89+
}
90+
8391
if ownership.UID == "" {
8492
// no user, running with LOCAL SYSTEM (do nothing)
8593
return nil

internal/pkg/agent/install/uninstall.go

-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ func containsString(str string, a []string, caseSensitive bool) bool {
230230
}
231231

232232
func uninstallComponents(ctx context.Context, cfgFile string, uninstallToken string, log *logp.Logger, pt *progressbar.ProgressBar, unprivileged bool) error {
233-
234233
platform, err := component.LoadPlatformDetail()
235234
if err != nil {
236235
return fmt.Errorf("failed to gather system information: %w", err)

0 commit comments

Comments
 (0)