forked from gaochao1/swcollector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg.go
159 lines (133 loc) · 4.04 KB
/
cfg.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
package g
import (
"encoding/json"
"log"
"sync"
"github.com/toolkits/file"
)
type DebugmetricConfig struct {
Endpoints []string `json:"endpoints`
Metrics []string `json:"metrics`
Tags string `json:"tags"`
}
type SwitchConfig struct {
Enabled bool `json:"enabled"`
IpRange []string `json:"ipRange"`
IndexTag bool `json:"index_tag"`
Gosnmp bool `json:"gosnmp"`
PingTimeout int `json:"pingTimeout"`
PingRetry int `json:"pingRetry"`
Community string `json:"community"`
SnmpTimeout int `json:"snmpTimeout"`
SnmpRetry int `json:"snmpRetry"`
IgnoreIface []string `json:"ignoreIface"`
IgnoreOperStatus bool `json:"ignoreOperStatus"`
Speedlimit float64 `json:"speedlimit"`
IgnorePkt bool `json:"ignorePkt"`
Pktlimit float64 `json:"pktlimit"`
IgnoreBroadcastPkt bool `json:"ignoreBroadcastPkt"`
BroadcastPktlimit float64 `josn:"broadcastPktlimit"`
IgnoreMulticastPkt bool `json:"ignoreMulticastPkt"`
MulticastPktlimit float64 `json:"multicastPktlimit"`
IgnoreDiscards bool `json:"ignoreDiscards"`
DiscardsPktlimit float64 `json:"discardsPktlimit"`
IgnoreErrors bool `json:"ignoreErrors"`
ErrorsPktlimit float64 `json:"errorsPktlimit"`
IgnoreUnknownProtos bool `json:"ignoreUnknownProtos`
UnknownProtosPktlimit float64 `json:"unknownProtosPktlimit"`
IgnoreOutQLen bool `json:"ignoreOutQLen`
OutQLenPktlimit float64 `json:"outQLenPktlimit"`
LimitCon int `json:limitCon`
LimitConcur int `json:"limitConcur"`
FastPingMode bool `json:"fastPingMode"`
}
type TransferConfig struct {
Enabled bool `json:"enabled"`
N9eMode bool `json:"n9eMode"`
Addr string `json:"addr"`
Interval int `json:"interval"`
Timeout int `json:"timeout"`
}
type HttpConfig struct {
Enabled bool `json:"enabled"`
Listen string `json:"listen"`
TrustIps []string `json:trustIps`
}
type SwitchHostsConfig struct {
Enabled bool `json:enabled`
Hosts string `json:hosts`
}
type CustomMetricsConfig struct {
Enabled bool `json:enbaled`
Template string `json:template`
}
type EcmcConfig struct {
Enabled bool `json:"enabled"`
Addr string `json:"addr"`
Token string `json:"token"`
Nodes []int64 `json:"nodes"`
}
type N9eConfig struct {
Enabled bool `json:"enabled"`
Addr string `json:"addr"`
User string `json:"user"`
Pass string `json:"pass"`
Nodes []int64 `json:"nodes"`
}
type GlobalConfig struct {
Debug bool `json:"debug"`
Debugmetric *DebugmetricConfig `json:"debugmetric`
Switch *SwitchConfig `json:"switch"`
Ecmc EcmcConfig `json:"ecmc"`
N9e N9eConfig `json:"n9e"`
Transfer *TransferConfig `json:"transfer"`
SwitchHosts *SwitchHostsConfig `json:switchhosts`
CustomMetrics *CustomMetricsConfig `json:customMetrics`
Http *HttpConfig `json:"http"`
}
var (
ConfigFile string
config *GlobalConfig
reloadType bool
lock = new(sync.RWMutex)
rlock = new(sync.RWMutex)
)
func SetReloadType(t bool) {
rlock.RLock()
defer rlock.RUnlock()
reloadType = t
return
}
func ReloadType() bool {
rlock.RLock()
defer rlock.RUnlock()
return reloadType
}
func Config() *GlobalConfig {
lock.RLock()
defer lock.RUnlock()
return config
}
func ParseConfig(cfg string) {
if cfg == "" {
log.Fatalln("use -c to specify configuration file")
}
if !file.IsExist(cfg) {
log.Fatalln("config file:", cfg, "is not existent. maybe you need `mv cfg.example.json cfg.json`")
}
ConfigFile = cfg
configContent, err := file.ToTrimString(cfg)
if err != nil {
log.Fatalln("read config file:", cfg, "fail:", err)
}
var c GlobalConfig
err = json.Unmarshal([]byte(configContent), &c)
if err != nil {
log.Fatalln("parse config file:", cfg, "fail:", err)
}
lock.Lock()
defer lock.Unlock()
config = &c
SetReloadType(false)
log.Println("read config file:", cfg, "successfully")
}