/
hardware.go
128 lines (109 loc) · 3.09 KB
/
hardware.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
// Copyright (c) 2020 Sylvain Afchain
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hardware
import (
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host"
"github.com/skydive-project/skydive/graffiti/graph"
"github.com/skydive-project/skydive/probe"
"github.com/skydive-project/skydive/topology/probes"
)
type Probe struct {
Ctx probes.Context
}
// Start the probe
func (p *Probe) Start() error {
p.Ctx.Graph.Lock()
defer p.Ctx.Graph.Unlock()
tr := p.Ctx.Graph.StartMetadataTransaction(p.Ctx.RootNode)
defer tr.Commit()
if instanceID, err := getInstanceID(); err != nil {
p.Ctx.Logger.Error(err)
} else if instanceID != "" {
tr.AddMetadata("InstanceID", instanceID)
}
if isolated, err := getIsolatedCPUs(); err != nil {
p.Ctx.Logger.Error(err)
} else if isolated != nil {
tr.AddMetadata("IsolatedCPU", isolated)
}
cpuInfo, err := cpu.Info()
if err != nil {
return err
}
var cpus CPUInfos
for _, cpu := range cpuInfo {
c := &CPUInfo{
CPU: int64(cpu.CPU),
VendorID: cpu.VendorID,
Family: cpu.Family,
Model: cpu.Model,
Stepping: int64(cpu.Stepping),
PhysicalID: cpu.PhysicalID,
CoreID: cpu.CoreID,
Cores: int64(cpu.Cores),
ModelName: cpu.ModelName,
Mhz: int64(cpu.Mhz),
CacheSize: int64(cpu.CacheSize),
Microcode: cpu.Microcode,
}
cpus = append(cpus, c)
}
tr.AddMetadata("CPU", cpus)
hostInfo, err := host.Info()
if err != nil {
return err
}
if hostInfo.OS != "" {
tr.AddMetadata("OS", hostInfo.OS)
}
if hostInfo.Platform != "" {
tr.AddMetadata("Platform", hostInfo.Platform)
}
if hostInfo.PlatformFamily != "" {
tr.AddMetadata("PlatformFamily", hostInfo.PlatformFamily)
}
if hostInfo.PlatformVersion != "" {
tr.AddMetadata("PlatformVersion", hostInfo.PlatformVersion)
}
if hostInfo.KernelVersion != "" {
tr.AddMetadata("KernelVersion", hostInfo.KernelVersion)
}
if hostInfo.VirtualizationSystem != "" {
tr.AddMetadata("VirtualizationSystem", hostInfo.VirtualizationSystem)
}
if hostInfo.VirtualizationRole != "" {
tr.AddMetadata("VirtualizationRole", hostInfo.VirtualizationRole)
}
if cmd, err := getKernelCmd(); err != nil {
p.Ctx.Logger.Error(err)
} else if cmd != nil {
tr.AddMetadata("KernelCmdLine", cmd)
}
return nil
}
// Stop the probe
func (u *Probe) Stop() {
}
// NewProbe returns a BESS topology probe
func NewProbe(ctx probes.Context, bundle *probe.Bundle) (probe.Handler, error) {
p := &Probe{
Ctx: ctx,
}
return p, nil
}
// Register registers graph metadata decoders
func Register() {
graph.NodeMetadataDecoders["CPU"] = MetadataDecoder
}