rrd plugins requires TINE to be built with -tags rrd which need librrd-dev package to be installed in advance.
Let's create an example application that collects CPU usage and system load average, saves the data into RRD, and serves a web page that displays the collected data as graphs.
The code provided starts a simple web server that listens on http://127.0.0.1:8080 and exposes three endpoints: /, /graph/load, and /graph/cpu.
Code
Please find the complete source code in the .
package main
import (
"fmt"
"net/http"
"github.com/OutOfBedlam/tine/engine"
_ "github.com/OutOfBedlam/tine/plugins/all"
_ "github.com/OutOfBedlam/tine/x/rrd"
)
func main() {
addr := "127.0.0.1:8080"
// start data collector that save metrics to rrd file
collect, _ := engine.New(engine.WithConfig(collectorPipeline))
collect.Start()
router := http.NewServeMux()
router.HandleFunc("GET /", getView)
router.HandleFunc("GET /graph/load", HttpHandler(graphLoadPipeline))
router.HandleFunc("GET /graph/cpu", HttpHandler(graphCpuPipeline))
fmt.Printf("\nlistener start at http://%s\n", addr)
http.ListenAndServe(addr, router)
// stop data collector
collect.Stop()
}
Run
Create a file named rrd_graph_web.go with the provided code and build it using the -tags rrd build flags.
go run --tags rrd ./rrd_graph_web.go
Then open a web browser to view the graphs displaying the system's load average and CPU usage.
How this works
Start collecting pipeline
Make a pipeline that collecting CPU usage and load average and merge them into a record then writing on the RRD file.
In the collectorPipeline pipeline, the inlets.cpu and inlets.load collect CPU usage and load average data respectively. The flows.merge merges the collected data into a single record. The outlets.rrd writes the merged data to the RRD file specified by the path parameter.
You can customize the pipeline configuration according to your requirements. Make sure to adjust the path parameter in the outlets.rrd section to specify the desired location for the RRD file.