forked from intelsdi-x/snap-plugin-lib-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
985 lines (883 loc) · 27.6 KB
/
plugin.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
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2016 Intel Corporation
Licensed 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 plugin
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/urfave/cli"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"text/tabwriter"
log "github.com/Sirupsen/logrus"
"github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin/rpc"
)
var (
app *cli.App
appArgs struct {
plugin Plugin
name string
version int
opts []MetaOpt
}
// Flags required by the plugin lib flags - plugin authors can provide their
// own flags. Checkout https://github.com/intelsdi-x/snap-plugin-lib-go/blob/master/examples/snap-plugin-collector-rand/rand/rand.go
// for an example of a plugin adding a custom flag.
Flags []cli.Flag = []cli.Flag{
flConfig,
flAddr,
flPort,
flPprof,
flTLS,
flCertPath,
flKeyPath,
flRootCertPaths,
flStandAlone,
flHTTPPort,
flLogLevel,
flMaxCollectDuration,
flMaxMetricsBuffer,
}
)
// Plugin is the base plugin type. All plugins must implement GetConfigPolicy.
type Plugin interface {
GetConfigPolicy() (ConfigPolicy, error)
}
// Collector is a plugin which is the source of new data in the Snap pipeline.
type Collector interface {
Plugin
GetMetricTypes(Config) ([]Metric, error)
CollectMetrics([]Metric) ([]Metric, error)
}
// Processor is a plugin which filters, aggregates, or decorates data in the
// Snap pipeline.
type Processor interface {
Plugin
Process([]Metric, Config) ([]Metric, error)
}
// Publisher is a sink in the Snap pipeline. It publishes data into another
// System, completing a Workflow path.
type Publisher interface {
Plugin
Publish([]Metric, Config) error
}
var App *cli.App
/*
StreamCollector is a Collector that can send back metrics within configurable limits defined in task manifest.
These limits might be determined by user by set a value of:
- `max-metrics-buffer`, default to 0 what means no buffering and sending reply with streaming metrics immediately
- `max-collect-duration`, default to 10s what means after 10s no new metrics are received, send a reply whatever data it has
in buffer instead of waiting longer
*/
type StreamCollector interface {
Plugin
// StreamMetrics allows the plugin to send/receive metrics on a channel
// Arguments are (in order):
//
// A channel for metrics into the plugin from Snap -- which
// are the metric types snap is requesting the plugin to collect.
//
// A channel for metrics from the plugin to Snap -- the actual
// collected metrics from the plugin.
//
// A channel for error strings that the library will report to snap
// as task errors.
StreamMetrics(context.Context, chan []Metric, chan []Metric, chan string) error
GetMetricTypes(Config) ([]Metric, error)
}
var getOSArgs = func() []string { return os.Args }
// tlsServerSetup offers functions supporting TLS server setup
type tlsServerSetup interface {
// makeTLSConfig delivers TLS config suitable to use for plugins, excluding
// setup of certificates (either subject or root CA certificates).
makeTLSConfig() *tls.Config
// readRootCAs is a function that delivers root CA certificates for the purpose
// of TLS initialization
readRootCAs(rootCertPaths string) (*x509.CertPool, error)
// updateServerOptions configures any additional options for GRPC server
updateServerOptions(options ...grpc.ServerOption) []grpc.ServerOption
}
// osInputOutput supports interactions with OS for the plugin lib
type OSInputOutput interface {
// readOSArgs gets command line arguments passed to application
readOSArg() string
// printOut outputs given data to application standard output
printOut(data string)
args() int
setContext(c *cli.Context)
}
// standardInputOutput delivers standard implementation for OS
// interactions
type standardInputOutput struct {
context *cli.Context
}
// libInputOutput holds utility used for OS interactions
var libInputOutput OSInputOutput = &standardInputOutput{}
// readOSArgs implementation that returns application args passed by OS
func (io *standardInputOutput) readOSArg() string {
if io.context != nil {
return io.context.Args().First()
}
if len(os.Args) > 0 {
return os.Args[0]
}
return ""
}
// printOut implementation that emits data into standard output
func (io *standardInputOutput) printOut(data string) {
fmt.Println(data)
}
func (io *standardInputOutput) setContext(c *cli.Context) {
io.context = c
}
func (io *standardInputOutput) args() int {
if io.context != nil {
return io.context.NArg()
}
return 0
}
// tlsServerDefaultSetup provides default implementation for TLS setup routines
type tlsServerDefaultSetup struct {
}
// tlsSetup holds TLS setup utility for plugin lib
var tlsSetup tlsServerSetup = tlsServerDefaultSetup{}
// makeTLSConfig provides TLS configuraton template for plugins, setting
// required verification of client cert and preferred server suites.
func (ts tlsServerDefaultSetup) makeTLSConfig() *tls.Config {
config := tls.Config{
ClientAuth: tls.RequireAndVerifyClientCert,
PreferServerCipherSuites: true,
CipherSuites: []uint16{
tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
},
}
return &config
}
// readRootCAs delivers a standard source of root CAs from system
func (ts tlsServerDefaultSetup) readRootCAs(rootCertPaths string) (*x509.CertPool, error) {
if rootCertPaths == "" {
return x509.SystemCertPool()
}
certPaths := filepath.SplitList(rootCertPaths)
return ts.loadRootCerts(certPaths)
}
// updateServerOptions a standard implementation delivers no additional options
func (ts tlsServerDefaultSetup) updateServerOptions(options ...grpc.ServerOption) []grpc.ServerOption {
return options
}
func (ts tlsServerDefaultSetup) loadRootCerts(certPaths []string) (rootCAs *x509.CertPool, err error) {
var path string
var filepaths []string
// list potential certificate files
for _, path := range certPaths {
var stat os.FileInfo
if stat, err = os.Stat(path); err != nil {
return nil, fmt.Errorf("unable to process CA cert source path %s: %v", path, err)
}
if !stat.IsDir() {
filepaths = append(filepaths, path)
continue
}
var subfiles []os.FileInfo
if subfiles, err = ioutil.ReadDir(path); err != nil {
return nil, fmt.Errorf("unable to process CA cert source directory %s: %v", path, err)
}
for _, subfile := range subfiles {
subpath := filepath.Join(path, subfile.Name())
if subfile.IsDir() {
log.WithField("path", subpath).Debug("Skipping second level directory found among certificate files")
continue
}
filepaths = append(filepaths, subpath)
}
}
rootCAs = x509.NewCertPool()
numread := 0
for _, path = range filepaths {
b, err := ioutil.ReadFile(path)
if err != nil {
log.WithFields(log.Fields{"path": path, "error": err}).Debug("Unable to read cert file")
continue
}
if !rootCAs.AppendCertsFromPEM(b) {
log.WithField("path", path).Debug("Didn't find any usable certificates in cert file")
continue
}
numread++
}
if numread == 0 {
return nil, fmt.Errorf("found no usable certificates in given locations")
}
return rootCAs, nil
}
// makeGRPCCredentials delivers credentials object suitable for setting up gRPC
// server, with TLS optionally turned on.
func makeGRPCCredentials(m *meta) (creds credentials.TransportCredentials, err error) {
var config *tls.Config
if !m.TLSEnabled {
config = &tls.Config{
InsecureSkipVerify: true,
}
} else {
cert, err := tls.LoadX509KeyPair(m.CertPath, m.KeyPath)
if err != nil {
return nil, fmt.Errorf("unable to setup credentials for plugin - loading key pair failed: %v", err.Error())
}
config = tlsSetup.makeTLSConfig()
config.Certificates = []tls.Certificate{cert}
if config.ClientCAs, err = tlsSetup.readRootCAs(m.RootCertPaths); err != nil {
return nil, fmt.Errorf("unable to read root CAs: %v", err.Error())
}
}
creds = credentials.NewTLS(config)
return creds, nil
}
// applySecurityArgsToMeta validates plugin runtime arguments from OS, focusing on
// TLS functionality.
func applySecurityArgsToMeta(m *meta, args *Arg) error {
if !args.TLSEnabled {
if args.CertPath != "" || args.KeyPath != "" {
return fmt.Errorf("excessive arguments given - CertPath and KeyPath are unused with TLS not enabled")
}
return nil
}
if args.CertPath == "" || args.KeyPath == "" {
return fmt.Errorf("failed to enable TLS for plugin - need both CertPath and KeyPath")
}
m.CertPath = args.CertPath
m.KeyPath = args.KeyPath
m.TLSEnabled = true
m.RootCertPaths = args.RootCertPaths
return nil
}
// buildGRPCServer configures and builds GRPC server ready to server a plugin
// instance
func buildGRPCServer(typeOfPlugin pluginType, name string, version int, arg *Arg, opts ...MetaOpt) (server *grpc.Server, m *meta, err error) {
m = newMeta(typeOfPlugin, name, version, opts...)
if err := applySecurityArgsToMeta(m, arg); err != nil {
return nil, nil, err
}
creds, err := makeGRPCCredentials(m)
if err != nil {
return nil, nil, err
}
if m.TLSEnabled {
server = grpc.NewServer(tlsSetup.updateServerOptions(grpc.Creds(creds))...)
} else {
server = grpc.NewServer(tlsSetup.updateServerOptions()...)
}
return server, m, nil
}
// StartCollector is given a Collector implementation and its metadata,
// generates a response for the initial stdin / stdout handshake, and starts
// the plugin's gRPC server.
func StartCollector(plugin Collector, name string, version int, opts ...MetaOpt) int {
app = cli.NewApp()
app.Flags = Flags
app.Action = startPlugin
appArgs.plugin = plugin
appArgs.name = name
appArgs.version = version
appArgs.opts = opts
app.Version = strconv.Itoa(version)
app.Usage = "a Snap collector"
err := app.Run(getOSArgs())
if err != nil {
log.WithFields(log.Fields{
"_block": "StartCollector",
}).Error(err)
return 1
}
return 0
}
// StartProcessor is given a Processor implementation and its metadata,
// generates a response for the initial stdin / stdout handshake, and starts
// the plugin's gRPC server.
func StartProcessor(plugin Processor, name string, version int, opts ...MetaOpt) int {
app = cli.NewApp()
app.Flags = Flags
app.Action = startPlugin
appArgs.plugin = plugin
appArgs.name = name
appArgs.version = version
appArgs.opts = opts
app.Version = strconv.Itoa(version)
app.Usage = "a Snap processor"
err := app.Run(getOSArgs())
if err != nil {
log.WithFields(log.Fields{
"_block": "StartProcessor",
}).Error(err)
return 1
}
return 0
}
// StartPublisher is given a Publisher implementation and its metadata,
// generates a response for the initial stdin / stdout handshake, and starts
// the plugin's gRPC server.
func StartPublisher(plugin Publisher, name string, version int, opts ...MetaOpt) int {
app = cli.NewApp()
app.Flags = Flags
app.Action = startPlugin
appArgs.plugin = plugin
appArgs.name = name
appArgs.version = version
appArgs.opts = opts
app.Version = strconv.Itoa(version)
app.Usage = "a Snap publisher"
err := app.Run(getOSArgs())
if err != nil {
log.WithFields(log.Fields{
"_block": "StartPublisher",
}).Error(err)
return 1
}
return 0
}
// StartStreamCollector is given a StreamCollector implementation and its metadata,
// generates a response for the initial stdin / stdout handshake, and starts
// the plugin's gRPC server.
func StartStreamCollector(plugin StreamCollector, name string, version int, opts ...MetaOpt) int {
app = cli.NewApp()
app.Flags = Flags
app.Action = startPlugin
appArgs.plugin = plugin
appArgs.name = name
appArgs.version = version
//set gRPCStream as RPC type
opts = append(opts, rpcType(gRPCStream))
appArgs.opts = opts
app.Version = strconv.Itoa(version)
app.Usage = "a Snap collector"
err := app.Run(getOSArgs())
if err != nil {
log.WithFields(log.Fields{
"_block": "StartStreamCollector",
}).Error(err)
return 1
}
return 0
}
type server interface {
Serve(net.Listener) error
}
type preamble struct {
Meta meta
ListenAddress string
PprofAddress string
Type pluginType
State int
ErrorMessage string
}
func startPlugin(c *cli.Context) error {
var (
server *grpc.Server
meta *meta
pluginProxy *pluginProxy
MaxMetricsBuffer int64
)
libInputOutput.setContext(c)
arg, err := processInput(c)
if err != nil {
return err
}
if lvl := c.Int("log-level"); lvl > arg.LogLevel {
log.SetLevel(log.Level(lvl))
} else {
log.SetLevel(log.Level(arg.LogLevel))
}
logger := log.WithFields(
log.Fields{
"_block": "startPlugin",
})
switch plugin := appArgs.plugin.(type) {
case Collector:
proxy := &collectorProxy{
plugin: plugin,
pluginProxy: *newPluginProxy(plugin),
}
pluginProxy = &proxy.pluginProxy
server, meta, err = buildGRPCServer(collectorType, appArgs.name, appArgs.version, arg, appArgs.opts...)
if err != nil {
return cli.NewExitError(err, 2)
}
rpc.RegisterCollectorServer(server, proxy)
case Processor:
proxy := &processorProxy{
plugin: plugin,
pluginProxy: *newPluginProxy(plugin),
}
pluginProxy = &proxy.pluginProxy
server, meta, err = buildGRPCServer(processorType, appArgs.name, appArgs.version, arg, appArgs.opts...)
if err != nil {
return cli.NewExitError(err, 2)
}
rpc.RegisterProcessorServer(server, proxy)
case Publisher:
proxy := &publisherProxy{
plugin: plugin,
pluginProxy: *newPluginProxy(plugin),
}
pluginProxy = &proxy.pluginProxy
server, meta, err = buildGRPCServer(publisherType, appArgs.name, appArgs.version, arg, appArgs.opts...)
if err != nil {
return cli.NewExitError(err, 2)
}
rpc.RegisterPublisherServer(server, proxy)
case StreamCollector:
if c.IsSet("max-metrics-buffer") {
MaxMetricsBuffer = c.Int64("max-metrics-buffer")
} else {
MaxMetricsBuffer = defaultMaxMetricsBuffer
}
logger.WithFields(log.Fields{
"option": "max-metrics-buffer",
"value": MaxMetricsBuffer,
}).Debug("setting max metrics buffer")
maxCollectDuration, err := time.ParseDuration(collectDurationStr)
if err != nil {
return err
}
logger.WithFields(log.Fields{
"option": "max-collect-duration",
"value": maxCollectDuration,
}).Debug("setting max collect duration")
proxy := &StreamProxy{
plugin: plugin,
ctx: context.Background(),
pluginProxy: *newPluginProxy(plugin),
maxCollectDuration: maxCollectDuration,
maxMetricsBuffer: MaxMetricsBuffer,
}
pluginProxy = &proxy.pluginProxy
server, meta, err = buildGRPCServer(streamCollectorType, appArgs.name, appArgs.version, arg, appArgs.opts...)
if err != nil {
return cli.NewExitError(err, 2)
}
rpc.RegisterStreamCollectorServer(server, proxy)
default:
logger.WithField("type", fmt.Sprintf("%T", plugin)).Fatal("Unknown plugin type")
}
if c.Bool("stand-alone") {
httpPort := c.Int("stand-alone-port")
preamble, err := printPreambleAndServe(server, meta, pluginProxy, arg.ListenPort, arg.Pprof)
if err != nil {
return err
}
go func() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, preamble)
})
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", httpPort))
if err != nil {
log.WithFields(
log.Fields{
"port": httpPort,
},
).Fatal("Unable to get open port")
}
defer listener.Close()
fmt.Printf("Preamble URL: %v\n", listener.Addr().String())
err = http.Serve(listener, nil)
if err != nil {
log.Fatal(err)
}
}()
<-pluginProxy.halt
} else if libInputOutput.args() > 0 {
// snapteld is starting the plugin
// presumably with a single arg (valid json)
preamble, err := printPreambleAndServe(server, meta, pluginProxy, arg.ListenPort, arg.Pprof)
if err != nil {
log.Fatal(err)
}
libInputOutput.printOut(preamble)
go pluginProxy.HeartbeatWatch()
<-pluginProxy.halt
} else {
// no arguments provided - run and display diagnostics to the user
var config Config
if c.IsSet("config") {
err := json.Unmarshal([]byte(c.String("config")), &config)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("unable to parse config")
return err
}
}
switch pluginProxy.plugin.(type) {
case Collector:
return showDiagnostics(*meta, pluginProxy, config)
case StreamCollector:
fmt.Println("Diagnostics not currently available for streaming collector plugins.")
case Processor:
fmt.Println("Diagnostics not currently available for processor plugins.")
case Publisher:
fmt.Println("Diagnostics not currently available for publisher plugins.")
}
}
return nil
}
func printPreambleAndServe(srv server, m *meta, p *pluginProxy, port string, isPprof bool) (string, error) {
l, err := net.Listen("tcp", fmt.Sprintf("%s:%v", ListenAddr, port))
if err != nil {
return "", err
}
l.Close()
addr := fmt.Sprintf("%s:%v", ListenAddr, l.Addr().(*net.TCPAddr).Port)
lis, err := net.Listen("tcp", addr)
if err != nil {
return "", err
}
go func() {
err := srv.Serve(lis)
if err != nil {
log.Fatal(err)
}
}()
pprofAddr := "0"
if isPprof {
pprofAddr, err = startPprof()
if err != nil {
return "", err
}
}
advertisedAddr, err := getAddr(ListenAddr)
if err != nil {
return "", err
}
resp := preamble{
Meta: *m,
ListenAddress: fmt.Sprintf("%v:%v", advertisedAddr, l.Addr().(*net.TCPAddr).Port),
Type: m.Type,
PprofAddress: pprofAddr,
State: 0, // Hardcode success since panics on err
}
preambleJSON, err := json.Marshal(resp)
if err != nil {
return "", err
}
return string(preambleJSON), nil
}
// getAddr if we were provided the addr 0.0.0.0 we need to determine the
// address we will advertise to the framework in the preamble.
func getAddr(addr string) (string, error) {
if strings.Compare(addr, "0.0.0.0") == 0 {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", err
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String(), nil
}
}
}
}
return addr, nil
}
func showDiagnostics(m meta, p *pluginProxy, c Config) error {
defer timeTrack(time.Now(), "showDiagnostics")
printRuntimeDetails(m)
err := printConfigPolicy(p, c)
if err != nil {
return err
}
met, err := printMetricTypes(p, c)
if err != nil {
return err
}
err = printCollectMetrics(p, met)
if err != nil {
return err
}
printContactUs()
return nil
}
func printMetricTypes(p *pluginProxy, conf Config) ([]Metric, error) {
defer timeTrack(time.Now(), "printMetricTypes")
met, err := p.plugin.(Collector).GetMetricTypes(conf)
if err != nil {
return nil, fmt.Errorf("! Error in the call to GetMetricTypes: \n%v", err)
}
//apply any config passed in to met so that
//CollectMetrics can see the config for each metric
for i := range met {
met[i].Config = conf
}
fmt.Println("Metric catalog will be updated to include: ")
for _, j := range met {
fmt.Printf(" Namespace: %v \n", j.Namespace.String())
}
return met, nil
}
func printConfigPolicy(p *pluginProxy, conf Config) error {
defer timeTrack(time.Now(), "printConfigPolicy")
requiredConfigs := ""
cPolicy, err := p.plugin.(Collector).GetConfigPolicy()
if err != nil {
return err
}
fmt.Println("Config Policy:")
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
printFields(w, false, 0, "NAMESPACE", "KEY", "TYPE", "REQUIRED", "DEFAULT", "MINIMUM", "MAXIMUM")
requiredConfigs += printConfigPolicyStringRules(cPolicy, conf, w)
requiredConfigs += printConfigPolicyIntegerRules(cPolicy, conf, w)
requiredConfigs += printConfigPolicyFloatRules(cPolicy, conf, w)
requiredConfigs += printConfigPolicyBoolRules(cPolicy, conf, w)
w.Flush()
if requiredConfigs != "" {
requiredConfigs += "! Please provide config in form of: -config '{\"key\":\"kelly\", \"spirit-animal\":\"coatimundi\"}'\n"
err := fmt.Errorf(requiredConfigs)
return err
}
return nil
}
func printFields(tw *tabwriter.Writer, indent bool, width int, fields ...interface{}) {
var argArray []interface{}
if indent {
argArray = append(argArray, strings.Repeat(" ", width))
}
for i, field := range fields {
if field != nil {
argArray = append(argArray, field)
} else {
argArray = append(argArray, "")
}
if i < (len(fields) - 1) {
argArray = append(argArray, "\t")
}
}
fmt.Fprintln(tw, argArray...)
}
func stringInSlice(a string, list []string) (int, bool) {
for i, b := range list {
if b == a {
return i, true
}
}
return -1, false
}
func parseString(vals []string, name string, conf Config) (required string, minimum string, maximum string) {
//check if required
if _, okReq := stringInSlice("required:true", vals); okReq {
required = "true"
} else {
required = "false"
}
//check if has_min:true
if _, okMin := stringInSlice("has_min:true", vals); okMin {
//Check if minimum is specified, if not, default = 0
idxInArray := -1
valueAtIndex := ""
for i, b := range vals {
if strings.Contains(b, "minimum:") {
idxInArray = i
valueAtIndex = b
}
}
if idxInArray != -1 {
//parse val[idx] to get contents after :
idxOfColon := strings.Index(valueAtIndex, ":")
minimum = valueAtIndex[idxOfColon+1:]
} else {
minimum = "0"
}
}
//check if has_max:true
if _, okMax := stringInSlice("has_max:true", vals); okMax {
//Check if minimum is specified
idxInArray := -1
valueAtIndex := ""
for i, b := range vals {
if strings.Contains(b, "maximum:") {
idxInArray = i
valueAtIndex = b
}
}
if idxInArray != -1 {
//parse val[idx] to get contents after :
idxOfColon := strings.Index(valueAtIndex, ":")
maximum = valueAtIndex[idxOfColon+1:]
} else {
//No default max value
}
}
return required, minimum, maximum
}
func checkForMissingRequirements(vals []string, name string, conf Config) (requiredConfigs string) {
if _, okReq := stringInSlice("required:true", vals); okReq {
if _, ok := conf[name]; !ok {
requiredConfigs += "! Warning: \"" + name + "\" required by plugin and not provided in config \n"
}
}
return requiredConfigs
}
func printConfigPolicyStringRules(cPolicy ConfigPolicy, conf Config, w *tabwriter.Writer) (requiredConfigs string) {
if len(cPolicy.stringRules) > 0 {
for ns, v := range cPolicy.stringRules {
for key, val := range v.Rules {
defaultValue := ""
if val.HasDefault {
defaultValue = val.Default
}
if val.String() != "" {
vals := strings.Fields(val.String())
req, min, max := parseString(vals, ns, conf)
printFields(w, false, 0, ns, key, "string", req, defaultValue, min, max)
requiredConfigs += checkForMissingRequirements(vals, key, conf)
} else {
printFields(w, false, 0, ns, key, "string", "false", defaultValue, "", "")
}
}
}
}
return requiredConfigs
}
func printConfigPolicyIntegerRules(cPolicy ConfigPolicy, conf Config, w *tabwriter.Writer) (requiredConfigs string) {
if len(cPolicy.integerRules) > 0 {
for k, v := range cPolicy.integerRules {
for key, val := range v.Rules {
defaultValue := ""
if val.HasDefault {
defaultValue = strconv.FormatInt(val.Default, 10)
}
if val.String() != "" {
//parse info:
vals := strings.Fields(val.String())
req, min, max := parseString(vals, k, conf)
printFields(w, false, 0, k, key, "integer", req, defaultValue, min, max)
requiredConfigs += checkForMissingRequirements(vals, key, conf)
} else {
printFields(w, false, 0, k, key, "integer", "false", defaultValue, "", "")
}
}
}
}
return requiredConfigs
}
func printConfigPolicyFloatRules(cPolicy ConfigPolicy, conf Config, w *tabwriter.Writer) (requiredConfigs string) {
if len(cPolicy.floatRules) > 0 {
for k, v := range cPolicy.floatRules {
for key, val := range v.Rules {
defaultValue := ""
if val.HasDefault {
defaultValue = strconv.FormatFloat(val.Default, 'f', -1, 64)
}
if val.String() != "" {
//parse info:
vals := strings.Fields(val.String())
req, min, max := parseString(vals, k, conf)
printFields(w, false, 0, k, key, "float", req, defaultValue, min, max)
requiredConfigs += checkForMissingRequirements(vals, key, conf)
} else {
printFields(w, false, 0, k, key, "float", "false", defaultValue, "", "")
}
}
}
}
return requiredConfigs
}
func printConfigPolicyBoolRules(cPolicy ConfigPolicy, conf Config, w *tabwriter.Writer) (requiredConfigs string) {
if len(cPolicy.boolRules) > 0 {
for k, v := range cPolicy.boolRules {
for key, val := range v.Rules {
defaultValue := ""
if val.HasDefault {
defaultValue = strconv.FormatBool(val.Default)
}
if val.String() != "" {
//parse info:
vals := strings.Fields(val.String())
req, min, max := parseString(vals, k, conf)
printFields(w, false, 0, k, key, "bool", req, defaultValue, min, max)
requiredConfigs += checkForMissingRequirements(vals, key, conf)
} else {
printFields(w, false, 0, k, key, "bool", "false", defaultValue, "", "")
}
}
}
}
return requiredConfigs
}
func printCollectMetrics(p *pluginProxy, m []Metric) error {
defer timeTrack(time.Now(), "printCollectMetrics")
cltd, err := p.plugin.(Collector).CollectMetrics(m)
if err != nil {
return fmt.Errorf("! Error in the call to CollectMetrics. Please ensure your config contains any required fields mentioned in the error below. \n %v", err)
}
fmt.Println("Metrics that can be collected right now are: ")
for _, j := range cltd {
fmt.Printf(" Namespace: %-30v Type: %-10T Value: %v \n", j.Namespace, j.Data, j.Data)
}
return nil
}
func printRuntimeDetails(m meta) {
defer timeTrack(time.Now(), "printRuntimeDetails")
fmt.Printf("Runtime Details:\n PluginName: %v, Version: %v \n RPC Type: %v, RPC Version: %v \n", m.Name, m.Version, m.RPCType.String(), m.RPCVersion)
fmt.Printf(" Operating system: %v \n Architecture: %v \n Go version: %v \n", runtime.GOOS, runtime.GOARCH, runtime.Version())
}
func printContactUs() {
fmt.Print("Thank you for using this Snap plugin. If you have questions or are running \ninto errors, please contact us on Github (github.com/intelsdi-x/snap) or \nour Slack channel (intelsdi-x.herokuapp.com). \nThe repo for this plugin can be found: github.com/intelsdi-x/<plugin-name>. \nWhen submitting a new issue on Github, please include this diagnostic \nprint out so that we have a starting point for addressing your question. \nThank you. \n\n")
}
func timeTrack(start time.Time, name string) {
elapsed := time.Since(start)
fmt.Printf("%s took %s \n\n", name, elapsed)
}
func processInput(c *cli.Context) (*Arg, error) {
arg := &Arg{}
if c.IsSet("log-level") {
arg.LogLevel = c.Int("log-level")
}
if c.IsSet("port") {
arg.ListenPort = c.String("port")
}
if c.IsSet("pprof") {
arg.Pprof = c.Bool("pprof")
}
if c.IsSet("cert-path") {
arg.CertPath = c.String("cert-path")
}
if c.IsSet("key-path") {
arg.KeyPath = c.String("key-path")
}
if c.IsSet("root-cert-paths") {
arg.RootCertPaths = c.String("root-cert-paths")
}
if c.IsSet("tls") {
arg.TLSEnabled = true
}
if c.IsSet("max-collect-duration") {
arg.MaxCollectDuration = c.String("max-collect-duration")
}
if c.IsSet("max-metrics-buffer") {
arg.MaxMetricsBuffer = c.Int64("max-metrics-buffer")
}
return processArg(arg)
}