mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 11:37:01 -07:00
Merge pull request #7 from davidallendj/main
Add more output to `collect` command for SMD
This commit is contained in:
commit
662fb6168e
4 changed files with 321 additions and 107 deletions
|
|
@ -10,6 +10,10 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
forceUpdate bool
|
||||||
|
)
|
||||||
|
|
||||||
var collectCmd = &cobra.Command{
|
var collectCmd = &cobra.Command{
|
||||||
Use: "collect",
|
Use: "collect",
|
||||||
Short: "Query information about BMC",
|
Short: "Query information about BMC",
|
||||||
|
|
@ -36,6 +40,7 @@ var collectCmd = &cobra.Command{
|
||||||
Verbose: verbose,
|
Verbose: verbose,
|
||||||
WithSecureTLS: withSecureTLS,
|
WithSecureTLS: withSecureTLS,
|
||||||
OutputPath: outputPath,
|
OutputPath: outputPath,
|
||||||
|
ForceUpdate: forceUpdate,
|
||||||
}
|
}
|
||||||
magellan.CollectInfo(&probeStates, l, q)
|
magellan.CollectInfo(&probeStates, l, q)
|
||||||
|
|
||||||
|
|
@ -50,10 +55,11 @@ var collectCmd = &cobra.Command{
|
||||||
func init() {
|
func init() {
|
||||||
collectCmd.PersistentFlags().StringSliceVar(&drivers, "driver", []string{"redfish"}, "set the driver(s) and fallback drivers to use")
|
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().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(&user, "user", "", "set the BMC user")
|
||||||
collectCmd.PersistentFlags().StringVar(&pass, "pass", "", "set the BMC password")
|
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().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(&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().StringVar(&ipmitoolPath, "ipmitool.path", "/usr/bin/ipmitool", "set the path for ipmitool")
|
||||||
collectCmd.PersistentFlags().BoolVar(&withSecureTLS, "secure-tls", false, "enable secure TLS")
|
collectCmd.PersistentFlags().BoolVar(&withSecureTLS, "secure-tls", false, "enable secure TLS")
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ package smd
|
||||||
// https://github.com/alexlovelltroy/hms-smd
|
// https://github.com/alexlovelltroy/hms-smd
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/bikeshack/magellan/internal/util"
|
"github.com/bikeshack/magellan/internal/util"
|
||||||
// hms "github.com/alexlovelltroy/hms-smd"
|
// 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")
|
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
|
// Add redfish endpoint via POST `/hsm/v2/Inventory/RedfishEndpoints` endpoint
|
||||||
url := makeEndpointUrl("/Inventory/RedfishEndpoints")
|
url := makeEndpointUrl("/Inventory/RedfishEndpoints")
|
||||||
res, body, _ := util.MakeRequest(url, "POST", data, headers)
|
res, body, err := util.MakeRequest(url, "POST", data, headers)
|
||||||
fmt.Println("smd url: ", url)
|
fmt.Printf("smd url: %v\n", url)
|
||||||
fmt.Println("res: ", res)
|
fmt.Printf("res: %v\n", res.Status)
|
||||||
fmt.Println("body: ", string(body))
|
fmt.Printf("body: %v\n", string(body))
|
||||||
return nil
|
if res != nil {
|
||||||
|
if res.StatusCode != http.StatusOK {
|
||||||
|
return fmt.Errorf("could not add redfish endpoint")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateRedfishEndpoint() {
|
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
|
// 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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,7 @@ type QueryParams struct {
|
||||||
Verbose bool
|
Verbose bool
|
||||||
IpmitoolPath string
|
IpmitoolPath string
|
||||||
OutputPath string
|
OutputPath string
|
||||||
|
ForceUpdate bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(l *log.Logger, q *QueryParams) (*bmclib.Client, error) {
|
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")
|
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
|
// make the output directory to store files
|
||||||
outputPath := path.Clean(q.OutputPath)
|
outputPath := path.Clean(q.OutputPath)
|
||||||
outputPath, err := util.MakeOutputDirectory(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)
|
done := make(chan struct{}, q.Threads+1)
|
||||||
chanProbeState := make(chan BMCProbeResult, 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
|
// collect bmc information asynchronously
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(q.Threads)
|
wg.Add(q.Threads)
|
||||||
|
|
@ -159,35 +160,9 @@ func CollectInfo(probeStates *[]BMCProbeResult, l *log.Logger, q *QueryParams) e
|
||||||
continue
|
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
|
node.NodeBMC += 1
|
||||||
|
|
||||||
headers := make(map[string]string)
|
// data to be sent to smd
|
||||||
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 := make(map[string]any)
|
data := make(map[string]any)
|
||||||
data["ID"] = fmt.Sprintf("%v", node.String()[:len(node.String())-2])
|
data["ID"] = fmt.Sprintf("%v", node.String()[:len(node.String())-2])
|
||||||
data["Type"] = ""
|
data["Type"] = ""
|
||||||
|
|
@ -195,9 +170,95 @@ func CollectInfo(probeStates *[]BMCProbeResult, l *log.Logger, q *QueryParams) e
|
||||||
data["FQDN"] = ps.Host
|
data["FQDN"] = ps.Host
|
||||||
data["User"] = q.User
|
data["User"] = q.User
|
||||||
data["Password"] = q.Pass
|
data["Password"] = q.Pass
|
||||||
|
data["IPAddr"] = ""
|
||||||
|
data["MACAddr"] = ""
|
||||||
data["RediscoverOnUpdate"] = false
|
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, "", " ")
|
b, err := json.MarshalIndent(data, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -213,29 +274,17 @@ func CollectInfo(probeStates *[]BMCProbeResult, l *log.Logger, q *QueryParams) e
|
||||||
// add all endpoints to smd
|
// add all endpoints to smd
|
||||||
err = smd.AddRedfishEndpoint(b, headers)
|
err = smd.AddRedfishEndpoint(b, headers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Log.Errorf("could not add redfish endpoint: %v", err)
|
l.Log.Error(err)
|
||||||
|
|
||||||
// try updating instead
|
// 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
|
// got host information, so add to list of already probed hosts
|
||||||
found = append(found, ps.Host)
|
found = append(found, ps.Host)
|
||||||
}
|
}
|
||||||
|
|
@ -297,7 +346,7 @@ func QueryMetadata(client *bmclib.Client, l *log.Logger, q *QueryParams) ([]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
if q.Verbose {
|
if q.Verbose {
|
||||||
fmt.Printf("metadata: %v\n", string(b))
|
fmt.Printf("Metadata: %v\n", string(b))
|
||||||
}
|
}
|
||||||
ctxCancel()
|
ctxCancel()
|
||||||
return b, nil
|
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) {
|
func QueryEthernetInterfaces(client *bmclib.Client, l *log.Logger, q *QueryParams) ([]byte, error) {
|
||||||
config := gofish.ClientConfig{
|
c, err := connectGofish(q)
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not connect to bmc: %v", err)
|
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not get ethernet interfaces: %v", err)
|
return nil, fmt.Errorf("could not get ethernet interfaces: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := json.MarshalIndent(interfaces, "", " ")
|
data := map[string]any{"Interfaces": 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}
|
|
||||||
b, err := json.MarshalIndent(data, "", " ")
|
b, err := json.MarshalIndent(data, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not marshal JSON: %v", err)
|
return nil, fmt.Errorf("could not marshal JSON: %v", err)
|
||||||
|
|
@ -474,6 +486,183 @@ func QueryChassis(q *QueryParams) ([]byte, error) {
|
||||||
return b, nil
|
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) {
|
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))
|
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*time.Duration(timeout))
|
||||||
client.Registry.FilterForCompatible(ctx)
|
client.Registry.FilterForCompatible(ctx)
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"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) {
|
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, _ := http.NewRequest(httpMethod, url, bytes.NewBuffer(body))
|
||||||
req.Header.Add("User-Agent", "magellan")
|
req.Header.Add("User-Agent", "magellan")
|
||||||
for k, v := range headers {
|
for k, v := range headers {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue