-
Notifications
You must be signed in to change notification settings - Fork 44
/
rrd.go
503 lines (410 loc) · 10.1 KB
/
rrd.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// Simple wrapper for rrdtool C library
package rrd
import (
"fmt"
"math"
"os"
"runtime"
"strings"
"time"
)
type Error string
func (e Error) Error() string {
return string(e)
}
/*
type cstring []byte
func newCstring(s string) cstring {
cs := make(cstring, len(s)+1)
copy(cs, s)
return cs
}
func (cs cstring) p() unsafe.Pointer {
if len(cs) == 0 {
return nil
}
return unsafe.Pointer(&cs[0])
}
func (cs cstring) String() string {
return string(cs[:len(cs)-1])
}
*/
func join(args []interface{}) string {
sa := make([]string, len(args))
for i, a := range args {
var s string
switch v := a.(type) {
case time.Time:
s = i64toa(v.Unix())
default:
s = fmt.Sprint(v)
}
sa[i] = s
}
return strings.Join(sa, ":")
}
type Creator struct {
filename string
start time.Time
step uint
args []string
}
// NewCreator returns new Creator object. You need to call Create to really
// create database file.
// filename - name of database file
// start - don't accept any data timed before or at time specified
// step - base interval in seconds with which data will be fed into RRD
func NewCreator(filename string, start time.Time, step uint) *Creator {
return &Creator{
filename: filename,
start: start,
step: step,
}
}
// DS formats a DS argument and appends it to the list of arguments to be
// passed to rrdcreate(). Each element of args is formatted with fmt.Sprint().
// Please see the rrdcreate(1) manual page for in-depth documentation.
func (c *Creator) DS(name, compute string, args ...interface{}) {
c.args = append(c.args, "DS:"+name+":"+compute+":"+join(args))
}
// RRA formats an RRA argument and appends it to the list of arguments to be
// passed to rrdcreate(). Each element of args is formatted with fmt.Sprint().
// Please see the rrdcreate(1) manual page for in-depth documentation.
func (c *Creator) RRA(cf string, args ...interface{}) {
c.args = append(c.args, "RRA:"+cf+":"+join(args))
}
// Create creates new database file. If overwrite is true it overwrites
// database file if exists. If overwrite is false it returns error if file
// exists (you can use os.IsExist function to check this case).
func (c *Creator) Create(overwrite bool) error {
if !overwrite {
f, err := os.OpenFile(
c.filename,
os.O_WRONLY|os.O_CREATE|os.O_EXCL,
0666,
)
if err != nil {
return err
}
f.Close()
}
return c.create()
}
// Use cstring and unsafe.Pointer to avoid allocations for C calls
type Updater struct {
filename *cstring
template *cstring
args []*cstring
}
func NewUpdater(filename string) *Updater {
u := &Updater{filename: newCstring(filename)}
runtime.SetFinalizer(u, cfree)
return u
}
func cfree(u *Updater) {
u.filename.Free()
u.template.Free()
for _, a := range u.args {
a.Free()
}
}
func (u *Updater) SetTemplate(dsName ...string) {
u.template.Free()
u.template = newCstring(strings.Join(dsName, ":"))
}
// Cache chaches data for later save using Update(). Use it to avoid
// open/read/write/close for every update.
func (u *Updater) Cache(args ...interface{}) {
u.args = append(u.args, newCstring(join(args)))
}
// Update saves data in RRDB.
// Without args Update saves all subsequent updates buffered by Cache method.
// If you specify args it saves them immediately.
func (u *Updater) Update(args ...interface{}) error {
if len(args) != 0 {
cs := newCstring(join(args))
err := u.update([]*cstring{cs})
cs.Free()
return err
} else if len(u.args) != 0 {
err := u.update(u.args)
for _, a := range u.args {
a.Free()
}
u.args = nil
return err
}
return nil
}
type GraphInfo struct {
Print []string
Width, Height uint
Ymin, Ymax float64
}
type Grapher struct {
title string
vlabel string
width, height uint
borderWidth uint
upperLimit float64
lowerLimit float64
rigid bool
altAutoscale bool
altAutoscaleMin bool
altAutoscaleMax bool
noGridFit bool
logarithmic bool
unitsExponent int
unitsLength uint
rightAxisScale float64
rightAxisShift float64
rightAxisLabel string
noLegend bool
lazy bool
colors map[string]string
slopeMode bool
watermark string
base uint
imageFormat string
interlaced bool
daemon string
args []string
}
const (
maxUint = ^uint(0)
maxInt = int(maxUint >> 1)
minInt = -maxInt - 1
defWidth = 2
)
func NewGrapher() *Grapher {
return &Grapher{
upperLimit: -math.MaxFloat64,
lowerLimit: math.MaxFloat64,
unitsExponent: minInt,
borderWidth: defWidth,
colors: make(map[string]string),
}
}
func (g *Grapher) SetTitle(title string) {
g.title = title
}
func (g *Grapher) SetVLabel(vlabel string) {
g.vlabel = vlabel
}
func (g *Grapher) SetSize(width, height uint) {
g.width = width
g.height = height
}
func (g *Grapher) SetBorder(width uint) {
g.borderWidth = width
}
func (g *Grapher) SetLowerLimit(limit float64) {
g.lowerLimit = limit
}
func (g *Grapher) SetUpperLimit(limit float64) {
g.upperLimit = limit
}
func (g *Grapher) SetRigid() {
g.rigid = true
}
func (g *Grapher) SetAltAutoscale() {
g.altAutoscale = true
}
func (g *Grapher) SetAltAutoscaleMin() {
g.altAutoscaleMin = true
}
func (g *Grapher) SetAltAutoscaleMax() {
g.altAutoscaleMax = true
}
func (g *Grapher) SetNoGridFit() {
g.noGridFit = true
}
func (g *Grapher) SetLogarithmic() {
g.logarithmic = true
}
func (g *Grapher) SetUnitsExponent(e int) {
g.unitsExponent = e
}
func (g *Grapher) SetUnitsLength(l uint) {
g.unitsLength = l
}
func (g *Grapher) SetRightAxis(scale, shift float64) {
g.rightAxisScale = scale
g.rightAxisShift = shift
}
func (g *Grapher) SetRightAxisLabel(label string) {
g.rightAxisLabel = label
}
func (g *Grapher) SetNoLegend() {
g.noLegend = true
}
func (g *Grapher) SetLazy() {
g.lazy = true
}
func (g *Grapher) SetColor(colortag, color string) {
g.colors[colortag] = color
}
func (g *Grapher) SetSlopeMode() {
g.slopeMode = true
}
func (g *Grapher) SetImageFormat(format string) {
g.imageFormat = format
}
func (g *Grapher) SetInterlaced() {
g.interlaced = true
}
func (g *Grapher) SetBase(base uint) {
g.base = base
}
func (g *Grapher) SetWatermark(watermark string) {
g.watermark = watermark
}
func (g *Grapher) SetDaemon(daemon string) {
g.daemon = daemon
}
func (g *Grapher) AddOptions(options ...string) {
g.args = append(g.args, options...)
}
func (g *Grapher) push(cmd string, options []string) {
if len(options) > 0 {
cmd += ":" + strings.Join(options, ":")
}
g.args = append(g.args, cmd)
}
func (g *Grapher) Def(vname, rrdfile, dsname, cf string, options ...string) {
g.push(
fmt.Sprintf("DEF:%s=%s:%s:%s", vname, rrdfile, dsname, cf),
options,
)
}
func (g *Grapher) VDef(vname, rpn string) {
g.push("VDEF:"+vname+"="+rpn, nil)
}
func (g *Grapher) CDef(vname, rpn string) {
g.push("CDEF:"+vname+"="+rpn, nil)
}
func (g *Grapher) Print(vname, format string) {
g.push("PRINT:"+vname+":"+format, nil)
}
func (g *Grapher) PrintT(vname, format string) {
g.push("PRINT:"+vname+":"+format+":strftime", nil)
}
func (g *Grapher) GPrint(vname, format string) {
g.push("GPRINT:"+vname+":"+format, nil)
}
func (g *Grapher) GPrintT(vname, format string) {
g.push("GPRINT:"+vname+":"+format+":strftime", nil)
}
func (g *Grapher) Comment(s string) {
g.push("COMMENT:"+s, nil)
}
func (g *Grapher) VRule(t interface{}, color string, options ...string) {
if v, ok := t.(time.Time); ok {
t = v.Unix()
}
vr := fmt.Sprintf("VRULE:%v#%s", t, color)
g.push(vr, options)
}
func (g *Grapher) HRule(value, color string, options ...string) {
hr := "HRULE:" + value + "#" + color
g.push(hr, options)
}
func (g *Grapher) Line(width float32, value, color string, options ...string) {
line := fmt.Sprintf("LINE%f:%s", width, value)
if color != "" {
line += "#" + color
}
g.push(line, options)
}
func (g *Grapher) Area(value, color string, options ...string) {
area := "AREA:" + value
if color != "" {
area += "#" + color
}
g.push(area, options)
}
func (g *Grapher) Tick(vname, color string, options ...string) {
tick := "TICK:" + vname
if color != "" {
tick += "#" + color
}
g.push(tick, options)
}
func (g *Grapher) Shift(vname string, offset interface{}) {
if v, ok := offset.(time.Duration); ok {
offset = int64((v + time.Second/2) / time.Second)
}
shift := fmt.Sprintf("SHIFT:%s:%v", vname, offset)
g.push(shift, nil)
}
func (g *Grapher) TextAlign(align string) {
g.push("TEXTALIGN:"+align, nil)
}
// Graph returns GraphInfo and image as []byte or error
func (g *Grapher) Graph(start, end time.Time) (GraphInfo, []byte, error) {
return g.graph("-", start, end)
}
// SaveGraph saves image to file and returns GraphInfo or error
func (g *Grapher) SaveGraph(filename string, start, end time.Time) (GraphInfo, error) {
gi, _, err := g.graph(filename, start, end)
return gi, err
}
type FetchResult struct {
Filename string
Cf string
Start time.Time
End time.Time
Step time.Duration
DsNames []string
RowCnt int
values []float64
}
func (r *FetchResult) ValueAt(dsIndex, rowIndex int) float64 {
return r.values[len(r.DsNames)*rowIndex+dsIndex]
}
type Exporter struct {
maxRows uint
daemon string
args []string
}
func NewExporter() *Exporter {
return &Exporter{}
}
func (e *Exporter) SetMaxRows(maxRows uint) {
e.maxRows = maxRows
}
func (e *Exporter) push(cmd string, options []string) {
if len(options) > 0 {
cmd += ":" + strings.Join(options, ":")
}
e.args = append(e.args, cmd)
}
func (e *Exporter) Def(vname, rrdfile, dsname, cf string, options ...string) {
e.push(
fmt.Sprintf("DEF:%s=%s:%s:%s", vname, rrdfile, dsname, cf),
options,
)
}
func (e *Exporter) CDef(vname, rpn string) {
e.push("CDEF:"+vname+"="+rpn, nil)
}
func (e *Exporter) XportDef(vname, label string) {
e.push("XPORT:"+vname+":"+label, nil)
}
func (e *Exporter) Xport(start, end time.Time, step time.Duration) (XportResult, error) {
return e.xport(start, end, step)
}
func (e *Exporter) SetDaemon(daemon string) {
e.daemon = daemon
}
type XportResult struct {
Start time.Time
End time.Time
Step time.Duration
Legends []string
RowCnt int
values []float64
}
func (r *XportResult) ValueAt(legendIndex, rowIndex int) float64 {
return r.values[len(r.Legends)*rowIndex+legendIndex]
}