-
Notifications
You must be signed in to change notification settings - Fork 44
/
rrd_test.go
194 lines (173 loc) · 4.36 KB
/
rrd_test.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
package rrd
import (
"fmt"
"io/ioutil"
"testing"
"time"
)
func TestAll(t *testing.T) {
// Create
const (
dbfile = "/tmp/test.rrd"
step = 1
heartbeat = 2 * step
)
c := NewCreator(dbfile, time.Now(), step)
c.RRA("AVERAGE", 0.5, 1, 100)
c.RRA("AVERAGE", 0.5, 5, 100)
c.DS("cnt", "COUNTER", heartbeat, 0, 100)
c.DS("g", "GAUGE", heartbeat, 0, 60)
err := c.Create(true)
if err != nil {
t.Fatal(err)
}
// Update
u := NewUpdater(dbfile)
for i := 0; i < 10; i++ {
time.Sleep(step * time.Second)
err := u.Update(time.Now(), i, 1.5*float64(i))
if err != nil {
t.Fatal(err)
}
}
// Update with cache
for i := 10; i < 20; i++ {
time.Sleep(step * time.Second)
u.Cache(time.Now(), i, 2*float64(i))
}
err = u.Update()
if err != nil {
t.Fatal(err)
}
// Info
inf, err := Info(dbfile)
if err != nil {
t.Fatal(err)
}
for k, v := range inf {
fmt.Printf("%s (%T): %v\n", k, v, v)
}
// Graph
g := NewGrapher()
g.SetTitle("Test")
g.SetVLabel("some variable")
g.SetSize(800, 300)
g.SetWatermark("some watermark")
g.Def("v1", dbfile, "g", "AVERAGE")
g.Def("v2", dbfile, "cnt", "AVERAGE")
g.VDef("max1", "v1,MAXIMUM")
g.VDef("avg2", "v2,AVERAGE")
g.Line(1, "v1", "ff0000", "var 1")
g.Area("v2", "0000ff", "var 2")
g.GPrintT("max1", "max1 at %c")
g.GPrint("avg2", "avg2=%lf")
g.PrintT("max1", "max1 at %c")
g.Print("avg2", "avg2=%lf")
now := time.Now()
i, err := g.SaveGraph("/tmp/test_rrd1.png", now.Add(-20*time.Second), now)
fmt.Printf("%+v\n", i)
if err != nil {
t.Fatal(err)
}
i, buf, err := g.Graph(now.Add(-20*time.Second), now)
fmt.Printf("%+v\n", i)
if err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile("/tmp/test_rrd2.png", buf, 0666)
if err != nil {
t.Fatal(err)
}
// Fetch
end := time.Unix(int64(inf["last_update"].(uint)), 0)
start := end.Add(-20 * step * time.Second)
fmt.Printf("Fetch Params:\n")
fmt.Printf("Start: %s\n", start)
fmt.Printf("End: %s\n", end)
fmt.Printf("Step: %s\n", step*time.Second)
fetchRes, err := Fetch(dbfile, "AVERAGE", start, end, step*time.Second)
if err != nil {
t.Fatal(err)
}
defer fetchRes.FreeValues()
fmt.Printf("FetchResult:\n")
fmt.Printf("Start: %s\n", fetchRes.Start)
fmt.Printf("End: %s\n", fetchRes.End)
fmt.Printf("Step: %s\n", fetchRes.Step)
for _, dsName := range fetchRes.DsNames {
fmt.Printf("\t%s", dsName)
}
fmt.Printf("\n")
row := 0
for ti := fetchRes.Start.Add(fetchRes.Step); ti.Before(end) || ti.Equal(end); ti = ti.Add(fetchRes.Step) {
fmt.Printf("%s / %d", ti, ti.Unix())
for i := 0; i < len(fetchRes.DsNames); i++ {
v := fetchRes.ValueAt(i, row)
fmt.Printf("\t%e", v)
}
fmt.Printf("\n")
row++
}
// Xport
end = time.Unix(int64(inf["last_update"].(uint)), 0)
start = end.Add(-20 * step * time.Second)
fmt.Printf("Xport Params:\n")
fmt.Printf("Start: %s\n", start)
fmt.Printf("End: %s\n", end)
fmt.Printf("Step: %s\n", step*time.Second)
e := NewExporter()
e.Def("def1", dbfile, "cnt", "AVERAGE")
e.Def("def2", dbfile, "g", "AVERAGE")
e.CDef("vdef1", "def1,def2,+")
e.XportDef("def1", "cnt")
e.XportDef("def2", "g")
e.XportDef("vdef1", "sum")
xportRes, err := e.Xport(start, end, step*time.Second)
if err != nil {
t.Fatal(err)
}
defer xportRes.FreeValues()
fmt.Printf("XportResult:\n")
fmt.Printf("Start: %s\n", xportRes.Start)
fmt.Printf("End: %s\n", xportRes.End)
fmt.Printf("Step: %s\n", xportRes.Step)
for _, legend := range xportRes.Legends {
fmt.Printf("\t%s", legend)
}
fmt.Printf("\n")
row = 0
for ti := xportRes.Start.Add(xportRes.Step); ti.Before(end) || ti.Equal(end); ti = ti.Add(xportRes.Step) {
fmt.Printf("%s / %d", ti, ti.Unix())
for i := 0; i < len(xportRes.Legends); i++ {
v := xportRes.ValueAt(i, row)
fmt.Printf("\t%e", v)
}
fmt.Printf("\n")
row++
}
}
func ExampleCreator_DS() {
c := &Creator{}
// Add a normal data source, i.e. one of GAUGE, COUNTER, DERIVE and ABSOLUTE:
c.DS("regular_ds", "DERIVE",
900, /* heartbeat */
0, /* min */
"U" /* max */)
// Add a computed
c.DS("computed_ds", "COMPUTE",
"regular_ds,8,*" /* RPN expression */)
}
func ExampleCreator_RRA() {
c := &Creator{}
// Add a normal consolidation function, i.e. one of MIN, MAX, AVERAGE and LAST:
c.RRA("AVERAGE",
0.3, /* xff */
5, /* steps */
1200 /* rows */)
// Add aberrant behavior detection:
c.RRA("HWPREDICT",
1200, /* rows */
0.4, /* alpha */
0.5, /* beta */
288 /* seasonal period */)
}