fix RRD implementation on network troughput
This commit is contained in:
Binary file not shown.
@@ -3,6 +3,7 @@ package system
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/atlasos/calypso/internal/common/logger"
|
"github.com/atlasos/calypso/internal/common/logger"
|
||||||
"github.com/atlasos/calypso/internal/tasks"
|
"github.com/atlasos/calypso/internal/tasks"
|
||||||
@@ -233,3 +234,22 @@ func (h *Handler) GetSystemLogs(c *gin.Context) {
|
|||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{"logs": logs})
|
c.JSON(http.StatusOK, gin.H{"logs": logs})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetNetworkThroughput retrieves network throughput data from RRD
|
||||||
|
func (h *Handler) GetNetworkThroughput(c *gin.Context) {
|
||||||
|
// Default to last 5 minutes
|
||||||
|
durationStr := c.DefaultQuery("duration", "5m")
|
||||||
|
duration, err := time.ParseDuration(durationStr)
|
||||||
|
if err != nil {
|
||||||
|
duration = 5 * time.Minute
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := h.service.GetNetworkThroughput(c.Request.Context(), duration)
|
||||||
|
if err != nil {
|
||||||
|
h.logger.Error("Failed to get network throughput", "error", err)
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to get network throughput"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{"data": data})
|
||||||
|
}
|
||||||
|
|||||||
@@ -24,11 +24,64 @@ type Service struct {
|
|||||||
rrdService *RRDService
|
rrdService *RRDService
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// detectPrimaryInterface detects the primary network interface (first non-loopback with IP)
|
||||||
|
func detectPrimaryInterface(ctx context.Context) string {
|
||||||
|
// Try to get default route interface
|
||||||
|
cmd := exec.CommandContext(ctx, "ip", "route", "show", "default")
|
||||||
|
output, err := cmd.Output()
|
||||||
|
if err == nil {
|
||||||
|
lines := strings.Split(string(output), "\n")
|
||||||
|
for _, line := range lines {
|
||||||
|
if strings.Contains(line, "dev ") {
|
||||||
|
parts := strings.Fields(line)
|
||||||
|
for i, part := range parts {
|
||||||
|
if part == "dev" && i+1 < len(parts) {
|
||||||
|
iface := parts[i+1]
|
||||||
|
if iface != "lo" {
|
||||||
|
return iface
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: get first non-loopback interface with IP
|
||||||
|
cmd = exec.CommandContext(ctx, "ip", "-4", "addr", "show")
|
||||||
|
output, err = cmd.Output()
|
||||||
|
if err == nil {
|
||||||
|
lines := strings.Split(string(output), "\n")
|
||||||
|
for _, line := range lines {
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
// Look for interface name line (e.g., "2: ens18: <BROADCAST...")
|
||||||
|
if len(line) > 0 && line[0] >= '0' && line[0] <= '9' && strings.Contains(line, ":") {
|
||||||
|
parts := strings.Fields(line)
|
||||||
|
if len(parts) >= 2 {
|
||||||
|
iface := strings.TrimSuffix(parts[1], ":")
|
||||||
|
if iface != "" && iface != "lo" {
|
||||||
|
// Check if this interface has an IP (next lines will have "inet")
|
||||||
|
// For simplicity, return first non-loopback interface
|
||||||
|
return iface
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Final fallback
|
||||||
|
return "eth0"
|
||||||
|
}
|
||||||
|
|
||||||
// NewService creates a new system service
|
// NewService creates a new system service
|
||||||
func NewService(log *logger.Logger) *Service {
|
func NewService(log *logger.Logger) *Service {
|
||||||
// Initialize RRD service for network monitoring (default to eth0, can be configured)
|
// Initialize RRD service for network monitoring
|
||||||
rrdDir := "/var/lib/calypso/rrd"
|
rrdDir := "/var/lib/calypso/rrd"
|
||||||
interfaceName := "eth0" // Default interface, can be made configurable
|
|
||||||
|
// Auto-detect primary interface
|
||||||
|
ctx := context.Background()
|
||||||
|
interfaceName := detectPrimaryInterface(ctx)
|
||||||
|
log.Info("Detected primary network interface", "interface", interfaceName)
|
||||||
|
|
||||||
rrdService := NewRRDService(log, rrdDir, interfaceName)
|
rrdService := NewRRDService(log, rrdDir, interfaceName)
|
||||||
|
|
||||||
return &Service{
|
return &Service{
|
||||||
|
|||||||
Reference in New Issue
Block a user