-
Notifications
You must be signed in to change notification settings - Fork 57
/
main.go
167 lines (144 loc) · 4.42 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// This file is part of Graylog.
//
// Graylog is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Graylog 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
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Graylog. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/Sirupsen/logrus"
"github.com/kardianos/service"
"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,
}
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)
// initialize backends
//backendSetup(ctx)
// 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
}
//func backendSetup(context *context.Ctx) {
// for _, collector := range context.UserConfig.Backends {
// backendCreator, err := backends.GetCreator(collector.Name)
// if err != nil {
// log.Error("Unsupported collector backend found in configuration: " + collector.Name)
// continue
// }
// backend := backendCreator(context)
// backends.Store.AddRunner(backend)
// if *collector.Enabled == true && backend.ValidatePreconditions() {
// log.Debug("Add collector backend: " + backend.Name())
// daemon.Daemon.AddRunner(backend, context)
// }
// }
//}