Skip to content

Commit ff3427e

Browse files
committed
Initial
0 parents  commit ff3427e

File tree

8 files changed

+639
-0
lines changed

8 files changed

+639
-0
lines changed

go.mod

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module github.com/iglov/netbox-agent
2+
3+
go 1.23.0
4+
5+
require (
6+
github.com/joho/godotenv v1.3.0
7+
github.com/netbox-community/go-netbox/v4 v4.0.3-0
8+
github.com/u-root/u-root v0.14.0
9+
github.com/yumaojun03/dmidecode v0.1.4
10+
)
11+
12+
require (
13+
github.com/digitalocean/go-smbios v0.0.0-20180907143718-390a4f403a8e // indirect
14+
github.com/google/go-cmp v0.6.0 // indirect
15+
github.com/vtolstov/go-ioctl v0.0.0-20151206205506-6be9cced4810 // indirect
16+
golang.org/x/sync v0.7.0 // indirect
17+
golang.org/x/sys v0.22.0 // indirect
18+
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
19+
)

go.sum

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s=
2+
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
3+
github.com/creack/pty v1.1.21 h1:1/QdRyBaHHJP61QkWMXlOIBfsgdDeeKfK8SYVUWJKf0=
4+
github.com/creack/pty v1.1.21/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
5+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
7+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8+
github.com/digitalocean/go-smbios v0.0.0-20180907143718-390a4f403a8e h1:vUmf0yezR0y7jJ5pceLHthLaYf4bA5T14B6q39S4q2Q=
9+
github.com/digitalocean/go-smbios v0.0.0-20180907143718-390a4f403a8e/go.mod h1:YTIHhz/QFSYnu/EhlF2SpU2Uk+32abacUYA5ZPljz1A=
10+
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
11+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
12+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
13+
github.com/hugelgupf/go-shlex v0.0.0-20200702092117-c80c9d0918fa h1:s3KPo0nThtvjEamF/aElD4k5jSsBHew3/sgNTnth+2M=
14+
github.com/hugelgupf/go-shlex v0.0.0-20200702092117-c80c9d0918fa/go.mod h1:I1uW6ymzwsy5TlQgD1bFAghdMgBYqH1qtCeHoZgHMqs=
15+
github.com/hugelgupf/vmtest v0.0.0-20240216064925-0561770280a1 h1:jWoR2Yqg8tzM0v6LAiP7i1bikZJu3gxpgvu3g1Lw+a0=
16+
github.com/hugelgupf/vmtest v0.0.0-20240216064925-0561770280a1/go.mod h1:B63hDJMhTupLWCHwopAyEo7wRFowx9kOc8m8j1sfOqE=
17+
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
18+
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
19+
github.com/netbox-community/go-netbox/v4 v4.0.3-0 h1:Q6NucmGe9ZmU3mjziuqQlKqHZmzC3FYbZn9r+Y507hs=
20+
github.com/netbox-community/go-netbox/v4 v4.0.3-0/go.mod h1:eux5UXpY7T1BADzdFI1V7o+iIGxSfPLUuZvcjVWca/w=
21+
github.com/pierrec/lz4/v4 v4.1.14 h1:+fL8AQEZtz/ijeNnpduH0bROTu0O3NZAlPjQxGn8LwE=
22+
github.com/pierrec/lz4/v4 v4.1.14/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
23+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
24+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
25+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
26+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
27+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
28+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
29+
github.com/u-root/gobusybox/src v0.0.0-20240225013946-a274a8d5d83a h1:eg5FkNoQp76ZsswyGZ+TjYqA/rhKefxK8BW7XOlQsxo=
30+
github.com/u-root/gobusybox/src v0.0.0-20240225013946-a274a8d5d83a/go.mod h1:e/8TmrdreH0sZOw2DFKBaUV7bvDWRq6SeM9PzkuVM68=
31+
github.com/u-root/mkuimage v0.0.0-20240225063926-11a3bcc79c2a h1:PsI71z55OoumrXP01sYr+cV3Ab4pL7Y2QW/ftMKG7CY=
32+
github.com/u-root/mkuimage v0.0.0-20240225063926-11a3bcc79c2a/go.mod h1:Yqr8aXRStz71Z1JVz2bUut8Xt9wmBijQIVOSn3eYEIw=
33+
github.com/u-root/u-root v0.14.0 h1:Ka4T10EEML7dQ5XDvO9c3MBN8z4nuSnGjcd1jmU2ivg=
34+
github.com/u-root/u-root v0.14.0/go.mod h1:hAyZorapJe4qzbLWlAkmSVCJGbfoU9Pu4jpJ1WMluqE=
35+
github.com/u-root/uio v0.0.0-20240209044354-b3d14b93376a h1:BH1SOPEvehD2kVrndDnGJiUF0TrBpNs+iyYocu6h0og=
36+
github.com/u-root/uio v0.0.0-20240209044354-b3d14b93376a/go.mod h1:P3a5rG4X7tI17Nn3aOIAYr5HbIMukwXG0urG0WuL8OA=
37+
github.com/vtolstov/go-ioctl v0.0.0-20151206205506-6be9cced4810 h1:X6ps8XHfpQjw8dUStzlMi2ybiKQ2Fmdw7UM+TinwvyM=
38+
github.com/vtolstov/go-ioctl v0.0.0-20151206205506-6be9cced4810/go.mod h1:dF0BBJ2YrV1+2eAIyEI+KeSidgA6HqoIP1u5XTlMq/o=
39+
github.com/yumaojun03/dmidecode v0.1.4 h1:ZmHBHs5sE5z1clgpry9tmuWeo2FBFi018MiSAWr+5n8=
40+
github.com/yumaojun03/dmidecode v0.1.4/go.mod h1:34bbsMNMNjDbijDpRuqd+2ZapDKxvhO+FlgGgOgS6G8=
41+
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 h1:LfspQV/FYTatPTr/3HzIcmiUFH7PGP+OQ6mgDYo3yuQ=
42+
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
43+
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
44+
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
45+
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
46+
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
47+
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
48+
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
49+
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
50+
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
51+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
52+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
53+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
54+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
55+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
56+
mvdan.cc/sh/v3 v3.7.0 h1:lSTjdP/1xsddtaKfGg7Myu7DnlHItd3/M2tomOcNNBg=
57+
mvdan.cc/sh/v3 v3.7.0/go.mod h1:K2gwkaesF/D7av7Kxl0HbF5kGOd2ArupNTX3X44+8l8=

