-
Notifications
You must be signed in to change notification settings - Fork 183
/
haproxy.go
91 lines (78 loc) · 2.55 KB
/
haproxy.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
package haproxy
import (
"errors"
"fmt"
"strings"
"github.com/signalfx/signalfx-agent/internal/monitors/collectd"
"github.com/signalfx/signalfx-agent/internal/core/config"
"github.com/signalfx/signalfx-agent/internal/monitors"
"github.com/signalfx/signalfx-agent/internal/monitors/collectd/python"
"github.com/signalfx/signalfx-agent/internal/monitors/pyrunner"
)
const monitorType = "collectd/haproxy"
// MONITOR(collectd/haproxy): Monitors an HAProxy instance.
//
// See https://github.com/signalfx/integrations/tree/master/collectd-haproxy.
func init() {
monitors.Register(monitorType, func() interface{} {
return &Monitor{
python.PyMonitor{
MonitorCore: pyrunner.New("sfxcollectd"),
},
}
}, &Config{})
}
// Config is the monitor-specific config with the generic config embedded
type Config struct {
config.MonitorConfig `yaml:",inline" acceptsEndpoints:"true"`
pyConf *python.Config
Host string `yaml:"host" validate:"required"`
Port uint16 `yaml:"port"`
ProxiesToMonitor []string `yaml:"proxiesToMonitor"`
ExcludedMetrics []string `yaml:"excludedMetrics"`
EnhancedMetrics *bool `yaml:"enhancedMetrics"`
}
// PythonConfig returns the embedded python.Config struct from the interface
func (c *Config) PythonConfig() *python.Config {
return c.pyConf
}
// Validate check config if host is TCP or socket. TCP requires port.
func (c *Config) Validate() error {
if !strings.HasPrefix(c.Host, "/") && c.Port == 0 {
return errors.New("when using TCP for HAProxy connection, port must be specified")
}
return nil
}
// Monitor is the main type that represents the monitor
type Monitor struct {
python.PyMonitor
}
// Configure configures and runs the plugin in collectd
func (m *Monitor) Configure(conf *Config) error {
socket := conf.Host
if conf.Port != 0 {
socket += fmt.Sprintf(":%d", conf.Port)
}
conf.pyConf = &python.Config{
MonitorConfig: conf.MonitorConfig,
Host: conf.Host,
Port: conf.Port,
ModuleName: "haproxy",
ModulePaths: []string{collectd.MakePath("haproxy")},
TypesDBPaths: []string{collectd.MakePath("types.db")},
PluginConfig: map[string]interface{}{
"Socket": socket,
"Interval": conf.IntervalSeconds,
"EnhancedMetrics": conf.EnhancedMetrics,
"ProxyMonitor": map[string]interface{}{
"#flatten": true,
"values": conf.ProxiesToMonitor,
},
"ExcludeMetric": map[string]interface{}{
"#flatten": true,
"values": conf.ExcludedMetrics,
},
},
}
return m.PyMonitor.Configure(conf)
}