-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrrd.go
78 lines (66 loc) · 1.79 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
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
"github.com/ziutek/rrd"
)
const (
rrdStep = 60
rrdHeartbeat = 10 * rrdStep
)
// UpdateRRD should return errors
// TODO implement return errors
func UpdateRRD(rrdPath, device, iface string, in, out uint64) {
// Prettify interface names
iface = strings.Replace(iface, "/", "", -1)
// rrd paths
folder := fmt.Sprintf("%s%s/", rrdPath, device)
filename := fmt.Sprintf("%s%s/%s.rrd", rrdPath, device, iface)
// Create folder if it doesn't exist
if _, err := os.Stat(folder); os.IsNotExist(err) {
log.Println("Creating folder:", folder)
os.MkdirAll(folder, 0744)
}
// Create file if it doesn't exist
if _, err := os.Stat(filename); os.IsNotExist(err) {
CreateRRD(filename)
// exit loop since we just created the file
// insert the data next iteration
return
}
// Update rrd with data
u := rrd.NewUpdater(filename)
err := u.Update(time.Now(), in, out)
if err != nil {
log.Println("Error updating rrd:", err)
}
log.Println("Updated file", filename)
}
// CreateRRD should return errors
// TODO implement return errors
func CreateRRD(filename string) {
c := rrd.NewCreator(filename, time.Now(), rrdStep)
c.DS("traffic_in", "COUNTER", rrdHeartbeat, 0, 1250000000000)
c.DS("traffic_out", "COUNTER", rrdHeartbeat, 0, 1250000000000)
c.RRA("MIN", 0, 360, 576)
c.RRA("MIN", 0, 30, 576)
c.RRA("MIN", 0, 7, 576)
c.RRA("AVERAGE", 0, 360, 576)
c.RRA("AVERAGE", 0, 30, 576)
c.RRA("AVERAGE", 0, 7, 576)
c.RRA("AVERAGE", 0, 1, 576)
c.RRA("MAX", 0, 360, 576)
c.RRA("MAX", 0, 7, 576)
c.RRA("MAX", 0, 1, 576)
// Change to c.Create(true) if you want to overwrite the file
// Only if you want to keep data but change rrd layout
err := c.Create(false)
if err != nil {
log.Fatal(err)
}
log.Println("Created file", filename)
return
}