lib/dmidecode/chassis.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package dmidecode
2+
3+
import (
4+
"strings"
5+
6+
"github.com/yumaojun03/dmidecode"
7+
)
8+
9+
// ChassisInfo holds the details of the chassis.
10+
type ChassisInfo struct {
11+
Version string `json:"version"`
12+
SerialNumber string `json:"serial_number"`
13+
Height string `json:"height"`
14+
Manufacturer string `json:"manufacturer"`
15+
}
16+
17+
// GetChassisInfo fetches and returns the chassis information as a list.
18+
func GetChassisInfo() ([]ChassisInfo, error) {
19+
dmi, err := dmidecode.New()
20+
if err != nil {
21+
return nil, err
22+
}
23+
24+
// Fetch chassis information
25+
chassisInfo, err := dmi.Chassis()
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
// Convert chassis information to ChassisInfo structs
31+
var chassisList []ChassisInfo
32+
for _, chassis := range chassisInfo {
33+
// Extract only the first part of Manufacturer (before space)
34+
manufacturerParts := strings.Split(chassis.Manufacturer, " ")
35+
manufacturer := manufacturerParts[0]
36+
37+
chassisList = append(chassisList, ChassisInfo{
38+
Version: chassis.Version,
39+
SerialNumber: chassis.SerialNumber,
40+
Height: chassis.Height.String(),
41+
Manufacturer: manufacturer,
42+
})
43+
}
44+
45+
return chassisList, nil
46+
}

lib/dmidecode/cpu.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package dmidecode
2+
3+
import (
4+
"github.com/yumaojun03/dmidecode"
5+
)
6+
7+
// CPUInfo holds the details of a CPU.
8+
type CPUInfo struct {
9+
SocketDesignation string `json:"socket_designation"`
10+
Version string `json:"version"`
11+
CoreCount uint8 `json:"core_count"`
12+
ThreadCount uint8 `json:"thread_count"`
13+
}
14+
15+
// GetCPUInfo fetches and returns the CPU information as a list.
16+
func GetCPUInfo() ([]CPUInfo, error) {
17+
dmi, err := dmidecode.New()
18+
if err != nil {
19+
return nil, err
20+
}
21+
22+
// Fetch processor information
23+
cpuInfo, err := dmi.Processor()
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
// Convert processor information to CPUInfo structs
29+
var cpus []CPUInfo
30+
for _, cpu := range cpuInfo {
31+
cpus = append(cpus, CPUInfo{
32+
SocketDesignation: cpu.SocketDesignation,
33+
Version: cpu.Version,
34+
CoreCount: cpu.CoreCount,
35+
ThreadCount: cpu.ThreadCount,
36+
})
37+
}
38+
39+
return cpus, nil
40+
}

