forked from cespare/gost
/
conf.go
203 lines (182 loc) · 5.25 KB
/
conf.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
package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/BurntSushi/toml"
)
type Conf struct {
GraphiteAddr string `toml:"graphite_addr"`
ForwardingAddr string `toml:"forwarding_addr"`
ForwarderListenAddr string `toml:"forwarder_listen_addr"`
ForwardedNamespace string `toml:"forwarded_namespace"`
Port int `toml:"port"`
DebugPort int `toml:"debug_port"`
DebugLogging bool `toml:"debug_logging"`
ClearStatsBetweenFlushes bool `toml:"clear_stats_between_flushes"`
FlushIntervalMS int `toml:"flush_interval_ms"`
Namespace string `toml:"namespace"`
OSStats *OSStatsConf `toml:"os_stats"`
Scripts *ScriptsConf `toml:"scripts"`
forwardingEnabled bool
forwarderEnabled bool
}
type ScriptsConf struct {
Path string `toml:"path"`
RunIntervalMS int `toml:"run_interval_ms"`
}
type OSStatsConf struct {
CheckIntervalMS int `toml:"check_interval_ms"`
Mem *MemConf `toml:"mem"`
CPU *CPUConf `toml:"cpu"`
Net *NetConf `toml:"net"`
Disk map[string]*DiskConf `toml:"disk"`
}
type MemConf struct {
Breakdown string `toml:"breakdown"`
}
type CPUConf struct {
Stat bool `toml:"stat"`
LoadAvg string `toml:"load_avg"`
}
type NetConf struct {
TCP bool `toml:"tcp"`
UDP bool `toml:"udp"`
Devices []string `toml:"devices"`
}
type DiskConf struct {
Path string `toml:"path"`
Usage string `toml:"usage"`
IO bool `toml:"io"`
}
// filterNamespace replaces templated fields in the user-provided namespace and sanitizes it.
func filterNamespace(ns string) (string, error) {
hostname, err := os.Hostname()
if err != nil {
return "", err
}
ns = strings.NewReplacer("%H", hostname).Replace(ns)
sanitized, ok, _, rest := parseKey([]byte(ns + ":"), false)
if !ok || len(rest) > 0 {
return "", fmt.Errorf("Bad tag: %s", ns)
}
return sanitized, nil
}
func parseConf() (*Conf, error) {
conf := &Conf{}
f, err := os.Open(*configFile)
if err != nil {
return nil, err
}
meta, err := toml.DecodeReader(f, conf)
if err != nil {
return nil, fmt.Errorf("Error decoding %s: %s", *configFile, err)
}
for _, field := range []string{"graphite_addr", "port", "debug_port", "flush_interval_ms", "namespace"} {
if !meta.IsDefined(field) {
return nil, fmt.Errorf("field %s is required", field)
}
}
if conf.FlushIntervalMS <= 0 {
return nil, errors.New("flush_interval_ms must be positive")
}
if meta.IsDefined("forwarding_addr") {
conf.forwardingEnabled = true
}
if meta.IsDefined("forwarder_listen_addr") {
conf.forwarderEnabled = true
if !meta.IsDefined("forwarded_namespace") {
return nil, errors.New("forwarded_namespace is required if gost is configured as a forwarder")
}
}
if err := validateOSStatsConf(conf.OSStats, meta); err != nil {
return nil, err
}
if !meta.IsDefined("os_stats", "check_interval_ms") {
conf.OSStats.CheckIntervalMS = conf.FlushIntervalMS
}
if err := validateScriptsConf(conf.Scripts, meta); err != nil {
return nil, err
}
conf.Namespace, err = filterNamespace(conf.Namespace)
if err != nil {
return nil, err
}
conf.ForwardedNamespace, err = filterNamespace(conf.ForwardedNamespace)
if err != nil {
return nil, err
}
return conf, nil
}
func validateOSStatsConf(osStats *OSStatsConf, meta toml.MetaData) error {
if osStats == nil {
return nil
}
if meta.IsDefined("os_stats", "check_interval_ms") {
if osStats.CheckIntervalMS <= 0 {
return errors.New("check_interval_ms must be positive")
}
}
if err := validateMemConf(osStats.Mem); err != nil {
return err
}
if err := validateCPUConf(osStats.CPU); err != nil {
return err
}
// For now, any parseable NetConf is valid.
for _, diskConf := range osStats.Disk {
if err := validateDiskConf(diskConf); err != nil {
return err
}
}
return nil
}
func validateMemConf(memConf *MemConf) error {
if memConf == nil {
return nil
}
switch memConf.Breakdown {
case "", "fraction", "breakdown":
default:
return fmt.Errorf("Bad 'breakdown' value for os_stats.mem: %q", memConf.Breakdown)
}
return nil
}
func validateCPUConf(cpuConf *CPUConf) error {
if cpuConf == nil {
return nil
}
switch cpuConf.LoadAvg {
case "", "total", "per_cpu":
default:
return fmt.Errorf("Bad 'load_avg' value for os_stats.cpu: %q", cpuConf.LoadAvg)
}
return nil
}
func validateDiskConf(diskConf *DiskConf) error {
if diskConf.Path == "" {
return errors.New("Disk section without a path specified")
}
switch diskConf.Usage {
case "", "fraction", "absolute":
default:
return fmt.Errorf("Bad 'usage' value for os_stats.disk.<device>: %q", diskConf.Usage)
}
return nil
}
func validateScriptsConf(scripts *ScriptsConf, meta toml.MetaData) error {
if scripts == nil {
return nil
}
if !meta.IsDefined("scripts", "path") {
return errors.New("scripts section provided without path.")
}
if !meta.IsDefined("scripts", "run_interval_ms") {
return errors.New("scripts section provided without run_interval_ms.")
}
if scripts.RunIntervalMS <= 0 {
return errors.New("scripts.run_interval_ms must be positive")
}
return nil
}