From c041acafeebc6174839dec6bc1c56b1843164cba Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Wed, 8 May 2024 13:29:07 -0600 Subject: [PATCH] More minor changes --- internal/api/smd/smd.go | 4 +- internal/collect.go | 123 +++++++++++++++++----------------------- 2 files changed, 55 insertions(+), 72 deletions(-) diff --git a/internal/api/smd/smd.go b/internal/api/smd/smd.go index dabf066..17a08c1 100644 --- a/internal/api/smd/smd.go +++ b/internal/api/smd/smd.go @@ -117,7 +117,7 @@ func (c *Client) AddRedfishEndpoint(data []byte, headers map[string]string) erro func (c *Client) UpdateRedfishEndpoint(xname string, data []byte, headers map[string]string) error { if data == nil { - return fmt.Errorf("could not add redfish endpoint: no data found") + return fmt.Errorf("failed to add redfish endpoint: no data found") } // Update redfish endpoint via PUT `/hsm/v2/Inventory/RedfishEndpoints` endpoint url := makeEndpointUrl("/Inventory/RedfishEndpoints/" + xname) @@ -126,7 +126,7 @@ func (c *Client) UpdateRedfishEndpoint(xname string, data []byte, headers map[st if res != nil { statusOk := res.StatusCode >= 200 && res.StatusCode < 300 if !statusOk { - return fmt.Errorf("could not update redfish endpoint") + return fmt.Errorf("failed to update redfish endpoint (returned %s)", res.Status) } } return err diff --git a/internal/collect.go b/internal/collect.go index 7dd32cb..b1ce902 100644 --- a/internal/collect.go +++ b/internal/collect.go @@ -2,8 +2,10 @@ package magellan import ( "context" + "crypto/tls" "encoding/json" "fmt" + "net" "net/http" "os" "path" @@ -62,7 +64,7 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err outputPath := path.Clean(q.OutputPath) outputPath, err := util.MakeOutputDirectory(outputPath) if err != nil { - l.Log.Errorf("could not make output directory: %v", err) + l.Log.Errorf("failed to make output directory: %v", err) } // collect bmc information asynchronously @@ -97,14 +99,9 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err } offset += 1 - // bmclibClient, err := NewClient(l, q) - // if err != nil { - // l.Log.Errorf("could not make client: %v", err) - // } - gofishClient, err := connectGofish(q) if err != nil { - l.Log.Errorf("could not connect to bmc (%v:%v): %v", q.Host, q.Port, err) + l.Log.Errorf("failed to connect to bmc (%v:%v): %v", q.Host, q.Port, err) } // data to be sent to smd @@ -122,21 +119,11 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err // unmarshal json to send in correct format var rm map[string]json.RawMessage - // inventories - // if bmclibClient != nil { - // inventory, err := CollectInventory(bmclibClient, q) - // if err != nil { - // l.Log.Errorf("could not query inventory (%v:%v): %v", q.Host, q.Port, err) - // } - // json.Unmarshal(inventory, &rm) - // data["Inventory"] = rm["Inventory"] - // } - // chassis if gofishClient != nil { chassis, err := CollectChassis(gofishClient, q) if err != nil { - l.Log.Errorf("could not query chassis: %v", err) + l.Log.Errorf("failed to query chassis: %v", err) continue } json.Unmarshal(chassis, &rm) @@ -145,7 +132,7 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err // systems systems, err := CollectSystems(gofishClient, q) if err != nil { - l.Log.Errorf("could not query systems: %v", err) + l.Log.Errorf("failed to query systems: %v", err) } json.Unmarshal(systems, &rm) data["Systems"] = rm["Systems"] @@ -168,7 +155,7 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err body, err := json.MarshalIndent(data, "", " ") if err != nil { - l.Log.Errorf("could not marshal JSON: %v", err) + l.Log.Errorf("failed to marshal JSON: %v", err) } if q.Verbose { @@ -179,7 +166,7 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err if outputPath != "" { err = os.WriteFile(path.Clean(outputPath+"/"+q.Host+".json"), body, os.ModePerm) if err != nil { - l.Log.Errorf("could not write data to file: %v", err) + l.Log.Errorf("failed to write data to file: %v", err) } } @@ -232,15 +219,13 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err } func CollectMetadata(client *bmclib.Client, q *QueryParams) ([]byte, error) { - // client, err := NewClient(l, q) - // open BMC session and update driver registry ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*time.Duration(q.Timeout)) client.Registry.FilterForCompatible(ctx) err := client.Open(ctx) if err != nil { ctxCancel() - return nil, fmt.Errorf("could not connect to bmc: %v", err) + return nil, fmt.Errorf("failed to connect to bmc: %v", err) } defer client.Close(ctx) @@ -248,14 +233,14 @@ func CollectMetadata(client *bmclib.Client, q *QueryParams) ([]byte, error) { metadata := client.GetMetadata() if err != nil { ctxCancel() - return nil, fmt.Errorf("could not get metadata: %v", err) + return nil, fmt.Errorf("failed to get metadata: %v", err) } // retrieve inventory data b, err := json.MarshalIndent(metadata, "", " ") if err != nil { ctxCancel() - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } ctxCancel() @@ -269,13 +254,13 @@ func CollectInventory(client *bmclib.Client, q *QueryParams) ([]byte, error) { err := client.PreferProvider(q.Preferred).Open(ctx) if err != nil { ctxCancel() - return nil, fmt.Errorf("could not open client: %v", err) + return nil, fmt.Errorf("failed to open client: %v", err) } inventory, err := client.Inventory(ctx) if err != nil { ctxCancel() - return nil, fmt.Errorf("could not get inventory: %v", err) + return nil, fmt.Errorf("failed to get inventory: %v", err) } // retrieve inventory data @@ -283,7 +268,7 @@ func CollectInventory(client *bmclib.Client, q *QueryParams) ([]byte, error) { b, err := json.MarshalIndent(data, "", " ") if err != nil { ctxCancel() - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } ctxCancel() @@ -296,13 +281,13 @@ func CollectPowerState(client *bmclib.Client, q *QueryParams) ([]byte, error) { err := client.PreferProvider(q.Preferred).Open(ctx) if err != nil { ctxCancel() - return nil, fmt.Errorf("could not open client: %v", err) + return nil, fmt.Errorf("failed to open client: %v", err) } powerState, err := client.GetPowerState(ctx) if err != nil { ctxCancel() - return nil, fmt.Errorf("could not get inventory: %v", err) + return nil, fmt.Errorf("failed to get inventory: %v", err) } // retrieve inventory data @@ -310,7 +295,7 @@ func CollectPowerState(client *bmclib.Client, q *QueryParams) ([]byte, error) { b, err := json.MarshalIndent(data, "", " ") if err != nil { ctxCancel() - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } ctxCancel() @@ -325,7 +310,7 @@ func CollectUsers(client *bmclib.Client, q *QueryParams) ([]byte, error) { err := client.Open(ctx) if err != nil { ctxCancel() - return nil, fmt.Errorf("could not connect to bmc: %v", err) + return nil, fmt.Errorf("failed to connect to bmc: %v", err) } defer client.Close(ctx) @@ -333,7 +318,7 @@ func CollectUsers(client *bmclib.Client, q *QueryParams) ([]byte, error) { users, err := client.ReadUsers(ctx) if err != nil { ctxCancel() - return nil, fmt.Errorf("could not get users: %v", err) + return nil, fmt.Errorf("failed to get users: %v", err) } // retrieve inventory data @@ -341,7 +326,7 @@ func CollectUsers(client *bmclib.Client, q *QueryParams) ([]byte, error) { b, err := json.MarshalIndent(data, "", " ") if err != nil { ctxCancel() - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } ctxCancel() @@ -351,7 +336,7 @@ func CollectUsers(client *bmclib.Client, q *QueryParams) ([]byte, error) { func CollectBios(client *bmclib.Client, q *QueryParams) ([]byte, error) { // client, err := NewClient(l, q) // if err != nil { - // return nil, fmt.Errorf("could not make query: %v", err) + // return nil, fmt.Errorf("failed to make query: %v", err) // } b, err := makeRequest(client, client.GetBiosConfiguration, q.Timeout) return b, err @@ -360,7 +345,7 @@ func CollectBios(client *bmclib.Client, q *QueryParams) ([]byte, error) { func CollectEthernetInterfaces(c *gofish.APIClient, q *QueryParams, systemID string) ([]byte, error) { systems, err := c.Service.Systems() if err != nil { - return nil, fmt.Errorf("could not query storage systems (%v:%v): %v", q.Host, q.Port, err) + return nil, fmt.Errorf("failed to query storage systems (%v:%v): %v", q.Host, q.Port, err) } var interfaces []*redfish.EthernetInterface @@ -373,13 +358,13 @@ func CollectEthernetInterfaces(c *gofish.APIClient, q *QueryParams, systemID str } if len(interfaces) <= 0 { - return nil, fmt.Errorf("could not get ethernet interfaces: %v", err) + return nil, fmt.Errorf("failed to get ethernet interfaces: %v", err) } data := map[string]any{"EthernetInterfaces": interfaces} b, err := json.MarshalIndent(data, "", " ") if err != nil { - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } return b, nil @@ -388,13 +373,13 @@ func CollectEthernetInterfaces(c *gofish.APIClient, q *QueryParams, systemID str func CollectChassis(c *gofish.APIClient, q *QueryParams) ([]byte, error) { chassis, err := c.Service.Chassis() if err != nil { - return nil, fmt.Errorf("could not query chassis (%v:%v): %v", q.Host, q.Port, err) + return nil, fmt.Errorf("failed to query chassis (%v:%v): %v", q.Host, q.Port, err) } data := map[string]any{"Chassis": chassis} b, err := json.MarshalIndent(data, "", " ") if err != nil { - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } return b, nil @@ -403,12 +388,12 @@ func CollectChassis(c *gofish.APIClient, q *QueryParams) ([]byte, error) { func CollectStorage(c *gofish.APIClient, q *QueryParams) ([]byte, error) { systems, err := c.Service.StorageSystems() if err != nil { - return nil, fmt.Errorf("could not query storage systems (%v:%v): %v", q.Host, q.Port, err) + return nil, fmt.Errorf("failed to query storage systems (%v:%v): %v", q.Host, q.Port, err) } services, err := c.Service.StorageServices() if err != nil { - return nil, fmt.Errorf("could not query storage services (%v:%v): %v", q.Host, q.Port, err) + return nil, fmt.Errorf("failed to query storage services (%v:%v): %v", q.Host, q.Port, err) } data := map[string]any{ @@ -419,7 +404,7 @@ func CollectStorage(c *gofish.APIClient, q *QueryParams) ([]byte, error) { } b, err := json.MarshalIndent(data, "", " ") if err != nil { - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } return b, nil @@ -428,7 +413,7 @@ func CollectStorage(c *gofish.APIClient, q *QueryParams) ([]byte, error) { func CollectSystems(c *gofish.APIClient, q *QueryParams) ([]byte, error) { systems, err := c.Service.Systems() if err != nil { - return nil, fmt.Errorf("could not query systems (%v:%v): %v", q.Host, q.Port, err) + return nil, fmt.Errorf("failed to query systems (%v:%v): %v", q.Host, q.Port, err) } // query the system's ethernet interfaces @@ -449,7 +434,7 @@ func CollectSystems(c *gofish.APIClient, q *QueryParams) ([]byte, error) { data := map[string]any{"Systems": temp} b, err := json.MarshalIndent(data, "", " ") if err != nil { - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } return b, nil @@ -458,13 +443,13 @@ func CollectSystems(c *gofish.APIClient, q *QueryParams) ([]byte, error) { func CollectRegisteries(c *gofish.APIClient, q *QueryParams) ([]byte, error) { registries, err := c.Service.Registries() if err != nil { - return nil, fmt.Errorf("could not query storage systems (%v:%v): %v", q.Host, q.Port, err) + return nil, fmt.Errorf("failed to query storage systems (%v:%v): %v", q.Host, q.Port, err) } data := map[string]any{"Registries": registries} b, err := json.MarshalIndent(data, "", " ") if err != nil { - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } return b, nil @@ -504,7 +489,7 @@ func CollectProcessors(q *QueryParams) ([]byte, error) { data := map[string]any{"Processors": procs} b, err := json.MarshalIndent(data, "", " ") if err != nil { - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } return b, nil @@ -517,7 +502,7 @@ func connectGofish(q *QueryParams) (*gofish.APIClient, error) { } c, err := gofish.Connect(config) if err != nil { - return nil, fmt.Errorf("could not connect to redfish endpoint: %v", err) + return nil, fmt.Errorf("failed to connect to redfish endpoint: %v", err) } if c != nil { c.Service.ProtocolFeaturesSupported = gofish.ProtocolFeaturesSupported{ @@ -532,24 +517,22 @@ func connectGofish(q *QueryParams) (*gofish.APIClient, error) { func makeGofishConfig(q *QueryParams) (gofish.ClientConfig, error) { var ( - client = &http.Client{} - url = baseRedfishUrl(q) + client = &http.Client{ + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, + }, + DisableKeepAlives: true, + Dial: (&net.Dialer{ + Timeout: 120 * time.Second, + KeepAlive: 120 * time.Second, + }).Dial, + TLSHandshakeTimeout: 120 * time.Second, + ResponseHeaderTimeout: 120 * time.Second, + }, + } + url = baseRedfishUrl(q) ) - // NOTE: CA certs are not used for BMCs as far as I know - // if q.CaCertPath != "" { - // cacert, err := os.ReadFile(q.CaCertPath) - // if err != nil { - // return config, fmt.Errorf("failed to read CA cert file: %v", err) - // } - // certPool := x509.NewCertPool() - // certPool.AppendCertsFromPEM(cacert) - // client.Transport = &http.Transport{ - // TLSClientConfig: &tls.Config{ - // RootCAs: certPool, - // InsecureSkipVerify: false, - // }, - // } - // } return gofish.ClientConfig{ Endpoint: url, Username: q.User, @@ -567,7 +550,7 @@ func makeRequest[T any](client *bmclib.Client, fn func(context.Context) (T, erro err := client.Open(ctx) if err != nil { ctxCancel() - return nil, fmt.Errorf("could not open client: %v", err) + return nil, fmt.Errorf("failed to open client: %v", err) } defer client.Close(ctx) @@ -575,7 +558,7 @@ func makeRequest[T any](client *bmclib.Client, fn func(context.Context) (T, erro response, err := fn(ctx) if err != nil { ctxCancel() - return nil, fmt.Errorf("could not get response: %v", err) + return nil, fmt.Errorf("failed to get response: %v", err) } ctxCancel() @@ -585,7 +568,7 @@ func makeRequest[T any](client *bmclib.Client, fn func(context.Context) (T, erro func makeJson(object any) ([]byte, error) { b, err := json.MarshalIndent(object, "", " ") if err != nil { - return nil, fmt.Errorf("could not marshal JSON: %v", err) + return nil, fmt.Errorf("failed to marshal JSON: %v", err) } return []byte(b), nil }