Merge pull request #7 from davidallendj/main

Add more output to `collect` command for SMD
This commit is contained in:
Alex Lovell-Troy 2023-10-02 17:05:34 +03:00 committed by GitHub
commit 662fb6168e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 321 additions and 107 deletions

View file

@ -10,6 +10,10 @@ import (
"github.com/spf13/cobra"
)
var (
forceUpdate bool
)
var collectCmd = &cobra.Command{
Use: "collect",
Short: "Query information about BMC",
@ -36,6 +40,7 @@ var collectCmd = &cobra.Command{
Verbose: verbose,
WithSecureTLS: withSecureTLS,
OutputPath: outputPath,
ForceUpdate: forceUpdate,
}
magellan.CollectInfo(&probeStates, l, q)
@ -50,10 +55,11 @@ var collectCmd = &cobra.Command{
func init() {
collectCmd.PersistentFlags().StringSliceVar(&drivers, "driver", []string{"redfish"}, "set the driver(s) and fallback drivers to use")
collectCmd.PersistentFlags().StringVar(&smd.Host, "host", smd.Host, "set the host to the smd API")
collectCmd.PersistentFlags().IntVar(&smd.Port, "port", smd.Port, "set the port to the smd API")
collectCmd.PersistentFlags().IntVarP(&smd.Port, "port", "p", smd.Port, "set the port to the smd API")
collectCmd.PersistentFlags().StringVar(&user, "user", "", "set the BMC user")
collectCmd.PersistentFlags().StringVar(&pass, "pass", "", "set the BMC password")
collectCmd.PersistentFlags().StringVarP(&outputPath, "output", "o", "/tmp/magellan/data/", "set the path to store collection data")
collectCmd.PersistentFlags().BoolVar(&forceUpdate, "force-update", true, "set flag to force update data sent to SMD ")
collectCmd.PersistentFlags().StringVar(&preferredDriver, "preferred-driver", "ipmi", "set the preferred driver to use")
collectCmd.PersistentFlags().StringVar(&ipmitoolPath, "ipmitool.path", "/usr/bin/ipmitool", "set the path for ipmitool")
collectCmd.PersistentFlags().BoolVar(&withSecureTLS, "secure-tls", false, "enable secure TLS")

View file

@ -5,6 +5,7 @@ package smd
// https://github.com/alexlovelltroy/hms-smd
import (
"fmt"
"net/http"
"github.com/bikeshack/magellan/internal/util"
// hms "github.com/alexlovelltroy/hms-smd"
@ -47,17 +48,34 @@ func AddRedfishEndpoint(data []byte, headers map[string]string) error {
return fmt.Errorf("could not add redfish endpoint: no data found")
}
// var ep hms.RedfishEP
// _ = ep
// Add redfish endpoint via POST `/hsm/v2/Inventory/RedfishEndpoints` endpoint
url := makeEndpointUrl("/Inventory/RedfishEndpoints")
res, body, _ := util.MakeRequest(url, "POST", data, headers)
fmt.Println("smd url: ", url)
fmt.Println("res: ", res)
fmt.Println("body: ", string(body))
return nil
res, body, err := util.MakeRequest(url, "POST", data, headers)
fmt.Printf("smd url: %v\n", url)
fmt.Printf("res: %v\n", res.Status)
fmt.Printf("body: %v\n", string(body))
if res != nil {
if res.StatusCode != http.StatusOK {
return fmt.Errorf("could not add redfish endpoint")
}
}
return err
}
func UpdateRedfishEndpoint() {
// Update redfish endpoint via PUT `/hsm/v2/Inventory/RedfishEndpoints` endpoint
func UpdateRedfishEndpoint(xname string, data []byte, headers map[string]string) error {
if data == nil {
return fmt.Errorf("could not add redfish endpoint: no data found")
}
// Update redfish endpoint via PUT `/hsm/v2/Inventory/RedfishEndpoints` endpoint
url := makeEndpointUrl("/Inventory/RedfishEndpoints/" + xname)
res, body, err := util.MakeRequest(url, "PUT", data, headers)
fmt.Printf("smd url: %v\n", url)
fmt.Printf("res: %v\n", res.Status)
fmt.Printf("body: %v\n", string(body))
if res != nil {
if res.StatusCode != http.StatusOK {
return fmt.Errorf("could not update redfish endpoint")
}
}
return err
}

View file

@ -55,6 +55,7 @@ type QueryParams struct {
Verbose bool
IpmitoolPath string
OutputPath string
ForceUpdate bool
}
func NewClient(l *log.Logger, q *QueryParams) (*bmclib.Client, error) {
@ -120,14 +121,6 @@ func CollectInfo(probeStates *[]BMCProbeResult, l *log.Logger, q *QueryParams) e
return fmt.Errorf("no probe states found")
}
// generate custom xnames for bmcs
node := xnames.Node{
Cabinet: 1000,
Chassis: 1,
ComputeModule: 7,
NodeBMC: -1,
}
// make the output directory to store files
outputPath := path.Clean(q.OutputPath)
outputPath, err := util.MakeOutputDirectory(outputPath)
@ -139,6 +132,14 @@ func CollectInfo(probeStates *[]BMCProbeResult, l *log.Logger, q *QueryParams) e
done := make(chan struct{}, q.Threads+1)
chanProbeState := make(chan BMCProbeResult, q.Threads+1)
// generate custom xnames for bmcs
node := xnames.Node{
Cabinet: 1000,
Chassis: 1,
ComputeModule: 7,
NodeBMC: -1,
}
// collect bmc information asynchronously
var wg sync.WaitGroup
wg.Add(q.Threads)
@ -159,35 +160,9 @@ func CollectInfo(probeStates *[]BMCProbeResult, l *log.Logger, q *QueryParams) e
continue
}
// metadata
// _, err = magellan.QueryMetadata(client, l, &q)
// if err != nil {
// l.Log.Errorf("could not query metadata: %v\n", err)
// }
// inventories
inventory, err := QueryInventory(client, l, q)
if err != nil {
l.Log.Errorf("could not query inventory (%v:%v): %v", q.Host, q.Port, err)
}
// chassis
chassis, err := QueryChassis(q)
if err != nil {
l.Log.Errorf("could not query chassis: %v", err)
continue
}
node.NodeBMC += 1
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
// unmarshal json to send in correct format
var iobj, cobj map[string]json.RawMessage
json.Unmarshal(inventory, &iobj)
json.Unmarshal(chassis, &cobj)
// data to be sent to smd
data := make(map[string]any)
data["ID"] = fmt.Sprintf("%v", node.String()[:len(node.String())-2])
data["Type"] = ""
@ -195,9 +170,95 @@ func CollectInfo(probeStates *[]BMCProbeResult, l *log.Logger, q *QueryParams) e
data["FQDN"] = ps.Host
data["User"] = q.User
data["Password"] = q.Pass
data["IPAddr"] = ""
data["MACAddr"] = ""
data["RediscoverOnUpdate"] = false
data["Inventory"] = iobj
data["Chassis"] = cobj
// unmarshal json to send in correct format
var rm map[string]json.RawMessage
// inventories
inventory, err := QueryInventory(client, l, 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
chassis, err := QueryChassis(q)
if err != nil {
l.Log.Errorf("could not query chassis: %v", err)
continue
}
json.Unmarshal(chassis, &rm)
data["Chassis"] = rm["Chassis"]
// ethernet interfaces
interfaces, err := QueryEthernetInterfaces(client, l, q)
if err != nil {
l.Log.Errorf("could not query ethernet interfaces: %v", err)
continue
}
json.Unmarshal(interfaces, &rm)
data["Interface"] = rm["Interface"]
// get MAC address of first interface (for now...)
if len(rm["Interface"]) > 0 {
var i map[string]interface{}
json.Unmarshal(rm["Interface"], &i)
data["MACAddr"] = i["MACAddress"]
data["IPAddr"] = i["IPAddress"]
if i["FQDN"] != "" {
data["FQDN"] = rm["FQDN"]
}
}
// storage
// storage, err := QueryStorage(q)
// if err != nil {
// l.Log.Errorf("could not query storage: %v", err)
// continue
// }
// json.Unmarshal(storage, &rm)
// data["Storage"] = rm["Storage"]
// get specific processor info
procs, err := QueryProcessors(q)
if err != nil {
l.Log.Errorf("could not query processors: %v", err)
}
var p map[string]interface{}
json.Unmarshal(procs, &p)
data["Processors"] = rm["Processors"]
// systems
systems, err := QuerySystems(q)
if err != nil {
l.Log.Errorf("could not query systems: %v", err)
}
json.Unmarshal(systems, &rm)
data["Systems"] = rm["Systems"]
// add other fields from systems
if len(rm["Systems"]) > 0 {
var s map[string][]interface{}
json.Unmarshal(rm["Systems"], &s)
data["Name"] = s["Name"]
}
// data["Type"] = rm[""]
// registries
// registries, err := QueryRegisteries(q)
// if err != nil {
// l.Log.Errorf("could not query registries: %v", err)
// }
// json.Unmarshal(registries, &rm)
// data["Registries"] = rm["Registries"]
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
@ -213,28 +274,16 @@ func CollectInfo(probeStates *[]BMCProbeResult, l *log.Logger, q *QueryParams) e
// add all endpoints to smd
err = smd.AddRedfishEndpoint(b, headers)
if err != nil {
l.Log.Errorf("could not add redfish endpoint: %v", err)
l.Log.Error(err)
// try updating instead
if q.ForceUpdate {
err = smd.UpdateRedfishEndpoint(data["ID"].(string), b, headers)
if err != nil {
l.Log.Error(err)
}
}
}
// users
// user, err := magellan.QueryUsers(client, l, &q)
// if err != nil {
// l.Log.Errorf("could not query users: %v\n", err)
// }
// users = append(users, user)
// bios
// _, err = magellan.QueryBios(client, l, &q)
// if err != nil {
// l.Log.Errorf("could not query bios: %v\n", err)
// }
// _, err = magellan.QueryPowerState(client, l, &q)
// if err != nil {
// l.Log.Errorf("could not query power state: %v\n", err)
// }
// got host information, so add to list of already probed hosts
found = append(found, ps.Host)
@ -297,7 +346,7 @@ func QueryMetadata(client *bmclib.Client, l *log.Logger, q *QueryParams) ([]byte
}
if q.Verbose {
fmt.Printf("metadata: %v\n", string(b))
fmt.Printf("Metadata: %v\n", string(b))
}
ctxCancel()
return b, nil
@ -415,54 +464,17 @@ func QueryBios(client *bmclib.Client, l *log.Logger, q *QueryParams) ([]byte, er
}
func QueryEthernetInterfaces(client *bmclib.Client, l *log.Logger, q *QueryParams) ([]byte, error) {
config := gofish.ClientConfig{
Endpoint: fmt.Sprintf("https://%s:%d", q.Host, q.Port),
Username: q.User,
Password: q.Pass,
Insecure: !q.WithSecureTLS,
TLSHandshakeTimeout: q.Timeout,
}
c, err := gofish.Connect(config)
c, err := connectGofish(q)
if err != nil {
return nil, fmt.Errorf("could not connect to bmc: %v", err)
}
interfaces, err := redfish.ListReferencedEthernetInterfaces(c, "")
interfaces, err := redfish.ListReferencedEthernetInterfaces(c, "/redfish/v1/Systems/")
if err != nil {
return nil, fmt.Errorf("could not get ethernet interfaces: %v", err)
}
b, err := json.MarshalIndent(interfaces, "", " ")
if err != nil {
return nil, fmt.Errorf("could not marshal JSON: %v", err)
}
return b, nil
}
func QueryChassis(q *QueryParams) ([]byte, error) {
url := "https://"
if q.User != "" && q.Pass != "" {
url += fmt.Sprintf("%s:%s@", q.User, q.Pass)
}
url += fmt.Sprintf("%s:%d", q.Host, q.Port)
config := gofish.ClientConfig{
Endpoint: url,
Username: q.User,
Password: q.Pass,
Insecure: !q.WithSecureTLS,
TLSHandshakeTimeout: q.Timeout,
}
c, err := gofish.Connect(config)
if err != nil {
return nil, fmt.Errorf("could not connect to bmc (%v:%v): %v", q.Host, q.Port, err)
}
chassis, err := c.Service.Chassis()
if err != nil {
return nil, fmt.Errorf("could not query chassis (%v:%v): %v", q.Host, q.Port, err)
}
data := map[string]any{"chassis": chassis}
data := map[string]any{"Interfaces": interfaces}
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
return nil, fmt.Errorf("could not marshal JSON: %v", err)
@ -474,6 +486,183 @@ func QueryChassis(q *QueryParams) ([]byte, error) {
return b, nil
}
func QueryChassis(q *QueryParams) ([]byte, error) {
c, err := connectGofish(q)
if err != nil {
return nil, fmt.Errorf("could not connect to bmc (%v:%v): %v", q.Host, q.Port, err)
}
chassis, err := c.Service.Chassis()
if err != nil {
return nil, fmt.Errorf("could not 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)
}
if q.Verbose {
fmt.Printf("%v\n", string(b))
}
return b, nil
}
func QueryStorage(q *QueryParams) ([]byte, error) {
c, err := connectGofish(q)
if err != nil {
return nil, fmt.Errorf("could not connect to bmc (%v:%v): %v", q.Host, q.Port, err)
}
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)
}
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)
}
data := map[string]any{
"Storage": map[string]any{
"Systems": systems,
"Services": services,
},
}
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
return nil, fmt.Errorf("could not marshal JSON: %v", err)
}
if q.Verbose {
fmt.Printf("%v\n", string(b))
}
return b, nil
}
func QuerySystems(q *QueryParams) ([]byte, error) {
c, err := connectGofish(q)
if err != nil {
return nil, fmt.Errorf("could not connect to bmc (%v:%v): %v", q.Host, q.Port, err)
}
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)
}
data := map[string]any{"Systems": systems }
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
return nil, fmt.Errorf("could not marshal JSON: %v", err)
}
if q.Verbose {
fmt.Printf("%v\n", string(b))
}
return b, nil
}
func QueryRegisteries(q *QueryParams) ([]byte, error) {
c, err := connectGofish(q)
if err != nil {
return nil, fmt.Errorf("could not connect to bmc (%v:%v): %v", q.Host, q.Port, err)
}
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)
}
data := map[string]any{"Registries": registries }
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
return nil, fmt.Errorf("could not marshal JSON: %v", err)
}
if q.Verbose {
fmt.Printf("%v\n", string(b))
}
return b, nil
}
func QueryProcessors(q *QueryParams) ([]byte, error) {
baseUrl := "https://"
if q.User != "" && q.Pass != "" {
baseUrl += fmt.Sprintf("%s:%s@", q.User, q.Pass)
}
baseUrl += fmt.Sprintf("%s:%d", q.Host, q.Port)
url := baseUrl + "/redfish/v1/Systems"
res, body, err := util.MakeRequest(url, "GET", nil, nil)
if err != nil {
return nil, fmt.Errorf("something went wrong: %v", err)
} else if res == nil {
return nil, fmt.Errorf("no response returned (url: %s)", url)
} else if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("returned status code %d", res.StatusCode)
}
// convert to not get base64 string
var procs map[string]json.RawMessage
var members []map[string]any
json.Unmarshal(body, &procs)
json.Unmarshal(procs["Members"], &members)
// request data about each processor member on node
for _, member := range members {
var oid = member["@odata.id"].(string)
var infoUrl = baseUrl + oid
res, _, err := util.MakeRequest(infoUrl, "GET", nil, nil)
if err != nil {
return nil, fmt.Errorf("something went wrong: %v", err)
} else if res == nil {
return nil, fmt.Errorf("no response returned (url: %s)", url)
} else if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("returned status code %d", res.StatusCode)
}
}
data := map[string]any{"Processors": procs }
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
return nil, fmt.Errorf("could not marshal JSON: %v", err)
}
if q.Verbose {
fmt.Printf("%v\n", string(b))
}
return b, nil
}
func connectGofish(q *QueryParams) (*gofish.APIClient, error) {
config := makeGofishConfig(q)
c, err := gofish.Connect(config)
c.Service.ProtocolFeaturesSupported = gofish.ProtocolFeaturesSupported{
ExpandQuery: gofish.Expand{
ExpandAll: true,
Links: true,
},
}
return c, err
}
func makeGofishConfig(q *QueryParams) gofish.ClientConfig {
url := "https://"
if q.User != "" && q.Pass != "" {
url += fmt.Sprintf("%s:%s@", q.User, q.Pass)
}
url += fmt.Sprintf("%s:%d", q.Host, q.Port)
return gofish.ClientConfig{
Endpoint: url,
Username: q.User,
Password: q.Pass,
Insecure: !q.WithSecureTLS,
TLSHandshakeTimeout: q.Timeout,
}
}
func makeRequest[T any](client *bmclib.Client, fn func(context.Context) (T, error), timeout int) ([]byte, error) {
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*time.Duration(timeout))
client.Registry.FilterForCompatible(ctx)

View file

@ -2,6 +2,7 @@ package util
import (
"bytes"
"crypto/tls"
"fmt"
"io"
"net/http"
@ -17,7 +18,7 @@ func PathExists(path string) (bool, error) {
}
func MakeRequest(url string, httpMethod string, body []byte, headers map[string]string) (*http.Response, []byte, error) {
// url := getSmdEndpointUrl(endpoint)
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
req, _ := http.NewRequest(httpMethod, url, bytes.NewBuffer(body))
req.Header.Add("User-Agent", "magellan")
for k, v := range headers {