2017-06-18 15:16:40 +00:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
2019-06-01 04:55:49 +00:00
|
|
|
"net/http"
|
2018-11-06 07:20:44 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
2017-06-18 15:16:40 +00:00
|
|
|
"github.com/aerogo/aero"
|
2019-11-17 07:59:34 +00:00
|
|
|
"github.com/animenotifier/notify.moe/arn"
|
2017-06-18 15:16:40 +00:00
|
|
|
"github.com/animenotifier/notify.moe/components"
|
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.
|
2019-06-01 04:55:49 +00:00
|
|
|
func Get(ctx aero.Context) error {
|
2019-11-17 07:59:34 +00:00
|
|
|
user := arn.GetUserFromContext(ctx)
|
2017-06-18 15:16:40 +00:00
|
|
|
|
2017-10-02 12:56:51 +00:00
|
|
|
if user == nil || (user.Role != "admin" && user.Role != "editor") {
|
2019-06-03 06:06:57 +00:00
|
|
|
return ctx.Redirect(http.StatusTemporaryRedirect, "/")
|
2017-06-18 15:16:40 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|