forked from flashcatcloud/categraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.go
73 lines (66 loc) · 1.37 KB
/
agent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package agent
import (
"errors"
"log"
)
type Agent struct {
agents []AgentModule
}
// AgentModule is the interface for agent modules
// Use NewXXXAgent() to create a new agent module
// if the agent module is not needed, return nil
type AgentModule interface {
Start() error
Stop() error
}
func NewAgent() (*Agent, error) {
agent := &Agent{
agents: []AgentModule{
NewMetricsAgent(),
NewTracesAgent(),
NewLogsAgent(),
NewPrometheusAgent(),
NewIbexAgent(),
},
}
for _, ag := range agent.agents {
if ag != nil {
return agent, nil
}
}
return nil, errors.New("no valid running agents, please check configuration")
}
func (a *Agent) Start() {
log.Println("I! agent starting")
for _, agent := range a.agents {
if agent == nil {
continue
}
if err := agent.Start(); err != nil {
log.Printf("E! start [%T] err: [%+v]", agent, err)
} else {
log.Printf("I! [%T] started", agent)
}
}
log.Println("I! agent started")
}
func (a *Agent) Stop() {
log.Println("I! agent stopping")
for _, agent := range a.agents {
if agent == nil {
continue
}
if err := agent.Stop(); err != nil {
log.Printf("E! stop [%T] err: [%+v]", agent, err)
} else {
log.Printf("I! [%T] stopped", agent)
}
}
log.Println("I! agent stopped")
}
func (a *Agent) Reload() {
log.Println("I! agent reloading")
a.Stop()
a.Start()
log.Println("I! agent reloaded")
}