aboutsummaryrefslogtreecommitdiff
path: root/config.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2018-04-01 05:01:02 +0700
committerShulhan <ms@kilabit.info>2018-04-01 05:01:02 +0700
commit3987f51dda643f770729ac39729def7d64ebb9f9 (patch)
tree0b20f329efc1972fe76dfda3de03de4af9ad9587 /config.go
downloadhaminer-3987f51dda643f770729ac39729def7d64ebb9f9.tar.xz
haminer: Library and program to parse and forward HAProxy logs
Diffstat (limited to 'config.go')
-rw-r--r--config.go121
1 files changed, 121 insertions, 0 deletions
diff --git a/config.go b/config.go
new file mode 100644
index 0000000..9a8ed80
--- /dev/null
+++ b/config.go
@@ -0,0 +1,121 @@
+// Copyright 2018, M. Shulhan (ms@kilabit.info). All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package haminer
+
+import (
+ "bytes"
+ "io/ioutil"
+ "log"
+ "strconv"
+ "strings"
+)
+
+// List of config keys.
+const (
+ ConfigKeyListen = "listen"
+ ConfigKeyAcceptBackend = "accept_backend"
+ ConfigKeyInfluxAPIWrite = "influxdb_api_write"
+)
+
+// List of default config key values.
+const (
+ DefListenAddr = "127.0.0.1"
+ DefListenPort = 5140
+ DefInfluxAPIWrite = "http://127.0.0.1:8086/write?db=haproxy"
+ DefMaxBufferedLogs = 10
+)
+
+//
+// Config define options to create and run Haminer instance.
+//
+type Config struct {
+ // ListenAddr is an IP address where Haminer will bind and receiving
+ // log from HAProxy.
+ ListenAddr string
+ ListenPort int
+
+ // AcceptBackend list of backend to be filtered.
+ AcceptBackend []string
+
+ // InfluxAPIWrite define HTTP API to write to Influxdb.
+ InfluxAPIWrite string
+
+ // MaxBufferedLogs define a number of logs that will be keep in buffer
+ // before being forwarded.
+ MaxBufferedLogs int
+}
+
+//
+// NewConfig will create, initialize, and return new config with defautl
+// values.
+//
+func NewConfig() (cfg *Config) {
+ return &Config{
+ ListenAddr: DefListenAddr,
+ ListenPort: DefListenPort,
+ MaxBufferedLogs: DefMaxBufferedLogs,
+ }
+}
+
+//
+// SetListen will parse `v` value as "addr:port", and set config address and port
+// based on it.
+//
+func (cfg *Config) SetListen(v string) {
+ var err error
+
+ addrPort := strings.Split(v, ":")
+ switch len(addrPort) {
+ case 0:
+ return
+ case 1:
+ cfg.ListenAddr = addrPort[0]
+ case 2:
+ cfg.ListenAddr = addrPort[0]
+ cfg.ListenPort, err = strconv.Atoi(addrPort[1])
+ if err != nil {
+ cfg.ListenPort = DefListenPort
+ }
+ }
+}
+
+//
+// Load will read configuration from file defined by `path`.
+//
+func (cfg *Config) Load(path string) {
+ bb, err := ioutil.ReadFile(path)
+ if err != nil {
+ log.Println(err)
+ return
+ }
+
+ lines := bytes.Split(bb, []byte("\n"))
+
+ for _, line := range lines {
+ if len(line) == 0 {
+ continue
+ }
+ if line[0] == '#' {
+ continue
+ }
+
+ kv := bytes.SplitN(line, []byte("="), 2)
+ if len(kv) != 2 {
+ continue
+ }
+
+ switch string(kv[0]) {
+ case ConfigKeyListen:
+ cfg.SetListen(string(kv[1]))
+ case ConfigKeyAcceptBackend:
+ v := string(bytes.TrimSpace(kv[1]))
+ if len(v) > 0 {
+ cfg.AcceptBackend = strings.Split(v, ",")
+ }
+ case ConfigKeyInfluxAPIWrite:
+ cfg.InfluxAPIWrite = string(kv[1])
+ }
+ }
+}