redesign Dashboard

This commit is contained in:
0xJacky 2022-02-23 11:22:50 +08:00
parent d50bb154f1
commit 46e86d2a3d
17 changed files with 834 additions and 167 deletions

View file

@ -10,6 +10,7 @@ import (
"github.com/shirou/gopsutil/v3/load"
"github.com/shirou/gopsutil/v3/mem"
"github.com/shirou/gopsutil/v3/net"
"math"
"net/http"
"runtime"
"strconv"
@ -54,6 +55,10 @@ func Analytic(c *gin.Context) {
response["memory_used"] = humanize.Bytes(memoryStat.Used)
response["memory_cached"] = humanize.Bytes(memoryStat.Cached)
response["memory_free"] = humanize.Bytes(memoryStat.Free)
response["memory_swap_used"] = humanize.Bytes(memoryStat.SwapTotal - memoryStat.SwapFree)
response["memory_swap_total"] = humanize.Bytes(memoryStat.SwapTotal)
response["memory_swap_cached"] = humanize.Bytes(memoryStat.SwapCached)
response["memory_swap_percent"] = float64(memoryStat.SwapFree) / math.Max(float64(memoryStat.SwapTotal), 1)
response["memory_pressure"], _ = strconv.ParseFloat(fmt.Sprintf("%.2f", memoryStat.UsedPercent), 64)
@ -83,8 +88,16 @@ func Analytic(c *gin.Context) {
response["disk_total"] = humanize.Bytes(diskUsage.Total)
response["disk_percentage"], _ = strconv.ParseFloat(fmt.Sprintf("%.2f", diskUsage.UsedPercent), 64)
response["diskIO"] = gin.H{
"writes": tool.DiskWriteBuffer[len(tool.DiskWriteBuffer)-1],
"reads": tool.DiskReadBuffer[len(tool.DiskReadBuffer)-1],
}
network, _ := net.IOCounters(false)
response["network"] = network
if len(network) > 0 {
response["network"] = network[0]
}
m, _ := json.Marshal(response)
message = m
@ -99,9 +112,30 @@ func Analytic(c *gin.Context) {
}
}
func GetCpuUsageRecord(c *gin.Context) {
func GetAnalyticInit(c *gin.Context) {
cpuInfo, _ := cpu.Info()
network, _ := net.IOCounters(false)
var _net net.IOCountersStat
if len(network) > 0 {
_net = network[0]
}
hostInfo, _ := host.Info()
c.JSON(http.StatusOK, gin.H{
"user": tool.CpuUserBuffer,
"total": tool.CpuTotalBuffer,
"host": hostInfo,
"cpu": gin.H{
"info": cpuInfo,
"user": tool.CpuUserBuffer,
"total": tool.CpuTotalBuffer,
},
"network": gin.H{
"init": _net,
"bytesRecv": tool.NetRecvBuffer,
"bytesSent": tool.NetSentBuffer,
},
"diskIO": gin.H{
"writes": tool.DiskWriteBuffer,
"reads": tool.DiskReadBuffer,
},
})
}

View file

@ -38,7 +38,7 @@ func InitRouter() *gin.Engine {
g := g.Group("/", authRequired())
{
g.GET("/analytic", api.Analytic)
g.GET("/analytic/cpu", api.GetCpuUsageRecord)
g.GET("/analytic/init", api.GetAnalyticInit)
g.GET("/users", api.GetUsers)
g.GET("/user/:id", api.GetUser)

View file

@ -15,6 +15,7 @@ type Server struct {
HTTPChallengePort string
Email string
Database string
DiskName string
}
var ServerSettings = &Server{
@ -22,6 +23,7 @@ var ServerSettings = &Server{
RunMode: "debug",
HTTPChallengePort: "9180",
Database: "database",
DiskName: "vda",
}
var ConfPath string

124
server/tool/analytic.go Normal file
View file

@ -0,0 +1,124 @@
package tool
import (
"github.com/0xJacky/Nginx-UI/server/settings"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/net"
"runtime"
"time"
)
type usage struct {
Time time.Time `json:"x"`
Usage float64 `json:"y"`
}
var CpuUserBuffer []usage
var CpuTotalBuffer []usage
var NetRecvBuffer []usage
var NetSentBuffer []usage
var DiskWriteBuffer []usage
var DiskReadBuffer []usage
var LastDiskWrites uint64
var LastDiskReads uint64
var LastNetRecv uint64
var LastNetSent uint64
func RecordServerAnalytic() {
network, _ := net.IOCounters(false)
diskIOCounters, _ := disk.IOCounters(settings.ServerSettings.DiskName)
diskIO, ok := diskIOCounters[settings.ServerSettings.DiskName]
if ok {
LastDiskWrites = diskIO.WriteCount
LastDiskReads = diskIO.ReadCount
}
if len(network) > 0 {
LastNetRecv = network[0].BytesRecv
LastNetSent = network[0].BytesSent
}
now := time.Now()
// 初始化记录数组
for i := 100; i > 0; i-- {
u := usage{Time: now.Add(time.Duration(-i) * time.Second)}
CpuUserBuffer = append(CpuUserBuffer, u)
CpuTotalBuffer = append(CpuTotalBuffer, u)
NetRecvBuffer = append(NetRecvBuffer, u)
NetSentBuffer = append(NetSentBuffer, u)
DiskWriteBuffer = append(DiskWriteBuffer, u)
DiskReadBuffer = append(DiskReadBuffer, u)
}
for {
cpuTimesBefore, _ := cpu.Times(false)
time.Sleep(1000 * time.Millisecond)
cpuTimesAfter, _ := cpu.Times(false)
threadNum := runtime.GOMAXPROCS(0)
cpuUserUsage := (cpuTimesAfter[0].User - cpuTimesBefore[0].User) / (float64(1000*threadNum) / 1000)
cpuUserUsage *= 100
cpuSystemUsage := (cpuTimesAfter[0].System - cpuTimesBefore[0].System) / (float64(1000*threadNum) / 1000)
cpuSystemUsage *= 100
now := time.Now()
u := usage{
Time: now,
Usage: cpuUserUsage,
}
CpuUserBuffer = append(CpuUserBuffer, u)
s := usage{
Time: now,
Usage: cpuUserUsage + cpuSystemUsage,
}
CpuTotalBuffer = append(CpuTotalBuffer, s)
if len(CpuUserBuffer) > 100 {
CpuUserBuffer = CpuUserBuffer[1:]
}
if len(CpuTotalBuffer) > 100 {
CpuTotalBuffer = CpuTotalBuffer[1:]
}
network, _ = net.IOCounters(false)
if len(network) == 0 {
continue
}
NetRecvBuffer = append(NetRecvBuffer, usage{
Time: now,
Usage: float64(network[0].BytesRecv - LastNetRecv),
})
NetSentBuffer = append(NetRecvBuffer, usage{
Time: now,
Usage: float64(network[0].BytesSent - LastNetSent),
})
LastNetRecv = network[0].BytesRecv
LastNetSent = network[0].BytesSent
if len(NetRecvBuffer) > 100 {
NetRecvBuffer = NetRecvBuffer[1:]
}
if len(NetSentBuffer) > 100 {
NetSentBuffer = NetSentBuffer[1:]
}
diskIOCounters, _ = disk.IOCounters(settings.ServerSettings.DiskName)
diskIO, ok = diskIOCounters[settings.ServerSettings.DiskName]
if ok {
DiskReadBuffer = append(DiskReadBuffer, usage{
Time: now,
Usage: float64(diskIO.ReadCount - LastDiskReads),
})
DiskWriteBuffer = append(DiskWriteBuffer, usage{
Time: now,
Usage: float64(diskIO.WriteCount - LastDiskWrites),
})
if len(DiskReadBuffer) > 100 {
DiskReadBuffer = DiskReadBuffer[1:]
}
if len(DiskWriteBuffer) > 100 {
DiskWriteBuffer = DiskWriteBuffer[1:]
}
LastDiskWrites = diskIO.WriteCount
LastDiskReads = diskIO.ReadCount
}
}
}

View file

@ -1,46 +0,0 @@
package tool
import (
"github.com/shirou/gopsutil/v3/cpu"
"runtime"
"time"
)
type cpuUsage struct {
Time time.Time `json:"x"`
Usage float64 `json:"y"`
}
var CpuUserBuffer []cpuUsage
var CpuTotalBuffer []cpuUsage
func RecordCpuUsage() {
for {
cpuTimesBefore, _ := cpu.Times(false)
time.Sleep(1000 * time.Millisecond)
cpuTimesAfter, _ := cpu.Times(false)
threadNum := runtime.GOMAXPROCS(0)
cpuUserUsage := (cpuTimesAfter[0].User - cpuTimesBefore[0].User) / (float64(1000*threadNum) / 1000)
cpuUserUsage *= 100
cpuSystemUsage := (cpuTimesAfter[0].System - cpuTimesBefore[0].System) / (float64(1000*threadNum) / 1000)
cpuSystemUsage *= 100
now := time.Now()
u := cpuUsage{
Time: now,
Usage: cpuUserUsage,
}
CpuUserBuffer = append(CpuUserBuffer, u)
s := cpuUsage{
Time: now,
Usage: cpuUserUsage + cpuSystemUsage,
}
CpuTotalBuffer = append(CpuTotalBuffer, s)
if len(CpuUserBuffer) > 200 {
CpuUserBuffer = CpuUserBuffer[1:]
}
if len(CpuTotalBuffer) > 200 {
CpuTotalBuffer = CpuTotalBuffer[1:]
}
}
}