forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
1237 lines (1049 loc) · 33 KB
/
server.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
// Provides a server type for starting and configuring a Kapacitor server.
package server
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"sync"
"github.com/influxdata/influxdb/influxql"
"github.com/influxdata/influxdb/services/collectd"
"github.com/influxdata/influxdb/services/graphite"
"github.com/influxdata/influxdb/services/meta"
"github.com/influxdata/influxdb/services/opentsdb"
"github.com/influxdata/kapacitor"
"github.com/influxdata/kapacitor/auth"
"github.com/influxdata/kapacitor/command"
iclient "github.com/influxdata/kapacitor/influxdb"
"github.com/influxdata/kapacitor/keyvalue"
"github.com/influxdata/kapacitor/server/vars"
"github.com/influxdata/kapacitor/services/alert"
"github.com/influxdata/kapacitor/services/alerta"
"github.com/influxdata/kapacitor/services/azure"
"github.com/influxdata/kapacitor/services/config"
"github.com/influxdata/kapacitor/services/consul"
"github.com/influxdata/kapacitor/services/deadman"
"github.com/influxdata/kapacitor/services/diagnostic"
"github.com/influxdata/kapacitor/services/dns"
"github.com/influxdata/kapacitor/services/ec2"
"github.com/influxdata/kapacitor/services/file_discovery"
"github.com/influxdata/kapacitor/services/gce"
"github.com/influxdata/kapacitor/services/hipchat"
"github.com/influxdata/kapacitor/services/httpd"
"github.com/influxdata/kapacitor/services/httppost"
"github.com/influxdata/kapacitor/services/influxdb"
"github.com/influxdata/kapacitor/services/k8s"
"github.com/influxdata/kapacitor/services/kafka"
"github.com/influxdata/kapacitor/services/load"
"github.com/influxdata/kapacitor/services/marathon"
"github.com/influxdata/kapacitor/services/mqtt"
"github.com/influxdata/kapacitor/services/nerve"
"github.com/influxdata/kapacitor/services/noauth"
"github.com/influxdata/kapacitor/services/opsgenie"
"github.com/influxdata/kapacitor/services/opsgenie2"
"github.com/influxdata/kapacitor/services/pagerduty"
"github.com/influxdata/kapacitor/services/pagerduty2"
"github.com/influxdata/kapacitor/services/pushover"
"github.com/influxdata/kapacitor/services/replay"
"github.com/influxdata/kapacitor/services/reporting"
"github.com/influxdata/kapacitor/services/scraper"
"github.com/influxdata/kapacitor/services/sensu"
"github.com/influxdata/kapacitor/services/serverset"
"github.com/influxdata/kapacitor/services/servicetest"
"github.com/influxdata/kapacitor/services/sideload"
"github.com/influxdata/kapacitor/services/slack"
"github.com/influxdata/kapacitor/services/smtp"
"github.com/influxdata/kapacitor/services/snmptrap"
"github.com/influxdata/kapacitor/services/static_discovery"
"github.com/influxdata/kapacitor/services/stats"
"github.com/influxdata/kapacitor/services/storage"
"github.com/influxdata/kapacitor/services/swarm"
"github.com/influxdata/kapacitor/services/talk"
"github.com/influxdata/kapacitor/services/task_store"
"github.com/influxdata/kapacitor/services/telegram"
"github.com/influxdata/kapacitor/services/triton"
"github.com/influxdata/kapacitor/services/udf"
"github.com/influxdata/kapacitor/services/udp"
"github.com/influxdata/kapacitor/services/victorops"
"github.com/influxdata/kapacitor/uuid"
"github.com/influxdata/kapacitor/waiter"
"github.com/pkg/errors"
)
const clusterIDFilename = "cluster.id"
const serverIDFilename = "server.id"
// BuildInfo represents the build details for the server code.
type BuildInfo struct {
Version string
Commit string
Branch string
Platform string
}
type Diagnostic interface {
Debug(msg string, ctx ...keyvalue.T)
Info(msg string, ctx ...keyvalue.T)
Error(msg string, err error, ctx ...keyvalue.T)
}
// Server represents a container for the metadata and storage data and services.
// It is built using a Config and it manages the startup and shutdown of all
// services in the proper order.
type Server struct {
dataDir string
hostname string
config *Config
err chan error
// clusterIDChanged signals when the cluster ID has changed.
clusterIDChanged *waiter.WaiterGroup
Commander command.Commander
TaskMaster *kapacitor.TaskMaster
TaskMasterLookup *kapacitor.TaskMasterLookup
LoadService *load.Service
SideloadService *sideload.Service
AuthService auth.Interface
HTTPDService *httpd.Service
StorageService *storage.Service
AlertService *alert.Service
TaskStore *task_store.Service
ReplayService *replay.Service
SessionService *diagnostic.SessionService
InfluxDBService *influxdb.Service
ConfigOverrideService *config.Service
TesterService *servicetest.Service
StatsService *stats.Service
ScraperService *scraper.Service
MetaClient *kapacitor.NoopMetaClient
QueryExecutor *Queryexecutor
// List of services in startup order
Services []Service
// Map of service name to index in Services list
ServicesByName map[string]int
// Map of services capable of receiving dynamic configuration updates.
DynamicServices map[string]Updater
// Channel of incoming configuration updates.
configUpdates chan config.ConfigUpdate
BuildInfo BuildInfo
clusterIDMu sync.Mutex
ClusterID uuid.UUID
ServerID uuid.UUID
// Profiling
CPUProfile string
MemProfile string
DiagService *diagnostic.Service
Diag Diagnostic
}
// New returns a new instance of Server built from a config.
func New(c *Config, buildInfo BuildInfo, diagService *diagnostic.Service) (*Server, error) {
err := c.Validate()
if err != nil {
return nil, fmt.Errorf("invalid configuration: %s. To generate a valid configuration file run `kapacitord config > kapacitor.generated.conf`.", err)
}
d := diagService.NewServerHandler()
s := &Server{
config: c,
BuildInfo: buildInfo,
dataDir: c.DataDir,
hostname: c.Hostname,
err: make(chan error),
configUpdates: make(chan config.ConfigUpdate, 100),
DiagService: diagService,
MetaClient: &kapacitor.NoopMetaClient{},
QueryExecutor: &Queryexecutor{},
Diag: d,
ServicesByName: make(map[string]int),
DynamicServices: make(map[string]Updater),
Commander: c.Commander,
clusterIDChanged: waiter.NewGroup(),
}
s.Diag.Info("listing Kapacitor hostname", keyvalue.KV("hostname", s.hostname))
// Setup IDs
err = s.setupIDs()
if err != nil {
return nil, err
}
// Set published vars
vars.ClusterIDVar.Set(s.ClusterID)
vars.ServerIDVar.Set(s.ServerID)
vars.HostVar.Set(s.hostname)
vars.ProductVar.Set(vars.Product)
vars.PlatformVar.Set(s.BuildInfo.Platform)
vars.VersionVar.Set(s.BuildInfo.Version)
s.Diag.Info("listing ClusterID and ServerID",
keyvalue.KV("cluster_id", s.ClusterID.String()), keyvalue.KV("server_id", s.ServerID.String()))
// Start Task Master
s.TaskMasterLookup = kapacitor.NewTaskMasterLookup()
kd := diagService.NewKapacitorHandler()
s.TaskMaster = kapacitor.NewTaskMaster(kapacitor.MainTaskMaster, vars.Info, kd)
s.TaskMaster.DefaultRetentionPolicy = c.DefaultRetentionPolicy
s.TaskMaster.Commander = s.Commander
s.TaskMasterLookup.Set(s.TaskMaster)
if err := s.TaskMaster.Open(); err != nil {
return nil, err
}
// Append Kapacitor services.
s.initHTTPDService()
s.appendStorageService()
s.appendAuthService()
s.appendConfigOverrideService()
s.appendTesterService()
s.appendSideloadService()
// Init alert service
s.initAlertService()
// Append all dynamic services after the config override and tester services.
s.appendUDFService()
s.appendDeadmanService()
if err := s.appendInfluxDBService(); err != nil {
return nil, errors.Wrap(err, "influxdb service")
}
if err := s.appendLoadService(); err != nil {
return nil, errors.Wrap(err, "load service")
}
// Append Alert integration services
s.appendAlertaService()
s.appendHipChatService()
s.appendKafkaService()
if err := s.appendMQTTService(); err != nil {
return nil, errors.Wrap(err, "mqtt service")
}
s.appendOpsGenieService()
s.appendOpsGenie2Service()
s.appendPagerDutyService()
s.appendPagerDuty2Service()
s.appendPushoverService()
if err := s.appendHTTPPostService(); err != nil {
return nil, errors.Wrap(err, "httppost service")
}
s.appendSMTPService()
s.appendTelegramService()
if err := s.appendSlackService(); err != nil {
return nil, errors.Wrap(err, "slack service")
}
s.appendSNMPTrapService()
s.appendSensuService()
s.appendTalkService()
s.appendVictorOpsService()
// Append alert service
s.appendAlertService()
// Append these after InfluxDB because they depend on it
s.appendTaskStoreService()
s.appendReplayService()
s.appendSessionService()
// Append third-party integrations
// Append extra input services
if err := s.appendCollectdService(); err != nil {
return nil, errors.Wrap(err, "collectd service")
}
s.appendUDPServices()
if err := s.appendOpenTSDBService(); err != nil {
return nil, errors.Wrap(err, "opentsdb service")
}
if err := s.appendGraphiteServices(); err != nil {
return nil, errors.Wrap(err, "graphite service")
}
// Append Scraper and discovery services
s.appendScraperService()
if err := s.appendK8sService(); err != nil {
return nil, errors.Wrap(err, "kubernetes service")
}
if err := s.appendSwarmService(); err != nil {
return nil, errors.Wrap(err, "docker swarm service")
}
if err := s.appendEC2Service(); err != nil {
return nil, errors.Wrap(err, "Aws service")
}
s.appendAzureService()
s.appendConsulService()
s.appendDNSService()
s.appendFileService()
s.appendGCEService()
s.appendMarathonService()
s.appendNerveService()
s.appendServersetService()
s.appendStaticService()
s.appendTritonService()
// Append StatsService and ReportingService after other services so all stats are ready
// to be reported
s.appendStatsService()
s.appendReportingService()
// Append HTTPD Service last so that the API is not listening till everything else succeeded.
s.appendHTTPDService()
return s, nil
}
func (s *Server) AppendService(name string, srv Service) {
if _, ok := s.ServicesByName[name]; ok {
// Should be unreachable code
panic("cannot append service twice")
}
i := len(s.Services)
s.Services = append(s.Services, srv)
s.ServicesByName[name] = i
}
type dynamicService interface {
Service
Updater
servicetest.Tester
}
func (s *Server) SetDynamicService(name string, srv dynamicService) {
s.DynamicServices[name] = srv
_ = s.TesterService.AddTester(name, srv)
}
func (s *Server) appendStorageService() {
d := s.DiagService.NewStorageHandler()
srv := storage.NewService(s.config.Storage, d)
srv.HTTPDService = s.HTTPDService
s.StorageService = srv
s.AppendService("storage", srv)
}
func (s *Server) appendConfigOverrideService() {
d := s.DiagService.NewConfigOverrideHandler()
srv := config.NewService(s.config.ConfigOverride, s.config, d, s.configUpdates)
srv.HTTPDService = s.HTTPDService
srv.StorageService = s.StorageService
s.ConfigOverrideService = srv
s.AppendService("config", srv)
}
func (s *Server) initAlertService() {
d := s.DiagService.NewAlertServiceHandler()
srv := alert.NewService(d)
srv.Commander = s.Commander
srv.HTTPDService = s.HTTPDService
srv.StorageService = s.StorageService
srv.PersistTopics = s.config.Alert.PersistTopics
s.AlertService = srv
s.TaskMaster.AlertService = srv
}
func (s *Server) appendAlertService() {
s.AppendService("alert", s.AlertService)
}
func (s *Server) appendTesterService() {
srv := servicetest.NewService(servicetest.NewConfig())
srv.HTTPDService = s.HTTPDService
s.TesterService = srv
s.AppendService("tests", srv)
}
func (s *Server) appendSideloadService() {
d := s.DiagService.NewSideloadHandler()
srv := sideload.NewService(d)
srv.HTTPDService = s.HTTPDService
s.SideloadService = srv
s.TaskMaster.SideloadService = srv
s.AppendService("sideload", srv)
}
func (s *Server) appendSMTPService() {
c := s.config.SMTP
d := s.DiagService.NewSMTPHandler()
srv := smtp.NewService(c, d)
s.TaskMaster.SMTPService = srv
s.AlertService.SMTPService = srv
s.SetDynamicService("smtp", srv)
s.AppendService("smtp", srv)
}
func (s *Server) appendLoadService() error {
c := s.config.Load
d := s.DiagService.NewLoadHandler()
if s.HTTPDService == nil {
return errors.New("httpd service must be set for load service")
}
if s.HTTPDService.LocalHandler == nil {
return errors.New("httpd service handler must be set for load service")
}
srv, err := load.NewService(c, s.HTTPDService.LocalHandler, d)
if err != nil {
return err
}
srv.StorageService = s.StorageService
s.LoadService = srv
s.AppendService("load", srv)
return nil
}
func (s *Server) appendInfluxDBService() error {
c := s.config.InfluxDB
d := s.DiagService.NewInfluxDBHandler()
httpPort, err := s.config.HTTP.Port()
if err != nil {
return errors.Wrap(err, "failed to get http port")
}
srv, err := influxdb.NewService(c, httpPort, s.config.Hostname, vars.Info, s.config.HTTP.AuthEnabled, d)
if err != nil {
return err
}
w, err := s.newClusterIDChangedWaiter()
if err != nil {
return err
}
srv.ClusterIDWaiter = w
srv.HTTPDService = s.HTTPDService
srv.PointsWriter = s.TaskMaster
srv.AuthService = s.AuthService
srv.ClientCreator = iclient.ClientCreator{}
s.InfluxDBService = srv
s.TaskMaster.InfluxDBService = srv
s.SetDynamicService("influxdb", srv)
s.AppendService("influxdb", srv)
return nil
}
func (s *Server) initHTTPDService() {
d := s.DiagService.NewHTTPDHandler()
srv := httpd.NewService(s.config.HTTP, s.hostname, d)
srv.LocalHandler.PointsWriter = s.TaskMaster
srv.Handler.PointsWriter = s.TaskMaster
srv.LocalHandler.DiagService = s.DiagService
srv.Handler.DiagService = s.DiagService
srv.LocalHandler.Version = s.BuildInfo.Version
srv.Handler.Version = s.BuildInfo.Version
s.HTTPDService = srv
s.TaskMaster.HTTPDService = srv
}
func (s *Server) appendHTTPDService() {
s.AppendService("httpd", s.HTTPDService)
}
func (s *Server) appendTaskStoreService() {
d := s.DiagService.NewTaskStoreHandler()
srv := task_store.NewService(s.config.Task, d)
srv.StorageService = s.StorageService
srv.HTTPDService = s.HTTPDService
srv.TaskMasterLookup = s.TaskMasterLookup
s.TaskStore = srv
s.TaskMaster.TaskStore = srv
s.AppendService("task_store", srv)
}
func (s *Server) appendSessionService() {
srv := s.DiagService.SessionService
srv.HTTPDService = s.HTTPDService
s.AppendService("session", srv)
}
func (s *Server) appendReplayService() {
d := s.DiagService.NewReplayHandler()
srv := replay.NewService(s.config.Replay, d)
srv.StorageService = s.StorageService
srv.TaskStore = s.TaskStore
srv.HTTPDService = s.HTTPDService
srv.InfluxDBService = s.InfluxDBService
srv.TaskMaster = s.TaskMaster
srv.TaskMasterLookup = s.TaskMasterLookup
s.ReplayService = srv
s.AppendService("replay", srv)
}
func (s *Server) appendK8sService() error {
c := s.config.Kubernetes
d := s.DiagService.NewK8sHandler()
srv, err := k8s.NewService(c, s.ScraperService, d)
if err != nil {
return err
}
s.TaskMaster.K8sService = srv
s.SetDynamicService("kubernetes", srv)
s.AppendService("kubernetes", srv)
return nil
}
func (s *Server) appendSwarmService() error {
c := s.config.Swarm
d := s.DiagService.NewSwarmHandler()
srv, err := swarm.NewService(c, d)
if err != nil {
return err
}
s.TaskMaster.SwarmService = srv
s.SetDynamicService("swarm", srv)
s.AppendService("swarm", srv)
return nil
}
func (s *Server) appendEC2Service() error {
c := s.config.EC2
d := s.DiagService.NewEC2Handler()
srv, err := ec2.NewService(c, s.ScraperService, d)
if err != nil {
return err
}
s.TaskMaster.EC2Service = srv
s.SetDynamicService("ec2", srv)
s.AppendService("ec2", srv)
return nil
}
func (s *Server) appendDeadmanService() {
d := s.DiagService.NewDeadmanHandler()
srv := deadman.NewService(s.config.Deadman, d)
s.TaskMaster.DeadmanService = srv
s.AppendService("deadman", srv)
}
func (s *Server) appendUDFService() {
d := s.DiagService.NewUDFServiceHandler()
srv := udf.NewService(s.config.UDF, d)
s.TaskMaster.UDFService = srv
s.AppendService("udf", srv)
}
func (s *Server) appendAuthService() {
d := s.DiagService.NewNoAuthHandler()
srv := noauth.NewService(d)
s.AuthService = srv
s.HTTPDService.Handler.AuthService = srv
s.AppendService("auth", srv)
}
func (s *Server) appendMQTTService() error {
cs := s.config.MQTT
d := s.DiagService.NewMQTTHandler()
srv, err := mqtt.NewService(cs, d)
if err != nil {
return err
}
s.TaskMaster.MQTTService = srv
s.AlertService.MQTTService = srv
s.SetDynamicService("mqtt", srv)
s.AppendService("mqtt", srv)
return nil
}
func (s *Server) appendOpsGenieService() {
c := s.config.OpsGenie
d := s.DiagService.NewOpsGenieHandler()
srv := opsgenie.NewService(c, d)
s.TaskMaster.OpsGenieService = srv
s.AlertService.OpsGenieService = srv
s.SetDynamicService("opsgenie", srv)
s.AppendService("opsgenie", srv)
}
func (s *Server) appendOpsGenie2Service() {
c := s.config.OpsGenie2
d := s.DiagService.NewOpsGenie2Handler()
srv := opsgenie2.NewService(c, d)
s.TaskMaster.OpsGenie2Service = srv
s.AlertService.OpsGenie2Service = srv
s.SetDynamicService("opsgenie2", srv)
s.AppendService("opsgenie2", srv)
}
func (s *Server) appendVictorOpsService() {
c := s.config.VictorOps
d := s.DiagService.NewVictorOpsHandler()
srv := victorops.NewService(c, d)
s.TaskMaster.VictorOpsService = srv
s.AlertService.VictorOpsService = srv
s.SetDynamicService("victorops", srv)
s.AppendService("victorops", srv)
}
func (s *Server) appendPagerDutyService() {
c := s.config.PagerDuty
d := s.DiagService.NewPagerDutyHandler()
srv := pagerduty.NewService(c, d)
srv.HTTPDService = s.HTTPDService
s.TaskMaster.PagerDutyService = srv
s.AlertService.PagerDutyService = srv
s.SetDynamicService("pagerduty", srv)
s.AppendService("pagerduty", srv)
}
func (s *Server) appendPagerDuty2Service() {
c := s.config.PagerDuty2
d := s.DiagService.NewPagerDuty2Handler()
srv := pagerduty2.NewService(c, d)
srv.HTTPDService = s.HTTPDService
s.TaskMaster.PagerDuty2Service = srv
s.AlertService.PagerDuty2Service = srv
s.SetDynamicService("pagerduty2", srv)
s.AppendService("pagerduty2", srv)
}
func (s *Server) appendPushoverService() {
c := s.config.Pushover
d := s.DiagService.NewPushoverHandler()
srv := pushover.NewService(c, d)
s.TaskMaster.PushoverService = srv
s.AlertService.PushoverService = srv
s.SetDynamicService("pushover", srv)
s.AppendService("pushover", srv)
}
func (s *Server) appendHTTPPostService() error {
c := s.config.HTTPPost
d := s.DiagService.NewHTTPPostHandler()
srv, err := httppost.NewService(c, d)
if err != nil {
return err
}
s.TaskMaster.HTTPPostService = srv
s.AlertService.HTTPPostService = srv
s.SetDynamicService("httppost", srv)
s.AppendService("httppost", srv)
return nil
}
func (s *Server) appendSensuService() {
c := s.config.Sensu
d := s.DiagService.NewSensuHandler()
srv := sensu.NewService(c, d)
s.TaskMaster.SensuService = srv
s.AlertService.SensuService = srv
s.SetDynamicService("sensu", srv)
s.AppendService("sensu", srv)
}
func (s *Server) appendSlackService() error {
c := s.config.Slack
d := s.DiagService.NewSlackHandler()
srv, err := slack.NewService(c, d)
if err != nil {
return err
}
s.TaskMaster.SlackService = srv
s.AlertService.SlackService = srv
s.SetDynamicService("slack", srv)
s.AppendService("slack", srv)
return nil
}
func (s *Server) appendSNMPTrapService() {
c := s.config.SNMPTrap
d := s.DiagService.NewSNMPTrapHandler()
srv := snmptrap.NewService(c, d)
s.TaskMaster.SNMPTrapService = srv
s.AlertService.SNMPTrapService = srv
s.SetDynamicService("snmptrap", srv)
s.AppendService("snmptrap", srv)
}
func (s *Server) appendTelegramService() {
c := s.config.Telegram
d := s.DiagService.NewTelegramHandler()
srv := telegram.NewService(c, d)
s.TaskMaster.TelegramService = srv
s.AlertService.TelegramService = srv
s.SetDynamicService("telegram", srv)
s.AppendService("telegram", srv)
}
func (s *Server) appendHipChatService() {
c := s.config.HipChat
d := s.DiagService.NewHipChatHandler()
srv := hipchat.NewService(c, d)
s.TaskMaster.HipChatService = srv
s.AlertService.HipChatService = srv
s.SetDynamicService("hipchat", srv)
s.AppendService("hipchat", srv)
}
func (s *Server) appendKafkaService() {
c := s.config.Kafka
d := s.DiagService.NewKafkaHandler()
srv := kafka.NewService(c, d)
s.TaskMaster.KafkaService = srv
s.AlertService.KafkaService = srv
s.SetDynamicService("kafka", srv)
s.AppendService("kafka", srv)
}
func (s *Server) appendAlertaService() {
c := s.config.Alerta
d := s.DiagService.NewAlertaHandler()
srv := alerta.NewService(c, d)
s.TaskMaster.AlertaService = srv
s.AlertService.AlertaService = srv
s.SetDynamicService("alerta", srv)
s.AppendService("alerta", srv)
}
func (s *Server) appendTalkService() {
c := s.config.Talk
d := s.DiagService.NewTalkHandler()
srv := talk.NewService(c, d)
s.TaskMaster.TalkService = srv
s.AlertService.TalkService = srv
s.SetDynamicService("talk", srv)
s.AppendService("talk", srv)
}
func (s *Server) appendCollectdService() error {
c := s.config.Collectd
if !c.Enabled {
return nil
}
srv := collectd.NewService(c)
w, err := s.DiagService.NewStaticLevelHandler("info", "collectd")
if err != nil {
return fmt.Errorf("failed to create static level handler for collectd: %v", err)
}
srv.SetLogOutput(w)
srv.MetaClient = s.MetaClient
srv.PointsWriter = s.TaskMaster
s.AppendService("collectd", srv)
return nil
}
func (s *Server) appendOpenTSDBService() error {
c := s.config.OpenTSDB
if !c.Enabled {
return nil
}
srv, err := opentsdb.NewService(c)
if err != nil {
return err
}
w, err := s.DiagService.NewStaticLevelHandler("info", "opentsdb")
if err != nil {
return fmt.Errorf("failed to create static level handler for opentsdb: %v", err)
}
srv.SetLogOutput(w)
srv.PointsWriter = s.TaskMaster
srv.MetaClient = s.MetaClient
s.AppendService("opentsdb", srv)
return nil
}
func (s *Server) appendGraphiteServices() error {
for i, c := range s.config.Graphite {
if !c.Enabled {
continue
}
srv, err := graphite.NewService(c)
if err != nil {
return errors.Wrap(err, "creating new graphite service")
}
w, err := s.DiagService.NewStaticLevelHandler("info", "graphite")
if err != nil {
return fmt.Errorf("failed to create static level handler for graphite: %v", err)
}
srv.SetLogOutput(w)
srv.PointsWriter = s.TaskMaster
srv.MetaClient = s.MetaClient
s.AppendService(fmt.Sprintf("graphite%d", i), srv)
}
return nil
}
func (s *Server) appendUDPServices() {
for i, c := range s.config.UDP {
if !c.Enabled {
continue
}
d := s.DiagService.NewUDPHandler()
srv := udp.NewService(c, d)
srv.PointsWriter = s.TaskMaster
s.AppendService(fmt.Sprintf("udp%d", i), srv)
}
}
func (s *Server) appendStatsService() {
c := s.config.Stats
if c.Enabled {
d := s.DiagService.NewStatsHandler()
srv := stats.NewService(c, d)
srv.TaskMaster = s.TaskMaster
s.StatsService = srv
s.TaskMaster.TimingService = srv
s.AppendService("stats", srv)
}
}
func (s *Server) appendReportingService() {
c := s.config.Reporting
if c.Enabled {
d := s.DiagService.NewReportingHandler()
srv := reporting.NewService(c, vars.Info, d)
s.AppendService("reporting", srv)
}
}
func (s *Server) appendScraperService() {
c := s.config.Scraper
d := s.DiagService.NewScraperHandler()
srv := scraper.NewService(c, d)
srv.PointsWriter = s.TaskMaster
s.ScraperService = srv
s.SetDynamicService("scraper", srv)
s.AppendService("scraper", srv)
}
func (s *Server) appendAzureService() {
c := s.config.Azure
d := s.DiagService.NewAzureHandler()
srv := azure.NewService(c, s.ScraperService, d)
s.SetDynamicService("azure", srv)
s.AppendService("azure", srv)
}
func (s *Server) appendConsulService() {
c := s.config.Consul
d := s.DiagService.NewConsulHandler()
srv := consul.NewService(c, s.ScraperService, d)
s.SetDynamicService("consul", srv)
s.AppendService("consul", srv)
}
func (s *Server) appendDNSService() {
c := s.config.DNS
d := s.DiagService.NewDNSHandler()
srv := dns.NewService(c, s.ScraperService, d)
s.SetDynamicService("dns", srv)
s.AppendService("dns", srv)
}
func (s *Server) appendFileService() {
c := s.config.FileDiscovery
d := s.DiagService.NewFileDiscoveryHandler()
srv := file_discovery.NewService(c, s.ScraperService, d)
s.SetDynamicService("file-discovery", srv)
s.AppendService("file-discovery", srv)
}
func (s *Server) appendGCEService() {
c := s.config.GCE
d := s.DiagService.NewGCEHandler()
srv := gce.NewService(c, s.ScraperService, d)
s.SetDynamicService("gce", srv)
s.AppendService("gce", srv)
}
func (s *Server) appendMarathonService() {
c := s.config.Marathon
d := s.DiagService.NewMarathonHandler()
srv := marathon.NewService(c, s.ScraperService, d)
s.SetDynamicService("marathon", srv)
s.AppendService("marathon", srv)
}
func (s *Server) appendNerveService() {
c := s.config.Nerve
d := s.DiagService.NewNerveHandler()
srv := nerve.NewService(c, s.ScraperService, d)
s.SetDynamicService("nerve", srv)
s.AppendService("nerve", srv)
}
func (s *Server) appendServersetService() {
c := s.config.Serverset
d := s.DiagService.NewServersetHandler()
srv := serverset.NewService(c, s.ScraperService, d)
s.SetDynamicService("serverset", srv)
s.AppendService("serverset", srv)
}
func (s *Server) appendStaticService() {
c := s.config.StaticDiscovery
d := s.DiagService.NewStaticDiscoveryHandler()
srv := static_discovery.NewService(c, s.ScraperService, d)
s.SetDynamicService("static-discovery", srv)
s.AppendService("static-discovery", srv)
}
func (s *Server) appendTritonService() {
c := s.config.Triton
d := s.DiagService.NewTritonHandler()
srv := triton.NewService(c, s.ScraperService, d)
s.SetDynamicService("triton", srv)
s.AppendService("triton", srv)
}
// Err returns an error channel that multiplexes all out of band errors received from all services.
func (s *Server) Err() <-chan error { return s.err }
// Open opens all the services.
func (s *Server) Open() error {
// Start profiling, if set.
if err := s.startProfile(s.CPUProfile, s.MemProfile); err != nil {
return err
}
if err := s.startServices(); err != nil {
s.Close()
return err
}
if err := s.LoadService.Load(); err != nil {
return fmt.Errorf("failed to reload tasks/templates/handlers: %v", err)
}
go s.watchServices()
go s.watchConfigUpdates()
return nil
}
func (s *Server) startServices() error {
for _, service := range s.Services {
s.Diag.Debug("opening service", keyvalue.KV("service", fmt.Sprintf("%T", service)))
if err := service.Open(); err != nil {
return fmt.Errorf("open service %T: %s", service, err)
}
s.Diag.Debug("opened service", keyvalue.KV("service", fmt.Sprintf("%T", service)))
// Apply config overrides after the config override service has been opened and before any dynamic services.
if service == s.ConfigOverrideService && !s.config.SkipConfigOverrides && s.config.ConfigOverride.Enabled {
// Apply initial config updates
s.Diag.Debug("applying config overrides")
configs, err := s.ConfigOverrideService.Config()
if err != nil {
return errors.Wrap(err, "failed to apply config overrides")
}