-
Notifications
You must be signed in to change notification settings - Fork 59
/
system.go
291 lines (260 loc) · 10.7 KB
/
system.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package selfhosted
import (
"encoding/json"
"os/exec"
"runtime"
"strings"
"time"
"github.com/pganalyze/collector/state"
"github.com/pganalyze/collector/util"
"github.com/prometheus/procfs"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
)
type helperStatus struct {
PostmasterPid int
DataDirectory string
XlogDirectory string
XlogUsedBytes uint64
SystemIdentifier string
}
// GetSystemState - Gets system information about a self-hosted (physical/virtual) system
func GetSystemState(server *state.Server, logger *util.Logger) (system state.SystemState) {
config := server.Config
var status helperStatus
system.Info.Type = state.SelfHostedSystem
system.Info.SelfHosted = &state.SystemInfoSelfHosted{
Architecture: runtime.GOARCH,
}
statusBytes, err := exec.Command("/usr/bin/pganalyze-collector-helper", "status", config.DbDataDirectory).Output()
if err != nil {
server.SelfTest.MarkCollectionAspectError(state.CollectionAspectSystemStats, "error running system stats helper process: %s", err)
logger.PrintVerbose("Selfhosted/System: Could not run helper process: %s", err)
} else {
err = json.Unmarshal(statusBytes, &status)
if err != nil {
server.SelfTest.MarkCollectionAspectError(state.CollectionAspectSystemStats, "error reading system stats helper output: %s", err)
logger.PrintVerbose("Selfhosted/System: Could not unmarshal helper status: %s", err)
}
system.XlogUsedBytes = status.XlogUsedBytes
system.Info.SelfHosted.DatabaseSystemIdentifier = status.SystemIdentifier
}
hostInfo, err := host.Info()
if err != nil {
server.SelfTest.MarkCollectionAspectError(state.CollectionAspectSystemStats, "error getting host information: %s", err)
logger.PrintVerbose("Selfhosted/System: Failed to get host information: %s", err)
} else {
system.Info.BootTime = time.Unix(int64(hostInfo.BootTime), 0)
system.Info.SelfHosted.Hostname = hostInfo.Hostname
system.Info.SelfHosted.OperatingSystem = hostInfo.OS
system.Info.SelfHosted.Platform = hostInfo.Platform
system.Info.SelfHosted.PlatformFamily = hostInfo.PlatformFamily
system.Info.SelfHosted.PlatformVersion = hostInfo.PlatformVersion
system.Info.SelfHosted.KernelVersion = hostInfo.KernelVersion
if hostInfo.VirtualizationRole == "guest" {
system.Info.SelfHosted.VirtualizationSystem = hostInfo.VirtualizationSystem
}
}
loadAvg, err := load.Avg()
if err != nil {
server.SelfTest.MarkCollectionAspectError(state.CollectionAspectSystemStats, "error getting load average: %s", err)
logger.PrintVerbose("Selfhosted/System: Failed to get load average: %s", err)
} else {
system.Scheduler.Loadavg1min = loadAvg.Load1
system.Scheduler.Loadavg5min = loadAvg.Load5
system.Scheduler.Loadavg15min = loadAvg.Load15
}
memory, err := mem.VirtualMemory()
if err != nil {
server.SelfTest.MarkCollectionAspectError(state.CollectionAspectSystemStats, "error getting virtual memory stats: %s", err)
logger.PrintVerbose("Selfhosted/System: Failed to get virtual memory stats: %s", err)
} else {
system.Memory.TotalBytes = memory.Total
system.Memory.CachedBytes = memory.Cached
system.Memory.BuffersBytes = memory.Buffers
system.Memory.FreeBytes = memory.Free
system.Memory.ActiveBytes = memory.Active
system.Memory.InactiveBytes = memory.Inactive
system.Memory.AvailableBytes = memory.Available
}
swap, err := mem.SwapMemory()
if err != nil {
server.SelfTest.MarkCollectionAspectError(state.CollectionAspectSystemStats, "error getting swap stats: %s", err)
logger.PrintVerbose("Selfhosted/System: Failed to get swap stats: %s", err)
} else {
system.Memory.SwapUsedBytes = swap.Used
system.Memory.SwapTotalBytes = swap.Total
}
// TODO: Read the stats below from /proc/meminfo (or patch gopsutil to do so)
system.Memory.WritebackBytes = 0
system.Memory.DirtyBytes = 0
system.Memory.SlabBytes = 0
system.Memory.MappedBytes = 0
system.Memory.PageTablesBytes = 0
system.Memory.HugePagesSizeBytes = 0
system.Memory.HugePagesFree = 0
system.Memory.HugePagesTotal = 0
system.Memory.HugePagesReserved = 0
system.Memory.HugePagesSurplus = 0
cpuInfos, err := cpu.Info()
if err != nil {
server.SelfTest.MarkCollectionAspectError(state.CollectionAspectSystemStats, "error getting CPU info: %s", err)
logger.PrintVerbose("Selfhosted/System: Failed to get CPU info: %s", err)
} else if len(cpuInfos) > 0 {
system.CPUInfo.Model = cpuInfos[0].ModelName
system.CPUInfo.CacheSizeBytes = cpuInfos[0].CacheSize * 1024
system.CPUInfo.SpeedMhz = cpuInfos[0].Mhz
physicalIds := make(map[string]bool)
cores := int32(0)
for _, cpuInfo := range cpuInfos {
physicalIds[cpuInfo.PhysicalID] = true
cores += cpuInfo.Cores
}
system.CPUInfo.SocketCount = int32(len(physicalIds))
system.CPUInfo.PhysicalCoreCount = cores
}
cpuStats, err := cpu.Times(true)
if err != nil {
server.SelfTest.MarkCollectionAspectError(state.CollectionAspectSystemStats, "error getting CPU stats: %s", err)
logger.PrintVerbose("Selfhosted/System: Failed to get CPU stats: %s", err)
} else {
system.CPUInfo.LogicalCoreCount = int32(len(cpuStats))
system.CPUStats = make(state.CPUStatisticMap)
for _, cpuStat := range cpuStats {
system.CPUStats[cpuStat.CPU] = state.CPUStatistic{
DiffedOnInput: false,
UserSeconds: cpuStat.User,
SystemSeconds: cpuStat.System,
IdleSeconds: cpuStat.Idle,
NiceSeconds: cpuStat.Nice,
IowaitSeconds: cpuStat.Iowait,
IrqSeconds: cpuStat.Irq,
SoftIrqSeconds: cpuStat.Softirq,
StealSeconds: cpuStat.Steal,
GuestSeconds: cpuStat.Guest,
GuestNiceSeconds: cpuStat.GuestNice,
}
}
}
netStats, err := net.IOCounters(true)
if err != nil {
server.SelfTest.MarkCollectionAspectError(state.CollectionAspectSystemStats, "error getting network stats: %s", err)
logger.PrintVerbose("Selfhosted/System: Failed to get network stats: %s", err)
} else {
system.NetworkStats = make(state.NetworkStatsMap)
for _, netStat := range netStats {
if (netStat.BytesRecv == 0 && netStat.BytesSent == 0) || netStat.Name == "lo" {
continue
}
system.NetworkStats[netStat.Name] = state.NetworkStats{
ReceiveThroughputBytes: netStat.BytesRecv,
TransmitThroughputBytes: netStat.BytesSent,
}
}
}
system.Disks = make(state.DiskMap)
disks, err := disk.IOCounters()
if err != nil {
server.SelfTest.MarkCollectionAspectError(state.CollectionAspectSystemStats, "error getting disk I/O stats: %s", err)
logger.PrintVerbose("Selfhosted/System: Failed to get disk I/O stats: %s", err)
// We need to insert a dummy device, otherwise we can't attach the partitions anywhere
system.Disks["/"] = state.Disk{}
} else {
system.DiskStats = make(state.DiskStatsMap)
for _, disk := range disks {
system.Disks[disk.Name] = state.Disk{
// TODO: DiskType, Scheduler
}
system.DiskStats[disk.Name] = state.DiskStats{
ReadsCompleted: disk.ReadCount,
ReadsMerged: disk.MergedReadCount,
BytesRead: disk.ReadBytes,
ReadTimeMs: disk.ReadTime,
WritesCompleted: disk.WriteCount,
WritesMerged: disk.MergedWriteCount,
BytesWritten: disk.WriteBytes,
WriteTimeMs: disk.WriteTime,
AvgQueueSize: int32(disk.IopsInProgress),
IoTime: disk.IoTime,
}
}
}
// Remember disk components for software RAID
fs, err := procfs.NewFS("/proc")
if err != nil {
server.SelfTest.MarkCollectionAspectError(state.CollectionAspectSystemStats, "error reading /proc: %s", err)
logger.PrintVerbose("Selfhosted/System: Could not access /proc: %s", err)
} else {
mdstats, err := fs.MDStat()
if err != nil {
server.SelfTest.MarkCollectionAspectError(state.CollectionAspectSystemStats, "error reading mdstat: %s", err)
logger.PrintVerbose("Selfhosted/System: Failed to get mdstat: %s", err)
} else {
for _, mdstat := range mdstats {
mdDisk, exists := system.Disks[mdstat.Name]
if exists {
mdDisk.ComponentDisks = mdstat.Devices
system.Disks[mdstat.Name] = mdDisk
} else {
system.Disks[mdstat.Name] = state.Disk{
ComponentDisks: mdstat.Devices,
}
}
}
}
}
diskPartitions, err := disk.Partitions(true)
if err != nil {
server.SelfTest.MarkCollectionAspectError(state.CollectionAspectSystemStats, "error reading disk partitions: %s", err)
logger.PrintVerbose("Selfhosted/System: Failed to get disk partitions: %s", err)
} else {
system.DiskPartitions = make(state.DiskPartitionMap)
for _, partition := range diskPartitions {
// Linux partition types we can ignore
if partition.Fstype == "devtmpfs" || partition.Fstype == "tmpfs" || partition.Fstype == "devpts" ||
partition.Fstype == "fusectl" || partition.Fstype == "proc" || partition.Fstype == "squashfs" ||
partition.Fstype == "securityfs" || partition.Fstype == "debugfs" || partition.Fstype == "sysfs" ||
partition.Fstype == "pstore" || partition.Fstype == "mqueue" || partition.Fstype == "hugetlbfs" ||
partition.Fstype == "cgroup" || partition.Fstype == "cgroup2" || partition.Fstype == "configfs" ||
partition.Fstype == "fuse.lxcfs" {
continue
}
// OSX partition types we can ignore
if partition.Fstype == "autofs" || partition.Fstype == "devfs" {
continue
}
diskUsage, err := disk.Usage(partition.Mountpoint)
if err != nil {
server.SelfTest.MarkCollectionAspectError(state.CollectionAspectSystemStats, "error reading partition usage stats for %s: %s", partition.Mountpoint, err)
logger.PrintVerbose("Selfhosted/System: Failed to get disk partition usage stats for %s: %s", partition.Mountpoint, err)
} else {
var diskName string
for name := range system.Disks {
if (strings.HasPrefix(partition.Device, name) || strings.HasPrefix(partition.Device, "/dev/"+name)) && len(diskName) < len(name) {
diskName = name
}
}
if status.DataDirectory != "" && strings.HasPrefix(status.DataDirectory, partition.Mountpoint) && len(system.DataDirectoryPartition) < len(partition.Mountpoint) {
system.DataDirectoryPartition = partition.Mountpoint
}
if status.XlogDirectory != "" && strings.HasPrefix(status.XlogDirectory, partition.Mountpoint) && len(system.XlogPartition) < len(partition.Mountpoint) {
system.XlogPartition = partition.Mountpoint
}
system.DiskPartitions[partition.Mountpoint] = state.DiskPartition{
DiskName: diskName,
PartitionName: partition.Device,
FilesystemType: partition.Fstype,
FilesystemOpts: partition.Opts,
UsedBytes: diskUsage.Total - diskUsage.Free,
TotalBytes: diskUsage.Total,
}
}
}
}
server.SelfTest.MarkCollectionAspectOk(state.CollectionAspectSystemStats)
return
}