RRD
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 GitHub repository.
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.
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.
The pipeline is defined as:
Rendering HTML
Attach the getView handler to the / endpoint.
The getView() handler embeds two <img> elements that runs on endpoints, /graph/load and /graph/cpu, and reloads them every 2 seconds.
HttpHandler wrapper for a pipeline
Running a pipeline as a HTTP Handler requires preparation, the below HttpHandler() code shows how to wrap your pipeline for a handle.
Attach pipelines to the endpoints
The pipeline definitions of graphLoadPipeline and graphCpuPipeline are:
graphLoadPipeline
graphCpuPipeline
Last updated