diff --git a/agent/plugins/plugins.go b/agent/plugins/plugins.go index 57fc83d..5ef1cfe 100644 --- a/agent/plugins/plugins.go +++ b/agent/plugins/plugins.go @@ -42,7 +42,9 @@ import ( func Load() { cnf := config.GetInstance() - cnf.Load() + if err := cnf.Load(); err != nil { + return + } for { // Flush old metrics: @@ -62,7 +64,7 @@ func Load() { } // Wait loop: - log.DebugWithFields("Wait until next collect metrics", log.Fields{"interval": cnf.General.Interval * time.Second}) - time.Sleep(config.GetInstance().General.Interval * time.Second) + log.DebugWithFields("Wait until next collect metrics", log.Fields{"interval": time.Duration(cnf.General.Interval) * time.Second}) + time.Sleep(time.Duration(cnf.General.Interval) * time.Second) } } diff --git a/config/config.go b/config/config.go index 1e9772f..a9b8376 100644 --- a/config/config.go +++ b/config/config.go @@ -17,15 +17,17 @@ func GetInstance() *Config { return this } -func (c *Config) Load() { +func (c *Config) Load() error { source := file.ReadExpandEnvAsString(c.Path) if err := yaml.Unmarshal([]byte(source), &c); err != nil { - log.ErrorWithFields("Config", log.Fields{"message": "Imposible to parse config file", "file": c.Path}) - return + log.ErrorWithFields("Config", log.Fields{"error": err, "file": c.Path}) + return err } c.SanityCheck() + + return nil } // SanityCheck verify the minimum config settings and set default values to start. @@ -33,12 +35,12 @@ func (c *Config) SanityCheck() { if c.General.Interval < 3 { c.General.Interval = 10 - log.Error("Use positive value, and minimun start from 3 seconds, using default 10 seconds.") + log.Warning("Use positive value, and minimun start from 3 seconds, using default 10 seconds.") } if len(c.General.Hostname) == 0 { c.General.Hostname = net.Hostname() - log.Error("general.hostname: Custom value is not set, using current.") + log.Warning("general.hostname: Custom value is not set, using current.") } } diff --git a/config/structure.go b/config/structure.go index 642b440..09af328 100644 --- a/config/structure.go +++ b/config/structure.go @@ -1,17 +1,13 @@ package config -import ( - "time" -) - // All is a struct to contain all configuration imported or loaded from config file. type Config struct { Path string IPAddress string General struct { - Hostname string `yaml:"hostname"` - Interval time.Duration `yaml:"interval"` - AWSRegion string `yaml:"aws_region"` + Hostname string `yaml:"hostname"` + Interval int `yaml:"interval"` + AWSRegion string `yaml:"aws_region"` } Inputs struct { AWS struct { diff --git a/go.mod b/go.mod index fde91b1..1b54591 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.21 require ( github.com/aws/aws-sdk-go v1.46.2 - github.com/debeando/go-common v0.4.2 + github.com/debeando/go-common v0.4.3 github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c github.com/kardianos/service v1.2.2 github.com/shirou/gopsutil v3.21.11+incompatible diff --git a/go.sum b/go.sum index 19eb1c8..b6c7382 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/debeando/go-common v0.4.2 h1:sn6Vuuhl+3D3mUwxwLWa9KdQTN93UquJUyNSOmWicvk= github.com/debeando/go-common v0.4.2/go.mod h1:0u1RTk8umumGN+i+2V07g0itNWfDIUU19bsxEeFLMG4= +github.com/debeando/go-common v0.4.3 h1:JUDvmSZONsNS8DvyJ/rrJBbRvTuFBqqjtBVUkEp4uvI= +github.com/debeando/go-common v0.4.3/go.mod h1:0u1RTk8umumGN+i+2V07g0itNWfDIUU19bsxEeFLMG4= github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=