-
Notifications
You must be signed in to change notification settings - Fork 3
/
basic_details.go
138 lines (119 loc) · 5.07 KB
/
basic_details.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package logrusKit
import (
"errors"
"fmt"
"github.com/richelieu-yang/chimera/v3/src/consts"
"github.com/richelieu-yang/chimera/v3/src/core/cpuKit"
"github.com/richelieu-yang/chimera/v3/src/core/memoryKit"
"github.com/richelieu-yang/chimera/v3/src/core/pathKit"
"github.com/richelieu-yang/chimera/v3/src/core/runtimeKit"
"github.com/richelieu-yang/chimera/v3/src/core/sliceKit"
"github.com/richelieu-yang/chimera/v3/src/core/userKit"
"github.com/richelieu-yang/chimera/v3/src/dataSizeKit"
"github.com/richelieu-yang/chimera/v3/src/diskKit"
"github.com/richelieu-yang/chimera/v3/src/dockerKit"
"github.com/richelieu-yang/chimera/v3/src/ip/ipKit"
"github.com/richelieu-yang/chimera/v3/src/processKit"
"github.com/richelieu-yang/chimera/v3/src/serialize/json/jsonKit"
"github.com/richelieu-yang/chimera/v3/src/time/timeKit"
"github.com/shirou/gopsutil/v3/docker"
"github.com/sirupsen/logrus"
)
// PrintBasicDetails 输出服务器的基本信息(以便于甩锅)
func PrintBasicDetails(logger *logrus.Logger) {
if logger == nil {
logger = logrus.StandardLogger()
}
out := logger.Out
_, _ = out.Write([]byte("===========================================================\n"))
_, _ = out.Write([]byte(consts.Banner))
logger.Infof("[CHIMERA, PROCESS] pid: [%d].", processKit.PID)
/* golang */
logger.Infof("[CHIMERA, GO] version: [%s].", runtimeKit.GoVersion)
logger.Infof("[CHIMERA, GO] GOROOT: [%s].", runtimeKit.GoRoot)
/* os */
printOsInfo()
/* user */
logger.Infof("[CHIMERA, USER] uid: [%s].", userKit.GetUid())
logger.Infof("[CHIMERA, USER] gid: [%s].", userKit.GetGid())
logger.Infof("[CHIMERA, USER] name: [%s].", userKit.GetName())
logger.Infof("[CHIMERA, USER] user name: [%s].", userKit.GetUserName())
logger.Infof("[CHIMERA, USER] home dir: [%s].", userKit.GetUserHomeDir())
/* path */
logger.Infof("[CHIMERA, PATH] working directory: [%s].", pathKit.GetWorkingDir())
logger.Infof("[CHIMERA, PATH] os temporary directory: [%s].", pathKit.GetOsTempDir())
logger.Infof("[CHIMERA, PATH] SelfDir: [%s].", pathKit.SelfDir())
logger.Infof("[CHIMERA, PATH] MainPkgPath: [%s].", pathKit.MainPkgPath())
/* json */
logger.Infof("[CHIMERA, JSON] library: [%s].", jsonKit.GetLibrary())
/* time */
machineTime := timeKit.GetMachineTime()
zoneName, zoneOffset := machineTime.Zone()
logger.Infof("[CHIMERA, TIME] machine time: [%v], zone: [%s, %d].", machineTime, zoneName, zoneOffset)
// Richelieu: 先注释掉,以防(断网或弱网环境下)导致拖延服务启动3s
//if networkTime, source, err := timeKit.GetNetworkTime(); err != nil {
// logger.WithError(err).Warn("[CHIMERA, TIME] fail to get network time")
//} else {
// logger.Infof("[CHIMERA, TIME] network time: [%v], source: [%s].", networkTime, source)
//}
/* ip */
logger.Infof("[CHIMERA, IP] internal ip: [%s].", ipKit.GetInternalIp())
ips := ipKit.GetIps()
logger.Infof("[CHIMERA, IP] ips: [%s].", sliceKit.Join(ips, ", "))
/* host */
if hostInfo, err := runtimeKit.GetHostInfo(); err != nil {
logger.WithError(err).Warn("[CHIMERA, HOST] Fail to get host info.")
} else {
logger.Infof("[CHIMERA, HOST] host name: [%s].", hostInfo.Hostname)
}
/* cpu */
cpuKit.PrintBasicDetails(logger)
/* mac */
//if macAddresses, err := runtimeKit.GetMacAddresses(); err != nil {
// logger.WithFields(logger.Fields{
// "error": err.Error(),
// }).Fatal("fail to get mac addresses")
//} else {
// logger.Infof("[CHIMERA, MAC] mac addresses: [%v].", macAddresses)
//}
/* memory */
stats, err := memoryKit.GetMachineMemoryStats()
if err != nil {
logger.WithError(err).Fatal("[CHIMERA, MEMORY] Fail to get machine memory stats.")
return
}
str := fmt.Sprintf("total: %s, available: %s, used: %s, free: %s, used percent: %.2f%%",
dataSizeKit.ToReadableIecString(float64(stats.Total)),
dataSizeKit.ToReadableIecString(float64(stats.Available)),
dataSizeKit.ToReadableIecString(float64(stats.Used)),
dataSizeKit.ToReadableIecString(float64(stats.Free)),
stats.UsedPercent,
)
logger.Infof("[CHIMERA, MEMORY] machine memory stats: [%s].", str)
/* disk */
diskKit.PrintBasicDetails(logger)
//if stats, err := diskKit.GetDiskUsageStats(); err != nil {
// logger.WithError(err).Warn("[CHIMERA, DISK] Fail to get disk usage stats.")
//} else {
// str := fmt.Sprintf("path: %s, free: %s, used: %s, total: %s, used percent: %.2f%%",
// stats.Path,
// dataSizeKit.ToReadableIecString(float64(stats.Free)),
// dataSizeKit.ToReadableIecString(float64(stats.Used)),
// dataSizeKit.ToReadableIecString(float64(stats.Total)),
// stats.UsedPercent,
// )
// logger.Infof("[CHIMERA, DISK] disk usage stats: [%s].", str)
//}
// Richelieu: 要有条件地使用 gopsutil
/* docker */
if dockerIds, err := dockerKit.GetDockerIdList(); err != nil {
if errors.Is(err, docker.ErrDockerNotAvailable) {
logger.Info("[CHIMERA, DOCKER] Docker isn't available.")
} else {
logger.WithError(err).Warn("[CHIMERA, DOCKER] Fail to get docker id list.")
}
} else {
logger.Infof("[CHIMERA, DOCKER] docker id list: %v.", dockerIds)
}
_, _ = out.Write([]byte("===========================================================\n"))
}