Skip to content

Commit 887b2d3

Browse files
committed
update
1 parent 869625d commit 887b2d3

33 files changed

+3819
-18
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.15
44

55
require (
66
github.com/gin-gonic/gin v1.6.3
7+
github.com/gorilla/websocket v1.4.2
78
github.com/shirou/gopsutil/v3 v3.20.11
89
github.com/sirupsen/logrus v1.7.0
910
github.com/wonderivan/logger v1.0.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GO
2020
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
2121
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
2222
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
23+
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
24+
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
2325
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
2426
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
2527
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=

route/route.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ package route
1010
import (
1111
"pi-monitor/api"
1212
"pi-monitor/controller"
13+
"pi-monitor/websocket"
1314
)
1415

1516
func route() {
1617
server.Gin.LoadHTMLGlob("views/**/*")
1718
server.Gin.Static("static/", "static/")
1819
server.Gin.GET("/", controller.Dashboard)
20+
server.Gin.GET("/ws", websocket.HandleWebSocket)
1921
server.Gin.GET("/api/get", api.Collect)
2022
}

service/cpu.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ type Freq struct {
2323
}
2424

2525
type CPULoad struct {
26-
Percent string
27-
Idle string
28-
User string
29-
Sys string
30-
Nice string
31-
Iowait string
32-
Irq string
33-
Softirq string
26+
Percent float64
27+
Idle float64
28+
User float64
29+
Sys float64
30+
Nice float64
31+
Iowait float64
32+
Irq float64
33+
Softirq float64
3434
}
3535

3636
var (
@@ -59,14 +59,14 @@ func cpuLoad() *CPULoad {
5959
used_total := cur_total - last_total
6060

6161
cpuload := &CPULoad{
62-
Percent: floatToString((1 - ((t.Idle - lastTimesStat.Idle) / used_total)) * 100),
63-
Idle: floatToString((t.Idle - lastTimesStat.Idle) / used_total * 100),
64-
User: floatToString((t.User - lastTimesStat.User) / used_total * 100),
65-
Sys: floatToString((t.System - lastTimesStat.System) / used_total * 100),
66-
Nice: floatToString((t.Nice - lastTimesStat.Nice) / used_total * 100),
67-
Iowait: floatToString((t.Iowait - lastTimesStat.Iowait) / used_total * 100),
68-
Irq: floatToString((t.Irq - lastTimesStat.Irq) / used_total * 100),
69-
Softirq: floatToString((t.Softirq - lastTimesStat.Softirq) / used_total * 100),
62+
Percent: (1 - ((t.Idle - lastTimesStat.Idle) / used_total)) * 100,
63+
Idle: (t.Idle - lastTimesStat.Idle) / used_total * 100,
64+
User: (t.User - lastTimesStat.User) / used_total * 100,
65+
Sys: (t.System - lastTimesStat.System) / used_total * 100,
66+
Nice: (t.Nice - lastTimesStat.Nice) / used_total * 100,
67+
Iowait: (t.Iowait - lastTimesStat.Iowait) / used_total * 100,
68+
Irq: (t.Irq - lastTimesStat.Irq) / used_total * 100,
69+
Softirq: (t.Softirq - lastTimesStat.Softirq) / used_total * 100,
7070
}
7171
lastTimesStat = t
7272
return cpuload

static/js/monitor.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,38 @@ $(document).ready(function() {
33
var memUsage = echarts.init(document.getElementById('mem-usage'));
44
var swapUsage = echarts.init(document.getElementById('swap-usage'));
55

6+
var ws = new WebSocket("ws://" + document.location.host + "/ws");
7+
//连接打开时触发
8+
ws.onopen = function(evt) {
9+
console.log("Connection open ...");
10+
ws.send("Hello WebSockets!");
11+
};
12+
//接收到消息时触发
13+
ws.onmessage = function(evt) {
14+
var msg = JSON.parse(evt.data);
15+
16+
cpuUsageOption.series[0].data[0].value = msg.CPU.Load.Percent.toFixed(1)
17+
memUsageOption.series[0].data[0].value = msg.Memory.UsedPercent.toFixed(1)
18+
19+
cpuUsage.setOption(cpuUsageOption, true);
20+
memUsage.setOption(memUsageOption, true);
21+
22+
$("#temp").text(msg.CPU.Temp+" C°");
23+
$("#freq").text(msg.CPU.Freq.Curfreq+" MHz");
24+
$("#idle").text(msg.CPU.Load.Idle.toFixed(1)+"%");
25+
$("#user").text(msg.CPU.Load.User.toFixed(1)+"%");
26+
$("#sys").text(msg.CPU.Load.Sys.toFixed(1)+"%");
27+
$("#nice").text(msg.CPU.Load.Nice.toFixed(1)+"%");
28+
$("#iow").text(msg.CPU.Load.Iowait.toFixed(1)+"%");
29+
$("#irq").text(msg.CPU.Load.Irq.toFixed(1)+"%");
30+
//console.log("Received Message: " + evt);
31+
console.log(msg);
32+
};
33+
//连接关闭时触发
34+
ws.onclose = function(evt) {
35+
console.log("Connection closed.");
36+
};
37+
638
cpuUsageOption = {
739
tooltip: {
840
formatter: '{a} <br/>{b} : {c}%'

vendor/github.com/gorilla/websocket/.gitignore

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gorilla/websocket/AUTHORS

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gorilla/websocket/LICENSE

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gorilla/websocket/README.md

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)