forked from ziutek/rrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rrd.go
336 lines (274 loc) · 6.65 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
// Simple wrapper for rrdtool C library
package rrd
import (
"fmt"
"math"
"os"
"strings"
"sync"
"time"
"unsafe"
)
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 = fmt.Sprint(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,
}
}
func (c *Creator) DS(name, compute string, args ...interface{}) {
c.args = append(c.args, "DS:"+name+":"+compute+":"+join(args))
}
func (c *Creator) RRA(cf string, args ...interface{}) {
c.args = append(c.args, "RRA:"+cf+":"+join(args))
}
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 alocations for C calls
type Updater struct {
filename cstring
template cstring
args []unsafe.Pointer
}
func NewUpdater(filename string) *Updater {
return &Updater{filename: newCstring(filename)}
}
func (u *Updater) SetTemplate(dsName ...string) {
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)).p())
}
// 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 {
a := make([]unsafe.Pointer, 1)
a[0] = newCstring(join(args)).p()
return u.update(a)
} else if len(u.args) != 0 {
err := u.update(u.args)
u.args = nil
return err
}
return nil
}
type GraphInfo struct {
Print []string
Width, Height uint
Ymin, Ymax float64
}
type Grapher struct {
m sync.Mutex
title string
vlabel string
width, height uint
upperLimit float64
lowerLimit float64
rigid bool
altAutoscale bool
altAutoscaleMin bool
altAutoscaleMax bool
noGridFit bool
logarithmic bool
noLegand bool
lazy bool
color string
slopeMode bool
args []string
}
func NewGrapher() *Grapher {
return &Grapher{
upperLimit: -math.MaxFloat64,
lowerLimit: math.MaxFloat64,
}
}
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) 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) SetNoLegand() {
g.noLegand = true
}
func (g *Grapher) SetLazy() {
g.lazy = true
}
func (g *Grapher) SetColor(colortag, color string) {
g.color = colortag + "#" + color
}
func (g *Grapher) SetSlopeMode() {
g.slopeMode = true
}
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:%s#%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:%s", 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
}