-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
beat.go
1098 lines (923 loc) · 30.5 KB
/
beat.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package instance
import (
"context"
cryptRand "crypto/rand"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"math"
"math/big"
"math/rand"
"os"
"runtime"
"strings"
"time"
"go.elastic.co/apm"
"github.com/gofrs/uuid"
errw "github.com/pkg/errors"
"go.uber.org/zap"
"github.com/elastic/beats/v7/libbeat/api"
"github.com/elastic/beats/v7/libbeat/asset"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/cloudid"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/file"
"github.com/elastic/beats/v7/libbeat/common/reload"
"github.com/elastic/beats/v7/libbeat/common/seccomp"
"github.com/elastic/beats/v7/libbeat/dashboards"
"github.com/elastic/beats/v7/libbeat/esleg/eslegclient"
"github.com/elastic/beats/v7/libbeat/idxmgmt"
"github.com/elastic/beats/v7/libbeat/keystore"
"github.com/elastic/beats/v7/libbeat/kibana"
"github.com/elastic/beats/v7/libbeat/logp"
"github.com/elastic/beats/v7/libbeat/logp/configure"
"github.com/elastic/beats/v7/libbeat/management"
"github.com/elastic/beats/v7/libbeat/metric/system/host"
"github.com/elastic/beats/v7/libbeat/monitoring"
"github.com/elastic/beats/v7/libbeat/monitoring/report"
"github.com/elastic/beats/v7/libbeat/monitoring/report/log"
"github.com/elastic/beats/v7/libbeat/outputs"
"github.com/elastic/beats/v7/libbeat/outputs/elasticsearch"
"github.com/elastic/beats/v7/libbeat/paths"
"github.com/elastic/beats/v7/libbeat/plugin"
"github.com/elastic/beats/v7/libbeat/publisher/pipeline"
"github.com/elastic/beats/v7/libbeat/publisher/processing"
svc "github.com/elastic/beats/v7/libbeat/service"
"github.com/elastic/beats/v7/libbeat/version"
sysinfo "github.com/elastic/go-sysinfo"
"github.com/elastic/go-sysinfo/types"
ucfg "github.com/elastic/go-ucfg"
)
// Beat provides the runnable and configurable instance of a beat.
type Beat struct {
beat.Beat
Config beatConfig
RawConfig *common.Config // Raw config that can be unpacked to get Beat specific config data.
IdxSupporter idxmgmt.Supporter
keystore keystore.Keystore
processing processing.Supporter
}
type beatConfig struct {
beat.BeatConfig `config:",inline"`
// instance internal configs
// beat top-level settings
Name string `config:"name"`
MaxProcs int `config:"max_procs"`
Seccomp *common.Config `config:"seccomp"`
// beat internal components configurations
HTTP *common.Config `config:"http"`
Path paths.Path `config:"path"`
Logging *common.Config `config:"logging"`
MetricLogging *common.Config `config:"logging.metrics"`
Keystore *common.Config `config:"keystore"`
// output/publishing related configurations
Pipeline pipeline.Config `config:",inline"`
// monitoring settings
MonitoringBeatConfig monitoring.BeatConfig `config:",inline"`
// central management settings
Management *common.Config `config:"management"`
// elastic stack 'setup' configurations
Dashboards *common.Config `config:"setup.dashboards"`
Kibana *common.Config `config:"setup.kibana"`
// Migration config to migration from 6 to 7
Migration *common.Config `config:"migration.6_to_7"`
}
var debugf = logp.MakeDebug("beat")
func init() {
initRand()
preventDefaultTracing()
}
// initRand initializes the runtime random number generator seed using
// global, shared cryptographically strong pseudo random number generator.
//
// On linux Reader might use getrandom(2) or /udev/random. On windows systems
// CryptGenRandom is used.
func initRand() {
n, err := cryptRand.Int(cryptRand.Reader, big.NewInt(math.MaxInt64))
var seed int64
if err != nil {
// fallback to current timestamp
seed = time.Now().UnixNano()
} else {
seed = n.Int64()
}
rand.Seed(seed)
}
func preventDefaultTracing() {
// By default, the APM tracer is active. We switch behaviour to not require users to have
// an APM Server running, making it opt-in
if os.Getenv("ELASTIC_APM_ACTIVE") == "" {
os.Setenv("ELASTIC_APM_ACTIVE", "false")
}
// we need to close the default tracer to prevent the beat sending events to localhost:8200
apm.DefaultTracer.Close()
}
// Run initializes and runs a Beater implementation. name is the name of the
// Beat (e.g. packetbeat or metricbeat). version is version number of the Beater
// implementation. bt is the `Creator` callback for creating a new beater
// instance.
// XXX Move this as a *Beat method?
func Run(settings Settings, bt beat.Creator) error {
err := setUmaskWithSettings(settings)
if err != nil && err != errNotImplemented {
return errw.Wrap(err, "could not set umask")
}
name := settings.Name
idxPrefix := settings.IndexPrefix
version := settings.Version
return handleError(func() error {
defer func() {
if r := recover(); r != nil {
logp.NewLogger(name).Fatalw("Failed due to panic.",
"panic", r, zap.Stack("stack"))
}
}()
b, err := NewBeat(name, idxPrefix, version)
if err != nil {
return err
}
// Add basic info
registry := monitoring.GetNamespace("info").GetRegistry()
monitoring.NewString(registry, "version").Set(b.Info.Version)
monitoring.NewString(registry, "beat").Set(b.Info.Beat)
monitoring.NewString(registry, "name").Set(b.Info.Name)
monitoring.NewString(registry, "hostname").Set(b.Info.Hostname)
// Add additional info to state registry. This is also reported to monitoring
stateRegistry := monitoring.GetNamespace("state").GetRegistry()
serviceRegistry := stateRegistry.NewRegistry("service")
monitoring.NewString(serviceRegistry, "version").Set(b.Info.Version)
monitoring.NewString(serviceRegistry, "name").Set(b.Info.Beat)
beatRegistry := stateRegistry.NewRegistry("beat")
monitoring.NewString(beatRegistry, "name").Set(b.Info.Name)
monitoring.NewFunc(stateRegistry, "host", host.ReportInfo, monitoring.Report)
return b.launch(settings, bt)
}())
}
// NewInitializedBeat creates a new beat where all information and initialization is derived from settings
func NewInitializedBeat(settings Settings) (*Beat, error) {
b, err := NewBeat(settings.Name, settings.IndexPrefix, settings.Version)
if err != nil {
return nil, err
}
if err := b.InitWithSettings(settings); err != nil {
return nil, err
}
return b, nil
}
// NewBeat creates a new beat instance
func NewBeat(name, indexPrefix, v string) (*Beat, error) {
if v == "" {
v = version.GetDefaultVersion()
}
if indexPrefix == "" {
indexPrefix = name
}
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
fields, err := asset.GetFields(name)
if err != nil {
return nil, err
}
id, err := uuid.NewV4()
if err != nil {
return nil, err
}
b := beat.Beat{
Info: beat.Info{
Beat: name,
IndexPrefix: indexPrefix,
Version: v,
Name: hostname,
Hostname: hostname,
ID: id,
EphemeralID: ephemeralID,
},
Fields: fields,
}
return &Beat{Beat: b}, nil
}
// InitWithSettings does initialization of things common to all actions (read confs, flags)
func (b *Beat) InitWithSettings(settings Settings) error {
err := b.handleFlags()
if err != nil {
return err
}
if err := plugin.Initialize(); err != nil {
return err
}
if err := b.configure(settings); err != nil {
return err
}
return nil
}
// Init does initialization of things common to all actions (read confs, flags)
//
// Deprecated: use InitWithSettings
func (b *Beat) Init() error {
return b.InitWithSettings(Settings{})
}
// BeatConfig returns config section for this beat
func (b *Beat) BeatConfig() (*common.Config, error) {
configName := strings.ToLower(b.Info.Beat)
if b.RawConfig.HasField(configName) {
sub, err := b.RawConfig.Child(configName, -1)
if err != nil {
return nil, err
}
return sub, nil
}
return common.NewConfig(), nil
}
// Keystore return the configured keystore for this beat
func (b *Beat) Keystore() keystore.Keystore {
return b.keystore
}
// create and return the beater, this method also initializes all needed items,
// including template registering, publisher, xpack monitoring
func (b *Beat) createBeater(bt beat.Creator) (beat.Beater, error) {
sub, err := b.BeatConfig()
if err != nil {
return nil, err
}
logSystemInfo(b.Info)
logp.Info("Setup Beat: %s; Version: %s", b.Info.Beat, b.Info.Version)
err = b.registerESIndexManagement()
if err != nil {
return nil, err
}
err = b.registerClusterUUIDFetching()
if err != nil {
return nil, err
}
reg := monitoring.Default.GetRegistry("libbeat")
if reg == nil {
reg = monitoring.Default.NewRegistry("libbeat")
}
err = setupMetrics(b.Info.Beat)
if err != nil {
return nil, err
}
// Report central management state
mgmt := monitoring.GetNamespace("state").GetRegistry().NewRegistry("management")
monitoring.NewBool(mgmt, "enabled").Set(b.ConfigManager.Enabled())
debugf("Initializing output plugins")
outputEnabled := b.Config.Output.IsSet() && b.Config.Output.Config().Enabled()
if !outputEnabled {
if b.ConfigManager.Enabled() {
logp.Info("Output is configured through Central Management")
} else {
msg := "No outputs are defined. Please define one under the output section."
logp.Info(msg)
return nil, errors.New(msg)
}
}
tracer, err := apm.NewTracer(b.Info.Beat, b.Info.Version)
if err != nil {
return nil, err
}
pipeline, err := pipeline.Load(b.Info,
pipeline.Monitors{
Metrics: reg,
Telemetry: monitoring.GetNamespace("state").GetRegistry(),
Logger: logp.L().Named("publisher"),
Tracer: tracer,
},
b.Config.Pipeline,
b.processing,
b.makeOutputFactory(b.Config.Output),
)
if err != nil {
return nil, fmt.Errorf("error initializing publisher: %+v", err)
}
reload.Register.MustRegister("output", b.makeOutputReloader(pipeline.OutputReloader()))
// TODO: some beats race on shutdown with publisher.Stop -> do not call Stop yet,
// but refine publisher to disconnect clients on stop automatically
// defer pipeline.Close()
b.Publisher = pipeline
beater, err := bt(&b.Beat, sub)
if err != nil {
return nil, err
}
return beater, nil
}
func (b *Beat) launch(settings Settings, bt beat.Creator) error {
defer logp.Sync()
defer logp.Info("%s stopped.", b.Info.Beat)
err := b.InitWithSettings(settings)
if err != nil {
return err
}
// Windows: Mark service as stopped.
// After this is run, a Beat service is considered by the OS to be stopped
// and another instance of the process can be started.
// This must be the first deferred cleanup task (last to execute).
defer svc.NotifyTermination()
// Try to acquire exclusive lock on data path to prevent another beat instance
// sharing same data path.
bl := newLocker(b)
err = bl.lock()
if err != nil {
return err
}
defer bl.unlock()
// Set Beat ID in registry vars, in case it was loaded from meta file
infoRegistry := monitoring.GetNamespace("info").GetRegistry()
monitoring.NewString(infoRegistry, "uuid").Set(b.Info.ID.String())
serviceRegistry := monitoring.GetNamespace("state").GetRegistry().GetRegistry("service")
monitoring.NewString(serviceRegistry, "id").Set(b.Info.ID.String())
svc.BeforeRun()
defer svc.Cleanup()
// Start the API Server before the Seccomp lock down, we do this so we can create the unix socket
// set the appropriate permission on the unix domain file without having to whitelist anything
// that would be set at runtime.
if b.Config.HTTP.Enabled() {
s, err := api.NewWithDefaultRoutes(logp.NewLogger(""), b.Config.HTTP, monitoring.GetNamespace)
if err != nil {
return errw.Wrap(err, "could not start the HTTP server for the API")
}
s.Start()
defer s.Stop()
}
if err = seccomp.LoadFilter(b.Config.Seccomp); err != nil {
return err
}
beater, err := b.createBeater(bt)
if err != nil {
return err
}
r, err := b.setupMonitoring(settings)
if err != nil {
return err
}
if r != nil {
defer r.Stop()
}
if b.Config.MetricLogging == nil || b.Config.MetricLogging.Enabled() {
reporter, err := log.MakeReporter(b.Info, b.Config.MetricLogging)
if err != nil {
return err
}
defer reporter.Stop()
}
ctx, cancel := context.WithCancel(context.Background())
svc.HandleSignals(beater.Stop, cancel)
err = b.loadDashboards(ctx, false)
if err != nil {
return err
}
logp.Info("%s start running.", b.Info.Beat)
// Launch config manager
b.ConfigManager.Start(beater.Stop)
defer b.ConfigManager.Stop()
return beater.Run(&b.Beat)
}
// TestConfig check all settings are ok and the beat can be run
func (b *Beat) TestConfig(settings Settings, bt beat.Creator) error {
return handleError(func() error {
err := b.InitWithSettings(settings)
if err != nil {
return err
}
// Create beater to ensure all settings are OK
_, err = b.createBeater(bt)
if err != nil {
return err
}
fmt.Println("Config OK")
return beat.GracefulExit
}())
}
//SetupSettings holds settings necessary for beat setup
type SetupSettings struct {
Dashboard bool
Pipeline bool
IndexManagement bool
//Deprecated: use IndexManagementKey instead
Template bool
//Deprecated: use IndexManagementKey instead
ILMPolicy bool
}
// Setup registers ES index template, kibana dashboards, ml jobs and pipelines.
func (b *Beat) Setup(settings Settings, bt beat.Creator, setup SetupSettings) error {
return handleError(func() error {
err := b.InitWithSettings(settings)
if err != nil {
return err
}
// Tell the beat that we're in the setup command
b.InSetupCmd = true
// Create beater to give it the opportunity to set loading callbacks
_, err = b.createBeater(bt)
if err != nil {
return err
}
if setup.IndexManagement || setup.Template || setup.ILMPolicy {
outCfg := b.Config.Output
if outCfg.Name() != "elasticsearch" {
return fmt.Errorf("Index management requested but the Elasticsearch output is not configured/enabled")
}
esClient, err := eslegclient.NewConnectedClient(outCfg.Config())
if err != nil {
return err
}
var loadTemplate, loadILM = idxmgmt.LoadModeUnset, idxmgmt.LoadModeUnset
if setup.IndexManagement || setup.Template {
loadTemplate = idxmgmt.LoadModeOverwrite
}
if setup.IndexManagement || setup.ILMPolicy {
loadILM = idxmgmt.LoadModeEnabled
}
m := b.IdxSupporter.Manager(idxmgmt.NewESClientHandler(esClient), idxmgmt.BeatsAssets(b.Fields))
if ok, warn := m.VerifySetup(loadTemplate, loadILM); !ok {
fmt.Println(warn)
}
if err = m.Setup(loadTemplate, loadILM); err != nil {
return err
}
fmt.Println("Index setup finished.")
}
if setup.Dashboard && settings.HasDashboards {
fmt.Println("Loading dashboards (Kibana must be running and reachable)")
err = b.loadDashboards(context.Background(), true)
if err != nil {
switch err := errw.Cause(err).(type) {
case *dashboards.ErrNotFound:
fmt.Printf("Skipping loading dashboards, %+v\n", err)
default:
return err
}
} else {
fmt.Println("Loaded dashboards")
}
}
if setup.Pipeline && b.OverwritePipelinesCallback != nil {
esConfig := b.Config.Output.Config()
err = b.OverwritePipelinesCallback(esConfig)
if err != nil {
return err
}
fmt.Println("Loaded Ingest pipelines")
}
return nil
}())
}
// handleFlags parses the command line flags. It invokes the HandleFlags
// callback if implemented by the Beat.
func (b *Beat) handleFlags() error {
flag.Parse()
return cfgfile.HandleFlags()
}
// config reads the configuration file from disk, parses the common options
// defined in BeatConfig, initializes logging, and set GOMAXPROCS if defined
// in the config. Lastly it invokes the Config method implemented by the beat.
func (b *Beat) configure(settings Settings) error {
var err error
cfg, err := cfgfile.Load("", settings.ConfigOverrides)
if err != nil {
return fmt.Errorf("error loading config file: %v", err)
}
if err := initPaths(cfg); err != nil {
return err
}
// We have to initialize the keystore before any unpack or merging the cloud
// options.
store, err := LoadKeystore(cfg, b.Info.Beat)
if err != nil {
return fmt.Errorf("could not initialize the keystore: %v", err)
}
if settings.DisableConfigResolver {
common.OverwriteConfigOpts(obfuscateConfigOpts())
} else {
// TODO: Allow the options to be more flexible for dynamic changes
common.OverwriteConfigOpts(configOpts(store))
}
b.keystore = store
b.Beat.Keystore = store
err = cloudid.OverwriteSettings(cfg)
if err != nil {
return err
}
b.RawConfig = cfg
err = cfg.Unpack(&b.Config)
if err != nil {
return fmt.Errorf("error unpacking config data: %v", err)
}
b.Beat.Config = &b.Config.BeatConfig
if name := b.Config.Name; name != "" {
b.Info.Name = name
}
if err := configure.Logging(b.Info.Beat, b.Config.Logging); err != nil {
return fmt.Errorf("error initializing logging: %v", err)
}
// log paths values to help with troubleshooting
logp.Info(paths.Paths.String())
metaPath := paths.Resolve(paths.Data, "meta.json")
err = b.loadMeta(metaPath)
if err != nil {
return err
}
logp.Info("Beat ID: %v", b.Info.ID)
// initialize config manager
b.ConfigManager, err = management.Factory(b.Config.Management)(b.Config.Management, reload.Register, b.Beat.Info.ID)
if err != nil {
return err
}
if err := b.ConfigManager.CheckRawConfig(b.RawConfig); err != nil {
return err
}
if maxProcs := b.Config.MaxProcs; maxProcs > 0 {
runtime.GOMAXPROCS(maxProcs)
}
b.Beat.BeatConfig, err = b.BeatConfig()
if err != nil {
return err
}
imFactory := settings.IndexManagement
if imFactory == nil {
imFactory = idxmgmt.MakeDefaultSupport(settings.ILM)
}
b.IdxSupporter, err = imFactory(nil, b.Beat.Info, b.RawConfig)
if err != nil {
return err
}
processingFactory := settings.Processing
if processingFactory == nil {
processingFactory = processing.MakeDefaultBeatSupport(true)
}
b.processing, err = processingFactory(b.Info, logp.L().Named("processors"), b.RawConfig)
return err
}
func (b *Beat) loadMeta(metaPath string) error {
type meta struct {
UUID uuid.UUID `json:"uuid"`
}
logp.Debug("beat", "Beat metadata path: %v", metaPath)
f, err := openRegular(metaPath)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("Beat meta file failed to open: %s", err)
}
if err == nil {
m := meta{}
if err := json.NewDecoder(f).Decode(&m); err != nil && err != io.EOF {
f.Close()
return fmt.Errorf("Beat meta file reading error: %v", err)
}
f.Close()
valid := m.UUID != uuid.Nil
if valid {
b.Info.ID = m.UUID
return nil
}
}
// file does not exist or ID is invalid, let's create a new one
// write temporary file first
tempFile := metaPath + ".new"
f, err = os.OpenFile(tempFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("Failed to create Beat meta file: %s", err)
}
encodeErr := json.NewEncoder(f).Encode(meta{UUID: b.Info.ID})
err = f.Sync()
if err != nil {
return fmt.Errorf("Beat meta file failed to write: %s", err)
}
err = f.Close()
if err != nil {
return fmt.Errorf("Beat meta file failed to write: %s", err)
}
if encodeErr != nil {
return fmt.Errorf("Beat meta file failed to write: %s", encodeErr)
}
// move temporary file into final location
err = file.SafeFileRotate(metaPath, tempFile)
return err
}
func openRegular(filename string) (*os.File, error) {
f, err := os.Open(filename)
if err != nil {
return f, err
}
info, err := f.Stat()
if err != nil {
f.Close()
return nil, err
}
if !info.Mode().IsRegular() {
f.Close()
if info.IsDir() {
return nil, fmt.Errorf("%s is a directory", filename)
}
return nil, fmt.Errorf("%s is not a regular file", filename)
}
return f, nil
}
func (b *Beat) loadDashboards(ctx context.Context, force bool) error {
if force {
// force implies dashboards.enabled=true
if b.Config.Dashboards == nil {
b.Config.Dashboards = common.NewConfig()
}
err := b.Config.Dashboards.SetBool("enabled", -1, true)
if err != nil {
return fmt.Errorf("Error setting dashboard.enabled=true: %v", err)
}
}
if b.Config.Dashboards.Enabled() {
// Initialize kibana config. If username and password is set in elasticsearch output config but not in kibana,
// initKibanaConfig will attach the username and password into kibana config as a part of the initialization.
kibanaConfig, err := initKibanaConfig(b.Config)
if err != nil {
return fmt.Errorf("error initKibanaConfig: %v", err)
}
client, err := kibana.NewKibanaClient(kibanaConfig)
if err != nil {
return fmt.Errorf("error connecting to Kibana: %v", err)
}
// This fetches the version for Kibana. For the alias feature the version of ES would be needed
// but it's assumed that KB and ES have the same minor version.
v := client.GetVersion()
indexPattern, err := kibana.NewGenerator(b.Info.IndexPrefix, b.Info.Beat, b.Fields, b.Info.Version, v, b.Config.Migration.Enabled())
if err != nil {
return fmt.Errorf("error creating index pattern generator: %v", err)
}
pattern, err := indexPattern.Generate()
if err != nil {
return fmt.Errorf("error generating index pattern: %v", err)
}
err = dashboards.ImportDashboards(ctx, b.Info, paths.Resolve(paths.Home, ""),
kibanaConfig, b.Config.Dashboards, nil, pattern)
if err != nil {
return errw.Wrap(err, "Error importing Kibana dashboards")
}
logp.Info("Kibana dashboards successfully loaded.")
}
return nil
}
// registerESIndexManagement registers the loading of the template and ILM
// policy as a callback with the elasticsearch output. It is important the
// registration happens before the publisher is created.
func (b *Beat) registerESIndexManagement() error {
if b.Config.Output.Name() != "elasticsearch" || !b.IdxSupporter.Enabled() {
return nil
}
_, err := elasticsearch.RegisterConnectCallback(b.indexSetupCallback())
if err != nil {
return fmt.Errorf("failed to register index management with elasticsearch: %+v", err)
}
return nil
}
func (b *Beat) indexSetupCallback() elasticsearch.ConnectCallback {
return func(esClient *eslegclient.Connection) error {
m := b.IdxSupporter.Manager(idxmgmt.NewESClientHandler(esClient), idxmgmt.BeatsAssets(b.Fields))
return m.Setup(idxmgmt.LoadModeEnabled, idxmgmt.LoadModeEnabled)
}
}
func (b *Beat) makeOutputReloader(outReloader pipeline.OutputReloader) reload.Reloadable {
return reload.ReloadableFunc(func(config *reload.ConfigWithMeta) error {
return outReloader.Reload(config, b.createOutput)
})
}
func (b *Beat) makeOutputFactory(
cfg common.ConfigNamespace,
) func(outputs.Observer) (string, outputs.Group, error) {
return func(outStats outputs.Observer) (string, outputs.Group, error) {
out, err := b.createOutput(outStats, cfg)
return cfg.Name(), out, err
}
}
func (b *Beat) createOutput(stats outputs.Observer, cfg common.ConfigNamespace) (outputs.Group, error) {
if !cfg.IsSet() {
return outputs.Group{}, nil
}
return outputs.Load(b.IdxSupporter, b.Info, stats, cfg.Name(), cfg.Config())
}
func (b *Beat) registerClusterUUIDFetching() error {
if b.Config.Output.Name() == "elasticsearch" {
callback, err := b.clusterUUIDFetchingCallback()
if err != nil {
return err
}
elasticsearch.RegisterConnectCallback(callback)
}
return nil
}
// Build and return a callback to fetch the Elasticsearch cluster_uuid for monitoring
func (b *Beat) clusterUUIDFetchingCallback() (elasticsearch.ConnectCallback, error) {
stateRegistry := monitoring.GetNamespace("state").GetRegistry()
elasticsearchRegistry := stateRegistry.NewRegistry("outputs.elasticsearch")
clusterUUIDRegVar := monitoring.NewString(elasticsearchRegistry, "cluster_uuid")
callback := func(esClient *eslegclient.Connection) error {
var response struct {
ClusterUUID string `json:"cluster_uuid"`
}
status, body, err := esClient.Request("GET", "/", "", nil, nil)
if err != nil {
return errw.Wrap(err, "error querying /")
}
if status > 299 {
return fmt.Errorf("Error querying /. Status: %d. Response body: %s", status, body)
}
err = json.Unmarshal(body, &response)
if err != nil {
return fmt.Errorf("Error unmarshaling json when querying /. Body: %s", body)
}
clusterUUIDRegVar.Set(response.ClusterUUID)
return nil
}
return callback, nil
}
func (b *Beat) setupMonitoring(settings Settings) (report.Reporter, error) {
monitoringCfg, reporterSettings, err := monitoring.SelectConfig(b.Config.MonitoringBeatConfig)
if err != nil {
return nil, err
}
monitoringClusterUUID, err := monitoring.GetClusterUUID(b.Config.MonitoringBeatConfig.Monitoring)
if err != nil {
return nil, err
}
// Expose monitoring.cluster_uuid in state API
if monitoringClusterUUID != "" {
stateRegistry := monitoring.GetNamespace("state").GetRegistry()
monitoringRegistry := stateRegistry.NewRegistry("monitoring")
clusterUUIDRegVar := monitoring.NewString(monitoringRegistry, "cluster_uuid")
clusterUUIDRegVar.Set(monitoringClusterUUID)
}
if monitoring.IsEnabled(monitoringCfg) {
err := monitoring.OverrideWithCloudSettings(monitoringCfg)
if err != nil {
return nil, err
}
settings := report.Settings{
DefaultUsername: settings.Monitoring.DefaultUsername,
Format: reporterSettings.Format,
ClusterUUID: monitoringClusterUUID,
}
reporter, err := report.New(b.Info, settings, monitoringCfg, b.Config.Output)
if err != nil {
return nil, err
}
return reporter, nil
}
return nil, nil
}
// handleError handles the given error by logging it and then returning the
// error. If the err is nil or is a GracefulExit error then the method will
// return nil without logging anything.
func handleError(err error) error {
if err == nil || err == beat.GracefulExit {
return nil
}
// logp may not be initialized so log the err to stderr too.
logp.Critical("Exiting: %v", err)
fmt.Fprintf(os.Stderr, "Exiting: %v\n", err)
return err
}
// logSystemInfo logs information about this system for situational awareness
// in debugging. This information includes data about the beat, build, go
// runtime, host, and process. If any of the data is not available it will be
// omitted.
func logSystemInfo(info beat.Info) {
defer logp.Recover("An unexpected error occurred while collecting " +
"information about the system.")
log := logp.NewLogger("beat").With(logp.Namespace("system_info"))
// Beat
beat := common.MapStr{
"type": info.Beat,
"uuid": info.ID,
"path": common.MapStr{
"config": paths.Resolve(paths.Config, ""),
"data": paths.Resolve(paths.Data, ""),
"home": paths.Resolve(paths.Home, ""),
"logs": paths.Resolve(paths.Logs, ""),
},
}
log.Infow("Beat info", "beat", beat)
// Build
build := common.MapStr{
"commit": version.Commit(),
"time": version.BuildTime(),
"version": info.Version,
"libbeat": version.GetDefaultVersion(),
}
log.Infow("Build info", "build", build)
// Go Runtime
log.Infow("Go runtime info", "go", sysinfo.Go())
// Host
if host, err := sysinfo.Host(); err == nil {
log.Infow("Host info", "host", host.Info())
}
// Process
if self, err := sysinfo.Self(); err == nil {
process := common.MapStr{}
if info, err := self.Info(); err == nil {
process["name"] = info.Name
process["pid"] = info.PID
process["ppid"] = info.PPID
process["cwd"] = info.CWD
process["exe"] = info.Exe
process["start_time"] = info.StartTime