forked from minio/madmin-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
health.go
1252 lines (1091 loc) · 33.8 KB
/
health.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 2015-2023 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package madmin
import (
"bufio"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
"time"
"github.com/minio/madmin-go/v3/cgroup"
"github.com/minio/madmin-go/v3/kernel"
"github.com/prometheus/procfs"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/mem"
"github.com/shirou/gopsutil/v3/process"
)
const (
// HealthInfoVersion0 is version 0
HealthInfoVersion0 = ""
// HealthInfoVersion1 is version 1
HealthInfoVersion1 = "1"
// HealthInfoVersion2 is version 2
HealthInfoVersion2 = "2"
// HealthInfoVersion3 is version 3
HealthInfoVersion3 = "3"
// HealthInfoVersion is current health info version.
HealthInfoVersion = HealthInfoVersion3
)
const (
SysErrAuditEnabled = "audit is enabled"
SysErrUpdatedbInstalled = "updatedb is installed"
)
const (
SrvSELinux = "selinux"
SrvNotInstalled = "not-installed"
)
const (
sysClassBlock = "/sys/class/block"
sysClassDMI = "/sys/class/dmi"
runDevDataPfx = "/run/udev/data/b"
devDir = "/dev/"
devLoopDir = "/dev/loop"
)
// NodeInfo - Interface to abstract any struct that contains address/endpoint and error fields
type NodeInfo interface {
GetAddr() string
SetAddr(addr string)
SetError(err string)
}
// NodeCommon - Common fields across most node-specific health structs
type NodeCommon struct {
Addr string `json:"addr"`
Error string `json:"error,omitempty"`
}
// GetAddr - return the address of the node
func (n *NodeCommon) GetAddr() string {
return n.Addr
}
// SetAddr - set the address of the node
func (n *NodeCommon) SetAddr(addr string) {
n.Addr = addr
}
// SetError - set the address of the node
func (n *NodeCommon) SetError(err string) {
n.Error = err
}
// SysErrors - contains a system error
type SysErrors struct {
NodeCommon
Errors []string `json:"errors,omitempty"`
}
// SysServices - info about services that affect minio
type SysServices struct {
NodeCommon
Services []SysService `json:"services,omitempty"`
}
// SysConfig - info about services that affect minio
type SysConfig struct {
NodeCommon
Config map[string]interface{} `json:"config,omitempty"`
}
// SysService - name and status of a sys service
type SysService struct {
Name string `json:"name"`
Status string `json:"status"`
}
// CPU contains system's CPU information.
type CPU struct {
VendorID string `json:"vendor_id"`
Family string `json:"family"`
Model string `json:"model"`
Stepping int32 `json:"stepping"`
PhysicalID string `json:"physical_id"`
ModelName string `json:"model_name"`
Mhz float64 `json:"mhz"`
CacheSize int32 `json:"cache_size"`
Flags []string `json:"flags"`
Microcode string `json:"microcode"`
Cores int `json:"cores"` // computed
}
// CPUs contains all CPU information of a node.
type CPUs struct {
NodeCommon
CPUs []CPU `json:"cpus,omitempty"`
CPUFreqStats []CPUFreqStats `json:"freq_stats,omitempty"`
}
// CPUFreqStats CPU frequency stats
type CPUFreqStats struct {
Name string
CpuinfoCurrentFrequency *uint64
CpuinfoMinimumFrequency *uint64
CpuinfoMaximumFrequency *uint64
CpuinfoTransitionLatency *uint64
ScalingCurrentFrequency *uint64
ScalingMinimumFrequency *uint64
ScalingMaximumFrequency *uint64
AvailableGovernors string
Driver string
Governor string
RelatedCpus string
SetSpeed string
}
// GetCPUs returns system's all CPU information.
func GetCPUs(ctx context.Context, addr string) CPUs {
infos, err := cpu.InfoWithContext(ctx)
if err != nil {
return CPUs{
NodeCommon: NodeCommon{
Addr: addr,
Error: err.Error(),
},
}
}
cpuMap := map[string]CPU{}
for _, info := range infos {
cpu, found := cpuMap[info.PhysicalID]
if found {
cpu.Cores++
} else {
cpu = CPU{
VendorID: info.VendorID,
Family: info.Family,
Model: info.Model,
Stepping: info.Stepping,
PhysicalID: info.PhysicalID,
ModelName: info.ModelName,
Mhz: info.Mhz,
CacheSize: info.CacheSize,
Flags: info.Flags,
Microcode: info.Microcode,
Cores: 1,
}
}
cpuMap[info.PhysicalID] = cpu
}
cpus := []CPU{}
for _, cpu := range cpuMap {
cpus = append(cpus, cpu)
}
var errMsg string
freqStats, err := getCPUFreqStats()
if err != nil {
errMsg = err.Error()
}
return CPUs{
NodeCommon: NodeCommon{Addr: addr, Error: errMsg},
CPUs: cpus,
CPUFreqStats: freqStats,
}
}
// Partition contains disk partition's information.
type Partition struct {
Error string `json:"error,omitempty"`
Device string `json:"device,omitempty"`
Model string `json:"model,omitempty"`
Revision string `json:"revision,omitempty"`
Mountpoint string `json:"mountpoint,omitempty"`
FSType string `json:"fs_type,omitempty"`
MountOptions string `json:"mount_options,omitempty"`
MountFSType string `json:"mount_fs_type,omitempty"`
SpaceTotal uint64 `json:"space_total,omitempty"`
SpaceFree uint64 `json:"space_free,omitempty"`
InodeTotal uint64 `json:"inode_total,omitempty"`
InodeFree uint64 `json:"inode_free,omitempty"`
}
// NetInfo contains information about a network inerface
type NetInfo struct {
NodeCommon
Interface string `json:"interface,omitempty"`
Driver string `json:"driver,omitempty"`
FirmwareVersion string `json:"firmware_version,omitempty"`
}
// Partitions contains all disk partitions information of a node.
type Partitions struct {
NodeCommon
Partitions []Partition `json:"partitions,omitempty"`
}
// driveHwInfo contains hardware information about a drive
type driveHwInfo struct {
Model string
Revision string
}
func getDriveHwInfo(partDevice string) (info driveHwInfo, err error) {
partDevName := strings.ReplaceAll(partDevice, devDir, "")
devPath := path.Join(sysClassBlock, partDevName, "dev")
_, err = os.Stat(devPath)
if err != nil {
return
}
var data []byte
data, err = os.ReadFile(devPath)
if err != nil {
return
}
majorMinor := strings.TrimSpace(string(data))
driveInfoPath := runDevDataPfx + majorMinor
var f *os.File
f, err = os.Open(driveInfoPath)
if err != nil {
return
}
defer f.Close()
buf := bufio.NewScanner(f)
for buf.Scan() {
field := strings.SplitN(buf.Text(), "=", 2)
if len(field) == 2 {
if field[0] == "E:ID_MODEL" {
info.Model = field[1]
}
if field[0] == "E:ID_REVISION" {
info.Revision = field[1]
}
if len(info.Model) > 0 && len(info.Revision) > 0 {
break
}
}
}
return
}
// GetPartitions returns all disk partitions information of a node running linux only operating system.
func GetPartitions(ctx context.Context, addr string) Partitions {
if runtime.GOOS != "linux" {
return Partitions{
NodeCommon: NodeCommon{
Addr: addr,
Error: "unsupported operating system " + runtime.GOOS,
},
}
}
parts, err := disk.PartitionsWithContext(ctx, false)
if err != nil {
return Partitions{
NodeCommon: NodeCommon{
Addr: addr,
Error: err.Error(),
},
}
}
partitions := []Partition{}
for i := range parts {
usage, err := disk.UsageWithContext(ctx, parts[i].Mountpoint)
if err != nil {
partitions = append(partitions, Partition{
Device: parts[i].Device,
Error: err.Error(),
})
} else {
var di driveHwInfo
device := parts[i].Device
if strings.HasPrefix(device, devDir) && !strings.HasPrefix(device, devLoopDir) {
// ignore any error in finding device model
di, _ = getDriveHwInfo(device)
}
partitions = append(partitions, Partition{
Device: device,
Mountpoint: parts[i].Mountpoint,
FSType: parts[i].Fstype,
MountOptions: strings.Join(parts[i].Opts, ","),
MountFSType: usage.Fstype,
SpaceTotal: usage.Total,
SpaceFree: usage.Free,
InodeTotal: usage.InodesTotal,
InodeFree: usage.InodesFree,
Model: di.Model,
Revision: di.Revision,
})
}
}
return Partitions{
NodeCommon: NodeCommon{Addr: addr},
Partitions: partitions,
}
}
// OSInfo contains operating system's information.
type OSInfo struct {
NodeCommon
Info host.InfoStat `json:"info,omitempty"`
Sensors []host.TemperatureStat `json:"sensors,omitempty"`
}
// TimeInfo contains current time with timezone, and
// the roundtrip duration when fetching it remotely
type TimeInfo struct {
CurrentTime time.Time `json:"current_time"`
RoundtripDuration int32 `json:"roundtrip_duration"`
TimeZone string `json:"time_zone"`
}
// XFSErrorConfigs - stores the error configs of all XFS devices on the server
type XFSErrorConfigs struct {
Configs []XFSErrorConfig `json:"configs,omitempty"`
Error string `json:"error,omitempty"`
}
// XFSErrorConfig - stores XFS error configuration info for max_retries
type XFSErrorConfig struct {
ConfigFile string `json:"config_file"`
MaxRetries int `json:"max_retries"`
}
// GetOSInfo returns linux only operating system's information.
func GetOSInfo(ctx context.Context, addr string) OSInfo {
if runtime.GOOS != "linux" {
return OSInfo{
NodeCommon: NodeCommon{
Addr: addr,
Error: "unsupported operating system " + runtime.GOOS,
},
}
}
kr, err := kernel.CurrentRelease()
if err != nil {
return OSInfo{
NodeCommon: NodeCommon{
Addr: addr,
Error: err.Error(),
},
}
}
info, err := host.InfoWithContext(ctx)
if err != nil {
return OSInfo{
NodeCommon: NodeCommon{
Addr: addr,
Error: err.Error(),
},
}
}
osInfo := OSInfo{
NodeCommon: NodeCommon{Addr: addr},
Info: *info,
}
osInfo.Info.KernelVersion = kr
osInfo.Sensors, _ = host.SensorsTemperaturesWithContext(ctx)
return osInfo
}
// GetSysConfig returns config values from the system
// (only those affecting minio performance)
func GetSysConfig(_ context.Context, addr string) SysConfig {
sc := SysConfig{
NodeCommon: NodeCommon{Addr: addr},
Config: map[string]interface{}{},
}
proc, err := procfs.Self()
if err != nil {
sc.Error = "rlimit: " + err.Error()
} else {
limits, err := proc.Limits()
if err != nil {
sc.Error = "rlimit: " + err.Error()
} else {
sc.Config["rlimit-max"] = limits.OpenFiles
}
}
zone, _ := time.Now().Zone()
sc.Config["time-info"] = TimeInfo{
CurrentTime: time.Now(),
TimeZone: zone,
}
xfsErrorConfigs := getXFSErrorMaxRetries()
if len(xfsErrorConfigs.Configs) > 0 || len(xfsErrorConfigs.Error) > 0 {
sc.Config["xfs-error-config"] = xfsErrorConfigs
}
sc.Config["thp-config"] = getTHPConfigs()
procCmdLine, err := getProcCmdLine()
if err != nil {
errMsg := "proc-cmdline: " + err.Error()
if len(sc.Error) == 0 {
sc.Error = errMsg
} else {
sc.Error = sc.Error + ", " + errMsg
}
} else {
sc.Config["proc-cmdline"] = procCmdLine
}
return sc
}
func readIntFromFile(filePath string) (num int, err error) {
var file *os.File
file, err = os.Open(filePath)
if err != nil {
return
}
defer file.Close()
var data []byte
data, err = io.ReadAll(file)
if err != nil {
return
}
return strconv.Atoi(strings.TrimSpace(string(data)))
}
func getTHPConfigs() map[string]string {
configs := map[string]string{}
captureTHPConfig(configs, "/sys/kernel/mm/transparent_hugepage/enabled", "enabled")
captureTHPConfig(configs, "/sys/kernel/mm/transparent_hugepage/defrag", "defrag")
captureTHPConfig(configs, "/sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_none", "max_ptes_none")
return configs
}
func getProcCmdLine() ([]string, error) {
fs, err := procfs.NewDefaultFS()
if err != nil {
return nil, err
}
return fs.CmdLine()
}
func captureTHPConfig(configs map[string]string, filePath string, cfgName string) {
errFieldName := cfgName + "_error"
data, err := os.ReadFile(filePath)
if err != nil {
configs[errFieldName] = err.Error()
return
}
configs[cfgName] = strings.TrimSpace(string(data))
}
func getXFSErrorMaxRetries() XFSErrorConfigs {
xfsErrCfgPattern := "/sys/fs/xfs/*/error/metadata/*/max_retries"
configFiles, err := filepath.Glob(xfsErrCfgPattern)
if err != nil {
return XFSErrorConfigs{Error: err.Error()}
}
configs := []XFSErrorConfig{}
var errMsg string
for _, configFile := range configFiles {
maxRetries, err := readIntFromFile(configFile)
if err != nil {
errMsg = err.Error()
break
}
configs = append(configs, XFSErrorConfig{
ConfigFile: configFile,
MaxRetries: maxRetries,
})
}
return XFSErrorConfigs{
Configs: configs,
Error: errMsg,
}
}
// ProductInfo defines a host's product information
type ProductInfo struct {
NodeCommon
Family string `json:"family"`
Name string `json:"name"`
Vendor string `json:"vendor"`
SerialNumber string `json:"serial_number"`
UUID string `json:"uuid"`
SKU string `json:"sku"`
Version string `json:"version"`
}
func getDMIInfo(ask string) string {
value, err := os.ReadFile(path.Join(sysClassDMI, "id", ask))
if err != nil {
return "unknown"
}
return strings.TrimSpace(string(value))
}
// GetProductInfo returns a host's product information
func GetProductInfo(addr string) ProductInfo {
return ProductInfo{
NodeCommon: NodeCommon{Addr: addr},
Family: getDMIInfo("product_family"),
Name: getDMIInfo("product_name"),
Vendor: getDMIInfo("sys_vendor"),
SerialNumber: getDMIInfo("product_serial"),
UUID: getDMIInfo("product_uuid"),
SKU: getDMIInfo("product_sku"),
Version: getDMIInfo("product_version"),
}
}
// GetSysServices returns info of sys services that affect minio
func GetSysServices(_ context.Context, addr string) SysServices {
ss := SysServices{
NodeCommon: NodeCommon{Addr: addr},
Services: []SysService{},
}
srv, e := getSELinuxInfo()
if e != nil {
ss.Error = e.Error()
} else {
ss.Services = append(ss.Services, srv)
}
return ss
}
func getSELinuxInfo() (SysService, error) {
ss := SysService{Name: SrvSELinux}
file, err := os.Open("/etc/selinux/config")
if err != nil {
if errors.Is(err, os.ErrNotExist) {
ss.Status = SrvNotInstalled
return ss, nil
}
return ss, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
tokens := strings.SplitN(strings.TrimSpace(scanner.Text()), "=", 2)
if len(tokens) == 2 && tokens[0] == "SELINUX" {
ss.Status = tokens[1]
return ss, nil
}
}
return ss, scanner.Err()
}
// GetSysErrors returns issues in system setup/config
func GetSysErrors(_ context.Context, addr string) SysErrors {
se := SysErrors{NodeCommon: NodeCommon{Addr: addr}}
if runtime.GOOS != "linux" {
return se
}
ae, err := isAuditEnabled()
if err != nil {
se.Error = "audit: " + err.Error()
} else if ae {
se.Errors = append(se.Errors, SysErrAuditEnabled)
}
_, err = exec.LookPath("updatedb")
if err == nil {
se.Errors = append(se.Errors, SysErrUpdatedbInstalled)
} else if !strings.HasSuffix(err.Error(), exec.ErrNotFound.Error()) {
errMsg := "updatedb: " + err.Error()
if len(se.Error) == 0 {
se.Error = errMsg
} else {
se.Error = se.Error + ", " + errMsg
}
}
return se
}
// Audit is enabled if either `audit=1` is present in /proc/cmdline
// or the `kauditd` process is running
func isAuditEnabled() (bool, error) {
file, err := os.Open("/proc/cmdline")
if err != nil {
return false, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if strings.Contains(scanner.Text(), "audit=1") {
return true, nil
}
}
return isKauditdRunning()
}
func isKauditdRunning() (bool, error) {
procs, err := process.Processes()
if err != nil {
return false, err
}
for _, proc := range procs {
pname, err := proc.Name()
if err == nil && pname == "kauditd" {
return true, nil
}
}
return false, nil
}
// MemInfo contains system's RAM and swap information.
type MemInfo struct {
NodeCommon
Total uint64 `json:"total,omitempty"`
Used uint64 `json:"used,omitempty"`
Free uint64 `json:"free,omitempty"`
Available uint64 `json:"available,omitempty"`
Shared uint64 `json:"shared,omitempty"`
Cache uint64 `json:"cache,omitempty"`
Buffers uint64 `json:"buffer,omitempty"`
SwapSpaceTotal uint64 `json:"swap_space_total,omitempty"`
SwapSpaceFree uint64 `json:"swap_space_free,omitempty"`
// Limit will store cgroup limit if configured and
// less than Total, otherwise same as Total
Limit uint64 `json:"limit,omitempty"`
}
// Get the final system memory limit chosen by the user.
// by default without any configuration on a vanilla Linux
// system you would see physical RAM limit. If cgroup
// is configured at some point in time this function
// would return the memory limit chosen for the given pid.
func getMemoryLimit(sysLimit uint64) uint64 {
// Following code is deliberately ignoring the error.
cGroupLimit, err := cgroup.GetMemoryLimit(os.Getpid())
if err == nil && cGroupLimit <= sysLimit {
// cgroup limit is lesser than system limit means
// user wants to limit the memory usage further
return cGroupLimit
}
return sysLimit
}
// GetMemInfo returns system's RAM and swap information.
func GetMemInfo(ctx context.Context, addr string) MemInfo {
meminfo, err := mem.VirtualMemoryWithContext(ctx)
if err != nil {
return MemInfo{
NodeCommon: NodeCommon{
Addr: addr,
Error: err.Error(),
},
}
}
swapinfo, err := mem.SwapMemoryWithContext(ctx)
if err != nil {
return MemInfo{
NodeCommon: NodeCommon{
Addr: addr,
Error: err.Error(),
},
}
}
return MemInfo{
NodeCommon: NodeCommon{Addr: addr},
Total: meminfo.Total,
Used: meminfo.Used,
Free: meminfo.Free,
Available: meminfo.Available,
Shared: meminfo.Shared,
Cache: meminfo.Cached,
Buffers: meminfo.Buffers,
SwapSpaceTotal: swapinfo.Total,
SwapSpaceFree: swapinfo.Free,
Limit: getMemoryLimit(meminfo.Total),
}
}
// ProcInfo contains current process's information.
type ProcInfo struct {
NodeCommon
PID int32 `json:"pid,omitempty"`
IsBackground bool `json:"is_background,omitempty"`
CPUPercent float64 `json:"cpu_percent,omitempty"`
ChildrenPIDs []int32 `json:"children_pids,omitempty"`
CmdLine string `json:"cmd_line,omitempty"`
NumConnections int `json:"num_connections,omitempty"`
CreateTime int64 `json:"create_time,omitempty"`
CWD string `json:"cwd,omitempty"`
ExecPath string `json:"exec_path,omitempty"`
GIDs []int32 `json:"gids,omitempty"`
IOCounters process.IOCountersStat `json:"iocounters,omitempty"`
IsRunning bool `json:"is_running,omitempty"`
MemInfo process.MemoryInfoStat `json:"mem_info,omitempty"`
MemMaps []process.MemoryMapsStat `json:"mem_maps,omitempty"`
MemPercent float32 `json:"mem_percent,omitempty"`
Name string `json:"name,omitempty"`
Nice int32 `json:"nice,omitempty"`
NumCtxSwitches process.NumCtxSwitchesStat `json:"num_ctx_switches,omitempty"`
NumFDs int32 `json:"num_fds,omitempty"`
NumThreads int32 `json:"num_threads,omitempty"`
PageFaults process.PageFaultsStat `json:"page_faults,omitempty"`
PPID int32 `json:"ppid,omitempty"`
Status string `json:"status,omitempty"`
TGID int32 `json:"tgid,omitempty"`
Times cpu.TimesStat `json:"times,omitempty"`
UIDs []int32 `json:"uids,omitempty"`
Username string `json:"username,omitempty"`
}
// GetProcInfo returns current MinIO process information.
func GetProcInfo(ctx context.Context, addr string) ProcInfo {
pid := int32(syscall.Getpid())
procInfo := ProcInfo{
NodeCommon: NodeCommon{Addr: addr},
PID: pid,
}
var err error
proc, err := process.NewProcess(pid)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
procInfo.IsBackground, err = proc.BackgroundWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
procInfo.CPUPercent, err = proc.CPUPercentWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
procInfo.ChildrenPIDs = []int32{}
children, _ := proc.ChildrenWithContext(ctx)
for i := range children {
procInfo.ChildrenPIDs = append(procInfo.ChildrenPIDs, children[i].Pid)
}
procInfo.CmdLine, err = proc.CmdlineWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
connections, err := proc.ConnectionsWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
procInfo.NumConnections = len(connections)
procInfo.CreateTime, err = proc.CreateTimeWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
procInfo.CWD, err = proc.CwdWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
procInfo.ExecPath, err = proc.ExeWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
procInfo.GIDs, err = proc.GidsWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
ioCounters, err := proc.IOCountersWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
procInfo.IOCounters = *ioCounters
procInfo.IsRunning, err = proc.IsRunningWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
memInfo, err := proc.MemoryInfoWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
procInfo.MemInfo = *memInfo
memMaps, err := proc.MemoryMapsWithContext(ctx, true)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
procInfo.MemMaps = *memMaps
procInfo.MemPercent, err = proc.MemoryPercentWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
procInfo.Name, err = proc.NameWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
procInfo.Nice, err = proc.NiceWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
numCtxSwitches, err := proc.NumCtxSwitchesWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
procInfo.NumCtxSwitches = *numCtxSwitches
procInfo.NumFDs, err = proc.NumFDsWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
procInfo.NumThreads, err = proc.NumThreadsWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
pageFaults, err := proc.PageFaultsWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
procInfo.PageFaults = *pageFaults
procInfo.PPID, _ = proc.PpidWithContext(ctx)
status, err := proc.StatusWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
procInfo.Status = status[0]
procInfo.TGID, err = proc.Tgid()
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
times, err := proc.TimesWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
procInfo.Times = *times
procInfo.UIDs, err = proc.UidsWithContext(ctx)
if err != nil {
procInfo.Error = err.Error()
return procInfo
}
// In certain environments, it is not possible to get username e.g. minio-operator
// Plus it's not a serious error. So ignore error if any.
procInfo.Username, err = proc.UsernameWithContext(ctx)
if err != nil {
procInfo.Username = "<non-root>"
}
return procInfo
}
// SysInfo - Includes hardware and system information of the MinIO cluster
type SysInfo struct {
CPUInfo []CPUs `json:"cpus,omitempty"`
Partitions []Partitions `json:"partitions,omitempty"`
OSInfo []OSInfo `json:"osinfo,omitempty"`
MemInfo []MemInfo `json:"meminfo,omitempty"`
ProcInfo []ProcInfo `json:"procinfo,omitempty"`
NetInfo []NetInfo `json:"netinfo,omitempty"`
SysErrs []SysErrors `json:"errors,omitempty"`
SysServices []SysServices `json:"services,omitempty"`
SysConfig []SysConfig `json:"config,omitempty"`
ProductInfo []ProductInfo `json:"productinfo,omitempty"`
KubernetesInfo KubernetesInfo `json:"kubernetes"`
}
// KubernetesInfo - Information about the kubernetes platform
type KubernetesInfo struct {
Major string `json:"major,omitempty"`
Minor string `json:"minor,omitempty"`
GitVersion string `json:"gitVersion,omitempty"`
GitCommit string `json:"gitCommit,omitempty"`