-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
329 lines (265 loc) · 10.7 KB
/
main.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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"github.com/iglov/netbox-agent/lib/dmidecode"
"github.com/iglov/netbox-agent/lib/ipmi"
"github.com/iglov/netbox-agent/lib/storage"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"os"
"strings"
"github.com/netbox-community/go-netbox/v4"
)
var (
version = flag.Bool("v", false, "Print current version and exit.")
logLevel = flag.String("loglevel", "info", "Set log level: DEBUG, INFO, WARN, ERROR")
)
// Version contains main version of build. Get from compiler variables
var Version string
// Initiate log
var log = logrus.New()
// FullSystemInfo is the common struct for all of our hardware components
type FullSystemInfo struct {
Memory []dmidecode.MemoryDeviceInfo `json:"memory"`
CPU []dmidecode.CPUInfo `json:"cpu"`
IPMI ipmi.BmcInfo `json:"ipmi"`
Chassis []dmidecode.ChassisInfo `json:"chassis"`
System []dmidecode.SystemInfo `json:"system"`
Storage []storage.DiskInfo `json:"storage"`
}
func main() {
flag.Parse()
if *version {
fmt.Println(Version)
os.Exit(0)
}
// Set log output to stdout
log.Out = os.Stdout
// Parse the log level and set it
level, err := logrus.ParseLevel(strings.ToLower(*logLevel))
if err != nil {
log.Fatalf("Invalid log level: %s", *logLevel)
}
log.SetLevel(level)
// Fetch memory device information
memDevices, err := dmidecode.GetMemoryDevices()
if err != nil {
log.Fatalf("Error fetching memory devices: %s", err)
}
// Fetch CPU information
cpuInfo, err := dmidecode.GetCPUInfo()
if err != nil {
log.Fatalf("Error fetching CPU information: %s", err)
}
// Fetch IPMI information
bmcInfo := ipmi.GetBmcInfo()
// Fetch chassis information
chassisInfo, err := dmidecode.GetChassisInfo()
if err != nil {
log.Fatalf("Error fetching chassis information: %s", err)
}
// Fetch system information
systemInfo, err := dmidecode.GetSystemInfo()
if err != nil {
log.Fatalf("Error fetching system information: %s", err)
}
// Fetch storage information
storageInfo, err := storage.GetStorageInfo()
if err != nil {
log.Fatalf("Error fetching storage information: %s", err)
}
// Combine all the data into SystemInfo struct
fullSystemInfo := FullSystemInfo{
Memory: memDevices,
CPU: cpuInfo,
Chassis: chassisInfo,
IPMI: bmcInfo,
System: systemInfo,
Storage: storageInfo,
}
// Convert the SystemInfo struct to JSON
finalJSON, err := json.MarshalIndent(fullSystemInfo, "", " ")
if err != nil {
log.Fatalf("Error marshalling final JSON: %s", err)
}
log.Debug(string(finalJSON))
// if strings.Contains(*logLevel, "debug") {
// os.Exit(0)
// }
err = godotenv.Load()
if err != nil {
log.Warn("Error loading .env , using local variables")
}
ctx := context.Background()
apiURL := os.Getenv("API_URL")
apiTOKEN := os.Getenv("API_TOKEN")
c := netbox.NewAPIClientFor(apiURL, apiTOKEN)
roleRequest := netbox.NewDeviceRoleRequestWithDefaults()
roleRequest.SetName("default device role")
roleRequest.SetSlug("default-device-role")
roleRequest.SetDescription("It's just a default role after server creation by API, it should be changed after server creation.")
roleRes, httpRes, err := c.DcimAPI.DcimDeviceRolesCreate(ctx).DeviceRoleRequest(*roleRequest).Execute()
if err != nil {
log.Errorf("Error creating role: %s", err)
}
log.Debugf("Response: %+v", roleRes)
log.Debugf("HTTP Response: %+v", httpRes)
hostname, err := os.Hostname()
if err != nil {
log.Errorf("Error get hostname: %s", err)
}
// Parse and set all variables
site := strings.Split(hostname, ".")[1]
productName := fullSystemInfo.System[0].ProductName
productVendor := fullSystemInfo.System[0].Manufacturer
productSerial := fullSystemInfo.System[0].SerialNumber
chassisVersion := fullSystemInfo.Chassis[0].Version
chassisSerial := fullSystemInfo.Chassis[0].SerialNumber
chassisVendor := fullSystemInfo.Chassis[0].Manufacturer
otherInfo := "Serial: " + productSerial + " | Chassis name: " + chassisVersion + " | Chassis serial: " + chassisSerial + " | Chassis vendor: " + chassisVendor
productVendorLower := strings.ToLower(productVendor)
productNameLower := strings.ToLower(productName)
productNameHyphenated := strings.ReplaceAll(productNameLower, " ", "-")
productVendorName := productVendorLower + "-" + productNameHyphenated
chassisVendorLower := strings.ToLower(chassisVendor)
chassisVersionLower := strings.ToLower(chassisVersion)
chassisVersionHyphenated := strings.ReplaceAll(chassisVersionLower, " ", "-")
chassisVendorName := chassisVendorLower + "-" + chassisVersionHyphenated
// Start creating objects
siteRequest := netbox.NewWritableSiteRequestWithDefaults()
siteRequest.SetName(site)
siteRequest.SetSlug(site)
siteRequest.SetDescription("It's just a default Site after server creation by API, it should be changed after server creation.")
siteRes, httpRes, err := c.DcimAPI.DcimSitesCreate(ctx).WritableSiteRequest(*siteRequest).Execute()
if err != nil {
log.Errorf("Error creating site: %s", err)
}
log.Debugf("Response: %+v", siteRes)
log.Debugf("HTTP Response: %+v", httpRes)
// Add blade chassis if exists
if chassisSerial != productSerial {
device := netbox.NewWritableDeviceWithConfigContextRequestWithDefaults()
device.SetSite(netbox.SiteRequest{Name: site, Slug: site})
device.SetRole(netbox.DeviceRoleRequest{Name: "default device role", Slug: "default-device-role"})
device.SetComments(otherInfo)
device.SetDeviceType(netbox.DeviceTypeRequest{Model: chassisVersion, Slug: chassisVendorName, Manufacturer: netbox.ManufacturerRequest{Name: chassisVendor, Slug: chassisVendorLower}})
device.SetName(chassisSerial)
device.SetSerial(chassisSerial)
deviceRes, httpRes, err := c.DcimAPI.DcimDevicesCreate(ctx).WritableDeviceWithConfigContextRequest(*device).Execute()
if err != nil {
log.Errorf("Error creating device: %v", err)
}
log.Debugf("Response: %+v", deviceRes)
log.Debugf("HTTP Response: %+v", httpRes)
}
device := netbox.NewWritableDeviceWithConfigContextRequestWithDefaults()
device.SetSite(netbox.SiteRequest{Name: site, Slug: site})
device.SetRole(netbox.DeviceRoleRequest{Name: "default device role", Slug: "default-device-role"})
device.SetComments(otherInfo)
device.SetDeviceType(netbox.DeviceTypeRequest{Model: productName, Slug: productVendorName, Manufacturer: netbox.ManufacturerRequest{Name: productVendor, Slug: productVendorLower}})
device.SetName(hostname)
device.SetSerial(productSerial)
device.SetLocalContextData(&fullSystemInfo)
deviceRes, httpRes, err := c.DcimAPI.DcimDevicesCreate(ctx).WritableDeviceWithConfigContextRequest(*device).Execute()
if err != nil {
log.Errorf("Error creating device: %v", err)
}
log.Debugf("Response: %+v", deviceRes)
log.Debugf("HTTP Response: %+v", httpRes)
for i := range fullSystemInfo.CPU {
man := netbox.NewManufacturerRequestWithDefaults()
man.SetName(fullSystemInfo.CPU[i].Manufacturer)
man.SetSlug(strings.ToLower(fullSystemInfo.CPU[i].Manufacturer))
manRes, httpRes, err := c.DcimAPI.DcimManufacturersCreate(ctx).ManufacturerRequest(*man).Execute()
if err != nil {
log.Errorf("Error creating device: %v", err)
}
log.Debugf("Response: %+v", manRes)
log.Debugf("HTTP Response: %+v", httpRes)
inv := netbox.NewInventoryItemRequestWithDefaults()
inv.SetName("CPU")
inv.SetManufacturer(*man)
inv.SetPartId(fullSystemInfo.CPU[i].Version)
inv.SetCustomFields(map[string]interface{}{
"cpu_cores": fullSystemInfo.CPU[i].CoreCount,
"cpu_threads": fullSystemInfo.CPU[i].ThreadCount,
})
inv.SetDevice(netbox.DeviceRequest{Name: *netbox.NewNullableString(&hostname)})
invRes, httpRes, err := c.DcimAPI.DcimInventoryItemsCreate(ctx).InventoryItemRequest(*inv).Execute()
if err != nil {
log.Errorf("Error creating device: %v", err)
}
log.Debugf("Response: %+v", invRes)
log.Debugf("HTTP Response: %+v", httpRes)
}
for i := range fullSystemInfo.Memory {
man := netbox.NewManufacturerRequestWithDefaults()
man.SetName(fullSystemInfo.Memory[i].Manufacturer)
man.SetSlug(strings.ToLower(fullSystemInfo.Memory[i].Manufacturer))
manRes, httpRes, err := c.DcimAPI.DcimManufacturersCreate(ctx).ManufacturerRequest(*man).Execute()
if err != nil {
log.Errorf("Error creating device: %v", err)
}
log.Debugf("Response: %+v", manRes)
log.Debugf("HTTP Response: %+v", httpRes)
inv := netbox.NewInventoryItemRequestWithDefaults()
inv.SetName("MEMORY")
inv.SetManufacturer(*man)
inv.SetPartId(fullSystemInfo.Memory[i].PartNumber)
inv.SetSerial(fullSystemInfo.Memory[i].SerialNumber)
inv.SetCustomFields(map[string]interface{}{
"memory_size": fullSystemInfo.Memory[i].Size,
"memory_slot": fullSystemInfo.Memory[i].DeviceLocator,
"memory_speed": fullSystemInfo.Memory[i].Speed,
"memory_type": fullSystemInfo.Memory[i].Type,
})
inv.SetDevice(netbox.DeviceRequest{Name: *netbox.NewNullableString(&hostname)})
invRes, httpRes, err := c.DcimAPI.DcimInventoryItemsCreate(ctx).InventoryItemRequest(*inv).Execute()
if err != nil {
log.Errorf("Error creating device: %v", err)
}
log.Debugf("Response: %+v", invRes)
log.Debugf("HTTP Response: %+v", httpRes)
}
for i := range fullSystemInfo.Storage {
man := netbox.NewManufacturerRequestWithDefaults()
man.SetName(fullSystemInfo.Storage[i].Manufacturer)
// man.SetName("Intel")
man.SetSlug(strings.ToLower(fullSystemInfo.Storage[i].Manufacturer))
manRes, httpRes, err := c.DcimAPI.DcimManufacturersCreate(ctx).ManufacturerRequest(*man).Execute()
if err != nil {
log.Errorf("Error creating device: %v", err)
}
log.Debugf("Response: %+v", manRes)
log.Debugf("HTTP Response: %+v", httpRes)
inv := netbox.NewInventoryItemRequestWithDefaults()
inv.SetName("DISK")
inv.SetManufacturer(*man)
inv.SetPartId(fullSystemInfo.Storage[i].Model)
inv.SetSerial(fullSystemInfo.Storage[i].SerialNumber)
inv.SetCustomFields(map[string]interface{}{
"disk_size": fullSystemInfo.Storage[i].Size,
"disk_slot": fullSystemInfo.Storage[i].Slot,
})
inv.SetDevice(netbox.DeviceRequest{Name: *netbox.NewNullableString(&hostname)})
invRes, httpRes, err := c.DcimAPI.DcimInventoryItemsCreate(ctx).InventoryItemRequest(*inv).Execute()
if err != nil {
log.Errorf("Error creating device: %v", err)
}
log.Debugf("Response: %+v", invRes)
log.Debugf("HTTP Response: %+v", httpRes)
}
netInt := netbox.NewWritableInterfaceRequestWithDefaults()
netInt.SetName("IMPI")
netInt.SetDevice(netbox.DeviceRequest{Name: *netbox.NewNullableString(&hostname)})
netInt.SetType("1000base-tx")
netIntRes, httpRes, err := c.DcimAPI.DcimInterfacesCreate(ctx).WritableInterfaceRequest(*netInt).Execute()
if err != nil {
log.Errorf("Error creating device: %v", err)
}
log.Debugf("Response: %+v", netIntRes)
log.Debugf("HTTP Response: %+v", httpRes)
}