summaryrefslogtreecommitdiff
path: root/cmd/dtailhealth/main.go
blob: 33e84dbf37660acff3338cd8bb0d6d19dc040c1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main

import (
	"context"
	"flag"
	"fmt"
	"os"
	"sync"
	"time"

	"github.com/mimecast/dtail/internal/cli"
	"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"
	"github.com/mimecast/dtail/internal/version"
)

// The evil begins here.
func main() {
	var args config.Args
	var displayVersion bool
	var pprof string

	flag.BoolVar(&displayVersion, "version", false, "Display version")
	flag.StringVar(&args.Logger, "logger", config.DefaultHealthCheckLogger, "Logger name")
	flag.StringVar(&args.LogLevel, "logLevel", "none", "Log level")
	flag.StringVar(&args.ServersStr, "server", "", "Remote server to connect")
	flag.BoolVar(&args.NoAuthKey, "no-auth-key", false, "Disable auth-key fast reconnect feature")
	flag.StringVar(&pprof, "pprof", "", "Start PProf server this address")
	flag.Parse()

	if displayVersion {
		version.PrintAndExit(false)
	}

	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)

	var pprofServer *cli.PProfServer
	if pprof != "" {
		pprofServer, pprofErr := cli.NewPProfServer(pprof)
		if pprofErr != nil {
			dlog.Client.Error("Unable to start PProf", pprofErr)
		} else {
			dlog.Client.Info("Starting PProf", pprofServer.Address())
			pprofServer.Start(nil)
		}
	}

	healthClient, err := clients.NewHealthClient(args)
	status := 0
	if err != nil {
		fmt.Fprintf(os.Stderr, "CRITICAL: unable to create dtailhealth client: %v\n", err)
		status = 2
	} else {
		status = healthClient.Start(ctx, signal.NoCh(ctx))
	}

	if pprofServer != nil {
		shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
		if err := pprofServer.Shutdown(shutdownCtx); err != nil {
			dlog.Client.Error("Unable to stop PProf", err)
		}
		shutdownCancel()
	}

	cancel()
	wg.Wait()
	os.Exit(status)
}