-
Notifications
You must be signed in to change notification settings - Fork 181
/
host.go
147 lines (137 loc) · 3.79 KB
/
host.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
//
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
//
package ghw
import (
"fmt"
"github.com/jaypipes/ghw/pkg/context"
"github.com/jaypipes/ghw/pkg/accelerator"
"github.com/jaypipes/ghw/pkg/baseboard"
"github.com/jaypipes/ghw/pkg/bios"
"github.com/jaypipes/ghw/pkg/block"
"github.com/jaypipes/ghw/pkg/chassis"
"github.com/jaypipes/ghw/pkg/cpu"
"github.com/jaypipes/ghw/pkg/gpu"
"github.com/jaypipes/ghw/pkg/marshal"
"github.com/jaypipes/ghw/pkg/memory"
"github.com/jaypipes/ghw/pkg/net"
"github.com/jaypipes/ghw/pkg/pci"
"github.com/jaypipes/ghw/pkg/product"
"github.com/jaypipes/ghw/pkg/topology"
)
// HostInfo is a wrapper struct containing information about the host system's
// memory, block storage, CPU, etc
type HostInfo struct {
ctx *context.Context
Memory *memory.Info `json:"memory"`
Block *block.Info `json:"block"`
CPU *cpu.Info `json:"cpu"`
Topology *topology.Info `json:"topology"`
Network *net.Info `json:"network"`
GPU *gpu.Info `json:"gpu"`
Accelerator *accelerator.Info `json:"accelerator"`
Chassis *chassis.Info `json:"chassis"`
BIOS *bios.Info `json:"bios"`
Baseboard *baseboard.Info `json:"baseboard"`
Product *product.Info `json:"product"`
PCI *pci.Info `json:"pci"`
}
// Host returns a pointer to a HostInfo struct that contains fields with
// information about the host system's CPU, memory, network devices, etc
func Host(opts ...*WithOption) (*HostInfo, error) {
ctx := context.New(opts...)
memInfo, err := memory.New(opts...)
if err != nil {
return nil, err
}
blockInfo, err := block.New(opts...)
if err != nil {
return nil, err
}
cpuInfo, err := cpu.New(opts...)
if err != nil {
return nil, err
}
topologyInfo, err := topology.New(opts...)
if err != nil {
return nil, err
}
netInfo, err := net.New(opts...)
if err != nil {
return nil, err
}
gpuInfo, err := gpu.New(opts...)
if err != nil {
return nil, err
}
acceleratorInfo, err := accelerator.New(opts...)
if err != nil {
return nil, err
}
chassisInfo, err := chassis.New(opts...)
if err != nil {
return nil, err
}
biosInfo, err := bios.New(opts...)
if err != nil {
return nil, err
}
baseboardInfo, err := baseboard.New(opts...)
if err != nil {
return nil, err
}
productInfo, err := product.New(opts...)
if err != nil {
return nil, err
}
pciInfo, err := pci.New(opts...)
if err != nil {
return nil, err
}
return &HostInfo{
ctx: ctx,
CPU: cpuInfo,
Memory: memInfo,
Block: blockInfo,
Topology: topologyInfo,
Network: netInfo,
GPU: gpuInfo,
Accelerator: acceleratorInfo,
Chassis: chassisInfo,
BIOS: biosInfo,
Baseboard: baseboardInfo,
Product: productInfo,
PCI: pciInfo,
}, nil
}
// String returns a newline-separated output of the HostInfo's component
// structs' String-ified output
func (info *HostInfo) String() string {
return fmt.Sprintf(
"%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
info.Block.String(),
info.CPU.String(),
info.GPU.String(),
info.Accelerator.String(),
info.Memory.String(),
info.Network.String(),
info.Topology.String(),
info.Chassis.String(),
info.BIOS.String(),
info.Baseboard.String(),
info.Product.String(),
info.PCI.String(),
)
}
// YAMLString returns a string with the host information formatted as YAML
// under a top-level "host:" key
func (i *HostInfo) YAMLString() string {
return marshal.SafeYAML(i.ctx, i)
}
// JSONString returns a string with the host information formatted as JSON
// under a top-level "host:" key
func (i *HostInfo) JSONString(indent bool) string {
return marshal.SafeJSON(i.ctx, i, indent)
}