-
Notifications
You must be signed in to change notification settings - Fork 13
/
conf.go
164 lines (146 loc) · 4.35 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
package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/BurntSushi/toml"
)
type Conf struct {
GraphiteAddrs []string `toml:"graphite_addrs"`
ForwardingAddrs []string `toml:"forwarding_addrs"`
ForwarderListenAddr string `toml:"forwarder_listen_addr"`
ForwardedNamespace string `toml:"forwarded_namespace"`
Port int `toml:"port"`
DebugPort int `toml:"debug_port"`
ClearStatsBetweenFlushes bool `toml:"clear_stats_between_flushes"`
ClearGauges bool `toml:"clear_gauges"`
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 bool `toml:"mem"`
CPU *CPUConf `toml:"cpu"`
Net *NetConf `toml:"net"`
Disk map[string]*DiskConf `toml:"disk"`
}
type CPUConf struct {
Stat bool `toml:"stat"`
LoadAvg bool `toml:"load_avg"`
}
type NetConf struct {
TCP bool `toml:"tcp"`
UDP bool `toml:"udp"`
Devices bool `toml:"devices"`
}
type DiskConf struct {
Path string `toml:"path"`
Usage bool `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_addrs", "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_addrs") {
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")
}
}
for _, diskConf := range osStats.Disk {
if err := validateDiskConf(diskConf); err != nil {
return err
}
}
return nil
}
func validateDiskConf(diskConf *DiskConf) error {
if diskConf.Path == "" {
return errors.New("Disk section without a path specified")
}
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
}