-
Notifications
You must be signed in to change notification settings - Fork 21
/
sensu.go
239 lines (192 loc) · 4.8 KB
/
sensu.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
package sensu
import (
"encoding/json"
"strconv"
"strings"
"sync"
log "github.com/Sirupsen/logrus"
"github.com/amonapp/amonagent/internal/util"
"github.com/amonapp/amonagent/plugins"
)
// Start - XXX
func (s *Sensu) Start() error {
return nil
}
// Stop - XXX
func (s *Sensu) Stop() {
}
// Description - XXX
func (s *Sensu) Description() string {
return "Collects data from Sensu plugins"
}
var sampleConfig = `
# Available config options:
#
# [
# "metrics-es-node-graphite.rb",
# "metrics-net.rb",
# "metrics-redis-graphite.rb",
# "metrics-iostat-extended.rb"
# ]
#
# List of preinstalled sensu plugins + params
#
# Config location: /etc/opt/amonagent/plugins-enabled/sensu.conf
`
// SampleConfig - XXX
func (s *Sensu) SampleConfig() string {
return sampleConfig
}
// Sensu - XXX
type Sensu struct {
Config Config
}
// Config - XXX
type Config struct {
Commands []util.Command `json:"commands"`
}
// SetConfigDefaults - XXX
func (s *Sensu) SetConfigDefaults() error {
// Commands already set. For example - in the test suite
if len(s.Config.Commands) > 0 {
return nil
}
configFile, err := plugins.ReadPluginConfig("sensu")
if err != nil {
log.WithFields(log.Fields{
"plugin": "sensu",
"error": err,
}).Error("Can't read config file")
return err
}
var Commands []util.Command
var CommandStrings []string
if e := json.Unmarshal(configFile, &CommandStrings); e != nil {
log.WithFields(log.Fields{"plugin": "sensu", "error": e.Error()}).Error("Can't decode JSON file")
return e
}
for _, str := range CommandStrings {
var command = util.Command{Command: str}
Commands = append(Commands, command)
}
s.Config.Commands = Commands
return nil
}
func (m Metric) String() string {
s, _ := json.Marshal(m)
return string(s)
}
// Metric - XXX
type Metric struct {
Plugin string `json:"plugin"`
Gauge string `json:"gauge"`
Value string `json:"value"`
}
// ParsedLine - XXX
type ParsedLine struct {
Plugin string
Elements []string
Value string
}
// ParseLine - XXX
func (s *Sensu) ParseLine(lineToParse string) (Metric, error) {
// split by space
f := func(c rune) bool {
return c == ' '
}
// split metric name by .
dot := func(c rune) bool {
return c == '.'
}
//split metric name by _
underscore := func(c rune) bool {
return c == '_'
}
fields := strings.FieldsFunc(lineToParse, f)
line := ParsedLine{}
m := Metric{}
if len(fields) == 3 {
toFloat, _ := strconv.ParseFloat(fields[1], 64)
value := strconv.FormatFloat(toFloat, 'f', -1, 64)
metricFields := strings.FieldsFunc(fields[0], dot)
var cleanName string
var pluginName string
// Eliminate host and plugin name here
// Example ubuntu.elasticsearch.thread_pool......
if len(metricFields) > 2 {
cleanName = strings.Join(metricFields[2:], ".")
pluginName = "sensu." + metricFields[1]
} else {
// Example response_time ......
cleanName = strings.Join(metricFields[:], ".")
pluginName = "sensu." + cleanName
}
CleanMetricFields := strings.FieldsFunc(cleanName, dot)
splitOnUnderscore := strings.FieldsFunc(cleanName, underscore)
// Standart use case here
// Example: thread_pool.search.active
if len(CleanMetricFields) > 1 {
line.Elements = CleanMetricFields
} else {
line.Elements = splitOnUnderscore
}
elements := line.Elements
if len(elements) > 2 {
chart := strings.Join(elements[:2], "_")
line := strings.Join(elements[2:], "_")
m.Gauge = chart + "." + line
} else {
chart := elements[0]
line := strings.Join(elements[1:], "_")
m.Gauge = chart + "." + line
}
m.Value = value
m.Plugin = pluginName
}
return m, nil
}
// Collect - XXX
// This should return the following: sensu.plugin_name: {"gauges": {}}, sensu.another_plugin :{"gauges":{}}
func (s *Sensu) Collect() (interface{}, error) {
s.SetConfigDefaults()
var wg sync.WaitGroup
plugins := make(map[string]interface{})
resultChan := make(chan util.CommandResult, len(s.Config.Commands))
for _, command := range s.Config.Commands {
wg.Add(1)
go func(command util.Command) {
CheckResult := util.ExecWithExitCode(command)
resultChan <- CheckResult
defer wg.Done()
}(command)
}
wg.Wait()
close(resultChan)
for command := range resultChan {
var result []Metric
gauges := make(map[string]string)
GaugesWrapper := make(map[string]map[string]string)
plugin := ""
lines := strings.Split(command.Output, "\n")
for _, line := range lines {
metric, _ := s.ParseLine(line)
if len(metric.Gauge) > 0 {
result = append(result, metric)
}
}
for _, r := range result {
gauges[r.Gauge] = r.Value
plugin = r.Plugin
}
GaugesWrapper["gauges"] = gauges
if len(plugin) > 0 {
plugins[plugin] = GaugesWrapper
}
}
return plugins, nil
}
func init() {
plugins.Add("sensu", func() plugins.Plugin {
return &Sensu{}
})
}