Created
January 11, 2017 09:07
-
-
Save doublemarket/96b6dfdb9187330ad444c294d3324d76 to your computer and use it in GitHub Desktop.
Sample program to fetch data from an RRD file using ziutek/rrd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"path/filepath" | |
"strings" | |
"time" | |
"github.com/ziutek/rrd" | |
) | |
func main() { | |
const ( | |
dbPath = "/hoge/fuga/rrd/*" | |
step = 10 | |
heartbeat = 2 * step | |
) | |
directories, _ := filepath.Glob(dbPath) | |
for _, d := range directories { | |
dName := filepath.Base(d) | |
files, _ := filepath.Glob(d + "/*.rrd") | |
for _, f := range files { | |
fName := strings.TrimRight(filepath.Base(f), ".rrd") | |
println(dName + ":" + fName) | |
inf, err := rrd.Info(f) | |
if err != nil { | |
fmt.Print(err) | |
} | |
/*for k, v := range inf { | |
fmt.Printf("%s (%T): %v\n", k, v, v) | |
}*/ | |
end := time.Unix(int64(inf["last_update"].(uint)), 0) | |
start := end.Add(-60 * 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 := rrd.Fetch(f, "AVERAGE", start, end, step*time.Second) | |
if err != nil { | |
fmt.Print(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) | |
fmt.Printf("Row count: %d\n", fetchRes.RowCnt) | |
for _, dsName := range fetchRes.DsNames { | |
fmt.Printf("\t%s", dsName) | |
} | |
fmt.Printf("\n") | |
timestamp := fetchRes.Start | |
for i := 0; i < fetchRes.RowCnt; i++ { | |
fmt.Print(timestamp, " : ") | |
for ds := range fetchRes.DsNames { | |
fmt.Print(fetchRes.ValueAt(i, ds), " ") | |
} | |
fmt.Println() | |
timestamp = timestamp.Add(step * time.Second) | |
}*/ | |
end = time.Unix(int64(inf["last_update"].(uint)), 0) | |
start = end.Add(-60 * 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 := rrd.NewExporter() | |
e.Def("def1", f, "shortterm", "AVERAGE") | |
e.Def("def2", f, "midterm", "AVERAGE") | |
e.Def("def3", f, "longterm", "AVERAGE") | |
e.CDef("vdef1", "def1,def2,def3,+") | |
e.XportDef("def1", "shortterm") | |
e.XportDef("def2", "midterm") | |
e.XportDef("def3", "longterm") | |
e.XportDef("vdef1", "sum") | |
xportRes, err := e.Xport(start, end, step*time.Second) | |
if err != nil { | |
fmt.Println(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++ | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment