/
cfg.go
142 lines (118 loc) · 3.19 KB
/
cfg.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"net"
"os"
"strings"
"sync/atomic"
"time"
"github.com/Cepave/open-falcon-backend/common/model"
"github.com/Cepave/open-falcon-backend/common/vipercfg"
"github.com/chyeh/pubip"
log "github.com/sirupsen/logrus"
)
type AgentConfig struct {
PushURL string `json:"pushURL"`
}
type HbsConfig struct {
RPCServer string `json:"RPCServer"`
Interval time.Duration `json:"interval"`
}
type JSONConfig struct {
Agent *AgentConfig `json:"agent"`
Hbs *HbsConfig `json:"hbs"`
Hostname string `json:"hostname"`
IPAddress string `json:"ipAddress"`
ConnectionID string `json:"connectionID"`
}
type Metadata struct {
Hostname string
IPAddress string
ConnectionID string
}
var (
jsonConfig atomic.Value // for JSONConfig
hbsResp atomic.Value // for receiving model.NqmTaskResponse
metadata = Metadata{}
)
func Config() JSONConfig {
return jsonConfig.Load().(JSONConfig)
}
func SetConfig(c JSONConfig) {
jsonConfig.Store(c)
}
func HBSResp() model.NqmTaskResponse {
return hbsResp.Load().(model.NqmTaskResponse)
}
func SetHBSResp(r model.NqmTaskResponse) {
hbsResp.Store(r)
}
func hostname() string {
hostname := Config().Hostname
if hostname != "" {
log.Println("Hostname set in config: [", hostname, "]")
return hostname
}
hostname, err := os.Hostname()
if err != nil {
log.Fatalln("os.Hostname() ERROR:", err)
}
// hostname -s
// -s, --short
// Display the short host name. This is the host name cut at the first dot.
hostname = strings.Split(hostname, ".")[0]
log.Println("Hostname not set in config, using system's hostname...succeeded: [", hostname, "]")
return hostname
}
func ip() string {
ip := net.ParseIP(Config().IPAddress)
if ip != nil {
log.Println("IP set in config: [", ip, "]")
return ip.String()
}
log.Errorln("Invalid IP in config")
ip, err := pubip.Get()
if err != nil {
log.Fatalln("Getting public IP...failed:", err)
}
log.Println("Getting public IP...succeeded: [", ip, "]")
return ip.String()
}
func connectionID() string {
connectionID := Config().ConnectionID
if connectionID != "" {
log.Println("ConnectionID set in config: [", connectionID, "]")
return connectionID
}
// Logically it shouldn't happen because ConnectionID is always generated
// after Hostname and IPAddress are set.
if Meta().Hostname == "" || Meta().IPAddress == "" {
log.Fatalln("ConnectionID not set in config, generating...failed!")
}
connectionID = Meta().Hostname + "@" + Meta().IPAddress
log.Println("ConnectionID not set in config, generating...succeeded: [", connectionID, "]")
return connectionID
}
func jsonUnmarshaller() JSONConfig {
var c = JSONConfig{
Agent: &AgentConfig{},
Hbs: &HbsConfig{},
}
err := vipercfg.Config().Unmarshal(&c)
if err != nil {
log.Fatal("Parsing configuration file [", vipercfg.Config().GetString("config"), "] failed:", err)
}
log.Println("Reading configuration file [", vipercfg.Config().GetString("config"), "] succeeded")
return c
}
func InitConfig() {
c := jsonUnmarshaller()
SetConfig(c)
}
func Meta() *Metadata {
return &metadata
}
func GenMeta() {
Meta().Hostname = hostname()
Meta().IPAddress = ip()
Meta().ConnectionID = connectionID()
}