-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.go
More file actions
71 lines (61 loc) · 1.84 KB
/
Copy pathmonitor.go
File metadata and controls
71 lines (61 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"time"
"github.com/gorilla/websocket"
"github.com/ricochet2200/go-disk-usage/du"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/mem"
)
type SystemStats struct {
Hostname string `json:"hostname"`
IP string `json:"ip"`
Timestamp time.Time `json:"timestamp"`
CPUPercent float64 `json:"cpu_percent"`
MemoryUsed uint64 `json:"memory_used"`
MemoryTotal uint64 `json:"memory_total"`
MemoryUsage float64 `json:"memory_usage_percent"`
DiskUsage uint64 `json:"disk_usage"`
Services []ServiceConfig `json:"services"`
}
func startSystemStatsWebSocket(config Config) {
for {
url := fmt.Sprintf("ws://%s/api/%s/ws/system-stats", config.ServerURLNoProtocol(), config.ServerVersion)
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
log.Println("Error al conectar WebSocket:", err)
time.Sleep(10 * time.Second)
continue
}
defer conn.Close()
log.Println("WebSocket de stats del sistema conectado.")
for {
hostname, _ := os.Hostname()
ip, _ := GetOutboundIP()
cpuPercentages, _ := cpu.Percent(0, false)
memStats, _ := mem.VirtualMemory()
diskUsage := du.NewDiskUsage("C:\\").Available()
stats := SystemStats{
Hostname: hostname,
IP: ip,
Timestamp: time.Now(),
CPUPercent: cpuPercentages[0],
MemoryUsed: memStats.Used,
MemoryTotal: memStats.Total,
MemoryUsage: memStats.UsedPercent,
DiskUsage: diskUsage,
Services: config.Services,
}
payload, _ := json.Marshal(stats)
err = conn.WriteMessage(websocket.TextMessage, payload)
if err != nil {
log.Println("WebSocket cerrado:", err)
break
}
time.Sleep(time.Duration(config.MonitorInterval) * time.Millisecond)
}
}
}