-
Notifications
You must be signed in to change notification settings - Fork 32
/
config.go
73 lines (63 loc) · 1.94 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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/mackerelio/mackerel-agent/config"
)
const idFileName = "id"
func idFilePath(root string) string {
return filepath.Join(root, idFileName)
}
func loadHostID(root string) (string, error) {
content, err := ioutil.ReadFile(idFilePath(root))
if err != nil {
return "", err
}
return string(content), nil
}
// LoadApibaseFromConfig gets mackerel api Base URL (usually https://api.mackerelio.com/) from mackerel-agent.conf if it's installed mackerel-agent on localhost
func LoadApibaseFromConfig(conffile string) string {
conf, err := config.LoadConfig(conffile)
if err != nil {
return ""
}
return conf.Apibase
}
// LoadApibaseFromConfigWithFallback get mackerel api Base URL from mackerel-agent.conf,
// and fallbacks to default (https://api.mackerelio.com/) if not specified.
func LoadApibaseFromConfigWithFallback(conffile string) string {
apiBase := LoadApibaseFromConfig(conffile)
if apiBase == "" {
return config.DefaultConfig.Apibase
}
return apiBase
}
// LoadApikeyFromConfig gets mackerel.io apikey from mackerel-agent.conf if it's installed mackerel-agent on localhost
func LoadApikeyFromConfig(conffile string) string {
conf, err := config.LoadConfig(conffile)
if err != nil {
return ""
}
return conf.Apikey
}
// LoadApikeyFromEnvOrConfig is similar to LoadApikeyFromConfig. return MACKEREL_APIKEY environment value if defined MACKEREL_APIKEY
func LoadApikeyFromEnvOrConfig(conffile string) string {
if apiKey := os.Getenv("MACKEREL_APIKEY"); apiKey != "" {
return apiKey
}
key := LoadApikeyFromConfig(conffile)
return key
}
// LoadHostIDFromConfig gets localhost's hostID from conf.Root (ex. /var/lib/mackerel/id) if it's installed mackerel-agent on localhost
func LoadHostIDFromConfig(conffile string) string {
conf, err := config.LoadConfig(conffile)
if err != nil {
return ""
}
hostID, err := loadHostID(conf.Root)
if err != nil {
return ""
}
return hostID
}