forked from Juniper/jtimon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
137 lines (117 loc) · 3.19 KB
/
config.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
/*
* Copyright (c) 2018, Juniper Networks, Inc.
* All rights reserved.
*/
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"os"
)
// Config struct
type Config struct {
Port int `json:"port"`
Host string `json:"host"`
User string `json:"user"`
Password string `json:"password"`
CID string `json:"cid"`
Meta bool `json:"meta"`
EOS bool `json:"eos"`
API APIConfig `json:"api"`
GRPC GRPCConfig `json:"grpc"`
TLS TLSConfig `json:"tls"`
Influx InfluxConfig `json:"influx"`
Paths []PathsConfig `json:"paths"`
Log LogConfig `json:"log"`
}
//LogConfig is config struct for logging
type LogConfig struct {
File string `json:"file"`
PeriodicStats int `json:"periodic-stats"`
Verbose bool `json:"verbose"`
DropCheck bool `json:"drop-check"`
LatencyCheck bool `json:"latency-check"`
CSVStats bool `json:"csv-stats"`
FileHandle *os.File
Logger *log.Logger
}
// APIConfig is config struct for API Server
type APIConfig struct {
Port int `json:"port"`
}
//GRPCConfig is to specify GRPC params
type GRPCConfig struct {
WS int32 `json:"ws"`
}
// TLSConfig is to specify TLS params
type TLSConfig struct {
ClientCrt string `json:"clientcrt"`
ClientKey string `json:"clientkey"`
CA string `json:"ca"`
ServerName string `json:"servername"`
}
// PathsConfig to specify subscription path, reporting-interval (freq), etc,.
type PathsConfig struct {
Path string `json:"path"`
Freq uint64 `json:"freq"`
Mode string `json:"mode"`
}
// NewJTIMONConfig to return config object
func NewJTIMONConfig(file string) (Config, error) {
// parse config file
config, err := ParseJSON(file)
return config, err
}
func fillupDefaults(config *Config) {
// fill up defaults
if config.GRPC.WS == 0 {
config.GRPC.WS = DefaultGRPCWindowSize
}
if config.Influx.BatchFrequency == 0 {
config.Influx.BatchFrequency = DefaultIDBBatchFreq
}
if config.Influx.BatchSize == 0 {
config.Influx.BatchSize = DefaultIDBBatchSize
}
}
// ParseJSON parses JSON encoded config of JTIMON
func ParseJSON(file string) (Config, error) {
var config Config
f, err := ioutil.ReadFile(file)
if err != nil {
return config, err
}
if err := json.Unmarshal(f, &config); err != nil {
return config, err
}
fillupDefaults(&config)
if _, err := ValidateConfig(config); err != nil {
log.Fatalf("Invalid config %v\n", err)
}
return config, nil
}
// ValidateConfig for config validation
func ValidateConfig(config Config) (string, error) {
b, err := json.MarshalIndent(config, "", " ")
if err != nil {
return "", err
}
return string(b), nil
}
// ExploreConfig of JTIMON
func ExploreConfig() (string, error) {
var config Config
c := "{\"paths\": [{}]}"
if err := json.Unmarshal([]byte(c), &config); err == nil {
if b, err := json.MarshalIndent(config, "", " "); err == nil {
return string(b), nil
}
}
return "", errors.New("Something is very wrong - This should have not happened")
}
// IsVerboseLogging returns true if verbose logging is enabled, false otherwise
func IsVerboseLogging(jctx *JCtx) bool {
return jctx.config.Log.Verbose
}