-
Notifications
You must be signed in to change notification settings - Fork 183
/
monitorcore.go
130 lines (107 loc) · 3.75 KB
/
monitorcore.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
// +build !windows
package collectd
import (
"bytes"
"fmt"
"os"
"path/filepath"
"sync"
"text/template"
"github.com/pkg/errors"
"github.com/signalfx/signalfx-agent/internal/core/config"
"github.com/signalfx/signalfx-agent/internal/monitors/types"
log "github.com/sirupsen/logrus"
)
// MonitorCore contains common data/logic for collectd monitors, mainly
// stuff related to templating of the plugin config files. This should
// generally not be used directly, but rather one of the structs that embeds
// this: StaticMonitorCore or ServiceMonitorCore.
type MonitorCore struct {
Template *template.Template
Output types.Output
// Where to write the plugin config to on the filesystem
configFilename string
config config.MonitorCustomConfig
monitorID types.MonitorID
lock sync.Mutex
UsesGenericJMX bool
collectdInstanceOverride *Manager
}
// NewMonitorCore creates a new initialized but unconfigured MonitorCore with
// the given template.
func NewMonitorCore(template *template.Template) *MonitorCore {
return &MonitorCore{
Template: template,
}
}
// Init generates a unique file name for each distinct monitor instance
func (bm *MonitorCore) Init() error {
InjectTemplateFuncs(bm.Template)
return nil
}
// SetCollectdInstance allows you to override the instance of collectd used by
// this monitor
func (bm *MonitorCore) SetCollectdInstance(instance *Manager) {
bm.collectdInstanceOverride = instance
}
func (bm *MonitorCore) collectdInstance() *Manager {
if bm.collectdInstanceOverride != nil {
return bm.collectdInstanceOverride
}
return MainInstance()
}
// SetConfigurationAndRun sets the configuration to be used when rendering
// templates, and writes config before queueing a collectd restart.
func (bm *MonitorCore) SetConfigurationAndRun(conf config.MonitorCustomConfig) error {
bm.lock.Lock()
defer bm.lock.Unlock()
bm.config = conf
bm.monitorID = conf.MonitorConfigCore().MonitorID
bm.configFilename = fmt.Sprintf("20-%s.%s.conf", bm.Template.Name(), string(bm.monitorID))
if err := bm.WriteConfigForPlugin(); err != nil {
return err
}
return bm.SetConfiguration(conf)
}
// SetConfiguration adds various fields from the config to the template context
// but does not render the config.
func (bm *MonitorCore) SetConfiguration(conf config.MonitorCustomConfig) error {
return bm.collectdInstance().ConfigureFromMonitor(bm.monitorID, bm.Output, bm.UsesGenericJMX)
}
// WriteConfigForPlugin will render the config template to the filesystem and
// queue a collectd restart
func (bm *MonitorCore) WriteConfigForPlugin() error {
pluginConfigText := bytes.Buffer{}
err := bm.Template.Execute(&pluginConfigText, bm.config)
if err != nil {
return errors.Wrapf(err, "Could not render collectd config file for %s. Context was %#v",
bm.Template.Name(), bm.config)
}
log.WithFields(log.Fields{
"renderPath": bm.renderPath(),
"context": bm.config,
}).Debug("Writing collectd plugin config file")
if err := WriteConfFile(pluginConfigText.String(), bm.renderPath()); err != nil {
log.WithFields(log.Fields{
"error": err,
"path": bm.renderPath(),
}).Error("Could not render collectd plugin config")
return err
}
return nil
}
func (bm *MonitorCore) renderPath() string {
return filepath.Join(bm.collectdInstance().ManagedConfigDir(), bm.configFilename)
}
// RemoveConfFile deletes the collectd config file for this monitor
func (bm *MonitorCore) RemoveConfFile() {
os.Remove(bm.renderPath())
}
// Shutdown removes the config file and restarts collectd
func (bm *MonitorCore) Shutdown() {
log.WithFields(log.Fields{
"path": bm.renderPath(),
}).Debug("Removing collectd plugin config")
bm.RemoveConfFile()
bm.collectdInstance().MonitorDidShutdown(bm.monitorID)
}