forked from open-falcon/rrdlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rrd_c.go
302 lines (261 loc) · 6.45 KB
/
rrd_c.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
package rrdlite
/*
#include <stdlib.h>
#include "rrd.h"
#include "rrdfunc.h"
#cgo CFLAGS: -std=c99 -DRRD_LITE -D_BSD_SOURCE -DHAVE_CONFIG_H -D_POSIX_SOURCE -DNUMVERS=1.4009
#cgo LDFLAGS: -lm
*/
import "C"
import (
"reflect"
"strconv"
"strings"
"sync"
"time"
"unsafe"
)
var mutex sync.Mutex
func makeArgs(args []string) []*C.char {
ret := make([]*C.char, len(args))
for i, s := range args {
ret[i] = C.CString(s)
}
return ret
}
func freeCString(s *C.char) {
C.free(unsafe.Pointer(s))
}
func freeArgs(cArgs []*C.char) {
for _, s := range cArgs {
freeCString(s)
}
}
func makeError(e *C.char) error {
var null *C.char
if e == null {
return nil
}
return Error(C.GoString(e))
}
func (c *Creator) create() error {
filename := C.CString(c.filename)
defer freeCString(filename)
args := makeArgs(c.args)
defer freeArgs(args)
e := C.rrdCreate(
filename,
C.ulong(c.step),
C.time_t(c.start.Unix()),
C.int(len(args)),
&args[0],
)
return makeError(e)
}
func (u *Updater) update(args []unsafe.Pointer) error {
e := C.rrdUpdate(
(*C.char)(u.filename.p()),
(*C.char)(u.template.p()),
C.int(len(args)),
(**C.char)(unsafe.Pointer(&args[0])),
)
return makeError(e)
}
var (
oStart = C.CString("-s")
oEnd = C.CString("-e")
oTitle = C.CString("-t")
oVlabel = C.CString("-v")
oWidth = C.CString("-w")
oHeight = C.CString("-h")
oUpperLimit = C.CString("-u")
oLowerLimit = C.CString("-l")
oRigid = C.CString("-r")
oAltAutoscale = C.CString("-A")
oAltAutoscaleMin = C.CString("-J")
oAltAutoscaleMax = C.CString("-M")
oNoGridFit = C.CString("-N")
oLogarithmic = C.CString("-o")
oUnitsExponent = C.CString("-X")
oUnitsLength = C.CString("-L")
oRightAxis = C.CString("--right-axis")
oRightAxisLabel = C.CString("--right-axis-label")
oDaemon = C.CString("--daemon")
oNoLegend = C.CString("-g")
oLazy = C.CString("-z")
oColor = C.CString("-c")
oSlopeMode = C.CString("-E")
oImageFormat = C.CString("-a")
oInterlaced = C.CString("-i")
oBase = C.CString("-b")
oWatermark = C.CString("-W")
oStep = C.CString("--step")
oMaxRows = C.CString("-m")
)
func ftoa(f float64) string {
return strconv.FormatFloat(f, 'e', 10, 64)
}
func ftoc(f float64) *C.char {
return C.CString(ftoa(f))
}
func i64toa(i int64) string {
return strconv.FormatInt(i, 10)
}
func i64toc(i int64) *C.char {
return C.CString(i64toa(i))
}
func u64toa(u uint64) string {
return strconv.FormatUint(u, 10)
}
func u64toc(u uint64) *C.char {
return C.CString(u64toa(u))
}
func itoa(i int) string {
return i64toa(int64(i))
}
func itoc(i int) *C.char {
return i64toc(int64(i))
}
func utoa(u uint) string {
return u64toa(uint64(u))
}
func utoc(u uint) *C.char {
return u64toc(uint64(u))
}
func parseInfoKey(ik string) (kname, kkey string, kid int) {
kid = -1
o := strings.IndexRune(ik, '[')
if o == -1 {
kname = ik
return
}
c := strings.IndexRune(ik[o+1:], ']')
if c == -1 {
kname = ik
return
}
c += o + 1
kname = ik[:o] + ik[c+1:]
kkey = ik[o+1 : c]
if strings.HasPrefix(kname, "ds.") {
return
} else if id, err := strconv.Atoi(kkey); err == nil && id >= 0 {
kid = id
}
return
}
func updateInfoValue(i *C.struct_rrd_info_t, v interface{}) interface{} {
switch i._type {
case C.RD_I_VAL:
return float64(*(*C.rrd_value_t)(unsafe.Pointer(&i.value[0])))
case C.RD_I_CNT:
return uint(*(*C.ulong)(unsafe.Pointer(&i.value[0])))
case C.RD_I_STR:
return C.GoString(*(**C.char)(unsafe.Pointer(&i.value[0])))
case C.RD_I_INT:
return int(*(*C.int)(unsafe.Pointer(&i.value[0])))
case C.RD_I_BLO:
blob := *(*C.rrd_blob_t)(unsafe.Pointer(&i.value[0]))
b := C.GoBytes(unsafe.Pointer(blob.ptr), C.int(blob.size))
if v == nil {
return b
}
return append(v.([]byte), b...)
}
return nil
}
func parseRRDInfo(i *C.rrd_info_t) map[string]interface{} {
defer C.rrd_info_free(i)
r := make(map[string]interface{})
for w := (*C.struct_rrd_info_t)(i); w != nil; w = w.next {
kname, kkey, kid := parseInfoKey(C.GoString(w.key))
v, ok := r[kname]
switch {
case kid != -1:
var a []interface{}
if ok {
a = v.([]interface{})
}
if len(a) < kid+1 {
oldA := a
a = make([]interface{}, kid+1)
copy(a, oldA)
}
a[kid] = updateInfoValue(w, a[kid])
v = a
case kkey != "":
var m map[string]interface{}
if ok {
m = v.(map[string]interface{})
} else {
m = make(map[string]interface{})
}
old, _ := m[kkey]
m[kkey] = updateInfoValue(w, old)
v = m
default:
v = updateInfoValue(w, v)
}
r[kname] = v
}
return r
}
// Info returns information about RRD file.
func Info(filename string) (map[string]interface{}, error) {
fn := C.CString(filename)
defer freeCString(fn)
var i *C.rrd_info_t
err := makeError(C.rrdInfo(&i, fn))
if err != nil {
return nil, err
}
return parseRRDInfo(i), nil
}
// Fetch retrieves data from RRD file.
func Fetch(filename, cf string, start, end time.Time, step time.Duration) (FetchResult, error) {
fn := C.CString(filename)
defer freeCString(fn)
cCf := C.CString(cf)
defer freeCString(cCf)
cStart := C.time_t(start.Unix())
cEnd := C.time_t(end.Unix())
cStep := C.ulong(step.Seconds())
var (
ret C.int
cDsCnt C.ulong
cDsNames **C.char
cData *C.double
)
err := makeError(C.rrdFetch(&ret, fn, cCf, &cStart, &cEnd, &cStep, &cDsCnt, &cDsNames, &cData))
if err != nil {
return FetchResult{filename, cf, start, end, step, nil, 0, nil}, err
}
start = time.Unix(int64(cStart), 0)
end = time.Unix(int64(cEnd), 0)
step = time.Duration(cStep) * time.Second
dsCnt := int(cDsCnt)
dsNames := make([]string, dsCnt)
for i := 0; i < dsCnt; i++ {
dsName := C.arrayGetCString(cDsNames, C.int(i))
dsNames[i] = C.GoString(dsName)
C.free(unsafe.Pointer(dsName))
}
C.free(unsafe.Pointer(cDsNames))
rowCnt := (int(cEnd)-int(cStart))/int(cStep) + 1
valuesLen := dsCnt * rowCnt
var values []float64
sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&values)))
sliceHeader.Cap = valuesLen
sliceHeader.Len = valuesLen
sliceHeader.Data = uintptr(unsafe.Pointer(cData))
return FetchResult{filename, cf, start, end, step, dsNames, rowCnt, values}, nil
}
// FreeValues free values memory allocated by C.
func (r *FetchResult) FreeValues() {
sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&r.values)))
C.free(unsafe.Pointer(sliceHeader.Data))
}
// Values returns copy of internal array of values.
func (r *FetchResult) Values() []float64 {
return append([]float64{}, r.values...)
}