-
Notifications
You must be signed in to change notification settings - Fork 13
/
circonus-unified-agent.go
406 lines (357 loc) · 11.2 KB
/
circonus-unified-agent.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//go:build go1.17
// +build go1.17
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"net/http"
_ "net/http/pprof" //nolint:gosec // G108 -- Comment this line to disable pprof endpoint.
"os"
"os/signal"
"sort"
"strings"
"syscall"
"time"
"github.com/circonus-labs/circonus-unified-agent/agent"
"github.com/circonus-labs/circonus-unified-agent/config"
"github.com/circonus-labs/circonus-unified-agent/internal"
"github.com/circonus-labs/circonus-unified-agent/internal/circonus"
"github.com/circonus-labs/circonus-unified-agent/internal/goplugin"
"github.com/circonus-labs/circonus-unified-agent/internal/release"
"github.com/circonus-labs/circonus-unified-agent/logger"
_ "github.com/circonus-labs/circonus-unified-agent/plugins/aggregators/all"
"github.com/circonus-labs/circonus-unified-agent/plugins/inputs"
_ "github.com/circonus-labs/circonus-unified-agent/plugins/inputs/all"
"github.com/circonus-labs/circonus-unified-agent/plugins/outputs"
_ "github.com/circonus-labs/circonus-unified-agent/plugins/outputs/all"
_ "github.com/circonus-labs/circonus-unified-agent/plugins/processors/all"
)
// If you update these, update usage.go and usage_windows.go
var fDebug = flag.Bool("debug", false,
"turn on debug logging")
var pprofAddr = flag.String("pprof-addr", "",
"pprof address to listen on, not activate pprof if empty")
var fQuiet = flag.Bool("quiet", false,
"run in quiet mode")
var fTest = flag.Bool("test", false,
"enable test mode: gather metrics, print them out, and exit. Note: Test mode only runs inputs, not processors, aggregators, or outputs")
var fTestWait = flag.Int("test-wait", 0,
"wait up to this many seconds for service inputs to complete in test mode")
var fConfig = flag.String("config", "",
"configuration file to load")
var fConfigDirectory = flag.String("config-directory", "",
"directory containing additional *.conf files")
var fVersion = flag.Bool("version", false,
"display the version and exit")
var fSampleConfig = flag.Bool("sample-config", false,
"print out full sample configuration")
var fPidfile = flag.String("pidfile", "",
"file to write our pid to")
var fSectionFilters = flag.String("section-filter", "",
"filter the sections to print, separator is ':'. Valid values are 'agent', 'global_tags', 'outputs', 'processors', 'aggregators' and 'inputs'")
var fInputFilters = flag.String("input-filter", "",
"filter the inputs to enable, separator is :")
var fInputList = flag.Bool("input-list", false,
"print available input plugins.")
var fOutputFilters = flag.String("output-filter", "",
"filter the outputs to enable, separator is :")
var fOutputList = flag.Bool("output-list", false,
"print available output plugins.")
var fAggregatorFilters = flag.String("aggregator-filter", "",
"filter the aggregators to enable, separator is :")
var fProcessorFilters = flag.String("processor-filter", "",
"filter the processors to enable, separator is :")
var fUsage = flag.String("usage", "",
"print usage for a plugin, ie, 'circonus-unified-agent --usage mysql'")
var fPlugins = flag.String("plugin-directory", "",
"path to directory containing external plugins")
var fRunOnce = flag.Bool("once", false,
"run one gather and exit")
var (
version string
commit string
branch string
buildDate string
buildTag string
)
var stop chan struct{}
func reloadLoop(
inputFilters []string,
outputFilters []string,
aggregatorFilters []string, //nolint:unparam
processorFilters []string, //nolint:unparam
) {
reload := make(chan bool, 1)
reload <- true
for <-reload {
reload <- false
ctx, cancel := context.WithCancel(context.Background())
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGINT)
go func() {
select {
case sig := <-signals:
if sig == syscall.SIGHUP {
log.Printf("I! Reloading config")
<-reload
reload <- true
}
cancel()
case <-stop:
cancel()
}
}()
err := runAgent(ctx, inputFilters, outputFilters)
if err != nil && !errors.Is(err, context.Canceled) {
log.Fatalf("E! [circonus-unified-agent] Error running agent: %v", err)
}
}
}
func runAgent(ctx context.Context,
inputFilters []string,
outputFilters []string,
) error {
log.Printf("I! Starting Circonus Unified Agent %s", version)
// If no other options are specified, load the config file and run.
c := config.NewConfig()
c.OutputFilters = outputFilters
c.InputFilters = inputFilters
err := c.LoadConfig(*fConfig)
if err != nil {
return fmt.Errorf("loadconfig (%s): %w", *fConfig, err)
}
if *fConfigDirectory != "" {
err = c.LoadDirectory(*fConfigDirectory)
if err != nil {
return fmt.Errorf("loaddir (%s): %w", *fConfigDirectory, err)
}
log.Printf("I! Completed loading configs from %s", *fConfigDirectory)
}
// mgm: add default plugins and agent plugins
if err := c.LoadDefaultPlugins(); err != nil {
return fmt.Errorf("loading defaults: %w", err)
}
// mgm: initialize the internal circonus config.
if err := circonus.Initialize(c.GetGlobalCirconusConfig()); err != nil {
log.Fatalf("E! unable to initialize circonus %s", err)
}
if len(c.Tags) > 0 {
circonus.AddGlobalTags(c.Tags)
}
if !*fTest && len(c.Outputs) == 0 {
return fmt.Errorf("Error: no outputs found, did you provide a valid config file?")
}
if *fPlugins == "" && len(c.Inputs) == 0 {
return fmt.Errorf("Error: no inputs found, did you provide a valid config file?")
}
if int64(c.Agent.Interval.Duration) <= 0 {
return fmt.Errorf("Agent interval must be positive, found %s", c.Agent.Interval.Duration)
}
if int64(c.Agent.FlushInterval.Duration) <= 0 {
return fmt.Errorf("Agent flush_interval must be positive; found %s", c.Agent.Interval.Duration)
}
ag, err := agent.NewAgent(c)
if err != nil {
return fmt.Errorf("new agent: %w", err)
}
// Setup logging as configured.
logConfig := logger.LogConfig{
Debug: ag.Config.Agent.Debug || *fDebug,
Quiet: ag.Config.Agent.Quiet || *fQuiet,
LogTarget: ag.Config.Agent.LogTarget,
Logfile: ag.Config.Agent.Logfile,
RotationInterval: ag.Config.Agent.LogfileRotationInterval,
RotationMaxSize: ag.Config.Agent.LogfileRotationMaxSize,
RotationMaxArchives: ag.Config.Agent.LogfileRotationMaxArchives,
}
logger.SetupLogging(logConfig)
if *fRunOnce {
wait := time.Duration(*fTestWait) * time.Second
return ag.Once(ctx, wait)
}
if *fTest || *fTestWait != 0 {
wait := time.Duration(*fTestWait) * time.Second
return ag.Test(ctx, wait)
}
log.Printf("I! Loaded inputs: %s", strings.Join(c.InputNames(), " "))
log.Printf("I! Loaded aggregators: %s", strings.Join(c.AggregatorNames(), " "))
log.Printf("I! Loaded processors: %s", strings.Join(c.ProcessorNames(), " "))
log.Printf("I! Loaded outputs: %s", strings.Join(c.OutputNames(), " "))
log.Printf("I! Tags enabled: %s", c.ListTags())
if *fPidfile != "" {
f, err := os.OpenFile(*fPidfile, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Printf("E! Unable to create pidfile: %s", err)
} else {
fmt.Fprintf(f, "%d\n", os.Getpid())
f.Close()
defer func() {
err := os.Remove(*fPidfile)
if err != nil {
log.Printf("E! Unable to remove pidfile: %s", err)
}
}()
}
}
return ag.Run(ctx)
}
func usageExit(rc int) {
fmt.Println(internal.Usage) //nolint:govet
os.Exit(rc)
}
func formatFullVersion() string {
var parts = []string{"circonus-unified-agent"}
if version != "" {
parts = append(parts, version)
} else {
parts = append(parts, "unknown")
}
if branch != "" || commit != "" || buildTag != "" {
if branch == "" {
branch = "unknown"
}
if commit == "" {
commit = "unknown"
}
if buildTag == "" {
buildTag = "n/a"
}
git := fmt.Sprintf("(git: %s %s %s)", branch, commit, buildTag)
parts = append(parts, git)
}
if buildDate != "" {
parts = append(parts, "built:"+buildDate)
}
return strings.Join(parts, " ")
}
func setReleaseInfo() {
ri := release.GetInfo()
version = ri.Version
commit = ri.Commit
branch = ri.Branch
buildDate = ri.BuildDate
buildTag = ri.BuildTag
}
func main() {
setReleaseInfo()
flag.Usage = func() { usageExit(0) }
flag.Parse()
args := flag.Args()
sectionFilters, inputFilters, outputFilters := []string{}, []string{}, []string{}
if *fSectionFilters != "" {
sectionFilters = strings.Split(":"+strings.TrimSpace(*fSectionFilters)+":", ":")
}
if *fInputFilters != "" {
inputFilters = strings.Split(":"+strings.TrimSpace(*fInputFilters)+":", ":")
}
if *fOutputFilters != "" {
outputFilters = strings.Split(":"+strings.TrimSpace(*fOutputFilters)+":", ":")
}
aggregatorFilters, processorFilters := []string{}, []string{}
if *fAggregatorFilters != "" {
aggregatorFilters = strings.Split(":"+strings.TrimSpace(*fAggregatorFilters)+":", ":")
}
if *fProcessorFilters != "" {
processorFilters = strings.Split(":"+strings.TrimSpace(*fProcessorFilters)+":", ":")
}
logger.SetupLogging(logger.LogConfig{})
// Load external plugins, if requested.
if *fPlugins != "" {
log.Printf("I! Loading external plugins from: %s", *fPlugins)
if err := goplugin.LoadExternalPlugins(*fPlugins); err != nil {
log.Fatal("E! " + err.Error())
}
}
if *pprofAddr != "" {
go func() {
pprofHostPort := *pprofAddr
parts := strings.Split(pprofHostPort, ":")
if len(parts) == 2 && parts[0] == "" {
pprofHostPort = fmt.Sprintf("localhost:%s", parts[1])
}
pprofHostPort = "http://" + pprofHostPort + "/debug/pprof"
log.Printf("I! Starting pprof HTTP server at: %s", pprofHostPort)
err := http.ListenAndServe(*pprofAddr, nil) // #nosec G114 // G114: Use of net/http serve function that has no support for setting timeouts
if err != nil {
log.Fatal("E! " + err.Error())
}
}()
}
if len(args) > 0 {
switch args[0] {
case "version":
fmt.Println(formatFullVersion())
return
case "config":
config.PrintSampleConfig(
sectionFilters,
inputFilters,
outputFilters,
aggregatorFilters,
processorFilters,
)
return
}
}
// switch for flags which just do something and exit immediately
switch {
case *fOutputList:
fmt.Println("Available Output Plugins: ")
names := make([]string, 0, len(outputs.Outputs))
for k := range outputs.Outputs {
names = append(names, k)
}
sort.Strings(names)
for _, k := range names {
fmt.Printf(" %s\n", k)
}
return
case *fInputList:
fmt.Println("Available Input Plugins:")
names := make([]string, 0, len(inputs.Inputs))
for k := range inputs.Inputs {
names = append(names, k)
}
sort.Strings(names)
for _, k := range names {
fmt.Printf(" %s\n", k)
}
return
case *fVersion:
fmt.Println(formatFullVersion())
return
case *fSampleConfig:
config.PrintSampleConfig(
sectionFilters,
inputFilters,
outputFilters,
aggregatorFilters,
processorFilters,
)
return
case *fUsage != "":
err := config.PrintInputConfig(*fUsage)
err2 := config.PrintOutputConfig(*fUsage)
if err != nil && err2 != nil {
log.Fatalf("E! %s and %s", err, err2)
}
return
}
shortVersion := version
if shortVersion == "" {
shortVersion = "unknown"
}
// Configure version
if err := internal.SetVersion(shortVersion); err != nil {
log.Println("circonus-unified-agent version already configured to: " + internal.Version())
}
run(
inputFilters,
outputFilters,
aggregatorFilters,
processorFilters,
)
}