forked from amonapp/amonagent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.go
197 lines (146 loc) · 4.39 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package amonagent
import (
"fmt"
"time"
log "github.com/Sirupsen/logrus"
"github.com/amonapp/amonagent/collectors"
"github.com/amonapp/amonagent/internal/remote"
"github.com/amonapp/amonagent/internal/settings"
"github.com/amonapp/amonagent/plugins"
)
// Agent - XXX
type Agent struct {
// Interval at which to gather information
Interval time.Duration
ConfiguredPlugins []plugins.ConfiguredPlugin
}
// TestPlugin - XXX
func (a *Agent) TestPlugin(pluginName string) error {
_, err := plugins.GetConfigPath(pluginName)
if err != nil {
fmt.Printf("Can't get config file for plugin: %s", err)
}
for _, p := range a.ConfiguredPlugins {
if pluginName == p.Name {
start := time.Now()
PluginResult, err := p.Plugin.Collect()
if err != nil {
fmt.Printf("Can't get stats for plugin: %s", err)
}
fmt.Println(PluginResult)
elapsed := time.Since(start)
fmt.Printf("\nExecuted in \033[92m%s\033[0m", elapsed)
}
}
return nil
}
// Test - XXX
func (a *Agent) Test(config settings.Struct) error {
allMetrics := collectors.CollectAllData(a.ConfiguredPlugins)
ProcessesData := collectors.CollectProcessData()
SystemData := collectors.CollectSystemData()
HostData := collectors.CollectHostData()
fmt.Println("\n------------------")
fmt.Println("\033[92mSystem Metrics: \033[0m")
fmt.Println("")
fmt.Println(SystemData)
fmt.Println("\n------------------")
fmt.Println("\n------------------")
fmt.Println("\033[92mProcess Metrics: \033[0m")
fmt.Println("")
fmt.Println(ProcessesData)
fmt.Println("\n------------------")
fmt.Println("\n------------------")
fmt.Println("\033[92mPlugins: \033[0m")
fmt.Println("")
for _, p := range a.ConfiguredPlugins {
start := time.Now()
PluginResult, err := p.Plugin.Collect()
if err != nil {
log.Errorf("Can't get stats for plugin: %s", err)
}
fmt.Println("\n------------------")
fmt.Print("\033[92mPlugin: ")
fmt.Print(p.Name)
fmt.Print("\033[0m \n")
fmt.Println(PluginResult)
elapsed := time.Since(start)
fmt.Printf("\n Executed in %s", elapsed)
}
fmt.Println("\n------------------")
fmt.Println("\033[92mHost Data: \033[0m")
fmt.Println("")
fmt.Println(HostData)
fmt.Println("\n------------------")
fmt.Println("\033[92mTesting settings: \033[0m")
fmt.Println("")
machineID := collectors.GetOrCreateMachineID()
if len(machineID) == 0 && len(config.ServerKey) == 0 {
fmt.Println("Can't find Machine ID (looking in /etc/opt/amonagent/machine-id).")
fmt.Println("To solve this problem, run the following command:")
fmt.Println("---")
fmt.Println("amonagent -machineid")
fmt.Println("---")
} else {
fmt.Println("Settings OK")
}
fmt.Println("\n------------------")
err := remote.SendData(allMetrics, true)
if err != nil {
return fmt.Errorf("%s\n", err.Error())
}
return nil
}
// GatherAndSend - XXX
func (a *Agent) GatherAndSend(debug bool) error {
allMetrics := collectors.CollectAllData(a.ConfiguredPlugins)
log.Infof("Metrics collected (Interval:%s)\n", a.Interval)
err := remote.SendData(allMetrics, debug)
if err != nil {
return fmt.Errorf("Can't connect to the Amon API on %s\n", err.Error())
}
return nil
}
// NewAgent - XXX
func NewAgent(config settings.Struct) (*Agent, error) {
var configuredPlugins = []plugins.ConfiguredPlugin{}
EnabledPlugins, _ := plugins.GetAllEnabledPlugins()
for _, p := range EnabledPlugins {
creator, _ := plugins.Plugins[p.Name]
plugin := creator()
t := plugins.ConfiguredPlugin{Name: p.Name, Plugin: plugin}
configuredPlugins = append(configuredPlugins, t)
}
agent := &Agent{
Interval: time.Duration(config.Interval) * time.Second,
ConfiguredPlugins: configuredPlugins,
}
return agent, nil
}
// Run runs the agent daemon, gathering every Interval
func (a *Agent) Run(shutdown chan struct{}, debug bool) error {
log.Infof("Agent Config: Interval:%s\n", a.Interval)
ticker := time.NewTicker(a.Interval)
defer ticker.Stop()
for _, p := range a.ConfiguredPlugins {
if err := p.Plugin.Start(); err != nil {
log.WithFields(log.Fields{
"plugin": p.Name,
"error": err.Error(),
}).Error("Service for plugin failed to start, exiting")
}
defer p.Plugin.Stop()
}
for {
if err := a.GatherAndSend(debug); err != nil {
log.Infof("Can not collect and send metrics, exiting: %s\n", err.Error())
}
select {
case <-shutdown:
log.Info("Shutting down Amon Agent")
return nil
case <-ticker.C:
continue
}
}
}