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
78
79
80
81
|
package main
import (
"context"
"flag"
"fmt"
"net/http"
_ "net/http"
_ "net/http/pprof"
"os"
"os/signal"
"syscall"
"time"
"github.com/mimecast/dtail/internal/color"
"github.com/mimecast/dtail/internal/config"
"github.com/mimecast/dtail/internal/io/logger"
"github.com/mimecast/dtail/internal/server"
"github.com/mimecast/dtail/internal/user"
"github.com/mimecast/dtail/internal/version"
)
// The evil begins here.
func main() {
var cfgFile string
var debugEnable bool
var displayVersion bool
var noColor bool
var pprof int
var shutdownAfter int
var sshPort int
user.NoRootCheck()
flag.BoolVar(&debugEnable, "debug", false, "Activate debug messages")
flag.BoolVar(&displayVersion, "version", false, "Display version")
flag.BoolVar(&noColor, "noColor", false, "Disable ANSII terminal colors")
flag.IntVar(&shutdownAfter, "shutdownAfter", 0, "Automatically shutdown after so many seconds")
flag.IntVar(&sshPort, "port", 2222, "SSH server port")
flag.IntVar(&pprof, "pprof", -1, "Start PProf server this port")
flag.StringVar(&cfgFile, "cfg", "", "Config file path")
flag.Parse()
config.Read(cfgFile, sshPort)
color.Colored = !noColor
if displayVersion {
version.PrintAndExit()
}
ctx, cancel := context.WithCancel(context.Background())
if shutdownAfter > 0 {
ctx, cancel = context.WithTimeout(ctx, time.Duration(shutdownAfter)*time.Second)
}
sigCh := make(chan os.Signal)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
go func() {
select {
case <-sigCh:
cancel()
case <-ctx.Done():
}
}()
logger.Start(ctx, logger.Modes{Server: true, Debug: debugEnable || config.Common.DebugEnable})
if pprof > -1 {
// For debugging purposes only
pprofArgs := fmt.Sprintf("0.0.0.0:%d", pprof)
logger.Info("Starting PProf", pprofArgs)
go http.ListenAndServe(pprofArgs, nil)
}
serv := server.New()
status := serv.Start(ctx)
logger.Flush()
os.Exit(status)
}
|