mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-12 19:05:55 +02:00
enhance: ignore ws expected error
This commit is contained in:
parent
0c1b5f12c6
commit
2bc14bd23b
4 changed files with 168 additions and 162 deletions
File diff suppressed because one or more lines are too long
|
@ -1 +1 @@
|
||||||
{"version":"2.0.0-beta.2","build_id":5,"total_build":209}
|
{"version":"2.0.0-beta.2","build_id":6,"total_build":210}
|
|
@ -1 +1 @@
|
||||||
{"version":"2.0.0-beta.2","build_id":5,"total_build":209}
|
{"version":"2.0.0-beta.2","build_id":6,"total_build":210}
|
|
@ -1,208 +1,214 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/0xJacky/Nginx-UI/server/internal/analytic"
|
"github.com/0xJacky/Nginx-UI/server/internal/analytic"
|
||||||
"github.com/0xJacky/Nginx-UI/server/internal/logger"
|
"github.com/0xJacky/Nginx-UI/server/internal/logger"
|
||||||
"github.com/shirou/gopsutil/v3/cpu"
|
"github.com/shirou/gopsutil/v3/cpu"
|
||||||
"github.com/shirou/gopsutil/v3/host"
|
"github.com/shirou/gopsutil/v3/host"
|
||||||
"github.com/shirou/gopsutil/v3/load"
|
"github.com/shirou/gopsutil/v3/load"
|
||||||
"github.com/shirou/gopsutil/v3/net"
|
"github.com/shirou/gopsutil/v3/net"
|
||||||
"github.com/spf13/cast"
|
"github.com/spf13/cast"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CPUStat struct {
|
type CPUStat struct {
|
||||||
User float64 `json:"user"`
|
User float64 `json:"user"`
|
||||||
System float64 `json:"system"`
|
System float64 `json:"system"`
|
||||||
Idle float64 `json:"idle"`
|
Idle float64 `json:"idle"`
|
||||||
Total float64 `json:"total"`
|
Total float64 `json:"total"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Stat struct {
|
type Stat struct {
|
||||||
Uptime uint64 `json:"uptime"`
|
Uptime uint64 `json:"uptime"`
|
||||||
LoadAvg *load.AvgStat `json:"loadavg"`
|
LoadAvg *load.AvgStat `json:"loadavg"`
|
||||||
CPU CPUStat `json:"cpu"`
|
CPU CPUStat `json:"cpu"`
|
||||||
Memory analytic.MemStat `json:"memory"`
|
Memory analytic.MemStat `json:"memory"`
|
||||||
Disk analytic.DiskStat `json:"disk"`
|
Disk analytic.DiskStat `json:"disk"`
|
||||||
Network net.IOCountersStat `json:"network"`
|
Network net.IOCountersStat `json:"network"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Analytic(c *gin.Context) {
|
func Analytic(c *gin.Context) {
|
||||||
var upGrader = websocket.Upgrader{
|
var upGrader = websocket.Upgrader{
|
||||||
CheckOrigin: func(r *http.Request) bool {
|
CheckOrigin: func(r *http.Request) bool {
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
// upgrade http to websocket
|
// upgrade http to websocket
|
||||||
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
|
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer ws.Close()
|
defer ws.Close()
|
||||||
|
|
||||||
var stat Stat
|
var stat Stat
|
||||||
|
|
||||||
for {
|
for {
|
||||||
stat.Memory, err = analytic.GetMemoryStat()
|
stat.Memory, err = analytic.GetMemoryStat()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cpuTimesBefore, _ := cpu.Times(false)
|
cpuTimesBefore, _ := cpu.Times(false)
|
||||||
time.Sleep(1000 * time.Millisecond)
|
time.Sleep(1000 * time.Millisecond)
|
||||||
cpuTimesAfter, _ := cpu.Times(false)
|
cpuTimesAfter, _ := cpu.Times(false)
|
||||||
threadNum := runtime.GOMAXPROCS(0)
|
threadNum := runtime.GOMAXPROCS(0)
|
||||||
cpuUserUsage := (cpuTimesAfter[0].User - cpuTimesBefore[0].User) / (float64(1000*threadNum) / 1000)
|
cpuUserUsage := (cpuTimesAfter[0].User - cpuTimesBefore[0].User) / (float64(1000*threadNum) / 1000)
|
||||||
cpuSystemUsage := (cpuTimesAfter[0].System - cpuTimesBefore[0].System) / (float64(1000*threadNum) / 1000)
|
cpuSystemUsage := (cpuTimesAfter[0].System - cpuTimesBefore[0].System) / (float64(1000*threadNum) / 1000)
|
||||||
|
|
||||||
stat.CPU = CPUStat{
|
stat.CPU = CPUStat{
|
||||||
User: cast.ToFloat64(fmt.Sprintf("%.2f", cpuUserUsage*100)),
|
User: cast.ToFloat64(fmt.Sprintf("%.2f", cpuUserUsage*100)),
|
||||||
System: cast.ToFloat64(fmt.Sprintf("%.2f", cpuSystemUsage*100)),
|
System: cast.ToFloat64(fmt.Sprintf("%.2f", cpuSystemUsage*100)),
|
||||||
Idle: cast.ToFloat64(fmt.Sprintf("%.2f", (1-cpuUserUsage-cpuSystemUsage)*100)),
|
Idle: cast.ToFloat64(fmt.Sprintf("%.2f", (1-cpuUserUsage-cpuSystemUsage)*100)),
|
||||||
Total: cast.ToFloat64(fmt.Sprintf("%.2f", (cpuUserUsage+cpuSystemUsage)*100)),
|
Total: cast.ToFloat64(fmt.Sprintf("%.2f", (cpuUserUsage+cpuSystemUsage)*100)),
|
||||||
}
|
}
|
||||||
|
|
||||||
stat.Uptime, _ = host.Uptime()
|
stat.Uptime, _ = host.Uptime()
|
||||||
|
|
||||||
stat.LoadAvg, _ = load.Avg()
|
stat.LoadAvg, _ = load.Avg()
|
||||||
|
|
||||||
stat.Disk, err = analytic.GetDiskStat()
|
stat.Disk, err = analytic.GetDiskStat()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
network, _ := net.IOCounters(false)
|
network, _ := net.IOCounters(false)
|
||||||
|
|
||||||
if len(network) > 0 {
|
if len(network) > 0 {
|
||||||
stat.Network = network[0]
|
stat.Network = network[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
// write
|
// write
|
||||||
err = ws.WriteJSON(stat)
|
if err != nil || websocket.IsUnexpectedCloseError(err,
|
||||||
if err != nil {
|
websocket.CloseGoingAway,
|
||||||
logger.Error(err)
|
websocket.CloseNoStatusReceived,
|
||||||
break
|
websocket.CloseNormalClosure) {
|
||||||
}
|
logger.Error(err)
|
||||||
time.Sleep(800 * time.Microsecond)
|
break
|
||||||
}
|
}
|
||||||
|
time.Sleep(800 * time.Microsecond)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAnalyticInit(c *gin.Context) {
|
func GetAnalyticInit(c *gin.Context) {
|
||||||
cpuInfo, _ := cpu.Info()
|
cpuInfo, _ := cpu.Info()
|
||||||
network, _ := net.IOCounters(false)
|
network, _ := net.IOCounters(false)
|
||||||
memory, err := analytic.GetMemoryStat()
|
memory, err := analytic.GetMemoryStat()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
diskStat, err := analytic.GetDiskStat()
|
diskStat, err := analytic.GetDiskStat()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var _net net.IOCountersStat
|
var _net net.IOCountersStat
|
||||||
if len(network) > 0 {
|
if len(network) > 0 {
|
||||||
_net = network[0]
|
_net = network[0]
|
||||||
}
|
}
|
||||||
hostInfo, _ := host.Info()
|
hostInfo, _ := host.Info()
|
||||||
|
|
||||||
switch hostInfo.Platform {
|
switch hostInfo.Platform {
|
||||||
case "ubuntu":
|
case "ubuntu":
|
||||||
hostInfo.Platform = "Ubuntu"
|
hostInfo.Platform = "Ubuntu"
|
||||||
case "centos":
|
case "centos":
|
||||||
hostInfo.Platform = "CentOS"
|
hostInfo.Platform = "CentOS"
|
||||||
}
|
}
|
||||||
|
|
||||||
loadAvg, _ := load.Avg()
|
loadAvg, _ := load.Avg()
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"host": hostInfo,
|
"host": hostInfo,
|
||||||
"cpu": gin.H{
|
"cpu": gin.H{
|
||||||
"info": cpuInfo,
|
"info": cpuInfo,
|
||||||
"user": analytic.CpuUserRecord,
|
"user": analytic.CpuUserRecord,
|
||||||
"total": analytic.CpuTotalRecord,
|
"total": analytic.CpuTotalRecord,
|
||||||
},
|
},
|
||||||
"network": gin.H{
|
"network": gin.H{
|
||||||
"init": _net,
|
"init": _net,
|
||||||
"bytesRecv": analytic.NetRecvRecord,
|
"bytesRecv": analytic.NetRecvRecord,
|
||||||
"bytesSent": analytic.NetSentRecord,
|
"bytesSent": analytic.NetSentRecord,
|
||||||
},
|
},
|
||||||
"disk_io": gin.H{
|
"disk_io": gin.H{
|
||||||
"writes": analytic.DiskWriteRecord,
|
"writes": analytic.DiskWriteRecord,
|
||||||
"reads": analytic.DiskReadRecord,
|
"reads": analytic.DiskReadRecord,
|
||||||
},
|
},
|
||||||
"memory": memory,
|
"memory": memory,
|
||||||
"disk": diskStat,
|
"disk": diskStat,
|
||||||
"loadavg": loadAvg,
|
"loadavg": loadAvg,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetNodeStat(c *gin.Context) {
|
func GetNodeStat(c *gin.Context) {
|
||||||
var upGrader = websocket.Upgrader{
|
var upGrader = websocket.Upgrader{
|
||||||
CheckOrigin: func(r *http.Request) bool {
|
CheckOrigin: func(r *http.Request) bool {
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
// upgrade http to websocket
|
// upgrade http to websocket
|
||||||
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
|
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer ws.Close()
|
defer ws.Close()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// write
|
// write
|
||||||
err = ws.WriteJSON(analytic.GetNodeStat())
|
if err != nil || websocket.IsUnexpectedCloseError(err,
|
||||||
if err != nil {
|
websocket.CloseGoingAway,
|
||||||
logger.Error(err)
|
websocket.CloseNoStatusReceived,
|
||||||
break
|
websocket.CloseNormalClosure) {
|
||||||
}
|
logger.Error(err)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
time.Sleep(10 * time.Second)
|
time.Sleep(10 * time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetNodesAnalytic(c *gin.Context) {
|
func GetNodesAnalytic(c *gin.Context) {
|
||||||
var upGrader = websocket.Upgrader{
|
var upGrader = websocket.Upgrader{
|
||||||
CheckOrigin: func(r *http.Request) bool {
|
CheckOrigin: func(r *http.Request) bool {
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
// upgrade http to websocket
|
// upgrade http to websocket
|
||||||
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
|
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer ws.Close()
|
defer ws.Close()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// write
|
// write
|
||||||
err = ws.WriteJSON(analytic.NodeMap)
|
if err != nil || websocket.IsUnexpectedCloseError(err,
|
||||||
if err != nil {
|
websocket.CloseGoingAway,
|
||||||
logger.Error(err)
|
websocket.CloseNoStatusReceived,
|
||||||
break
|
websocket.CloseNormalClosure) {
|
||||||
}
|
logger.Error(err)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
time.Sleep(10 * time.Second)
|
time.Sleep(10 * time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue