-
Notifications
You must be signed in to change notification settings - Fork 21
/
custom.go
208 lines (167 loc) · 4.22 KB
/
custom.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
package custom
import (
"encoding/json"
"strconv"
"strings"
"sync"
log "github.com/Sirupsen/logrus"
"github.com/amonapp/amonagent/internal/util"
"github.com/amonapp/amonagent/plugins"
)
// Metric - XXX
type Metric struct {
Name string `json:"name"`
Value float64 `json:"value"`
Type string `json:"type"`
}
// ParseLine - XXX
func ParseLine(s string) (Metric, error) {
// Split line by : and | -> name:value|type (requests.per_second:100|gauge)
f := func(c rune) bool {
return c == '|' || c == ':'
}
fields := strings.FieldsFunc(s, f)
m := Metric{}
if len(fields) == 3 {
if s, err := strconv.ParseFloat(fields[1], 64); err == nil {
m = Metric{Name: fields[0], Value: s, Type: fields[2]}
}
}
return m, nil
}
// Start - XXX
func (c *Custom) Start() error { return nil }
// Stop - XXX
func (c *Custom) Stop() {}
var sampleConfig = `
# Available config options:
#
# A JSON list with commands
# [
# {
# "command":"python /path/to/yourplugin.py",
# "name":"requests"
# },
# {
# "command":"python /path/to/yourpingplugin.py",
# "name":"ping"
# },
# ]
#
# You can create a custom plugin in any language. Amon parses all the lines from STDOUT:
#
# Format: metric.line_on_chart_name:value|type
# Example:
# requests.per_second:12|gauge
#
# Available types: counter and gauge
#
# To group metrics on a single chart:
#
# connections.active:12|gauge
# connections.waiting:12|gauge
#
# In Python you can emmit metrics with:
# print "requests.per_second:12|gauge"
#
# In Bash with printf "requests.per_second:12|gauge\n", Ruby "puts requests.per_second:12|gauge"
#
# Config location: /etc/opt/amonagent/plugins-enabled/custom.conf
`
// SampleConfig - XXX
func (c *Custom) SampleConfig() string {
return sampleConfig
}
// Custom - XXX
type Custom struct {
Config Config
}
// Config - XXX
type Config struct {
Commands []util.Command `json:"commands"`
}
// SetConfigDefaults - XXX
func (c *Custom) SetConfigDefaults() error {
// Commands already set. For example - in the test suite
if len(c.Config.Commands) > 0 {
return nil
}
configFile, err := plugins.ReadPluginConfig("custom")
if err != nil {
log.WithFields(log.Fields{
"plugin": "custom",
"error": err,
}).Error("Can't read config file")
}
var Commands []util.Command
if e := json.Unmarshal(configFile, &Commands); e != nil {
log.WithFields(log.Fields{"plugin": "custom", "error": e.Error()}).Error("Can't decode JSON file")
}
c.Config.Commands = Commands
return nil
}
func (p PerformanceStruct) String() string {
s, _ := json.Marshal(p)
return string(s)
}
// PerformanceStruct - XXX
type PerformanceStruct struct {
Gauges map[string]interface{} `json:"gauges,omitempty"`
Counters map[string]interface{} `json:"counters,omitempty"`
}
func (p PerformanceStructBlock) String() string {
s, _ := json.Marshal(p)
return string(s)
}
// PerformanceStructBlock - XXX
type PerformanceStructBlock map[string]PerformanceStruct
// Description - XXX
func (c *Custom) Description() string {
return "Collects metrics from custom collectors"
}
// Collect - XXX
func (c *Custom) Collect() (interface{}, error) {
c.SetConfigDefaults()
var wg sync.WaitGroup
results := make(PerformanceStructBlock, 0)
resultChan := make(chan util.CommandResult, len(c.Config.Commands))
for _, v := range c.Config.Commands {
wg.Add(1)
go func(v util.Command) {
CheckResult := util.ExecWithExitCode(v)
resultChan <- CheckResult
defer wg.Done()
}(v)
}
wg.Wait()
close(resultChan)
for i := range resultChan {
PerformanceStruct := PerformanceStruct{}
lines := strings.Split(i.Output, "\n")
gauges := make(map[string]interface{})
counters := make(map[string]interface{})
for _, line := range lines {
metric, _ := ParseLine(line)
if metric.Type == "gauge" {
gauges[metric.Name] = metric.Value
}
if metric.Type == "counter" {
counters[metric.Name] = metric.Value
}
pluginName := i.Name
if len(gauges) > 0 {
PerformanceStruct.Gauges = gauges
}
if len(counters) > 0 {
PerformanceStruct.Counters = counters
}
results[pluginName] = PerformanceStruct
}
}
return results, nil
}
func init() {
plugins.Add("custom", func() plugins.Plugin {
return &Custom{}
})
}