2022-04-13 20:12:20 +08:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-04-27 17:54:11 +08:00
|
|
|
"net"
|
2022-06-09 19:16:57 +08:00
|
|
|
"os"
|
2022-04-13 20:12:20 +08:00
|
|
|
"path"
|
2022-04-27 17:54:11 +08:00
|
|
|
"strings"
|
2022-04-14 23:00:39 +08:00
|
|
|
"time"
|
2022-04-13 20:12:20 +08:00
|
|
|
|
2022-06-28 16:33:27 +08:00
|
|
|
"flashcat.cloud/categraf/config/traces"
|
2022-04-13 20:12:20 +08:00
|
|
|
"flashcat.cloud/categraf/pkg/cfg"
|
|
|
|
"github.com/toolkits/pkg/file"
|
|
|
|
)
|
|
|
|
|
2022-06-09 19:16:57 +08:00
|
|
|
var envVarEscaper = strings.NewReplacer(
|
|
|
|
`"`, `\"`,
|
|
|
|
`\`, `\\`,
|
|
|
|
)
|
|
|
|
|
2022-04-13 20:12:20 +08:00
|
|
|
type Global struct {
|
2022-04-17 12:54:32 +08:00
|
|
|
PrintConfigs bool `toml:"print_configs"`
|
|
|
|
Hostname string `toml:"hostname"`
|
2022-05-31 16:53:21 +08:00
|
|
|
IP string `toml:"-"`
|
2022-04-17 12:54:32 +08:00
|
|
|
OmitHostname bool `toml:"omit_hostname"`
|
|
|
|
Labels map[string]string `toml:"labels"`
|
|
|
|
Precision string `toml:"precision"`
|
|
|
|
Interval Duration `toml:"interval"`
|
2022-04-13 20:12:20 +08:00
|
|
|
}
|
|
|
|
|
2022-04-13 23:56:04 +08:00
|
|
|
type WriterOpt struct {
|
2022-04-16 16:52:23 +08:00
|
|
|
Batch int `toml:"batch"`
|
|
|
|
ChanSize int `toml:"chan_size"`
|
2022-04-13 23:56:04 +08:00
|
|
|
}
|
|
|
|
|
2022-04-13 20:12:20 +08:00
|
|
|
type WriterOption struct {
|
2022-04-15 13:41:02 +08:00
|
|
|
Url string `toml:"url"`
|
|
|
|
BasicAuthUser string `toml:"basic_auth_user"`
|
|
|
|
BasicAuthPass string `toml:"basic_auth_pass"`
|
|
|
|
|
2022-05-28 09:24:30 +08:00
|
|
|
Timeout int64 `toml:"timeout"`
|
|
|
|
DialTimeout int64 `toml:"dial_timeout"`
|
|
|
|
MaxIdleConnsPerHost int `toml:"max_idle_conns_per_host"`
|
2022-04-13 20:12:20 +08:00
|
|
|
}
|
|
|
|
|
2022-08-03 10:41:44 +08:00
|
|
|
type HTTP struct {
|
|
|
|
Enable bool `toml:"enable"`
|
|
|
|
Address string `toml:"address"`
|
|
|
|
PrintAccess bool `toml:"print_access"`
|
|
|
|
RunMode string `toml:"run_mode"`
|
|
|
|
CertFile string `toml:"cert_file"`
|
|
|
|
KeyFile string `toml:"key_file"`
|
|
|
|
ReadTimeout int `toml:"read_timeout"`
|
|
|
|
WriteTimeout int `toml:"write_timeout"`
|
|
|
|
IdleTimeout int `toml:"idle_timeout"`
|
2022-08-03 09:05:49 +08:00
|
|
|
}
|
|
|
|
|
2022-04-13 20:12:20 +08:00
|
|
|
type ConfigType struct {
|
2022-04-15 13:41:02 +08:00
|
|
|
// from console args
|
2022-04-13 20:12:20 +08:00
|
|
|
ConfigDir string
|
|
|
|
DebugMode bool
|
2022-04-13 23:35:43 +08:00
|
|
|
TestMode bool
|
2022-04-13 20:12:20 +08:00
|
|
|
|
2022-04-15 13:41:02 +08:00
|
|
|
// from config.toml
|
2022-06-17 19:04:02 +08:00
|
|
|
Global Global `toml:"global"`
|
|
|
|
WriterOpt WriterOpt `toml:"writer_opt"`
|
|
|
|
Writers []WriterOption `toml:"writers"`
|
|
|
|
Logs Logs `toml:"logs"`
|
|
|
|
MetricsHouse MetricsHouse `toml:"metricshouse"`
|
2022-06-28 16:33:27 +08:00
|
|
|
Traces *traces.Config `toml:"traces"`
|
2022-08-03 10:41:44 +08:00
|
|
|
HTTP *HTTP `toml:"http"`
|
2022-07-11 22:55:33 +08:00
|
|
|
Prometheus *Prometheus `toml:"prometheus"`
|
2022-04-13 20:12:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var Config *ConfigType
|
|
|
|
|
2022-07-11 20:40:30 +08:00
|
|
|
func InitConfig(configDir string, debugMode, testMode bool, interval int64) error {
|
2022-04-13 20:12:20 +08:00
|
|
|
configFile := path.Join(configDir, "config.toml")
|
|
|
|
if !file.IsExist(configFile) {
|
|
|
|
return fmt.Errorf("configuration file(%s) not found", configFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
Config = &ConfigType{
|
|
|
|
ConfigDir: configDir,
|
2022-04-18 23:35:47 +08:00
|
|
|
DebugMode: debugMode,
|
2022-04-13 23:35:43 +08:00
|
|
|
TestMode: testMode,
|
2022-04-13 20:12:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := cfg.LoadConfigs(configDir, Config); err != nil {
|
|
|
|
return fmt.Errorf("failed to load configs of dir: %s", configDir)
|
|
|
|
}
|
|
|
|
|
2022-07-11 20:40:30 +08:00
|
|
|
if interval > 0 {
|
|
|
|
Config.Global.Interval = Duration(time.Duration(interval) * time.Second)
|
|
|
|
}
|
|
|
|
|
2022-05-31 16:53:21 +08:00
|
|
|
if err := Config.fillIP(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := InitHostname(); err != nil {
|
2022-04-27 17:54:11 +08:00
|
|
|
return err
|
2022-04-13 20:12:20 +08:00
|
|
|
}
|
|
|
|
|
2022-06-28 16:33:27 +08:00
|
|
|
if err := traces.Parse(Config.Traces); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-15 13:41:02 +08:00
|
|
|
if Config.Global.PrintConfigs {
|
2022-04-13 20:12:20 +08:00
|
|
|
bs, _ := json.MarshalIndent(Config, "", " ")
|
|
|
|
fmt.Println(string(bs))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-14 23:00:39 +08:00
|
|
|
|
2022-05-31 16:53:21 +08:00
|
|
|
func (c *ConfigType) fillIP() error {
|
|
|
|
if !strings.Contains(c.Global.Hostname, "$ip") {
|
2022-04-27 17:54:11 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-31 16:53:21 +08:00
|
|
|
ip, err := GetOutboundIP()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-04-27 17:54:11 +08:00
|
|
|
}
|
|
|
|
|
2022-05-31 16:53:21 +08:00
|
|
|
c.Global.IP = fmt.Sprint(ip)
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-27 17:54:11 +08:00
|
|
|
|
2022-05-31 16:53:21 +08:00
|
|
|
func (c *ConfigType) GetHostname() string {
|
|
|
|
ret := c.Global.Hostname
|
|
|
|
|
|
|
|
name := Hostname.Get()
|
|
|
|
if ret == "" {
|
|
|
|
return name
|
2022-04-27 17:54:11 +08:00
|
|
|
}
|
|
|
|
|
2022-05-31 16:53:21 +08:00
|
|
|
ret = strings.Replace(ret, "$hostname", name, -1)
|
|
|
|
ret = strings.Replace(ret, "$ip", c.Global.IP, -1)
|
2022-06-14 23:47:10 +08:00
|
|
|
ret = os.Expand(ret, GetEnv)
|
2022-05-31 16:53:21 +08:00
|
|
|
|
|
|
|
return ret
|
2022-04-27 17:54:11 +08:00
|
|
|
}
|
|
|
|
|
2022-06-14 23:47:10 +08:00
|
|
|
func GetEnv(key string) string {
|
2022-06-09 19:16:57 +08:00
|
|
|
v := os.Getenv(key)
|
|
|
|
return envVarEscaper.Replace(v)
|
|
|
|
}
|
|
|
|
|
2022-04-14 23:00:39 +08:00
|
|
|
func GetInterval() time.Duration {
|
2022-04-17 12:54:32 +08:00
|
|
|
if Config.Global.Interval <= 0 {
|
|
|
|
return time.Second * 15
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.Duration(Config.Global.Interval)
|
2022-04-14 23:00:39 +08:00
|
|
|
}
|
2022-04-27 17:54:11 +08:00
|
|
|
|
|
|
|
// Get preferred outbound ip of this machine
|
|
|
|
func GetOutboundIP() (net.IP, error) {
|
|
|
|
conn, err := net.Dial("udp", "8.8.8.8:80")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get outbound ip: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
|
|
|
|
|
|
|
return localAddr.IP, nil
|
|
|
|
}
|