-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitors.go
72 lines (58 loc) · 1.85 KB
/
monitors.go
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
72
package main
import (
"bufio"
"fmt"
"os/exec"
"strconv"
"strings"
"time"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/mem"
)
// getCPULoads returns the CPU load percentages for each core.
func getCPULoads() ([]float64, error) {
// Specify the interval to calculate CPU usage (e.g., 1 second).
interval := time.Millisecond * 750
// Fetch CPU usage for each core.
percentages, err := cpu.Percent(interval, true)
if err != nil {
return nil, err
}
return percentages, nil
}
func monitorRam() (float64, float64, float64) {
virtualMemory, errorMemory := mem.VirtualMemory()
if errorMemory != nil {
fmt.Println("Cannot ready virtual memory")
return -1.0, -1.0, -1.0
}
usedMemory, totalMemory := virtualMemory.Used/1024/1024, virtualMemory.Total/1024/1024
return float64(usedMemory), float64(totalMemory), virtualMemory.UsedPercent
}
func monitorGPU() (GPUInfo, error) {
cmd := exec.Command("nvidia-smi", "--query-gpu=name,temperature.gpu,utilization.gpu,utilization.memory,memory.total,memory.free,memory.used", "--format=csv,noheader,nounits")
output, err := cmd.Output()
if err != nil {
return GPUInfo{}, err
}
scanner := bufio.NewScanner(strings.NewReader(string(output)))
var gpuInfo GPUInfo
if scanner.Scan() {
line := scanner.Text()
fields := strings.Split(line, ", ")
if len(fields) == 7 {
gpuInfo.Name = fields[0]
// Parse numeric fields
gpuInfo.Temperature, _ = strconv.ParseFloat(fields[1], 64)
gpuInfo.UtilizationGPU, _ = strconv.ParseFloat(fields[2], 64)
gpuInfo.UtilizationMem, _ = strconv.ParseFloat(fields[3], 64)
gpuInfo.MemoryTotal, _ = strconv.ParseFloat(fields[4], 64)
gpuInfo.MemoryFree, _ = strconv.ParseFloat(fields[5], 64)
gpuInfo.MemoryUsed, _ = strconv.ParseFloat(fields[6], 64)
}
}
if err := scanner.Err(); err != nil {
return GPUInfo{}, err
}
return gpuInfo, nil
}