83 lines
1.5 KiB
Go
Raw Normal View History

2017-06-18 15:16:40 +00:00
package admin
import (
2018-11-06 07:20:44 +00:00
"runtime"
"strings"
2017-06-18 15:16:40 +00:00
"github.com/aerogo/aero"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
2018-11-06 07:20:44 +00:00
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
2017-10-02 12:28:10 +00:00
"github.com/shirou/gopsutil/host"
2018-11-06 07:20:44 +00:00
"github.com/shirou/gopsutil/mem"
2017-06-18 15:16:40 +00:00
)
// Get admin page.
func Get(ctx *aero.Context) string {
user := utils.GetUser(ctx)
2017-10-02 12:56:51 +00:00
if user == nil || (user.Role != "admin" && user.Role != "editor") {
2017-06-18 15:16:40 +00:00
return ctx.Redirect("/")
}
2017-11-03 08:34:21 +00:00
// // CPU
2018-11-06 07:20:44 +00:00
cpuModel := ""
cpuInfo, err := cpu.Info()
if err == nil {
cpuModel = cpuInfo[0].ModelName
}
cpuUsage := 0.0
cpuUsages, err := cpu.Percent(0, false)
2017-10-02 12:28:10 +00:00
2018-11-06 07:20:44 +00:00
if err == nil {
cpuUsage = cpuUsages[0]
}
// Memory
memUsage := 0.0
memTotal := uint64(0)
memInfo, err := mem.VirtualMemory()
2017-10-02 12:28:10 +00:00
2018-11-06 07:20:44 +00:00
if err == nil {
memUsage = memInfo.UsedPercent
memTotal = memInfo.Total
}
2017-10-02 12:28:10 +00:00
2018-11-06 07:20:44 +00:00
// Disk
diskUsage := 0.0
diskTotal := uint64(0)
diskInfo, err := disk.Usage("/")
2017-10-02 12:28:10 +00:00
2018-11-06 07:20:44 +00:00
if err == nil {
diskUsage = diskInfo.UsedPercent
diskTotal = diskInfo.Total
}
2017-10-02 12:28:10 +00:00
2018-11-06 07:20:44 +00:00
// GC
memStats := &runtime.MemStats{}
runtime.ReadMemStats(memStats)
2017-10-02 12:28:10 +00:00
// Host
platform, family, platformVersion, _ := host.PlatformInformation()
2017-11-03 08:34:21 +00:00
kernelVersion, _ := host.KernelVersion()
2018-11-06 07:20:44 +00:00
kernelVersion = strings.Replace(kernelVersion, "-generic", "", 1)
2017-10-02 12:28:10 +00:00
2018-11-06 07:20:44 +00:00
return ctx.HTML(components.Admin(
user,
platform,
family,
platformVersion,
kernelVersion,
cpuUsage,
memUsage,
diskUsage,
cpuModel,
memTotal,
diskTotal,
memStats,
))
2017-10-02 12:28:10 +00:00
}