categraf/config/config.go

145 lines
3.2 KiB
Go
Raw Normal View History

2022-04-13 20:12:20 +08:00
package config
import (
"encoding/json"
"fmt"
2022-04-27 17:54:11 +08:00
"net"
2022-04-13 20:12:20 +08:00
"os"
"path"
2022-04-27 17:54:11 +08:00
"strings"
"time"
2022-04-13 20:12:20 +08:00
"flashcat.cloud/categraf/pkg/cfg"
"github.com/toolkits/pkg/file"
)
type Global struct {
2022-04-17 12:54:32 +08:00
PrintConfigs bool `toml:"print_configs"`
Hostname string `toml:"hostname"`
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"`
Timeout int64 `toml:"timeout"`
DialTimeout int64 `toml:"dial_timeout"`
TLSHandshakeTimeout int64 `toml:"tls_handshake_timeout"`
ExpectContinueTimeout int64 `toml:"expect_continue_timeout"`
IdleConnTimeout int64 `toml:"idle_conn_timeout"`
KeepAlive int64 `toml:"keep_alive"`
MaxConnsPerHost int `toml:"max_conns_per_host"`
MaxIdleConns int `toml:"max_idle_conns"`
MaxIdleConnsPerHost int `toml:"max_idle_conns_per_host"`
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
Global Global `toml:"global"`
WriterOpt WriterOpt `toml:"writer_opt"`
Writers []WriterOption `toml:"writers"`
2022-04-13 20:12:20 +08:00
}
var Config *ConfigType
2022-04-18 23:35:47 +08:00
func InitConfig(configDir string, debugMode bool, testMode bool) 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-04-27 17:54:11 +08:00
if err := Config.fillHostname(); err != nil {
return err
2022-04-13 20:12:20 +08:00
}
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-27 17:54:11 +08:00
func (c *ConfigType) fillHostname() error {
if c.Global.Hostname == "" {
name, err := GetHostname()
if err != nil {
return err
}
c.Global.Hostname = name
return nil
}
if strings.Contains(c.Global.Hostname, "$hostname") {
name, err := GetHostname()
if err != nil {
return err
}
c.Global.Hostname = strings.Replace(c.Global.Hostname, "$hostname", name, -1)
}
if strings.Contains(c.Global.Hostname, "$ip") {
ip, err := GetOutboundIP()
if err != nil {
return err
}
c.Global.Hostname = strings.Replace(c.Global.Hostname, "$ip", fmt.Sprint(ip), -1)
}
return nil
}
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-27 17:54:11 +08:00
func GetHostname() (string, error) {
return os.Hostname()
}
// 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
}