diff options
| author | Paul Buetow <paul@buetow.org> | 2021-10-06 10:55:50 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2021-10-06 10:55:50 +0300 |
| commit | 7306afe9ab073c424ddca0ddc57950f237948118 (patch) | |
| tree | 063c7181425f0657ec97e415c0224d311bafe6c5 | |
| parent | fab5dc3e70434ea0abc7a0976487a1973b662331 (diff) | |
move health check to separate client binary
| -rw-r--r-- | Makefile | 5 | ||||
| -rw-r--r-- | cmd/dtail/main.go | 22 | ||||
| -rw-r--r-- | cmd/dtailhealthcheck/main.go | 31 | ||||
| -rw-r--r-- | internal/clients/baseclient.go | 1 | ||||
| -rw-r--r-- | internal/clients/handlers/healthhandler.go | 25 | ||||
| -rw-r--r-- | internal/clients/healthclient.go | 29 | ||||
| -rw-r--r-- | internal/config/initializer.go | 10 | ||||
| -rw-r--r-- | internal/io/dlog/dlog.go | 2 | ||||
| -rw-r--r-- | internal/io/signal/signal.go | 5 | ||||
| -rw-r--r-- | internal/server/handlers/healthhandler.go | 4 | ||||
| -rw-r--r-- | internal/server/handlers/serverhandler.go | 2 | ||||
| -rwxr-xr-x | samples/check_dserver.sh.sample | 3 |
12 files changed, 96 insertions, 43 deletions
@@ -1,6 +1,6 @@ GO ?= go all: build test -build: dserver dcat dgrep dmap dtail +build: dserver dcat dgrep dmap dtail dtailhealthcheck dserver: ifndef USE_ACL ${GO} build ${GO_FLAGS} -o dserver ./cmd/dserver/main.go @@ -15,6 +15,8 @@ dmap: ${GO} build ${GO_FLAGS} -o dmap ./cmd/dmap/main.go dtail: ${GO} build ${GO_FLAGS} -o dtail ./cmd/dtail/main.go +dtailhealthcheck: + ${GO} build ${GO_FLAGS} -o dtailhealthcheck ./cmd/dtailhealthcheck/main.go install: ifndef USE_ACL ${GO} install ./cmd/dserver/main.go @@ -25,6 +27,7 @@ endif ${GO} install ./cmd/dgrep/main.go ${GO} install ./cmd/dmap/main.go ${GO} install ./cmd/dtail/main.go + ${GO} install ./cmd/dtailhealthcheck/main.go clean: ls ./cmd/ | while read cmd; do \ test -f $$cmd && rm $$cmd; \ diff --git a/cmd/dtail/main.go b/cmd/dtail/main.go index d333825..301fc08 100644 --- a/cmd/dtail/main.go +++ b/cmd/dtail/main.go @@ -41,7 +41,7 @@ func main() { flag.BoolVar(&args.RegexInvert, "invert", false, "Invert regex") flag.BoolVar(&args.Spartan, "spartan", false, "Spartan output mode") flag.BoolVar(&args.TrustAllHosts, "trustAllHosts", false, "Trust all unknown host keys") - flag.BoolVar(&checkHealth, "checkHealth", false, "Only check for server health") + flag.BoolVar(&checkHealth, "checkHealth", false, "Deprecated, flag will be removed soon") flag.BoolVar(&displayColorTable, "colorTable", false, "Show color table") flag.BoolVar(&displayWideColorTable, "wideColorTable", false, "Show a large color table") flag.BoolVar(&displayVersion, "version", false, "Display version") @@ -66,27 +66,23 @@ func main() { if grep != "" { args.RegexStr = grep } - sourceProcess := source.Client - if checkHealth { - sourceProcess = source.HealthCheck - } - config.Setup(sourceProcess, &args, flag.Args()) - + config.Setup(source.Client, &args, flag.Args()) if displayVersion { version.PrintAndExit() } if !args.Spartan { + if !checkHealth { + version.Print() + } if displayWideColorTable { color.TablePrintAndExit(true) } if displayColorTable { color.TablePrintAndExit(false) } - version.Print() } ctx, cancel := context.WithCancel(context.Background()) - if shutdownAfter > 0 { ctx, cancel = context.WithTimeout(ctx, time.Duration(shutdownAfter)*time.Second) defer cancel() @@ -94,12 +90,12 @@ func main() { var wg sync.WaitGroup wg.Add(1) - dlog.Start(ctx, &wg, sourceProcess, config.Common.LogLevel) + dlog.Start(ctx, &wg, source.Client, config.Common.LogLevel) if checkHealth { - defer cancel() - healthClient, _ := clients.NewHealthClient(args) - os.Exit(healthClient.Start(ctx, signal.InterruptCh(ctx))) + fmt.Println("WARN: DTail health check has moved to separate binary dtailhealtcheck - please adjust the monitoring scripts!") + cancel() + os.Exit(1) } if pprof > -1 { diff --git a/cmd/dtailhealthcheck/main.go b/cmd/dtailhealthcheck/main.go new file mode 100644 index 0000000..e0cb795 --- /dev/null +++ b/cmd/dtailhealthcheck/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "context" + "flag" + "os" + "sync" + + "github.com/mimecast/dtail/internal/clients" + "github.com/mimecast/dtail/internal/config" + "github.com/mimecast/dtail/internal/io/dlog" + "github.com/mimecast/dtail/internal/io/signal" + "github.com/mimecast/dtail/internal/source" +) + +// The evil begins here. +func main() { + var args config.Args + flag.StringVar(&args.ServersStr, "server", "", "Remote server to connect") + flag.Parse() + config.Setup(source.HealthCheck, &args, flag.Args()) + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + var wg sync.WaitGroup + wg.Add(1) + + dlog.Start(ctx, &wg, source.HealthCheck, config.Common.LogLevel) + healthClient, _ := clients.NewHealthClient(args) + os.Exit(healthClient.Start(ctx, signal.NoCh(ctx))) +} diff --git a/internal/clients/baseclient.go b/internal/clients/baseclient.go index b474208..d5d7c2c 100644 --- a/internal/clients/baseclient.go +++ b/internal/clients/baseclient.go @@ -88,7 +88,6 @@ func (c *baseClient) Start(ctx context.Context, statsCh <-chan string) (status i go func(i int, conn connectors.Connector) { defer wg.Done() connStatus := c.startConnection(ctx, i, conn) - // Update global status. mutex.Lock() defer mutex.Unlock() if connStatus > status { diff --git a/internal/clients/handlers/healthhandler.go b/internal/clients/handlers/healthhandler.go index 4b16ce4..10ba1f7 100644 --- a/internal/clients/handlers/healthhandler.go +++ b/internal/clients/handlers/healthhandler.go @@ -1,7 +1,6 @@ package handlers import ( - "fmt" "strings" "github.com/mimecast/dtail/internal" @@ -22,7 +21,7 @@ func NewHealthHandler(server string) *HealthHandler { server: server, shellStarted: false, commands: make(chan string), - status: -1, + status: 2, // Assume CRITICAL status by default. done: internal.NewDone(), }, } @@ -34,14 +33,12 @@ func (h *HealthHandler) Write(p []byte) (n int, err error) { switch b { case '\n', protocol.MessageDelimiter: message := h.baseHandler.receiveBuf.String() - dlog.Client.Debug(message) h.handleMessage(message) h.baseHandler.receiveBuf.Reset() default: h.baseHandler.receiveBuf.WriteByte(b) } } - return len(p), nil } @@ -52,21 +49,7 @@ func (h *HealthHandler) handleMessage(message string) { } s := strings.Split(message, protocol.FieldDelimiter) message = s[len(s)-1] - status := strings.Split(message, ":") - fmt.Println(status) - /* - switch status { - case "OK": - h.HealthStatusCh <- 0 - case "WARNING": - h.HealthStatusCh <- 1 - case "CRITICAL": - h.HealthStatusCh <- 2 - case "UNKNOWN": - h.HealthStatusCh <- 3 - default: - fmt.Println("CRITICAL: Unexpected server response: '%s'") - h.HealthStatusCh <- 2 - } - */ + if message == "OK" { + h.baseHandler.status = 0 + } } diff --git a/internal/clients/healthclient.go b/internal/clients/healthclient.go index e2c8ccb..ac1dc20 100644 --- a/internal/clients/healthclient.go +++ b/internal/clients/healthclient.go @@ -1,6 +1,8 @@ package clients import ( + "context" + "fmt" "runtime" "github.com/mimecast/dtail/internal/clients/handlers" @@ -42,3 +44,30 @@ func (c HealthClient) makeCommands() (commands []string) { commands = append(commands, "health") return } + +func (c *HealthClient) Start(ctx context.Context, statsCh <-chan string) int { + status := c.baseClient.Start(ctx, statsCh) + + switch status { + case 0: + if c.Serverless { + fmt.Printf("WARNING: All seems fine but the check only run in serverless mode, please specify a remote server via --server hostname:port\n") + return 1 + } + fmt.Printf("OK: All fine at %s :-)\n", c.ServersStr) + case 2: + if c.Serverless { + fmt.Printf("CRITICAL: DTail server not operating properly (using serverless connction)!\n") + return 2 + } + fmt.Printf("CRITICAL: DTail server not operating properly at %s!\n", c.ServersStr) + default: + if c.Serverless { + fmt.Printf("UNKNOWN: Received unknown status code %d (using serverless connection)\n", status) + return status + } + fmt.Printf("UNKNOWN: Received unknown status code %d from %s!\n", status, c.ServersStr) + } + + return status +} diff --git a/internal/config/initializer.go b/internal/config/initializer.go index a58f82a..ec758c8 100644 --- a/internal/config/initializer.go +++ b/internal/config/initializer.go @@ -96,11 +96,19 @@ func (c *initializer) transformConfig(sourceProcess source.Source, args *Args, a common.SSHPort = args.SSHPort } - if args.Discovery == "" && args.ServersStr == "" { + if args.Discovery == "" && (args.ServersStr == "" || + strings.ToLower(args.ServersStr) == "serverless") { // We are not connecting to any servers. args.Serverless = true } + if sourceProcess == source.HealthCheck { + args.TrustAllHosts = true + if !args.Serverless && strings.ToLower(args.ServersStr) == "" { + args.ServersStr = fmt.Sprintf("localhost:%d", DefaultSSHPort) + } + } + // Interpret additional args as file list. if args.What == "" { var files []string diff --git a/internal/io/dlog/dlog.go b/internal/io/dlog/dlog.go index a17d6e9..2ae3b04 100644 --- a/internal/io/dlog/dlog.go +++ b/internal/io/dlog/dlog.go @@ -59,7 +59,7 @@ func Start(ctx context.Context, wg *sync.WaitGroup, sourceProcess source.Source, Common = Server case source.HealthCheck: // Health check isn't logging anything. - impl := loggers.STDOUT + impl := loggers.NONE Client = New(source.HealthCheck, source.Client, level, impl, strategy) Server = New(source.HealthCheck, source.Server, level, impl, strategy) Common = Client diff --git a/internal/io/signal/signal.go b/internal/io/signal/signal.go index 500c530..14056c4 100644 --- a/internal/io/signal/signal.go +++ b/internal/io/signal/signal.go @@ -44,3 +44,8 @@ func InterruptCh(ctx context.Context) <-chan string { return statsCh } + +// NoCh doesn't listen on a signal. +func NoCh(ctx context.Context) <-chan string { + return make(chan string) +} diff --git a/internal/server/handlers/healthhandler.go b/internal/server/handlers/healthhandler.go index 3f3b932..347ff66 100644 --- a/internal/server/handlers/healthhandler.go +++ b/internal/server/handlers/healthhandler.go @@ -48,8 +48,8 @@ func (h *HealthHandler) handleHealthCommand(ctx context.Context, argc int, args switch commandName { case "health": - h.send(h.serverMessages, "OK: DTail SSH Server seems fine") - case "ack", ".ack": + h.send(h.serverMessages, "OK") + case ".ack": h.handleAckCommand(argc, args) default: h.send(h.serverMessages, dlog.Server.Error(h.user, "Received unknown health command", commandName, argc, args)) diff --git a/internal/server/handlers/serverhandler.go b/internal/server/handlers/serverhandler.go index aaffe14..aed8956 100644 --- a/internal/server/handlers/serverhandler.go +++ b/internal/server/handlers/serverhandler.go @@ -106,7 +106,7 @@ func (h *ServerHandler) handleUserCommand(ctx context.Context, argc int, args [] commandFinished() }() - case "ack", ".ack": + case ".ack": h.handleAckCommand(argc, args) commandFinished() diff --git a/samples/check_dserver.sh.sample b/samples/check_dserver.sh.sample index 96c96de..9462037 100755 --- a/samples/check_dserver.sh.sample +++ b/samples/check_dserver.sh.sample @@ -1,4 +1,3 @@ #!/bin/bash -declare -r CONFIG_FILE=/etc/dserver/dtail.json -exec /usr/local/bin/dtail --cfg $CONFIG_FILE --checkHealth +exec /usr/local/bin/dtailhealthcheck --server localhost:2222 |
