2022-04-13 20:12:20 +08:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
2022-04-14 23:00:39 +08:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if Config.Global.Hostname == "" {
|
|
|
|
name, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get hostname: %v", err)
|
|
|
|
}
|
|
|
|
Config.Global.Hostname = name
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|