mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-12 02:45:49 +02:00
use goroutine to record cpu usage
This commit is contained in:
parent
16a3d02d9c
commit
897c3cddcd
7 changed files with 134 additions and 63 deletions
9
frontend/src/api/analytic.js
Normal file
9
frontend/src/api/analytic.js
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
import http from '@/lib/http'
|
||||||
|
|
||||||
|
const analytic = {
|
||||||
|
cpu_usage() {
|
||||||
|
return http.get('/analytic/cpu')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default analytic
|
|
@ -3,11 +3,13 @@ import config from './config'
|
||||||
import auth from './auth'
|
import auth from './auth'
|
||||||
import user from './user'
|
import user from './user'
|
||||||
import install from './install'
|
import install from './install'
|
||||||
|
import analytic from './analytic'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
domain,
|
domain,
|
||||||
config,
|
config,
|
||||||
auth,
|
auth,
|
||||||
user,
|
user,
|
||||||
install
|
install,
|
||||||
|
analytic
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,10 +10,16 @@
|
||||||
<span>%</span>
|
<span>%</span>
|
||||||
</template>
|
</template>
|
||||||
</a-statistic>
|
</a-statistic>
|
||||||
<p><translate>Uptime</translate> {{ uptime }}</p>
|
<p>
|
||||||
<p><translate>Load Averages: </translate> 1min:{{ loadavg?.load1?.toFixed(2) }} |
|
<translate>Uptime</translate>
|
||||||
|
{{ uptime }}
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<translate>Load Averages:</translate>
|
||||||
|
1min:{{ loadavg?.load1?.toFixed(2) }} |
|
||||||
5min:{{ loadavg?.load5?.toFixed(2) }} |
|
5min:{{ loadavg?.load5?.toFixed(2) }} |
|
||||||
15min:{{ loadavg?.load15?.toFixed(2) }}</p>
|
15min:{{ loadavg?.load15?.toFixed(2) }}
|
||||||
|
</p>
|
||||||
<line-chart :chart-data="cpu_analytic" :options="cpu_analytic.options" :height="150"/>
|
<line-chart :chart-data="cpu_analytic" :options="cpu_analytic.options" :height="150"/>
|
||||||
</a-col>
|
</a-col>
|
||||||
<a-col :lg="6" :sm="8" :xs="12" class="chart_dashboard">
|
<a-col :lg="6" :sm="8" :xs="12" class="chart_dashboard">
|
||||||
|
@ -120,11 +126,10 @@ export default {
|
||||||
+ btoa(this.$store.state.user.token))
|
+ btoa(this.$store.state.user.token))
|
||||||
this.websocket.onmessage = this.wsOnMessage
|
this.websocket.onmessage = this.wsOnMessage
|
||||||
this.websocket.onopen = this.wsOpen
|
this.websocket.onopen = this.wsOpen
|
||||||
const time = new Date()
|
this.$api.analytic.cpu_usage().then(r => {
|
||||||
for (let i = 200; i > 0; i--) {
|
this.cpu_analytic.datasets[0].data.concat(r.user)
|
||||||
this.cpu_analytic.datasets[0].data.push({x: time-i*1000, y: 0})
|
this.cpu_analytic.datasets[1].data.concat(r.total)
|
||||||
this.cpu_analytic.datasets[1].data.push({x: time-i*1000, y: 0})
|
})
|
||||||
}
|
|
||||||
},
|
},
|
||||||
destroyed() {
|
destroyed() {
|
||||||
this.websocket.close()
|
this.websocket.close()
|
||||||
|
|
7
main.go
7
main.go
|
@ -6,7 +6,7 @@ import (
|
||||||
"github.com/0xJacky/Nginx-UI/server/model"
|
"github.com/0xJacky/Nginx-UI/server/model"
|
||||||
"github.com/0xJacky/Nginx-UI/server/router"
|
"github.com/0xJacky/Nginx-UI/server/router"
|
||||||
"github.com/0xJacky/Nginx-UI/server/settings"
|
"github.com/0xJacky/Nginx-UI/server/settings"
|
||||||
tool2 "github.com/0xJacky/Nginx-UI/server/tool"
|
"github.com/0xJacky/Nginx-UI/server/tool"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"log"
|
"log"
|
||||||
"mime"
|
"mime"
|
||||||
|
@ -32,10 +32,11 @@ func main() {
|
||||||
gin.SetMode(settings.ServerSettings.RunMode)
|
gin.SetMode(settings.ServerSettings.RunMode)
|
||||||
|
|
||||||
settings.Init(confPath)
|
settings.Init(confPath)
|
||||||
log.Printf("nginx config dir path: %s", tool2.GetNginxConfPath(""))
|
log.Printf("nginx config dir path: %s", tool.GetNginxConfPath(""))
|
||||||
if "" != settings.ServerSettings.JwtSecret {
|
if "" != settings.ServerSettings.JwtSecret {
|
||||||
model.Init()
|
model.Init()
|
||||||
go tool2.AutoCert()
|
go tool.AutoCert()
|
||||||
|
go tool.RecordCpuUsage()
|
||||||
}
|
}
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
|
|
|
@ -3,6 +3,7 @@ package api
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/0xJacky/Nginx-UI/server/tool"
|
||||||
"github.com/shirou/gopsutil/v3/cpu"
|
"github.com/shirou/gopsutil/v3/cpu"
|
||||||
"github.com/shirou/gopsutil/v3/disk"
|
"github.com/shirou/gopsutil/v3/disk"
|
||||||
"github.com/shirou/gopsutil/v3/host"
|
"github.com/shirou/gopsutil/v3/host"
|
||||||
|
@ -97,3 +98,10 @@ func Analytic(c *gin.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetCpuUsageRecord(c *gin.Context) {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"user": tool.CpuUserBuffer,
|
||||||
|
"total": tool.CpuTotalBuffer,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -38,6 +38,7 @@ func InitRouter() *gin.Engine {
|
||||||
g := g.Group("/", authRequired())
|
g := g.Group("/", authRequired())
|
||||||
{
|
{
|
||||||
g.GET("/analytic", api.Analytic)
|
g.GET("/analytic", api.Analytic)
|
||||||
|
g.GET("/analytic/cpu", api.GetCpuUsageRecord)
|
||||||
|
|
||||||
g.GET("/users", api.GetUsers)
|
g.GET("/users", api.GetUsers)
|
||||||
g.GET("/user/:id", api.GetUser)
|
g.GET("/user/:id", api.GetUser)
|
||||||
|
|
45
server/tool/cpu_usage.go
Normal file
45
server/tool/cpu_usage.go
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
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)
|
||||||
|
cpuSystemUsage := (cpuTimesAfter[0].System - cpuTimesBefore[0].System) / (float64(1000*threadNum) / 1000)
|
||||||
|
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:]
|
||||||
|
}
|
||||||
|
// time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue