2024-03-16 17:09:58 +00:00
|
|
|
package host
|
|
|
|
|
|
|
|
import (
|
2024-03-17 06:10:04 +00:00
|
|
|
"fmt"
|
2024-03-16 17:34:17 +00:00
|
|
|
"log"
|
2024-03-16 17:09:58 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/wcharczuk/go-chart/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This file contains utilities to create charts based on the different data
|
|
|
|
// When a chart is rendered, it can return either an SVG or PNG. SVG is preferrable.
|
|
|
|
|
|
|
|
func (h *Host) ChartMemory() string {
|
2024-03-16 17:34:17 +00:00
|
|
|
h.getNodeInfo()
|
|
|
|
log.Println("Generating Chart")
|
2024-03-17 06:10:04 +00:00
|
|
|
memFree := float64(h.NodeMemory.Free)
|
|
|
|
memCached := float64(h.NodeMemory.Cached)
|
|
|
|
memBuffer := float64(h.NodeMemory.Buffers)
|
|
|
|
memTotal := float64(h.NodeMemory.Total)
|
|
|
|
|
2024-03-16 17:09:58 +00:00
|
|
|
c := chart.PieChart{
|
2024-03-17 06:10:04 +00:00
|
|
|
Title: fmt.Sprintf("Memory Info %s", h.SystemHostName),
|
|
|
|
Width: 256,
|
|
|
|
Height: 256,
|
2024-03-16 17:09:58 +00:00
|
|
|
Values: []chart.Value{
|
2024-03-17 06:10:04 +00:00
|
|
|
{Value: memTotal - memFree, Label: fmt.Sprintf("%.2f%% Free", memFree/memTotal*100)},
|
|
|
|
{Value: memTotal - memCached, Label: fmt.Sprintf("%.2f%% Cached", memCached/memTotal*100)},
|
|
|
|
{Value: memTotal - memBuffer, Label: fmt.Sprintf("%.2f%% Buffers", memBuffer/memTotal*100)},
|
2024-03-16 17:09:58 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
sb := new(strings.Builder)
|
2024-03-16 17:34:17 +00:00
|
|
|
log.Println("Rendering chart")
|
|
|
|
if err := c.Render(chart.SVG, sb); err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
2024-03-16 17:09:58 +00:00
|
|
|
return sb.String()
|
|
|
|
}
|