diff --git a/cmd/collect.go b/cmd/collect.go index 4b730cf..e9a0cf3 100644 --- a/cmd/collect.go +++ b/cmd/collect.go @@ -10,9 +10,8 @@ import ( "github.com/spf13/cobra" ) - var collectCmd = &cobra.Command{ - Use: "collect", + Use: "collect", Short: "Query information about BMC", Run: func(cmd *cobra.Command, args []string) { // make application logger @@ -28,14 +27,14 @@ var collectCmd = &cobra.Command{ threads = mathutil.Clamp(len(probeStates), 1, 255) } q := &magellan.QueryParams{ - User: user, - Pass: pass, - Drivers: drivers, - Preferred: preferredDriver, - Timeout: timeout, - Threads: threads, - Verbose: verbose, - WithSecureTLS: withSecureTLS, + User: user, + Pass: pass, + Drivers: drivers, + Preferred: preferredDriver, + Timeout: timeout, + Threads: threads, + Verbose: verbose, + WithSecureTLS: withSecureTLS, } magellan.CollectInfo(&probeStates, l, q) @@ -47,7 +46,7 @@ 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().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") @@ -58,4 +57,4 @@ func init(){ collectCmd.PersistentFlags().BoolVar(&withSecureTLS, "secure-tls", false, "enable secure TLS") collectCmd.PersistentFlags().StringVar(&certPoolFile, "cert-pool", "", "path to CA cert. (defaults to system CAs; used with --secure-tls=true)") rootCmd.AddCommand(collectCmd) -} \ No newline at end of file +} diff --git a/cmd/list.go b/cmd/list.go index fa69c41..136ed28 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -8,9 +8,8 @@ import ( "github.com/spf13/cobra" ) - var listCmd = &cobra.Command{ - Use: "list", + Use: "list", Short: "List information from scan", Run: func(cmd *cobra.Command, args []string) { probeResults, err := sqlite.GetProbeResults(dbpath) @@ -25,4 +24,4 @@ var listCmd = &cobra.Command{ func init() { rootCmd.AddCommand(listCmd) -} \ No newline at end of file +} diff --git a/cmd/root.go b/cmd/root.go index cd727fa..bb9ea50 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -8,19 +8,19 @@ import ( ) var ( - timeout int - threads int - ports []int - hosts []string - withSecureTLS bool - certPoolFile string - user string - pass string - dbpath string - drivers []string + timeout int + threads int + ports []int + hosts []string + withSecureTLS bool + certPoolFile string + user string + pass string + dbpath string + drivers []string preferredDriver string - ipmitoolPath string - verbose bool + ipmitoolPath string + verbose bool ) // TODO: discover bmc's on network (dora) diff --git a/internal/api/dora/dora.go b/internal/api/dora/dora.go index b3f2dbd..055c23d 100644 --- a/internal/api/dora/dora.go +++ b/internal/api/dora/dora.go @@ -9,23 +9,23 @@ import ( ) const ( - Host = "http://localhost" - DbType = "sqlite3" - DbPath = "../data/assets.db" + Host = "http://localhost" + DbType = "sqlite3" + DbPath = "../data/assets.db" BaseEndpoint = "/v1" - Port = 8000 + Port = 8000 ) type ScannedResult struct { - id string - site any - cidr string - ip string - port int + id string + site any + cidr string + ip string + port int protocol string - scanner string - state string - updated string + scanner string + state string + updated string } func makeEndpointUrl(endpoint string) string { @@ -67,8 +67,8 @@ func LoadScannedPortsFromDB(dbPath string, dbType string) { for rows.Next() { var r ScannedResult rows.Scan( - &r.id, &r.site, &r.cidr, &r.ip, &r.port, &r.protocol, &r.scanner, + &r.id, &r.site, &r.cidr, &r.ip, &r.port, &r.protocol, &r.scanner, &r.state, &r.updated, ) } -} \ No newline at end of file +} diff --git a/internal/api/smd/smd.go b/internal/api/smd/smd.go index 492375d..442be79 100644 --- a/internal/api/smd/smd.go +++ b/internal/api/smd/smd.go @@ -10,12 +10,11 @@ import ( ) var ( - Host = "http://localhost" + Host = "http://localhost" BaseEndpoint = "/hsm/v2" - Port = 27779 + Port = 27779 ) - func makeEndpointUrl(endpoint string) string { return Host + ":" + fmt.Sprint(Port) + BaseEndpoint + endpoint } @@ -49,7 +48,7 @@ func AddRedfishEndpoint(data []byte, headers map[string]string) error { // 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") res, body, _ := api.MakeRequest(url, "POST", data, headers) fmt.Println("smd url: ", url) @@ -60,4 +59,4 @@ func AddRedfishEndpoint(data []byte, headers map[string]string) error { func UpdateRedfishEndpoint() { // Update redfish endpoint via PUT `/hsm/v2/Inventory/RedfishEndpoints` endpoint -} \ No newline at end of file +} diff --git a/internal/api/util.go b/internal/api/util.go index 74d5693..ef121c3 100644 --- a/internal/api/util.go +++ b/internal/api/util.go @@ -7,7 +7,6 @@ import ( "net/http" ) - func MakeRequest(url string, httpMethod string, body []byte, headers map[string]string) (*http.Response, []byte, error) { // url := getSmdEndpointUrl(endpoint) req, _ := http.NewRequest(httpMethod, url, bytes.NewBuffer(body)) @@ -25,4 +24,4 @@ func MakeRequest(url string, httpMethod string, body []byte, headers map[string] return nil, nil, fmt.Errorf("could not read response body: %v", err) } return res, b, err -} \ No newline at end of file +} diff --git a/internal/collect.go b/internal/collect.go index 9024e26..cd4b78c 100644 --- a/internal/collect.go +++ b/internal/collect.go @@ -23,9 +23,9 @@ import ( ) const ( - IPMI_PORT = 623 - SSH_PORT = 22 - HTTPS_PORT = 443 + IPMI_PORT = 623 + SSH_PORT = 22 + HTTPS_PORT = 443 ) type BMCProbeResult struct { @@ -42,13 +42,13 @@ type QueryParams struct { User string Pass string Drivers []string - Threads int - Preferred string + Threads int + Preferred string Timeout int WithSecureTLS bool CertPoolFile string Verbose bool - IpmitoolPath string + IpmitoolPath string } func NewClient(l *Logger, q *QueryParams) (*bmclib.Client, error) { @@ -113,18 +113,18 @@ func CollectInfo(probeStates *[]BMCProbeResult, l *Logger, q *QueryParams) error if len(*probeStates) <= 0 { return fmt.Errorf("no probe states found") } - + // generate custom xnames for bmcs node := xnames.Node{ - Cabinet: 1000, - Chassis: 1, - ComputeModule: 7, - NodeBMC: -1, + Cabinet: 1000, + Chassis: 1, + ComputeModule: 7, + NodeBMC: -1, } - found := make([]string, 0, len(*probeStates)) - done := make(chan struct{}, q.Threads+1) - chanProbeState := make(chan BMCProbeResult, q.Threads+1) + found := make([]string, 0, len(*probeStates)) + done := make(chan struct{}, q.Threads+1) + chanProbeState := make(chan BMCProbeResult, q.Threads+1) // collect bmc information asynchronously var wg sync.WaitGroup @@ -132,18 +132,18 @@ func CollectInfo(probeStates *[]BMCProbeResult, l *Logger, q *QueryParams) error for i := 0; i < q.Threads; i++ { go func() { for { - ps, ok := <- chanProbeState + ps, ok := <-chanProbeState if !ok { wg.Done() return } q.Host = ps.Host q.Port = ps.Port - + client, err := NewClient(l, q) if err != nil { l.Log.Errorf("could not make client: %v", err) - continue + continue } // metadata @@ -155,7 +155,7 @@ func CollectInfo(probeStates *[]BMCProbeResult, l *Logger, q *QueryParams) error // 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) + l.Log.Errorf("could not query inventory (%v:%v): %v", q.Host, q.Port, err) } // chassis @@ -171,15 +171,15 @@ func CollectInfo(probeStates *[]BMCProbeResult, l *Logger, q *QueryParams) error headers["Content-Type"] = "application/json" data := make(map[string]any) - data["ID"] = fmt.Sprintf("%v", node.String()[:len(node.String())-2]) - data["Type"] = "" - data["Name"] = "" - data["FQDN"] = ps.Host - data["User"] = q.User - data["Password"] = q.Pass - data["RediscoverOnUpdate"] = false - data["Inventory"] = inventory - data["Chassis"] = chassis + data["ID"] = fmt.Sprintf("%v", node.String()[:len(node.String())-2]) + data["Type"] = "" + data["Name"] = "" + data["FQDN"] = ps.Host + data["User"] = q.User + data["Password"] = q.Pass + data["RediscoverOnUpdate"] = false + data["Inventory"] = inventory + data["Chassis"] = chassis b, err := json.MarshalIndent(data, "", " ") if err != nil { @@ -222,7 +222,7 @@ func CollectInfo(probeStates *[]BMCProbeResult, l *Logger, q *QueryParams) error for _, ps := range *probeStates { // skip if found info from host foundHost := slices.Index(found, ps.Host) - if !ps.State || foundHost >= 0{ + if !ps.State || foundHost >= 0 { continue } chanProbeState <- ps @@ -388,11 +388,11 @@ func QueryBios(client *bmclib.Client, l *Logger, q *QueryParams) ([]byte, error) func QueryEthernetInterfaces(client *bmclib.Client, l *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, + 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 { @@ -417,12 +417,12 @@ func QueryChassis(q *QueryParams) ([]byte, error) { 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, + 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 { diff --git a/internal/db/postgresql/postgresql.go b/internal/db/postgresql/postgresql.go index 090c4e2..4e9a54a 100644 --- a/internal/db/postgresql/postgresql.go +++ b/internal/db/postgresql/postgresql.go @@ -1 +1 @@ -package postgresql \ No newline at end of file +package postgresql diff --git a/internal/db/sqlite/sqlite.go b/internal/db/sqlite/sqlite.go index d7c3aeb..6a8d457 100644 --- a/internal/db/sqlite/sqlite.go +++ b/internal/db/sqlite/sqlite.go @@ -58,4 +58,4 @@ func GetProbeResults(path string) ([]magellan.BMCProbeResult, error) { return nil, fmt.Errorf("could not retrieve probes: %v", err) } return results, nil -} \ No newline at end of file +} diff --git a/internal/logger.go b/internal/logger.go index d821d60..31f68e1 100644 --- a/internal/logger.go +++ b/internal/logger.go @@ -4,23 +4,19 @@ import ( "github.com/sirupsen/logrus" ) - type Logger struct { - Log *logrus.Logger + Log *logrus.Logger Path string } - func NewLogger(l *logrus.Logger, level logrus.Level) *Logger { l.SetLevel(level) return &Logger{ - Log: logrus.New(), - Path: "", + Log: logrus.New(), + Path: "", } } - -func (l *Logger)WriteFile(path string) { +func (l *Logger) WriteFile(path string) { } - diff --git a/internal/scan.go b/internal/scan.go index 55b2d6a..330a019 100644 --- a/internal/scan.go +++ b/internal/scan.go @@ -7,7 +7,6 @@ import ( "time" ) - func rawConnect(host string, ports []int, timeout int, keepOpenOnly bool) []BMCProbeResult { results := []BMCProbeResult{} for _, p := range ports { @@ -52,11 +51,11 @@ func GenerateHosts(subnet string, begin uint8, end uint8) []string { } func ScanForAssets(hosts []string, ports []int, threads int, timeout int) []BMCProbeResult { - results := make([]BMCProbeResult, 0, len(hosts)) - done := make(chan struct{}, threads+1) - chanHost := make(chan string, threads+1) + results := make([]BMCProbeResult, 0, len(hosts)) + done := make(chan struct{}, threads+1) + chanHost := make(chan string, threads+1) // chanPort := make(chan int, threads+1) - + var wg sync.WaitGroup wg.Add(threads) for i := 0; i < threads; i++ { @@ -93,4 +92,4 @@ func ScanForAssets(hosts []string, ports []int, threads int, timeout int) []BMCP func GetDefaultPorts() []int { return []int{HTTPS_PORT, IPMI_PORT} -} \ No newline at end of file +}