nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix
at devShellTools-shell 100 lines 2.7 kB view raw
1diff --git a/internal/config/config.go b/internal/config/config.go 2index ba8f066..1c801cd 100644 3--- a/internal/config/config.go 4+++ b/internal/config/config.go 5@@ -2,8 +2,11 @@ package config 6 7 import ( 8 "encoding/base64" 9+ "fmt" 10 "os" 11+ "strconv" 12 "sync" 13+ 14 "github.com/mrlhansen/idrac_exporter/internal/logging" 15 "gopkg.in/yaml.v2" 16 ) 17@@ -17,9 +20,9 @@ type HostConfig struct { 18 19 type RootConfig struct { 20 mutex sync.Mutex 21- Address string `yaml:"address"` 22- Port uint `yaml:"port"` 23- MetricsPrefix string `yaml:"metrics_prefix"` 24+ Address string `yaml:"address"` 25+ Port uint `yaml:"port"` 26+ MetricsPrefix string `yaml:"metrics_prefix"` 27 Collect struct { 28 System bool `yaml:"system"` 29 Sensors bool `yaml:"sensors"` 30@@ -28,9 +31,29 @@ type RootConfig struct { 31 Storage bool `yaml:"storage"` 32 Memory bool `yaml:"memory"` 33 } `yaml:"metrics"` 34- Timeout uint `yaml:"timeout"` 35- Retries uint `yaml:"retries"` 36- Hosts map[string]*HostConfig `yaml:"hosts"` 37+ Timeout uint `yaml:"timeout"` 38+ Retries uint `yaml:"retries"` 39+ Hosts map[string]*HostConfig `yaml:"hosts"` 40+} 41+ 42+func getEnv(envvar string, defvalue string) string { 43+ value := os.Getenv(envvar) 44+ if len(value) == 0 { 45+ return defvalue 46+ } 47+ return value 48+} 49+ 50+func getEnvUint(envvar string, defvalue uint) uint { 51+ value, err := strconv.Atoi(getEnv(envvar, fmt.Sprint(defvalue))) 52+ if err != nil { 53+ logging.Fatalf("Failed parse integer value: %s", err) 54+ } 55+ if value == 0 { 56+ return defvalue 57+ } 58+ 59+ return uint(value) 60 } 61 62 func (config *RootConfig) GetHostCfg(target string) *HostConfig { 63@@ -70,29 +93,29 @@ func ReadConfigFile(fileName string) { 64 } 65 66 if Config.Address == "" { 67- Config.Address = "0.0.0.0" 68+ Config.Address = getEnv("IDRAC_EXPORTER_LISTEN_ADDRESS", "0.0.0.0") 69 } 70 71 if Config.Port == 0 { 72- Config.Port = 9348 73+ Config.Port = getEnvUint("IDRAC_EXPORTER_LISTEN_PORT", 9348) 74 } 75 76 if Config.Timeout == 0 { 77- Config.Timeout = 10 78+ Config.Timeout = getEnvUint("IDRAC_EXPORTER_TIMEOUT", 10) 79 } 80 81 if Config.Retries == 0 { 82- Config.Retries = 1 83+ Config.Retries = getEnvUint("IDRAC_EXPORTER_RETRIES", 1) 84+ } 85+ 86+ if Config.MetricsPrefix == "" { 87+ Config.MetricsPrefix = getEnv("IDRAC_EXPORTER_PREFIX", "idrac") 88 } 89 90 if len(Config.Hosts) == 0 { 91 parseError("missing section", "hosts") 92 } 93 94- if Config.MetricsPrefix == "" { 95- Config.MetricsPrefix = "idrac" 96- } 97- 98 for k, v := range Config.Hosts { 99 if v.Username == "" { 100 parseError("missing username for host", k)