lib/dmidecode/memory.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package dmidecode
2+
3+
import (
4+
"strings"
5+
6+
"github.com/yumaojun03/dmidecode"
7+
)
8+
9+
// MemoryDeviceInfo holds the details of a memory device.
10+
type MemoryDeviceInfo struct {
11+
Size uint16 `json:"size"`
12+
FormFactor string `json:"form_factor"`
13+
Speed uint16 `json:"speed"`
14+
Type string `json:"type"`
15+
Manufacturer string `json:"manufacturer"`
16+
SerialNumber string `json:"serial_number"`
17+
AssetTag string `json:"asset_tag"`
18+
PartNumber string `json:"part_number"`
19+
DeviceLocator string `json:"device_locator"`
20+
}
21+
22+
// GetMemoryDevices fetches and returns the memory device information as a list.
23+
func GetMemoryDevices() ([]MemoryDeviceInfo, error) {
24+
dmi, err := dmidecode.New()
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
// Fetch memory devices information
30+
memDevices, err := dmi.MemoryDevice()
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
// Convert memory device information to MemoryDeviceInfo structs
36+
var memoryDevices []MemoryDeviceInfo
37+
for _, device := range memDevices {
38+
// Skip empty or unknown memory slots (where size is 0 or unknown)
39+
if device.Size == 0 || device.Size == 0xFFFF {
40+
continue
41+
}
42+
43+
// Trim spaces from part number
44+
partNumber := strings.TrimSpace(device.PartNumber)
45+
46+
// Add the device to the list
47+
memoryDevices = append(memoryDevices, MemoryDeviceInfo{
48+
Size: device.Size,
49+
FormFactor: device.FormFactor.String(),
50+
Speed: device.Speed,
51+
Type: device.Type.String(),
52+
Manufacturer: device.Manufacturer,
53+
SerialNumber: device.SerialNumber,
54+
AssetTag: device.AssetTag,
55+
PartNumber: partNumber,
56+
DeviceLocator: device.DeviceLocator,
57+
})
58+
}
59+
60+
return memoryDevices, nil
61+
}

lib/dmidecode/system.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package dmidecode
2+
3+
import (
4+
"strings"
5+
6+
"github.com/yumaojun03/dmidecode"
7+
)
8+
9+
// SystemInfo holds the details of the system.
10+
type SystemInfo struct {
11+
Manufacturer string `json:"manufacturer"`
12+
ProductName string `json:"product_name"`
13+
Version string `json:"version"`
14+
SerialNumber string `json:"serial_number"`
15+
LocationInChassis string `json:"location_in_chassis"`
16+
}
17+
18+
// GetSystemInfo fetches and returns the system and baseboard information.
19+
func GetSystemInfo() ([]SystemInfo, error) {
20+
dmi, err := dmidecode.New()
21+
if err != nil {
22+
return nil, err
23+
}
24+
25+
// Fetch system information
26+
systemInfo, err := dmi.System()
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
// Fetch baseboard information (for LocationInChassis)
32+
baseboardInfo, err := dmi.BaseBoard()
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
// Convert system and baseboard information to SystemInfo structs
38+
var systemList []SystemInfo
39+
for i, sys := range systemInfo {
40+
// Extract only the first part of Manufacturer (before space)
41+
manufacturerParts := strings.Split(sys.Manufacturer, " ")
42+
manufacturer := manufacturerParts[0]
43+
44+
// Get the location_in_chassis from the baseboard information (we assume the baseboard corresponds to the system index)
45+
locationInChassis := ""
46+
if i < len(baseboardInfo) {
47+
locationInChassis = baseboardInfo[i].LocationInChassis
48+
}
49+
50+
systemList = append(systemList, SystemInfo{
51+
Manufacturer: manufacturer,
52+
ProductName: sys.ProductName,
53+
Version: sys.Version,
54+
SerialNumber: sys.SerialNumber,
55+
LocationInChassis: locationInChassis,
56+
})
57+
}
58+
59+
return systemList, nil
60+
}

0 commit comments

Comments
 (0)