Pārlūkot izejas kodu

[release] v0.12.0-unstable8

Yann Stepienik 1 gadu atpakaļ
vecāks
revīzija
1d650b3c4e
3 mainītis faili ar 32 papildinājumiem un 24 dzēšanām
  1. 1 1
      package.json
  2. 0 19
      src/metrics/index.go
  3. 31 4
      src/metrics/system.go

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "cosmos-server",
-  "version": "0.12.0-unstable7",
+  "version": "0.12.0-unstable8",
   "description": "",
   "main": "test-server.js",
   "bugs": {

+ 0 - 19
src/metrics/index.go

@@ -159,25 +159,6 @@ func PushSetMetric(key string, value int, def DataDef) {
 func Run() {
 	utils.Debug("Metrics - Run")
 	
-	// redirect docker monitoring
-	if os.Getenv("HOSTNAME") != "" {
-		// check if path /mnt/host exist
-		if _, err := os.Stat("/mnt/host"); os.IsNotExist(err) {
-			utils.Error("Metrics - Cannot start monitoring the server if you don't mount /mnt/host to /. Check the documentation for more information.", nil)
-			return
-		} else {
-			utils.Log("Metrics - Monitoring the server at /mnt/host")
-
-			os.Setenv("HOST_PROC", "/mnt/host/proc")
-			os.Setenv("HOST_SYS", "/mnt/host/sys")
-			os.Setenv("HOST_ETC", "/mnt/host/etc")
-			os.Setenv("HOST_VAR", "/mnt/host/var")
-			os.Setenv("HOST_RUN", "/mnt/host/run")
-			os.Setenv("HOST_DEV", "/mnt/host/dev")
-			os.Setenv("HOST_ROOT ", "/mnt/host/")
-		}
-	}
-
 	nextTime := ModuloTime(time.Now().Add(time.Second*30), time.Second*30)
 	nextTime = nextTime.Add(time.Second * 2)
 

+ 31 - 4
src/metrics/system.go

@@ -13,13 +13,40 @@ import (
 	
 	"github.com/azukaar/cosmos-server/src/utils"
 	"github.com/azukaar/cosmos-server/src/docker"
+	"github.com/shirou/gopsutil/v3/common"
 )
 
 func GetSystemMetrics() {
 	utils.Debug("Metrics - System")
 
+	ctx := context.Background()
+
+	// redirect docker monitoring
+	if os.Getenv("HOSTNAME") != "" {
+		// check if path /mnt/host exist
+		if _, err := os.Stat("/mnt/host"); os.IsNotExist(err) {
+			utils.Error("Metrics - Cannot start monitoring the server if you don't mount /mnt/host to /. Check the documentation for more information.", nil)
+			return
+		} else {
+			utils.Log("Metrics - Monitoring the server at /mnt/host")
+
+			ctx = context.WithValue(context.Background(), 
+				common.EnvKey, common.EnvMap{
+					common.HostProcEnvKey: "/mnt/host/proc",
+					common.HostSysEnvKey: "/mnt/host/sys",
+					common.HostEtcEnvKey: "/mnt/host/etc",
+					common.HostVarEnvKey: "/mnt/host/var",
+					common.HostRunEnvKey: "/mnt/host/run",
+					common.HostDevEnvKey: "/mnt/host/dev",
+					common.HostRootEnvKey: "/mnt/host/",
+				},
+			)
+		}
+	}
+
+
 	// Get CPU Usage
-	cpuPercent, err := cpu.Percent(0, false)
+	cpuPercent, err := cpu.PercentWithContext(ctx, 0, false)
 	if err != nil {
 		utils.Error("Metrics - Error fetching CPU usage:", err)
 		return
@@ -39,7 +66,7 @@ func GetSystemMetrics() {
 	// cpuPercents, _ := cpu.Percent(0, true)
 
 	// Get RAM Usage
-	memInfo, err := mem.VirtualMemory()
+	memInfo, err := mem.VirtualMemoryWithContext(ctx)
 	if err != nil {
 		utils.Error("Metrics - Error fetching RAM usage:", err)
 		return
@@ -52,7 +79,7 @@ func GetSystemMetrics() {
 	})
 	
 	// Get Network Usage
-	netIO, err := net.IOCounters(false)
+	netIO, err := net.IOCountersWithContext(ctx, false)
 	
 	// netIOTest, _ := net.IOCounters(true)
 	// for _, v := range netIOTest {
@@ -122,7 +149,7 @@ func GetSystemMetrics() {
 	}
 
 	// Get Disk Usage
-  parts, err := disk.Partitions(true)
+  parts, err := disk.PartitionsWithContext(ctx, true)
 	if err != nil {
 		utils.Error("Metrics - Error fetching Disk usage:", err)
 		return