-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
294 lines (267 loc) · 7.53 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
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
package main
import (
"errors"
"fmt"
"net"
"os"
"sort"
"time"
"code.google.com/p/gcfg"
"github.com/gravwell/ingest"
gravcfg "github.com/gravwell/ingest/config"
)
const (
ingesterName string = `kegarator`
defaultBindPort uint16 = 80
defaultProbeInterval uint16 = 5
defaultCompressorMinTime uint16 = 60 //compressor should stay on for 60 seconds
defaultTempRecordInterval uint = 60 * 60 //every hour
defaultBindAddress string = ``
defaultWwwDir string = `/opt/kegarator`
defaultMinTempC float32 = 0.0
defaultMaxTempC float32 = 6.0
defaultTargetTempC float32 = 5.0
defaultCompressorGPIO uint16 = 22
defaultDataFormat string = "binary"
defaultConfigFile = `/etc/kegarator.conf`
maxConfSize = 1024 * 1024
printTag string = `keglog`
kegTag string = `keg`
compTag string = `compressor`
)
var (
tagSet = []string{printTag, kegTag, compTag}
)
type probes struct {
alias string
compressorControl bool
minOverride float32
maxOverride float32
}
type conf struct {
port uint16
addr string
wwwdir string
interval uint16
tempRecordInterval uint
minTemp, maxTemp, targetTemp float32
compressorGPIO uint16
compressorOnTime uint16
powerRate float32 //in cents per KW/h
compressorDraw float32 //in watts
dataFormat string
aliases map[string]probes
Gravwell gravcfg.IngestConfig
}
type config struct {
Global struct {
Bind_Port uint16
Bind_Address string
WWW_Dir string
Temperature_Probe_Interval uint16
Temperature_Record_Interval uint
Minimum_Temperature float32
Maximum_Temperature float32
Target_Temperature float32
Compressor_GPIO uint16
Compressor_Power_Draw float32
Compressor_Min_On_Time uint16
Power_Rate float32
Data_Format string
}
Alias map[string]*struct {
ID string
Compressor_Control bool
Min_Override float32
Max_Override float32
}
Gravwell gravcfg.IngestConfig
}
func OpenConfig(confFile string) (*conf, error) {
if confFile == "" {
confFile = defaultConfigFile
}
fin, err := os.Open(confFile)
if err != nil {
return nil, err
}
fi, err := fin.Stat()
if err != nil {
return nil, err
}
defer fin.Close()
if fi.Size() > maxConfSize {
return nil, errors.New("Config file too large")
}
bb := make([]byte, fi.Size())
tot := 0
for int64(tot) < fi.Size() {
n, err := fin.Read(bb[tot:])
if err != nil {
return nil, err
}
tot += n
}
var cfg config
if err = prepopulate(&cfg); err != nil {
return nil, err
}
if err := gcfg.ReadStringInto(&cfg, string(bb)); err != nil {
return nil, err
}
amap := make(map[string]probes, 1)
for k, v := range cfg.Alias {
if k != "" && v.ID != "" {
_, ok := amap[k]
if ok {
return nil, errors.New("Duplicate alias")
}
amap[k] = probes{
alias: v.ID,
compressorControl: v.Compressor_Control,
minOverride: v.Min_Override,
maxOverride: v.Max_Override,
}
}
}
if err := cfg.Gravwell.Verify(); err != nil {
return nil, err
}
return &conf{
port: cfg.Global.Bind_Port,
addr: cfg.Global.Bind_Address,
wwwdir: cfg.Global.WWW_Dir,
interval: cfg.Global.Temperature_Probe_Interval,
tempRecordInterval: cfg.Global.Temperature_Record_Interval,
minTemp: cfg.Global.Minimum_Temperature,
maxTemp: cfg.Global.Maximum_Temperature,
targetTemp: cfg.Global.Target_Temperature,
compressorGPIO: cfg.Global.Compressor_GPIO,
compressorDraw: cfg.Global.Compressor_Power_Draw,
compressorOnTime: cfg.Global.Compressor_Min_On_Time,
powerRate: cfg.Global.Power_Rate,
dataFormat: cfg.Global.Data_Format,
aliases: amap,
Gravwell: cfg.Gravwell,
}, nil
}
func prepopulate(c *config) error {
if c == nil {
return errors.New("Invalid config pointer")
}
c.Global.WWW_Dir = defaultWwwDir
c.Global.Bind_Port = defaultBindPort
c.Global.Bind_Address = defaultBindAddress
c.Global.Temperature_Probe_Interval = defaultProbeInterval
c.Global.Minimum_Temperature = defaultMinTempC
c.Global.Maximum_Temperature = defaultMaxTempC
c.Global.Target_Temperature = defaultTargetTempC
c.Global.Compressor_GPIO = defaultCompressorGPIO
c.Global.Compressor_Min_On_Time = defaultCompressorMinTime
c.Global.Data_Format = defaultDataFormat
return nil
}
func (c conf) WebDir() string {
return c.wwwdir
}
func (c conf) Bind() string {
return net.JoinHostPort(c.addr, fmt.Sprintf("%d", c.port))
}
func (c conf) Aliases() map[string]string {
ret := make(map[string]string, 1)
for k, v := range c.aliases {
ret[k] = v.alias
}
return ret
}
func (c conf) CompressorGPIO() uint16 {
return c.compressorGPIO
}
func (c conf) TemperatureRange() (float32, float32, float32) {
return c.minTemp, c.maxTemp, c.targetTemp
}
func (c conf) ProbeInterval() time.Duration {
if c.interval <= 0 {
c.interval = defaultProbeInterval
}
return time.Duration(c.interval) * time.Second
}
func (c conf) TemperatureRecordInterval() time.Duration {
if c.tempRecordInterval <= 0 {
c.tempRecordInterval = defaultTempRecordInterval
}
return time.Duration(c.tempRecordInterval) * time.Second
}
func (c conf) CompressorMinOnTime() time.Duration {
return time.Duration(c.compressorOnTime) * time.Second
}
func (c conf) PowerRate() float32 {
return c.powerRate
}
func (c conf) CompressorPowerDraw() float32 {
return c.compressorDraw
}
func (c conf) AliasCompressorControl(alias string) (bool, error) {
p, ok := c.aliases[alias]
if !ok {
return false, errors.New("Alias not found")
}
return p.compressorControl, nil
}
type probeDesc struct {
ID string
Alias string
CompressorControl bool
MinOverride float32
MaxOverride float32
}
type probeDescL []probeDesc
func (c conf) ProbeList() []probeDesc {
var pd []probeDesc
for k, v := range c.aliases {
pd = append(pd, probeDesc{
ID: v.alias,
Alias: k,
CompressorControl: v.compressorControl,
MinOverride: v.minOverride,
MaxOverride: v.maxOverride,
})
}
sort.Sort(probeDescL(pd))
return pd
}
func (p probeDescL) Less(i, j int) bool {
//compressor control probes always have priority
if p[i].CompressorControl && !p[j].CompressorControl {
return true
} else if p[j].CompressorControl && !p[i].CompressorControl {
return false
}
//if compressor control is equivelent, sort on alias name
return p[i].Alias < p[j].Alias
}
func (p probeDescL) Len() int { return len(p) }
func (p probeDescL) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (c conf) MuxerConfig() (mc ingest.MuxerConfig, err error) {
var tgts []string
if tgts, err = c.Gravwell.Targets(); err != nil {
return
}
for _, tgt := range tgts {
c := ingest.Target{
Address: tgt,
Secret: c.Gravwell.Secret(),
}
mc.Destinations = append(mc.Destinations, c)
}
mc.Tags = tagSet
mc.VerifyCert = c.Gravwell.Verify_Remote_Certificates
mc.EnableCache = c.Gravwell.EnableCache()
mc.LogLevel = c.Gravwell.LogLevel()
mc.IngesterName = ingesterName
mc.CacheConfig = ingest.IngestCacheConfig{
FileBackingLocation: c.Gravwell.Ingest_Cache_Path,
MaxCacheSize: uint64(c.Gravwell.Max_Ingest_Cache),
}
return
}