forked from Graylog2/collector-sidecar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
149 lines (126 loc) · 4.17 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
// 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 (
"flag"
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/kardianos/service"
"github.com/Graylog2/collector-sidecar/backends"
"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/services"
// importing backend packages to ensure init() is called
_ "github.com/Graylog2/collector-sidecar/backends/beats/filebeat"
_ "github.com/Graylog2/collector-sidecar/backends/beats/winlogbeat"
_ "github.com/Graylog2/collector-sidecar/backends/nxlog"
_ "github.com/Graylog2/collector-sidecar/daemon"
)
var (
log = common.Log()
printVersion *bool
serviceParam *string
configurationFile *string
)
func init() {
var configurationPath string
if runtime.GOOS == "windows" {
configurationPath = filepath.Join(os.Getenv("SystemDrive")+"\\", "Program Files", "graylog", "collector-sidecar", "collector_sidecar.yml")
} else {
configurationPath = filepath.Join("/etc", "graylog", "collector-sidecar", "collector_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")
flag.Usage = func() {
fmt.Fprint(os.Stderr, "Usage: graylog-collector-sidecar -c [CONFIGURATION FILE]\n")
flag.PrintDefaults()
}
}
func main() {
if commandLineSetup() {
return
}
// 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.Errorf("Failed service action: %v", err)
}
return
}
// initialize application context
ctx := context.NewContext()
err = ctx.LoadConfig(configurationFile)
if err != nil {
log.Fatal("Loading configuration file failed.")
} else {
// Persist path for later reloads
cfgfile.SetConfigPath(*configurationFile)
}
if cfgfile.ValidateConfig() {
log.Info("Config OK")
return
}
backendSetup(ctx)
// start main loop
services.StartPeriodicals(ctx)
err = s.Run()
if err != nil {
log.Fatal(err)
}
}
func commandLineSetup() bool {
flag.Parse()
if *printVersion {
fmt.Printf("Graylog Collector Sidecar version %s (%s) [%s/%s]\n", common.CollectorVersion, common.GitRevision, runtime.Version(), runtime.GOARCH)
return true
}
if _, err := os.Stat(*configurationFile); os.IsNotExist(err) {
log.Fatal("Can not open collector-sidecar configuration " + *configurationFile)
}
return false
}
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.AddBackend(backend)
if *collector.Enabled == true && backend.ValidatePreconditions() {
log.Debug("Add collector backend: " + backend.Name())
daemon.Daemon.AddBackend(backend, context)
}
}
}