Added ability to store output of collect command

This commit is contained in:
David J. Allen 2023-09-19 13:57:47 -06:00
parent dc8689c26b
commit 5acccf9355
2 changed files with 55 additions and 26 deletions

View file

@ -4,7 +4,7 @@ import (
magellan "github.com/bikeshack/magellan/internal" magellan "github.com/bikeshack/magellan/internal"
"github.com/bikeshack/magellan/internal/api/smd" "github.com/bikeshack/magellan/internal/api/smd"
"github.com/bikeshack/magellan/internal/db/sqlite" "github.com/bikeshack/magellan/internal/db/sqlite"
"github.com/bikeshack/magellan/internal/log"
"github.com/cznic/mathutil" "github.com/cznic/mathutil"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -15,7 +15,7 @@ var collectCmd = &cobra.Command{
Short: "Query information about BMC", Short: "Query information about BMC",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// make application logger // make application logger
l := magellan.NewLogger(logrus.New(), logrus.DebugLevel) l := log.NewLogger(logrus.New(), logrus.DebugLevel)
// get probe states stored in db from scan // get probe states stored in db from scan
probeStates, err := sqlite.GetProbeResults(dbpath) probeStates, err := sqlite.GetProbeResults(dbpath)
@ -35,6 +35,7 @@ var collectCmd = &cobra.Command{
Threads: threads, Threads: threads,
Verbose: verbose, Verbose: verbose,
WithSecureTLS: withSecureTLS, WithSecureTLS: withSecureTLS,
OutputPath: outputPath,
} }
magellan.CollectInfo(&probeStates, l, q) magellan.CollectInfo(&probeStates, l, q)
@ -52,6 +53,7 @@ func init() {
collectCmd.PersistentFlags().IntVar(&smd.Port, "port", smd.Port, "set the port to the smd API") collectCmd.PersistentFlags().IntVar(&smd.Port, "port", 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().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")

View file

@ -8,10 +8,14 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
"path"
"sync" "sync"
"time" "time"
"github.com/bikeshack/magellan/internal/log"
"github.com/bikeshack/magellan/internal/api/smd" "github.com/bikeshack/magellan/internal/api/smd"
"github.com/bikeshack/magellan/internal/util"
"github.com/Cray-HPE/hms-xname/xnames" "github.com/Cray-HPE/hms-xname/xnames"
bmclib "github.com/bmc-toolbox/bmclib/v2" bmclib "github.com/bmc-toolbox/bmclib/v2"
@ -50,9 +54,10 @@ type QueryParams struct {
CertPoolFile string CertPoolFile string
Verbose bool Verbose bool
IpmitoolPath string IpmitoolPath string
OutputPath string
} }
func NewClient(l *Logger, q *QueryParams) (*bmclib.Client, error) { func NewClient(l *log.Logger, q *QueryParams) (*bmclib.Client, error) {
// NOTE: bmclib.NewClient(host, port, user, pass) // NOTE: bmclib.NewClient(host, port, user, pass)
// ...seems like the `port` params doesn't work like expected depending on interface // ...seems like the `port` params doesn't work like expected depending on interface
@ -106,7 +111,7 @@ func NewClient(l *Logger, q *QueryParams) (*bmclib.Client, error) {
return client, nil return client, nil
} }
func CollectInfo(probeStates *[]BMCProbeResult, l *Logger, q *QueryParams) error { func CollectInfo(probeStates *[]BMCProbeResult, l *log.Logger, q *QueryParams) error {
// check for available probe states // check for available probe states
if probeStates == nil { if probeStates == nil {
return fmt.Errorf("no probe states found") return fmt.Errorf("no probe states found")
@ -123,6 +128,13 @@ func CollectInfo(probeStates *[]BMCProbeResult, l *Logger, q *QueryParams) error
NodeBMC: -1, NodeBMC: -1,
} }
// make the output directory to store files
outputPath := path.Clean(q.OutputPath)
outputPath, err := util.MakeOutputDirectory(outputPath)
if err != nil {
l.Log.Errorf("could not make output directory: %v", err)
}
found := make([]string, 0, len(*probeStates)) found := make([]string, 0, len(*probeStates))
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)
@ -171,6 +183,11 @@ func CollectInfo(probeStates *[]BMCProbeResult, l *Logger, q *QueryParams) error
headers := make(map[string]string) headers := make(map[string]string)
headers["Content-Type"] = "application/json" 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"] = ""
@ -179,14 +196,20 @@ func CollectInfo(probeStates *[]BMCProbeResult, l *Logger, q *QueryParams) error
data["User"] = q.User data["User"] = q.User
data["Password"] = q.Pass data["Password"] = q.Pass
data["RediscoverOnUpdate"] = false data["RediscoverOnUpdate"] = false
data["Inventory"] = inventory data["Inventory"] = iobj
data["Chassis"] = chassis data["Chassis"] = cobj
b, err := json.MarshalIndent(data, "", " ") b, err := json.MarshalIndent(data, "", " ")
if err != nil { if err != nil {
l.Log.Errorf("could not marshal JSON: %v", err) l.Log.Errorf("could not marshal JSON: %v", err)
} }
// write JSON data to file
err = os.WriteFile(path.Clean(outputPath + "/" + q.Host + ".json"), b, os.ModePerm)
if err != nil {
l.Log.Errorf("could not write data to file: %v", err)
}
// 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 {
@ -246,7 +269,7 @@ func CollectInfo(probeStates *[]BMCProbeResult, l *Logger, q *QueryParams) error
return nil return nil
} }
func QueryMetadata(client *bmclib.Client, l *Logger, q *QueryParams) ([]byte, error) { func QueryMetadata(client *bmclib.Client, l *log.Logger, q *QueryParams) ([]byte, error) {
// client, err := NewClient(l, q) // client, err := NewClient(l, q)
// open BMC session and update driver registry // open BMC session and update driver registry
@ -280,7 +303,7 @@ func QueryMetadata(client *bmclib.Client, l *Logger, q *QueryParams) ([]byte, er
return b, nil return b, nil
} }
func QueryInventory(client *bmclib.Client, l *Logger, q *QueryParams) ([]byte, error) { func QueryInventory(client *bmclib.Client, l *log.Logger, q *QueryParams) ([]byte, error) {
// open BMC session and update driver registry // open BMC session and update driver registry
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*time.Duration(q.Timeout)) ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*time.Duration(q.Timeout))
client.Registry.FilterForCompatible(ctx) client.Registry.FilterForCompatible(ctx)
@ -297,21 +320,23 @@ func QueryInventory(client *bmclib.Client, l *Logger, q *QueryParams) ([]byte, e
return nil, fmt.Errorf("could not get inventory: %v", err) return nil, fmt.Errorf("could not get inventory: %v", err)
} }
// retrieve inventory data // retrieve inventory data
b, err := json.MarshalIndent(inventory, "", " ") data := map[string]any{"Inventory": inventory}
b, err := json.MarshalIndent(data, "", " ")
if err != nil { if err != nil {
ctxCancel() ctxCancel()
return nil, fmt.Errorf("could not marshal JSON: %v", err) return nil, fmt.Errorf("could not marshal JSON: %v", err)
} }
if q.Verbose { if q.Verbose {
fmt.Printf("inventory: %v\n", string(b)) fmt.Printf("%v\n", string(b))
} }
ctxCancel() ctxCancel()
return b, nil return b, nil
} }
func QueryPowerState(client *bmclib.Client, l *Logger, q *QueryParams) ([]byte, error) { func QueryPowerState(client *bmclib.Client, l *log.Logger, q *QueryParams) ([]byte, error) {
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*time.Duration(q.Timeout)) ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*time.Duration(q.Timeout))
client.Registry.FilterForCompatible(ctx) client.Registry.FilterForCompatible(ctx)
err := client.PreferProvider(q.Preferred).Open(ctx) err := client.PreferProvider(q.Preferred).Open(ctx)
@ -321,28 +346,29 @@ func QueryPowerState(client *bmclib.Client, l *Logger, q *QueryParams) ([]byte,
} }
defer client.Close(ctx) defer client.Close(ctx)
inventory, err := client.GetPowerState(ctx) powerState, err := client.GetPowerState(ctx)
if err != nil { if err != nil {
ctxCancel() ctxCancel()
return nil, fmt.Errorf("could not get inventory: %v", err) return nil, fmt.Errorf("could not get inventory: %v", err)
} }
// retrieve inventory data // retrieve inventory data
b, err := json.MarshalIndent(inventory, "", " ") data := map[string]any{"PowerState": powerState}
b, err := json.MarshalIndent(data, "", " ")
if err != nil { if err != nil {
ctxCancel() ctxCancel()
return nil, fmt.Errorf("could not marshal JSON: %v", err) return nil, fmt.Errorf("could not marshal JSON: %v", err)
} }
if q.Verbose { if q.Verbose {
fmt.Printf("power state: %v\n", string(b)) fmt.Printf("%v\n", string(b))
} }
ctxCancel() ctxCancel()
return b, nil return b, nil
} }
func QueryUsers(client *bmclib.Client, l *Logger, q *QueryParams) ([]byte, error) { func QueryUsers(client *bmclib.Client, l *log.Logger, q *QueryParams) ([]byte, error) {
// open BMC session and update driver registry // open BMC session and update driver registry
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*time.Duration(q.Timeout)) ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*time.Duration(q.Timeout))
client.Registry.FilterForCompatible(ctx) client.Registry.FilterForCompatible(ctx)
@ -361,7 +387,8 @@ func QueryUsers(client *bmclib.Client, l *Logger, q *QueryParams) ([]byte, error
} }
// retrieve inventory data // retrieve inventory data
b, err := json.MarshalIndent(users, "", " ") data := map[string]any {"Users": users}
b, err := json.MarshalIndent(data, "", " ")
if err != nil { if err != nil {
ctxCancel() ctxCancel()
return nil, fmt.Errorf("could not marshal JSON: %v", err) return nil, fmt.Errorf("could not marshal JSON: %v", err)
@ -370,24 +397,24 @@ func QueryUsers(client *bmclib.Client, l *Logger, q *QueryParams) ([]byte, error
// return b, nil // return b, nil
ctxCancel() ctxCancel()
if q.Verbose { if q.Verbose {
fmt.Printf("users: %v\n", string(b)) fmt.Printf("%v\n", string(b))
} }
return b, nil return b, nil
} }
func QueryBios(client *bmclib.Client, l *Logger, q *QueryParams) ([]byte, error) { func QueryBios(client *bmclib.Client, l *log.Logger, q *QueryParams) ([]byte, error) {
// client, err := NewClient(l, q) // client, err := NewClient(l, q)
// if err != nil { // if err != nil {
// return nil, fmt.Errorf("could not make query: %v", err) // return nil, fmt.Errorf("could not make query: %v", err)
// } // }
b, err := makeRequest(client, client.GetBiosConfiguration, q.Timeout) b, err := makeRequest(client, client.GetBiosConfiguration, q.Timeout)
if q.Verbose { if q.Verbose {
fmt.Printf("bios: %v\n", string(b)) fmt.Printf("%v\n", string(b))
} }
return b, err return b, err
} }
func QueryEthernetInterfaces(client *bmclib.Client, l *Logger, q *QueryParams) ([]byte, error) { func QueryEthernetInterfaces(client *bmclib.Client, l *log.Logger, q *QueryParams) ([]byte, error) {
config := gofish.ClientConfig{ config := gofish.ClientConfig{
Endpoint: fmt.Sprintf("https://%s:%d", q.Host, q.Port), Endpoint: fmt.Sprintf("https://%s:%d", q.Host, q.Port),
Username: q.User, Username: q.User,
@ -413,7 +440,6 @@ func QueryEthernetInterfaces(client *bmclib.Client, l *Logger, q *QueryParams) (
} }
func QueryChassis(q *QueryParams) ([]byte, error) { func QueryChassis(q *QueryParams) ([]byte, error) {
url := "https://" url := "https://"
if q.User != "" && q.Pass != "" { if q.User != "" && q.Pass != "" {
url += fmt.Sprintf("%s:%s@", q.User, q.Pass) url += fmt.Sprintf("%s:%s@", q.User, q.Pass)
@ -436,18 +462,19 @@ func QueryChassis(q *QueryParams) ([]byte, error) {
return nil, fmt.Errorf("could not query chassis (%v:%v): %v", q.Host, q.Port, err) return nil, fmt.Errorf("could not query chassis (%v:%v): %v", q.Host, q.Port, err)
} }
b, err := json.MarshalIndent(chassis, "", " ") data := map[string]any{"chassis": chassis}
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)
} }
if q.Verbose { if q.Verbose {
fmt.Printf("chassis: %v\n", string(b)) fmt.Printf("%v\n", string(b))
} }
return b, nil return b, nil
} }
func makeRequest[T interface{}](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)
err := client.Open(ctx) err := client.Open(ctx)
@ -468,7 +495,7 @@ func makeRequest[T interface{}](client *bmclib.Client, fn func(context.Context)
return makeJson(response) return makeJson(response)
} }
func makeJson(object interface{}) ([]byte, error) { func makeJson(object any) ([]byte, error) {
b, err := json.MarshalIndent(object, "", " ") b, err := json.MarshalIndent(object, "", " ")
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)