paperwork: fix installing translations (#370379)
[NixPkgs.git] / pkgs / servers / monitoring / prometheus / idrac-exporter / config-from-environment.patch
blobe3aa42480242855966cb062b4a156876af10bb74
1 diff --git a/internal/config/config.go b/internal/config/config.go
2 index ba8f066..1c801cd 100644
3 --- a/internal/config/config.go
4 +++ b/internal/config/config.go
5 @@ -2,8 +2,11 @@ package config
7 import (
8 "encoding/base64"
9 + "fmt"
10 "os"
11 + "strconv"
12 "sync"
14 "github.com/mrlhansen/idrac_exporter/internal/logging"
15 "gopkg.in/yaml.v2"
17 @@ -17,9 +20,9 @@ type HostConfig struct {
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"`
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
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 + }
59 + return uint(value)
62 func (config *RootConfig) GetHostCfg(target string) *HostConfig {
63 @@ -70,29 +93,29 @@ func ReadConfigFile(fileName string) {
66 if Config.Address == "" {
67 - Config.Address = "0.0.0.0"
68 + Config.Address = getEnv("IDRAC_EXPORTER_LISTEN_ADDRESS", "0.0.0.0")
71 if Config.Port == 0 {
72 - Config.Port = 9348
73 + Config.Port = getEnvUint("IDRAC_EXPORTER_LISTEN_PORT", 9348)
76 if Config.Timeout == 0 {
77 - Config.Timeout = 10
78 + Config.Timeout = getEnvUint("IDRAC_EXPORTER_TIMEOUT", 10)
81 if Config.Retries == 0 {
82 - Config.Retries = 1
83 + Config.Retries = getEnvUint("IDRAC_EXPORTER_RETRIES", 1)
84 + }
86 + if Config.MetricsPrefix == "" {
87 + Config.MetricsPrefix = getEnv("IDRAC_EXPORTER_PREFIX", "idrac")
90 if len(Config.Hosts) == 0 {
91 parseError("missing section", "hosts")
94 - if Config.MetricsPrefix == "" {
95 - Config.MetricsPrefix = "idrac"
96 - }
98 for k, v := range Config.Hosts {
99 if v.Username == "" {
100 parseError("missing username for host", k)