2022-04-13 14:09:33 +08:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-04-13 14:19:02 +08:00
|
|
|
"log"
|
2022-04-13 14:09:33 +08:00
|
|
|
"path"
|
2022-04-13 16:24:12 +08:00
|
|
|
"strings"
|
2022-04-13 14:09:33 +08:00
|
|
|
|
2022-04-13 20:12:20 +08:00
|
|
|
"flashcat.cloud/categraf/config"
|
2022-04-13 16:24:12 +08:00
|
|
|
"flashcat.cloud/categraf/inputs"
|
2022-04-13 20:12:20 +08:00
|
|
|
"flashcat.cloud/categraf/pkg/cfg"
|
2022-04-13 16:24:12 +08:00
|
|
|
"flashcat.cloud/categraf/types"
|
2022-04-13 20:12:20 +08:00
|
|
|
"flashcat.cloud/categraf/writer"
|
2022-04-13 14:09:33 +08:00
|
|
|
"github.com/toolkits/pkg/file"
|
2022-04-13 16:24:12 +08:00
|
|
|
|
2022-04-13 20:12:20 +08:00
|
|
|
// auto registry
|
2022-04-13 16:24:12 +08:00
|
|
|
_ "flashcat.cloud/categraf/inputs/redis"
|
2022-04-13 14:09:33 +08:00
|
|
|
)
|
|
|
|
|
2022-04-13 20:12:20 +08:00
|
|
|
type Agent struct{}
|
2022-04-13 14:09:33 +08:00
|
|
|
|
2022-04-13 23:35:43 +08:00
|
|
|
func NewAgent(configDir, debugMode string, testMode bool) (*Agent, error) {
|
|
|
|
if err := config.InitConfig(configDir, debugMode, testMode); err != nil {
|
2022-04-13 20:12:20 +08:00
|
|
|
return nil, fmt.Errorf("failed to init config: %v", err)
|
2022-04-13 14:09:33 +08:00
|
|
|
}
|
|
|
|
|
2022-04-13 20:12:20 +08:00
|
|
|
// init writers
|
|
|
|
if err := writer.Init(config.Config.Writers); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to init writers: %v", err)
|
2022-04-13 14:09:33 +08:00
|
|
|
}
|
|
|
|
|
2022-04-13 20:12:20 +08:00
|
|
|
return &Agent{}, nil
|
2022-04-13 14:09:33 +08:00
|
|
|
}
|
2022-04-13 14:19:02 +08:00
|
|
|
|
|
|
|
func (a *Agent) Start() {
|
|
|
|
log.Println("I! agent starting")
|
2022-04-13 16:24:12 +08:00
|
|
|
|
2022-04-13 17:16:16 +08:00
|
|
|
a.startInputs()
|
2022-04-13 14:19:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) Stop() {
|
|
|
|
log.Println("I! agent stopping")
|
|
|
|
|
2022-04-14 00:05:48 +08:00
|
|
|
for name := range InputReaders {
|
|
|
|
InputReaders[name].Instance.StopGoroutines()
|
|
|
|
close(InputReaders[name].Queue)
|
2022-04-13 17:16:16 +08:00
|
|
|
}
|
2022-04-13 14:19:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) Reload() {
|
|
|
|
log.Println("I! agent reloading")
|
|
|
|
|
2022-04-13 17:16:16 +08:00
|
|
|
a.Stop()
|
|
|
|
a.Start()
|
2022-04-13 14:19:02 +08:00
|
|
|
}
|
2022-04-13 16:24:12 +08:00
|
|
|
|
2022-04-13 17:16:16 +08:00
|
|
|
func (a *Agent) startInputs() error {
|
|
|
|
names, err := a.getInputsByDirs()
|
2022-04-13 16:24:12 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(names) == 0 {
|
|
|
|
log.Println("I! no inputs")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range names {
|
|
|
|
creator, has := inputs.InputCreators[name]
|
|
|
|
if !has {
|
|
|
|
log.Println("E! input:", name, "not supported")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct input instance
|
|
|
|
instance := creator()
|
|
|
|
|
|
|
|
// set configurations for input instance
|
2022-04-13 20:12:20 +08:00
|
|
|
cfg.LoadConfigs(path.Join(config.Config.ConfigDir, "input."+name), instance)
|
2022-04-13 16:24:12 +08:00
|
|
|
|
|
|
|
// check configurations
|
|
|
|
if err = instance.TidyConfig(); err != nil {
|
|
|
|
log.Println("E! input:", name, "configurations invalid:", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-04-14 00:05:48 +08:00
|
|
|
c := &Reader{
|
2022-04-13 16:24:12 +08:00
|
|
|
Instance: instance,
|
|
|
|
Queue: make(chan *types.Sample, 1000000),
|
|
|
|
}
|
|
|
|
|
2022-04-13 17:16:16 +08:00
|
|
|
log.Println("I! input:", name, "started")
|
|
|
|
c.Start()
|
2022-04-13 16:24:12 +08:00
|
|
|
|
2022-04-14 00:05:48 +08:00
|
|
|
InputReaders[name] = c
|
2022-04-13 16:24:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// input dir should has prefix input.
|
2022-04-13 17:16:16 +08:00
|
|
|
func (a *Agent) getInputsByDirs() ([]string, error) {
|
2022-04-13 20:12:20 +08:00
|
|
|
dirs, err := file.DirsUnder(config.Config.ConfigDir)
|
2022-04-13 16:24:12 +08:00
|
|
|
if err != nil {
|
2022-04-13 20:12:20 +08:00
|
|
|
return nil, fmt.Errorf("failed to get dirs under %s : %v", config.Config.ConfigDir, err)
|
2022-04-13 16:24:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
count := len(dirs)
|
|
|
|
if count == 0 {
|
|
|
|
return dirs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
names := make([]string, 0, count)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
if strings.HasPrefix(dirs[i], "input.") {
|
|
|
|
names = append(names, dirs[i][6:])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return names, nil
|
|
|
|
}
|