-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
325 lines (274 loc) · 10.1 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package main
import (
"fmt"
"gostat/service"
"os"
"strings"
"time"
"github.com/gdamore/tcell/v2"
)
const (
updateInterval = time.Second
maxDataPoints = 60
)
// NetworkData 存储网络数据
type NetworkData struct {
TotalRecv uint64
TotalSent uint64
}
// 初始化屏幕并启动主循环
func main() {
s, err := tcell.NewScreen()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
if err := s.Init(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
defer s.Fini()
defStyle := tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorWhite)
s.SetStyle(defStyle)
netData := &NetworkData{}
quit := make(chan struct{})
go func() {
for {
select {
case <-quit:
return
default:
drawLayout(s, netData)
s.Show()
time.Sleep(updateInterval)
}
}
}()
for {
switch ev := s.PollEvent().(type) {
case *tcell.EventResize:
s.Sync()
case *tcell.EventKey:
if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC {
close(quit)
return
}
}
}
}
func drawLayout(s tcell.Screen, netData *NetworkData) {
width, height := s.Size()
s.Clear()
drawBorder(s, 0, 0, width-1, height-1)
drawTitle(s, width)
// 左侧
leftWidth := width / 2
drawBox(s, 1, 2, leftWidth-2, height/3, "System Info")
drawSystemInfo(s, 2, 3, leftWidth-4, height/3-2)
drawBox(s, 1, height/3+2, leftWidth-2, height/3-1, "Memory Usage")
drawMemoryUsage(s, 2, height/3+3, leftWidth-4, height/3-3)
drawBox(s, 1, 2*height/3+1, leftWidth-2, height/3-2, "Docker Info")
drawDockerInfo(s, 2, 2*height/3+2, leftWidth-4, height/3-4)
// 右侧
rightStart := leftWidth - 1
drawBox(s, rightStart, 2, width-rightStart-1, height/3, "CPU & Load")
drawCPUInfo(s, rightStart+1, 3, width-rightStart-3, height/3-2)
drawBox(s, rightStart, height/3+2, width-rightStart-1, height/3-1, "Disk Usage")
drawDiskUsage(s, rightStart+1, height/3+3, width-rightStart-3, height/3-3)
drawBox(s, rightStart, 2*height/3+1, width-rightStart-1, height/3-2, "Network Traffic")
drawNetworkTraffic(s, rightStart+1, 2*height/3+2, width-rightStart-3, height/3-4, netData)
}
// drawTitle 函数在屏幕顶部绘制标题
func drawTitle(s tcell.Screen, width int) {
title := "GoStat - System Monitor"
style := tcell.StyleDefault.Foreground(tcell.ColorGreen).Bold(true)
drawCenteredText(s, 0, 1, width, title, style)
}
// drawSystemInfo 函数显示系统信息
func drawSystemInfo(s tcell.Screen, x, y, width, height int) {
osinfo, _ := service.GetOSInfo()
info := fmt.Sprintf(
"OS Version: %s\nHostName: %s\nUptime: %s\nCPU Info: %s",
osinfo.OS, osinfo.Hostname, osinfo.Uptime, osinfo.Cpu,
)
addCenteredContent(s, x, y, width, height, info)
}
// drawMemoryUsage 函数显示内存使用情况
func drawMemoryUsage(s tcell.Screen, x, y, width, height int) {
meminfo, _ := service.GetMemoryUse()
// 计算垂直中心
centerY := y + height/2 - 2
drawBar(s, x, centerY, width, 100, 100, tcell.ColorGreen)
drawCenteredText(s, x, centerY+1, width, fmt.Sprintf("Total: %dG", meminfo.MemoryTotal), tcell.StyleDefault.Foreground(tcell.ColorWhite))
usedPercent := float64(meminfo.MemoryUsed) / float64(meminfo.MemoryTotal) * 100
drawBar(s, x, centerY+3, width, usedPercent, 100, tcell.ColorOrange)
drawCenteredText(s, x, centerY+4, width, fmt.Sprintf("Used: %dG (%.2f%%)", meminfo.MemoryUsed, usedPercent), tcell.StyleDefault.Foreground(tcell.ColorOrange))
}
// drawDockerInfo 函数显示Docker相关信息
func drawDockerInfo(s tcell.Screen, x, y, width, height int) {
dockerInfo, err := service.GetDockerInfo()
if err != nil || dockerInfo == nil {
// 如果未安装 Docker,则显示提示信息
drawCenteredText(s, x, y, width, "Docker is not installed or not running.", tcell.StyleDefault.Foreground(tcell.ColorRed))
return
}
// 计算列宽
colWidth := width / 2
// 左列:基本信息
leftInfo := fmt.Sprintf(
"Running Containers: %d\nStopped Containers: %d\nTotal Images: %d",
dockerInfo.RunningContainers,
dockerInfo.StoppedContainers,
dockerInfo.TotalImages,
)
addContentVerticalAlign(s, x, y, colWidth, height, leftInfo, tcell.StyleDefault)
// 右列:最近的容器
rightX := x + colWidth
drawText(s, rightX, y, colWidth, 1, "Recent Containers:", tcell.StyleDefault.Foreground(tcell.ColorYellow))
for i, container := range dockerInfo.RecentContainers {
if i >= height-1 {
break
}
// 如果容器名称太长,则截断
containerInfo := fmt.Sprintf("- %s", container)
if len(containerInfo) > colWidth {
containerInfo = containerInfo[:colWidth-3] + "..."
}
drawText(s, rightX, y+1+i, colWidth, 1, containerInfo, tcell.StyleDefault.Foreground(tcell.ColorDarkCyan))
}
}
// drawCPUInfo 函数显示CPU使用率和负载信息
func drawCPUInfo(s tcell.Screen, x, y, width, height int) {
cpuUsage, _ := service.GetCPUUsage()
loadAvg, _ := service.GetLoadAverage()
// 计算垂直中心
centerY := y + height/2 - 2
drawCPUGraph(s, x, centerY, width, 1, cpuUsage)
drawCenteredText(s, x, centerY+2, width, fmt.Sprintf("CPU Usage: %.2f%%", cpuUsage), tcell.StyleDefault.Foreground(tcell.ColorWhite))
loadInfo := "Load Average:"
drawCenteredText(s, x, centerY+4, width, loadInfo, tcell.StyleDefault.Foreground(tcell.ColorYellow))
loadDetails := fmt.Sprintf("1min: %.2f 5min: %.2f 15min: %.2f", loadAvg.Load1, loadAvg.Load5, loadAvg.Load15)
drawCenteredText(s, x, centerY+5, width, loadDetails, tcell.StyleDefault.Foreground(tcell.ColorYellow))
}
// drawDiskUsage 函数显示磁盘使用情况
func drawDiskUsage(s tcell.Screen, x, y, width, height int) {
diskinfo, _ := service.GetDiskUse()
info := fmt.Sprintf(
"Total: %dG Used: %dG Total Usage: %.2f%%\n%s\n%s\n%s %s %dG %.2f%%",
diskinfo.DiskTotal, diskinfo.DiskUsed, diskinfo.DiskPercent,
strings.Repeat("=", width),
"Disk Mounted Free Used",
diskinfo.DiskRootDevice, diskinfo.DiskRootMount, diskinfo.DiskRootFree, diskinfo.DiskRootPercent,
)
addCenteredContent(s, x, y, width, height, info)
}
// drawNetworkTraffic 函数显示网络流量信息
func drawNetworkTraffic(s tcell.Screen, x, y, width, height int, netData *NetworkData) {
netinfo, _ := service.GetNetworkUsage()
updateNetworkData(netData, netinfo)
maxTraffic := uint64(1024 * 1024 * 1024)
// 计算垂直中心
centerY := y + height/2 - 2
recvPercent := float64(netData.TotalRecv) / float64(maxTraffic) * 100
drawBar(s, x, centerY, width, recvPercent, 100, tcell.ColorGreen)
drawCenteredText(s, x, centerY+1, width, fmt.Sprintf("Recv: %.2f MB", float64(netData.TotalRecv)/1024/1024), tcell.StyleDefault.Foreground(tcell.ColorGreen))
sentPercent := float64(netData.TotalSent) / float64(maxTraffic) * 100
drawBar(s, x, centerY+3, width, sentPercent, 100, tcell.ColorRed)
drawCenteredText(s, x, centerY+4, width, fmt.Sprintf("Sent: %.2f MB", float64(netData.TotalSent)/1024/1024), tcell.StyleDefault.Foreground(tcell.ColorRed))
}
// drawCPUGraph 函数绘制CPU使用率图形
func drawCPUGraph(s tcell.Screen, x, y, width, height int, cpuUsage float64) {
usedWidth := int(float64(width) * cpuUsage / 100)
for i := 0; i < width; i++ {
if i < usedWidth {
s.SetContent(x+i, y, '█', nil, tcell.StyleDefault.Foreground(tcell.ColorRed))
} else {
s.SetContent(x+i, y, '░', nil, tcell.StyleDefault.Foreground(tcell.ColorGray))
}
}
}
// drawBar 函数绘制一个进度条
func drawBar(s tcell.Screen, x, y, width int, value, max float64, color tcell.Color) {
filledWidth := int(float64(width) * value / max)
for i := 0; i < width; i++ {
if i < filledWidth {
s.SetContent(x+i, y, '█', nil, tcell.StyleDefault.Foreground(color))
} else {
s.SetContent(x+i, y, '░', nil, tcell.StyleDefault.Foreground(tcell.ColorGray))
}
}
}
// updateNetworkData 函数更新网络数据
func updateNetworkData(data *NetworkData, netinfo *service.HostsInfo) {
data.TotalRecv = netinfo.TotalBytesRecv
data.TotalSent = netinfo.TotalBytesSent
}
// drawBorder 函数绘制边框
func drawBorder(s tcell.Screen, x1, y1, x2, y2 int) {
borderStyle := tcell.StyleDefault.Foreground(tcell.ColorLightCoral)
// Top and bottom borders
for x := x1; x <= x2; x++ {
s.SetContent(x, y1, '-', nil, borderStyle)
s.SetContent(x, y2, '-', nil, borderStyle)
}
// Left and right borders
for y := y1; y <= y2; y++ {
s.SetContent(x1, y, '|', nil, borderStyle)
s.SetContent(x2, y, '|', nil, borderStyle)
}
// Corners
s.SetContent(x1, y1, '+', nil, borderStyle)
s.SetContent(x2, y1, '+', nil, borderStyle)
s.SetContent(x1, y2, '+', nil, borderStyle)
s.SetContent(x2, y2, '+', nil, borderStyle)
}
// drawBox 函数绘制一个带标题的框
func drawBox(s tcell.Screen, x, y, width, height int, title string) {
drawBorder(s, x, y, x+width-1, y+height-1)
// Draw title
titleStyle := tcell.StyleDefault.Foreground(tcell.ColorYellow).Bold(true)
titleX := x + (width-len(title))/2
drawText(s, titleX, y, width, 1, title, titleStyle)
}
// addCenteredContent 函数在指定区域内居中添加内容
func addCenteredContent(s tcell.Screen, x, y, width, height int, content string) {
lines := strings.Split(content, "\n")
startY := y + (height-len(lines))/2
for i, line := range lines {
if i >= height {
break
}
drawCenteredText(s, x, startY+i, width, line, tcell.StyleDefault)
}
}
// drawCenteredText 函数在指定位置绘制居中文本
func drawCenteredText(s tcell.Screen, x, y, width int, text string, style tcell.Style) {
textWidth := len([]rune(text))
startX := x + (width-textWidth)/2
for i, r := range []rune(text) {
if i >= width {
break
}
s.SetContent(startX+i, y, r, nil, style)
}
}
// drawText 函数在指定位置绘制左对齐文本
func drawText(s tcell.Screen, x, y, width, height int, text string, style tcell.Style) {
for i, r := range []rune(text) {
if i >= width {
break
}
s.SetContent(x+i, y, r, nil, style)
}
}
// addContentVerticalAlign 函数添加垂直对齐的内容
func addContentVerticalAlign(s tcell.Screen, x, y, width, height int, content string, style tcell.Style) {
lines := strings.Split(content, "\n")
startY := y + (height-len(lines))/2
for i, line := range lines {
if i >= height {
break
}
drawText(s, x, startY+i, width, 1, line, style)
}
}