-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathvsphere-graphite.go
438 lines (401 loc) · 11.6 KB
/
vsphere-graphite.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package main
//go:generate git-version
import (
"encoding/json"
"fmt"
"log"
"os"
"os/signal"
"path"
"path/filepath"
"reflect"
"regexp"
"runtime"
"runtime/debug"
"runtime/pprof"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/cblomart/vsphere-graphite/backend"
"github.com/cblomart/vsphere-graphite/config"
"github.com/cblomart/vsphere-graphite/vsphere"
"github.com/takama/daemon"
"code.cloudfoundry.org/bytefmt"
)
const (
// name of the service
name = "vsphere-graphite"
description = "send vsphere stats to graphite"
vcenterdefreg = "^VCENTER_.+=(?P<username>.+):(?P<password>.+)@(?P<hostname>.+)$"
)
var dependencies = []string{}
// Service has embedded daemon
type Service struct {
daemon.Daemon
}
func queryVCenter(vcenter vsphere.VCenter, conf config.Configuration, channel *chan backend.Point, wg *sync.WaitGroup) {
vcenter.Query(conf.Interval, conf.Domain, conf.ReplacePoint, conf.Properties, conf.VCenterResultLimit, conf.VCenterInstanceRatio, channel, wg)
}
// Manage by daemon commands or run the daemon
func (service *Service) Manage() (string, error) {
usage := "Usage: vsphere-graphite install | remove | start | stop | status"
// if received any kind of command, do it
if len(os.Args) > 1 {
command := os.Args[1]
text := usage
var err error
switch command {
case "install":
text, err = service.Install()
case "remove":
text, err = service.Remove()
case "start":
text, err = service.Start()
case "stop":
text, err = service.Stop()
case "status":
text, err = service.Status()
}
return text, err
}
log.Println("Starting daemon:", path.Base(os.Args[0]))
// find file location
basename := path.Base(os.Args[0])
configname := strings.TrimSuffix(basename, filepath.Ext(basename))
location := "/etc/" + configname + ".json"
if _, err := os.Stat(location); err != nil {
location = configname + ".json"
if _, err := os.Stat(location); err != nil {
return "Could not find config location in '.' or '/etc'", err
}
}
// read the configuration
file, err := os.Open(location) // #nosec
if err != nil {
return "Could not open configuration file", err
}
jsondec := json.NewDecoder(file)
conf := config.Configuration{}
err = jsondec.Decode(&conf)
if err != nil {
return "Could not decode configuration file", err
}
// replace all by all properties
all := false
if conf.Properties == nil {
all = true
} else {
for _, property := range conf.Properties {
if strings.ToLower(property) == "all" {
all = true
break
}
}
}
if all {
// Reset properties
conf.Properties = []string{}
// Fill it with all properties keys
for propkey := range vsphere.Properties {
conf.Properties = append(conf.Properties, propkey)
}
}
log.Printf("main: requested properties %s", strings.Join(conf.Properties, ", "))
// default flush size 1000
if conf.FlushSize == 0 {
conf.FlushSize = 1000
}
// default backend prefix to "vsphere"
if len(conf.Backend.Prefix) == 0 {
conf.Backend.Prefix = "vsphere"
}
// default resultlimit
if conf.VCenterResultLimit == 0 {
conf.VCenterResultLimit = 500000
}
// default result ratio
if conf.VCenterInstanceRatio == 0 {
conf.VCenterInstanceRatio = 3.0
}
if conf.CPUProfiling {
f, err := os.OpenFile("/tmp/vsphere-graphite-cpu.pb.gz", os.O_RDWR|os.O_CREATE, 0600) // nolint: vetshadow
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
log.Println("Will write cpu profiling to: ", f.Name())
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
//force backend values to environment variables if present
s := reflect.ValueOf(conf.Backend).Elem()
numfields := s.NumField()
for i := 0; i < numfields; i++ {
f := s.Field(i)
if f.CanSet() {
//exported field
envname := strings.ToUpper(s.Type().Name() + "_" + s.Type().Field(i).Name)
envval := os.Getenv(envname)
if len(envval) > 0 {
//environment variable set with name
switch ftype := f.Type().Name(); ftype {
case "string":
log.Printf("setting config value %s from env. '%s'", s.Type().Field(i).Name, envval)
f.SetString(envval)
case "int":
val, err := strconv.ParseInt(envval, 10, 64) // nolint: vetshadow
if err == nil {
log.Printf("setting config value %s from env. %d", s.Type().Field(i).Name, val)
f.SetInt(val)
}
}
}
}
}
//force vcenter values to environment variables if present
envvcenters := []*vsphere.VCenter{}
validvcenter := regexp.MustCompile(vcenterdefreg)
for _, e := range os.Environ() {
// check if a vcenter definition
if strings.HasPrefix(e, "VCENTER_") {
if !validvcenter.MatchString(e) {
log.Printf("cannot parse vcenter: '%s'\n", e)
continue
}
matches := validvcenter.FindStringSubmatch(e)
names := validvcenter.SubexpNames()
username := ""
password := ""
hostname := ""
for i, match := range matches {
if i == 0 {
continue
}
switch names[i] {
case "username":
username = match
case "password":
password = match
case "hostname":
hostname = match
}
}
if len(username) == 0 {
log.Printf("cannot find username in vcenter: '%s'", e)
continue
}
if len(password) == 0 {
log.Printf("cannot find password in vcenter: '%s'", e)
continue
}
if len(hostname) == 0 {
log.Printf("cannot find hostname in vcenter: '%s'", e)
continue
}
vcenter := vsphere.VCenter{
Username: username,
Password: password,
Hostname: hostname,
}
log.Printf("adding vcenter from env: %s", vcenter.ToString())
envvcenters = append(envvcenters, &vcenter)
}
}
if len(envvcenters) > 0 {
conf.VCenters = envvcenters
log.Println("config vcenter have been replaced by those in env")
}
for _, vcenter := range conf.VCenters {
vcenter.Init(conf.Metrics)
}
queries, err := conf.Backend.Init()
if err != nil {
return "Could not initialize backend", err
}
defer conf.Backend.Disconnect()
if conf.Backend.Type == "prometheus" {
for _, vcenter := range conf.VCenters {
err := conf.Backend.InitPrometheus(vcenter.Hostname)
if err != nil {
return "Could not initialize backend", err
}
}
}
//check properties in function of backend support of metadata
if !conf.Backend.HasMetadata() {
properties := []string{}
for _, confproperty := range conf.Properties {
found := false
for _, metricproperty := range vsphere.MetricProperties {
if strings.EqualFold(confproperty, metricproperty) {
found = true
break
}
}
if found {
properties = append(properties, confproperty)
}
}
conf.Properties = properties
log.Printf("main: properties filtered to '%s' (no metadata in backend)", strings.Join(conf.Properties, ", "))
}
// Set up channel on which to send signal notifications.
// We must use a buffered channel or risk missing the signal
// if we're not ready to receive when the signal is sent.
interrupt := make(chan os.Signal, 1)
//lint:ignore SA1016 in this case we want to quit
signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM) // nolint: megacheck
// Set up a channel to receive the metrics
metrics := make(chan backend.Point, conf.FlushSize)
ticker := time.NewTicker(time.Second * time.Duration(conf.Interval))
defer ticker.Stop()
// Set up a ticker to collect metrics at given interval (except for non scheduled backend)
if !conf.Backend.Scheduled() {
ticker.Stop()
} else {
// Start retriveing and sending metrics
log.Println("Retrieving metrics")
for _, vcenter := range conf.VCenters {
go queryVCenter(*vcenter, conf, &metrics, nil)
}
}
// Memory statistics
var memstats runtime.MemStats
// timer to execute memory collection
memtimer := time.NewTimer(time.Second * time.Duration(10))
// channel to cleanup
cleanup := make(chan bool, 1)
// buffer for points to send
pointbuffer := make([]*backend.Point, conf.FlushSize)
bufferindex := 0
for {
select {
case value := <-metrics:
// reset timer as a point has been received.
// do that in the main thread to avoid collisions
if !memtimer.Stop() {
select {
case <-memtimer.C:
default:
}
}
memtimer.Reset(time.Second * time.Duration(5))
pointbuffer[bufferindex] = &value
bufferindex++
if bufferindex == len(pointbuffer) {
t := make([]*backend.Point, bufferindex)
copy(t, pointbuffer)
ClearBuffer(pointbuffer)
bufferindex = 0
go conf.Backend.SendMetrics(t, false)
log.Printf("sent %d logs to backend\n", len(t))
}
case request := <-*queries:
go func() {
// wait group for non scheduled metric retrival
var wg sync.WaitGroup
log.Printf("adhoc metric retrieval for %s\n", request.Target)
if request.Target != "" {
for _, vcenter := range conf.VCenters {
if request.Target == vcenter.Hostname {
wg.Add(1)
go queryVCenter(*vcenter, conf, request.Request, &wg)
}
}
} else {
wg.Add(len(conf.VCenters))
for _, vcenter := range conf.VCenters {
go queryVCenter(*vcenter, conf, request.Request, &wg)
}
}
wg.Wait()
log.Printf("Completed adhoc metric retrieval for %s\n", request.Target)
close(*request.Request)
cleanup <- true
}()
case <-ticker.C:
// not doing go func as it will create threads itself
log.Println("scheduled metric retrieval")
for _, vcenter := range conf.VCenters {
go queryVCenter(*vcenter, conf, &metrics, nil)
}
case <-memtimer.C:
if !conf.Backend.Scheduled() {
continue
}
// sent remaining values
// copy to send point to appart buffer
t := make([]*backend.Point, bufferindex)
copy(t, pointbuffer)
// clear main buffer
ClearBuffer(pointbuffer)
bufferindex = 0
// send sent buffer
go conf.Backend.SendMetrics(t, true)
log.Printf("sent last %d logs to backend\n", len(t))
// empty point buffer
cleanup <- true
case <-cleanup:
go func() {
runtime.GC()
debug.FreeOSMemory()
runtime.ReadMemStats(&memstats)
log.Printf("memory usage: sys=%s alloc=%s\n", bytefmt.ByteSize(memstats.Sys), bytefmt.ByteSize(memstats.Alloc))
log.Printf("go routines: %d", runtime.NumGoroutine())
if conf.MEMProfiling {
f, err := os.OpenFile("/tmp/vsphere-graphite-mem.pb.gz", os.O_RDWR|os.O_CREATE, 0600) // nolint.vetshaddow
if err != nil {
log.Fatal("could not create Mem profile: ", err)
}
defer func() {
e := f.Close()
if err == nil {
err = e
}
}()
log.Println("Will write mem profiling to: ", f.Name())
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write Mem profile: ", err)
}
if err := f.Close(); err != nil {
log.Fatal("could close Mem profile: ", err)
}
}
}()
case killSignal := <-interrupt:
log.Println("Got signal:", killSignal)
if bufferindex > 0 {
conf.Backend.SendMetrics(pointbuffer[:bufferindex], true)
log.Printf("Sent %d logs to backend", bufferindex)
}
if killSignal == os.Interrupt {
return "Daemon was interrupted by system signal", nil
}
return "Daemon was killed", nil
}
}
}
// ClearBuffer : set all values in pointer array to nil
func ClearBuffer(buffer []*backend.Point) {
for i := 0; i < len(buffer); i++ {
buffer[i] = nil
}
}
func main() {
log.Printf("Version information: %s - %s@%s (%s)", gitTag, gitShortCommit, gitBranch, gitStatus)
srv, err := daemon.New(name, description, daemon.SystemDaemon, dependencies...)
if err != nil {
log.Println("Error: ", err)
os.Exit(1)
}
service := &Service{srv}
status, err := service.Manage()
if err != nil {
log.Println(status, "Error: ", err)
os.Exit(1)
}
fmt.Println(status)
}