forked from yubo/rrdlite
-
Notifications
You must be signed in to change notification settings - Fork 16
/
rrd.go
156 lines (133 loc) · 3.2 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
// Simple wrapper for rrdtool C library
package rrdlite
import (
"fmt"
"os"
"strings"
"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 = 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,
}
}
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))
}
// 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 alocations for C calls
type Updater struct {
filename cstring
template cstring
args []string
}
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, 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 {
a := make([]string, 1)
a[0] = join(args)
return u.update(a)
} else if len(u.args) != 0 {
err := u.update(u.args)
u.args = nil
return err
}
return nil
}
const (
maxUint = ^uint(0)
maxInt = int(maxUint >> 1)
minInt = -maxInt - 1
)
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]
}