forked from lostz/percona-agent
/
manager.go
328 lines (291 loc) · 9.63 KB
/
manager.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
Copyright (c) 2014, Percona LLC and/or its affiliates. All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package mm
/**
* mm is a proxy manager for monitors. It doesn't have its own config,
* it's job is to start and stop monitors, mostly done in Handle().
*/
import (
"encoding/json"
"errors"
"fmt"
"github.com/percona/cloud-protocol/proto"
"github.com/percona/percona-agent/data"
"github.com/percona/percona-agent/instance"
"github.com/percona/percona-agent/pct"
"github.com/percona/percona-agent/ticker"
"io/ioutil"
"path/filepath"
"sync"
"time"
)
// We use one binding per unique mm.Report interval. For example, if some monitors
// report every 60s and others every 10s, then there are two bindings. All monitors
// with the same report interval share the same binding: collectionChan to send
// metrics and aggregator summarizing and reporting those metrics.
type Binding struct {
aggregator *Aggregator
collectionChan chan *Collection // <- metrics from monitors
}
type Manager struct {
logger *pct.Logger
factory MonitorFactory
clock ticker.Manager
spool data.Spooler
im *instance.Repo
// --
monitors map[string]Monitor
running bool
mux *sync.RWMutex // guards monitors and running
status *pct.Status
aggregators map[uint]*Binding
}
func NewManager(logger *pct.Logger, factory MonitorFactory, clock ticker.Manager, spool data.Spooler, im *instance.Repo) *Manager {
m := &Manager{
logger: logger,
factory: factory,
clock: clock,
spool: spool,
im: im,
// --
monitors: make(map[string]Monitor),
status: pct.NewStatus([]string{"mm"}),
aggregators: make(map[uint]*Binding),
mux: &sync.RWMutex{},
}
return m
}
/////////////////////////////////////////////////////////////////////////////
// Interface
/////////////////////////////////////////////////////////////////////////////
// @goroutine[0]
func (m *Manager) Start() error {
// todo: should lock here but we call Handle() which also locks
//m.mux.Lock()
//defer m.mux.Unlock()
if m.running {
return pct.ServiceIsRunningError{Service: "mm"}
}
// Start all metric monitors.
glob := filepath.Join(pct.Basedir.Dir("config"), "mm-*.conf")
configFiles, err := filepath.Glob(glob)
if err != nil {
return err
}
for _, configFile := range configFiles {
data, err := ioutil.ReadFile(configFile)
if err != nil {
m.logger.Error("Read " + configFile + ": " + err.Error())
continue
}
config := &Config{}
if err := json.Unmarshal(data, config); err != nil {
m.logger.Error("Decode " + configFile + ": " + err.Error())
continue
}
cmd := &proto.Cmd{
Ts: time.Now().UTC(),
Cmd: "StartService",
Data: data,
}
reply := m.Handle(cmd)
if reply.Error != "" {
m.logger.Error("Start " + configFile + ": " + reply.Error)
continue
}
m.logger.Info("Started " + configFile)
}
m.running = true
m.logger.Info("Started")
m.status.Update("mm", "Running")
return nil
}
// @goroutine[0]
func (m *Manager) Stop() error {
m.mux.Lock()
defer m.mux.Unlock()
for name, monitor := range m.monitors {
m.status.Update("mm", "Stopping "+name)
if err := monitor.Stop(); err != nil {
m.logger.Warn("Failed to stop " + name + ": " + err.Error())
continue
}
m.clock.Remove(monitor.TickChan())
delete(m.monitors, name)
}
m.running = false
m.logger.Info("Stopped")
m.status.Update("mm", "Stopped")
return nil
}
// @goroutine[0]
func (m *Manager) Handle(cmd *proto.Cmd) *proto.Reply {
m.status.UpdateRe("mm", "Handling", cmd)
defer m.status.Update("mm", "Running")
switch cmd.Cmd {
case "StartService":
mm, name, err := m.getMonitorConfig(cmd)
if err != nil {
return cmd.Reply(nil, err)
}
m.status.UpdateRe("mm", "Starting "+name, cmd)
m.logger.Info("Start", name, cmd)
// Monitors names must be unique.
m.mux.RLock()
_, haveMonitor := m.monitors[name]
m.mux.RUnlock()
if haveMonitor {
return cmd.Reply(nil, errors.New("Duplicate monitor: "+name))
}
// Create the monitor based on its type.
monitor, err := m.factory.Make(mm.Service, mm.InstanceId, cmd.Data)
if err != nil {
return cmd.Reply(nil, errors.New("Factory: "+err.Error()))
}
// Make synchronized (3rd arg=true) ticker for collect interval. It's
// synchronized so all data aligns in charts, else we can get MySQL metrics
// at 00:03 and system metrics at 00:05 and other metrics at 00:06 which
// makes it very difficult to see all metrics at a single point in time
// or meaningfully compare a single interval, e.g. 00:00 to 00:05.
tickChan := make(chan time.Time)
m.clock.Add(tickChan, mm.Collect, true)
// We need one aggregator for each unique report interval. There's usually
// just one: 60s. Remember: report interval != collect interval. Monitors
// can collect at different intervals (typically 1s and 10s), yet all report
// at the same 60s interval, or different report intervals.
a, ok := m.aggregators[mm.Report]
if !ok {
// Make new aggregator for this report interval.
logger := pct.NewLogger(m.logger.LogChan(), fmt.Sprintf("mm-ag-%d", mm.Report))
collectionChan := make(chan *Collection, 5)
aggregator := NewAggregator(logger, int64(mm.Report), collectionChan, m.spool)
aggregator.Start()
// Save aggregator for other monitors with same report interval.
a = &Binding{aggregator, collectionChan}
m.aggregators[mm.Report] = a
m.logger.Info("Created", mm.Report, "second aggregator")
}
// Start the monitor.
if err := monitor.Start(tickChan, a.collectionChan); err != nil {
return cmd.Reply(nil, errors.New("Start "+name+": "+err.Error()))
}
m.mux.Lock()
m.monitors[name] = monitor
m.mux.Unlock()
// Save the monitor-specific config to disk so agent starts on restart.
monitorConfig := monitor.Config()
if err := pct.Basedir.WriteConfig(name, monitorConfig); err != nil {
return cmd.Reply(nil, errors.New("Write "+name+" config:"+err.Error()))
}
return cmd.Reply(nil) // success
case "StopService":
_, name, err := m.getMonitorConfig(cmd)
if err != nil {
return cmd.Reply(nil, err)
}
m.status.UpdateRe("mm", "Stopping "+name, cmd)
m.logger.Info("Stop", name, cmd)
m.mux.RLock()
monitor, ok := m.monitors[name]
m.mux.RUnlock()
if !ok {
return cmd.Reply(nil, errors.New("Unknown monitor: "+name))
}
if err := monitor.Stop(); err != nil {
return cmd.Reply(nil, errors.New("Stop "+name+": "+err.Error()))
}
m.clock.Remove(monitor.TickChan())
if err := pct.Basedir.RemoveConfig(name); err != nil {
return cmd.Reply(nil, errors.New("Remove "+name+": "+err.Error()))
}
m.mux.Lock()
delete(m.monitors, name)
m.mux.Unlock()
return cmd.Reply(nil) // success
case "GetConfig":
config, errs := m.GetConfig()
return cmd.Reply(config, errs...)
default:
// SetConfig does not work by design. To re-configure a monitor,
// stop it then start it again with the new config.
return cmd.Reply(nil, pct.UnknownCmdError{Cmd: cmd.Cmd})
}
}
// @goroutine[1]
func (m *Manager) Status() map[string]string {
status := m.status.All()
m.mux.RLock()
defer m.mux.RUnlock()
for _, monitor := range m.monitors {
monitorStatus := monitor.Status()
for k, v := range monitorStatus {
status[k] = v
}
}
return status
}
func (m *Manager) GetConfig() ([]proto.AgentConfig, []error) {
m.logger.Debug("GetConfig:call")
defer m.logger.Debug("GetConfig:return")
m.mux.RLock()
defer m.mux.RUnlock()
// Manager does not have its own config. It returns all monitors' configs instead.
// Configs are always returned as array of AgentConfig resources.
configs := []proto.AgentConfig{}
errs := []error{}
for _, monitor := range m.monitors {
monitorConfig := monitor.Config()
// Full monitor config as JSON string.
bytes, err := json.Marshal(monitorConfig)
if err != nil {
errs = append(errs, err)
continue
}
// Just the monitor's ServiceInstance, aka ExternalService.
mmConfig := &Config{}
if err := json.Unmarshal(bytes, mmConfig); err != nil {
errs = append(errs, err)
continue
}
config := proto.AgentConfig{
InternalService: "mm",
ExternalService: proto.ServiceInstance{
Service: mmConfig.Service,
InstanceId: mmConfig.InstanceId,
},
Config: string(bytes),
Running: true, // config removed if stopped, so it must be running
}
configs = append(configs, config)
}
return configs, errs
}
func (m *Manager) getMonitorConfig(cmd *proto.Cmd) (*Config, string, error) {
/**
* cmd.Data is a monitor-specific config, e.g. mysql.Config. But monitor-specific
* configs embed mm.Config, so get that first to determine the monitor's name and
* type which is all we need to start it. The monitor itself will decode cmd.Data
* into it's specific config, which we fetch back later by calling monitor.Config()
* to save to disk.
*/
mm := &Config{}
if cmd.Data != nil {
if err := json.Unmarshal(cmd.Data, mm); err != nil {
return nil, "", errors.New("mm.getMonitorConfig:json.Unmarshal:" + err.Error())
}
}
// The real name of the internal service, e.g. mm-mysql-1:
name := "mm-" + m.im.Name(mm.Service, mm.InstanceId)
return mm, name, nil
}