categraf/config/logs.go

111 lines
3.1 KiB
Go
Raw Normal View History

2022-05-31 01:23:19 +08:00
package config
2022-05-31 09:43:14 +08:00
import (
2022-06-01 18:15:10 +08:00
"encoding/json"
"fmt"
"path"
2022-06-01 23:44:03 +08:00
"github.com/toolkits/pkg/file"
2022-06-02 00:06:58 +08:00
logsconfig "flashcat.cloud/categraf/config/logs"
"flashcat.cloud/categraf/pkg/cfg"
2022-05-31 09:43:14 +08:00
)
const (
Docker = "docker"
Kubernetes = "kubernetes"
)
2022-06-02 00:06:58 +08:00
type (
Logs struct {
APIKey string `mapstructure:"api_key" toml:"api_key"`
Enable bool `mapstructure:"enable" toml:"enable"`
SendTo string `mapstructure:"send_to" toml:"send_to"`
SendType string `mapstructure:"send_type" toml:"send_type"`
UseCompression bool `mapstructure:"use_compression" toml:"use_compression"`
CompressionLevel int `mapstructure:"compression_level" toml:"compression_level"`
SendWithTLS bool `mapstructure:"send_with_tls" toml:"send_with_tls"`
BatchWait int `mapstructure:"batch_wait" toml:"batch_wait"`
RunPath string `mapstructure:"run_path" toml:"run_path"`
OpenFilesLimit int `mapstructure:"open_files_limit" toml:"open_files_limit"`
ScanPeriod int `mapstructure:"scan_period" toml:"scan_period"`
FrameSize int `mapstructure:"frame_size" toml:"frame_size"`
CollectContainerAll bool `mapstructure:"collect_container_all" toml:"collect_container_all"`
GlobalProcessingRules []*logsconfig.ProcessingRule `mapstructure:"processing_rules" toml:"processing_rules"`
Items []*logsconfig.LogsConfig `mapstructure:"items" toml:"items"`
}
2022-06-02 00:11:15 +08:00
LogType struct {
Logs Logs `toml:"logs"`
}
2022-06-02 00:06:58 +08:00
)
2022-06-01 18:15:10 +08:00
var (
LogConfig *Logs
)
func InitLogConfig(configDir string) error {
2022-06-01 23:44:03 +08:00
var (
err error
)
2022-06-01 18:15:10 +08:00
configFile := path.Join(configDir, "logs.toml")
if !file.IsExist(configFile) {
return fmt.Errorf("configuration file(%s) not found", configFile)
}
2022-06-02 00:11:15 +08:00
data := &LogType{}
err = cfg.LoadConfig(configFile, data)
2022-06-01 23:44:03 +08:00
if err != nil {
2022-06-01 18:54:26 +08:00
return fmt.Errorf("failed to load config: %s, err: %s", configFile, err)
2022-06-01 18:15:10 +08:00
}
2022-06-02 00:06:58 +08:00
LogConfig = &data.Logs
2022-06-01 18:15:10 +08:00
if Config != nil && Config.Global.PrintConfigs {
bs, _ := json.MarshalIndent(LogConfig, "", " ")
fmt.Println(string(bs))
}
return nil
2022-05-31 09:43:14 +08:00
}
func GetLogRunPath() string {
2022-06-01 18:15:10 +08:00
if len(LogConfig.RunPath) == 0 {
LogConfig.RunPath = "/opt/categraf/run"
2022-05-31 09:43:14 +08:00
}
2022-06-01 18:15:10 +08:00
return LogConfig.RunPath
2022-05-31 09:43:14 +08:00
}
func GetLogReadTimeout() int {
return 30
}
func OpenLogsLimit() int {
2022-06-01 18:15:10 +08:00
if LogConfig.OpenFilesLimit == 0 {
LogConfig.OpenFilesLimit = 100
2022-05-31 09:43:14 +08:00
}
2022-06-01 18:15:10 +08:00
return LogConfig.OpenFilesLimit
2022-05-31 09:43:14 +08:00
}
func FileScanPeriod() int {
2022-06-01 18:15:10 +08:00
if LogConfig.ScanPeriod == 0 {
LogConfig.ScanPeriod = 10
2022-05-31 09:43:14 +08:00
}
2022-06-01 18:15:10 +08:00
return LogConfig.ScanPeriod
2022-05-31 09:43:14 +08:00
}
func LogFrameSize() int {
2022-06-01 18:15:10 +08:00
if LogConfig.FrameSize == 0 {
LogConfig.FrameSize = 9000
2022-05-31 09:43:14 +08:00
}
2022-06-01 18:15:10 +08:00
return LogConfig.FrameSize
2022-05-31 09:43:14 +08:00
}
func ValidatePodContainerID() bool {
return false
}
func IsFeaturePresent(t string) bool {
return false
}
func GetContainerCollectAll() bool {
2022-06-01 18:15:10 +08:00
return LogConfig.CollectContainerAll
2022-05-31 09:43:14 +08:00
}