-
Notifications
You must be signed in to change notification settings - Fork 56
/
main.go
149 lines (128 loc) · 3.83 KB
/
main.go
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (C) 2020 Graylog, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the Server Side Public License, version 1,
// as published by MongoDB, Inc.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// Server Side Public License for more details.
//
// You should have received a copy of the Server Side Public License
// along with this program. If not, see
// <http://www.mongodb.com/licensing/server-side-public-license>.
package main
import (
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/kardianos/service"
"github.com/sirupsen/logrus"
"github.com/Graylog2/collector-sidecar/cfgfile"
"github.com/Graylog2/collector-sidecar/common"
"github.com/Graylog2/collector-sidecar/context"
"github.com/Graylog2/collector-sidecar/daemon"
"github.com/Graylog2/collector-sidecar/logger"
"github.com/Graylog2/collector-sidecar/logger/hooks"
"github.com/Graylog2/collector-sidecar/services"
// importing backend packages to ensure init() is called
_ "github.com/Graylog2/collector-sidecar/daemon"
)
var (
log = logger.Log()
printVersion *bool
debug *bool
serviceParam *string
configurationFile *string
)
func init() {
var configurationPath string
if runtime.GOOS == "windows" {
configurationPath = filepath.Join(os.Getenv("SystemDrive")+"\\", "Program Files", "graylog", "sidecar", "sidecar.yml")
} else {
configurationPath = filepath.Join("/etc", "graylog", "sidecar", "sidecar.yml")
}
serviceParam = flag.String("service", "", "Control the system service [start stop restart install uninstall]")
configurationFile = flag.String("c", configurationPath, "Configuration file")
printVersion = flag.Bool("version", false, "Print version and exit")
debug = flag.Bool("debug", false, "Set log level to debug")
flag.Usage = func() {
fmt.Fprint(os.Stderr, "Usage: graylog-sidecar -c [CONFIGURATION FILE]\n")
flag.PrintDefaults()
}
}
func main() {
err := commandLineSetup()
if err != nil {
log.Fatalln(err)
}
// setup system service
serviceConfig := &service.Config{
Name: daemon.Daemon.Name,
DisplayName: daemon.Daemon.DisplayName,
Description: daemon.Daemon.Description,
Option: services.ServiceOptions(),
}
distributor := daemon.Daemon.NewDistributor()
s, err := service.New(distributor, serviceConfig)
if err != nil {
log.Fatalf("Operating system is not supported: %v", err)
}
distributor.BindToService(s)
if len(*serviceParam) != 0 {
services.ControlHandler(*serviceParam)
err := service.Control(s, *serviceParam)
if err != nil {
log.Fatalf("Failed service action: %v", err)
}
return
}
// initialize application context
ctx := context.NewContext()
err = ctx.LoadConfig(configurationFile)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
} else {
// Persist path for later reloads
cfgfile.SetConfigPath(*configurationFile)
}
if cfgfile.ValidateConfig() {
// if ctx.LoadConfig didn't fail already print message and exit
fmt.Println("Config OK")
return
}
// setup logging
if *debug {
log.Level = logrus.DebugLevel
} else {
log.Level = logrus.InfoLevel
}
hooks.AddLogHooks(ctx, log)
// start main loop
services.StartPeriodicals(ctx)
err = s.Run()
if err != nil {
log.Fatal(err)
}
}
func commandLineSetup() error {
flag.Parse()
if *printVersion {
fmt.Printf("Graylog Collector Sidecar version %s%s (%s) [%s/%s]\n",
common.CollectorVersion,
common.CollectorVersionSuffix,
common.GitRevision,
runtime.Version(),
runtime.GOARCH)
os.Exit(0)
}
if _, err := os.Stat(*configurationFile); os.IsNotExist(err) {
return errors.New("Unable to open configuration file: " + *configurationFile)
}
return nil
}