This repository has been archived by the owner on Jun 4, 2022. It is now read-only.
/
debug.go
72 lines (64 loc) · 2.59 KB
/
debug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package utils
import (
"runtime"
"github.com/MikMuellerDev/smarthome/core/database"
"github.com/MikMuellerDev/smarthome/core/hardware"
)
type DebugInfo struct {
ServerVersion string `json:"version"`
DatabaseOnline bool `json:"databaseOnline"`
DatabaseStats database.DBStatus `json:"databaseStats"`
CpuCores uint8 `json:"cpuCores"`
Goroutines uint16 `json:"goroutines"`
GoVersion string `json:"goVersion"`
MemoryUsage uint16 `json:"memoryUsage"`
PowerJobCount uint16 `json:"powerJobCount"`
PowerJobWithErrorCount uint16 `json:"lastPowerJobErrorCount"`
PowerJobs []hardware.PowerJob `json:"powerJobs"`
PowerJobResults []hardware.JobResult `json:"powerJobResults"`
HardwareNodesCount uint8 `json:"hardwareNodesCount"`
HardwareNodesOnline uint8 `json:"hardwareNodesOnline"`
HardwareNodesEnabled uint8 `json:"hardwareNodesEnabled"`
Nodes []database.HardwareNode `json:"hardwareNodes"`
}
func SysInfo() DebugInfo {
var memoryStats runtime.MemStats
runtime.ReadMemStats(&memoryStats)
if err := hardware.RunNodeCheck(); err != nil {
log.Error("Failed to run node check: ", err.Error())
}
nodes, err := database.GetHardwareNodes()
if err != nil {
log.Error("Failed to obtain node information while getting debug info: ", err.Error())
}
nodesOnline := 0
nodesEnabled := 0
for index, node := range nodes {
if node.Online {
nodesOnline += 1
}
if node.Enabled {
nodesEnabled += 1
}
// Remove token visibility from debug info
nodes[index].Token = "redacted"
}
err = database.CheckDatabase()
return DebugInfo{
ServerVersion: Version,
DatabaseOnline: err == nil,
DatabaseStats: database.GetDatabaseStats(),
CpuCores: uint8(runtime.NumCPU()),
Goroutines: uint16(runtime.NumGoroutine()),
GoVersion: runtime.Version(),
MemoryUsage: uint16(memoryStats.Alloc / 1024 / 1024),
PowerJobCount: uint16(hardware.GetPendingJobCount()),
PowerJobs: hardware.GetPendingJobs(),
PowerJobResults: hardware.GetResults(),
PowerJobWithErrorCount: hardware.GetJobsWithErrorInHandler(),
HardwareNodesCount: uint8(len(nodes)),
HardwareNodesOnline: uint8(nodesOnline),
HardwareNodesEnabled: uint8(nodesEnabled),
Nodes: nodes,
}
}