-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
148 lines (123 loc) · 4.26 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"fmt"
"image/color"
i_net "net"
"strings"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
)
var CPU_COLOR color.Color
var RAM_COLOR color.Color
var DISK_COLOR color.Color
var WIFI_COLOR color.Color
var MAX_WIFI_SPEED float64
func getActiveNetInterface() string {
interfaces, err := i_net.Interfaces()
if err != nil {
fmt.Println("Error:", err)
return ""
}
for _, iface := range interfaces {
// Skip interfaces that are down or loopback interfaces
if iface.Flags&i_net.FlagUp == 0 || iface.Flags&i_net.FlagLoopback != 0 {
continue
}
// Check if the interface has an IP address
addrs, err := iface.Addrs()
if err != nil {
fmt.Println("Error:", err)
continue
}
if len(addrs) == 0 {
continue
}
// Determine if it's Wi-Fi or Ethernet
if strings.Contains(strings.ToLower(iface.Name), "wi-fi") || strings.Contains(strings.ToLower(iface.Name), "wlan") {
return strings.ToLower(iface.Name)
} else if strings.Contains(strings.ToLower(iface.Name), "eth") {
return strings.ToLower(iface.Name)
}
}
return ""
}
func getSystemStats(prevDiskStats map[string]disk.IOCountersStat) (float64, float64, float64, float64, float64, float64, float64, map[string]disk.IOCountersStat) {
cpuUsage, _ := cpu.Percent(0, false)
memStats, _ := mem.VirtualMemory()
diskStats, _ := disk.Usage("/")
initialStats, _ := net.IOCounters(true)
time.Sleep(time.Millisecond * 500)
netStats, _ := net.IOCounters(true)
activeNetInterfaceName := getActiveNetInterface()
netReadSpeed := 0.0
netWriteSpeed := 0.0
for i := 0; i < len(netStats); i++ {
if strings.ToLower(netStats[i].Name) == activeNetInterfaceName {
bytesReceived := netStats[i].BytesRecv - initialStats[i].BytesRecv
mbReceived := float64(bytesReceived) / (1024 * 1024)
netReadSpeed = mbReceived * 2
bytesSent := netStats[i].BytesSent - initialStats[i].BytesSent
mbSent := float64(bytesSent) / (1024 * 1024)
netWriteSpeed = mbSent * 2
break
}
}
ioStats, _ := disk.IOCounters()
var readSpeed, writeSpeed float64
for diskName, stats := range ioStats {
readSpeed = float64(stats.ReadBytes-prevDiskStats[diskName].ReadBytes) / float64(time.Second)
writeSpeed = float64(stats.WriteBytes-prevDiskStats[diskName].WriteBytes) / float64(time.Second)
break
}
return cpuUsage[0], memStats.UsedPercent, diskStats.UsedPercent, netReadSpeed, netWriteSpeed, readSpeed, writeSpeed, ioStats
}
func main() {
a := app.New()
w := a.NewWindow("System Monitor")
a.Settings().SetTheme(theme.DarkTheme())
MAX_WIFI_SPEED = 100
CPU_COLOR = color.RGBA{40, 255, 40, 255}
RAM_COLOR = color.RGBA{255, 40, 255, 255}
DISK_COLOR = color.RGBA{40, 255, 255, 255}
WIFI_COLOR = color.RGBA{255, 255, 40, 255}
cpuData := make([]float64, 11)
ramData := make([]float64, 11)
diskReadData := make([]float64, 11)
diskWriteData := make([]float64, 11)
networkReadData := make([]float64, 11)
networkWriteData := make([]float64, 11)
CPU_graph := container.NewWithoutLayout()
RAM_graph := container.NewWithoutLayout()
DISK_graph := container.NewWithoutLayout()
WIFI_graph := container.NewWithoutLayout()
prevDiskStats, _ := disk.IOCounters()
// Start the animation
ticker := time.NewTicker(time.Second)
go func() {
for range ticker.C {
cpuUsage, ramUsage, diskUsage, netReadSpeed, netWriteSpeed, diskReadSpeed, diskWriteSpeed, _ := getSystemStats(prevDiskStats)
cpuData = append(cpuData[1:], cpuUsage)
ramData = append(ramData[1:], ramUsage)
networkReadData = append(networkReadData[1:], netReadSpeed)
networkWriteData = append(networkWriteData[1:], netWriteSpeed)
diskReadData = append(diskReadData[1:], diskReadSpeed)
diskWriteData = append(diskWriteData[1:], diskWriteSpeed)
CPU_graph = DrawCPUGraph(cpuData)
RAM_graph = DrawRAMGraph(ramData)
DISK_graph = DrawDiskGraph(diskReadData, diskWriteData, diskUsage)
WIFI_graph = DrawWifiGraph(networkReadData, networkWriteData)
graphContent := container.New(layout.NewVBoxLayout(), CPU_graph, RAM_graph, DISK_graph, WIFI_graph)
w.SetContent(graphContent)
}
}()
w.Resize(fyne.NewSize(500, 800))
w.ShowAndRun()
}