Skip to content

Commit

Permalink
Add a show proc command #42
Browse files Browse the repository at this point in the history
Adds a new subcommand for printing out processor information.
  • Loading branch information
rustydb committed Mar 12, 2024
1 parent 1dc7d1f commit 554c845
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
43 changes: 43 additions & 0 deletions pkg/cmd/cli/proc/proc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
MIT License
(C) Copyright 2023-2024 Hewlett Packard Enterprise Development LP
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

package proc

// Processors represents a list of Processor types.
type Processors []Processor

// Processor represents a single, physical processor.
// The processor's model number is stored in either Processor.Model or Processor.VendorID depending
// on the vendor. The user may need to interpret both fields to understand what they have.
type Processor struct {
Architecture string `json:"architecture" yaml:"architecture"`
TotalCores int `json:"totalCores" yaml:"total_cores"`
Model string `json:"model" yaml:"model"`
Socket string `json:"socket" yaml:"socket"`
Threads int `json:"threads" yaml:"threads"`
VendorID string `json:"vendorID" yaml:"vendor_id"`
Error error `json:"error,omitempty" yaml:"error,omitempty"`
}
107 changes: 107 additions & 0 deletions pkg/cmd/cli/proc/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
MIT License
(C) Copyright 2023-2024 Hewlett Packard Enterprise Development LP
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

package proc

import (
"fmt"
"github.com/Cray-HPE/gru/internal/query"
"github.com/Cray-HPE/gru/pkg/auth"
"github.com/Cray-HPE/gru/pkg/cmd/cli"
"github.com/spf13/cobra"
"strings"
)

// NewShowCommand creates the `system` subcommand for `show`.
func NewShowCommand() *cobra.Command {
c := &cobra.Command{
Use: "proc host [...host]",
Short: "Processor information",
Long: `Show the Server's processors, a full list with their core count, model, architecture, and serial numbers.`,
Run: func(c *cobra.Command, args []string) {
hosts := cli.ParseHosts(args)
content := query.Async(getProcessors, hosts)
cli.PrettyPrint(content)
},
}
return c
}

func getProcessors(host string) interface{} {
foundProcessors := Processors{}
c, err := auth.Connection(host)
if err != nil {
foundProcessors = append(
foundProcessors, Processor{
Error: err,
},
)
return foundProcessors
}
defer c.Logout()
service := c.Service

managers, err := service.Managers()
if err != nil || len(managers) < 1 {
foundProcessors = append(
foundProcessors, Processor{
Error: err,
},
)
return foundProcessors
}

systems, err := service.Systems()
if err != nil || len(systems) < 1 {
foundProcessors = append(
foundProcessors, Processor{
Error: err,
},
)
return foundProcessors
}
systemProcessors, err := systems[0].Processors()
if err != nil {
foundProcessors = append(
foundProcessors, Processor{
Error: err,
},
)
} else {
for i := range systemProcessors {
processor := Processor{
Architecture: strings.TrimSpace(fmt.Sprintf("%v", systemProcessors[i].ProcessorArchitecture)),
TotalCores: systemProcessors[i].TotalCores,
Model: strings.TrimSpace(systemProcessors[i].Model),
Socket: strings.TrimSpace(systemProcessors[i].Socket),
Threads: systemProcessors[i].TotalThreads,
VendorID: strings.TrimSpace(systemProcessors[i].ProcessorID.VendorID),
}
foundProcessors = append(foundProcessors, processor)
}
}
return foundProcessors
}
2 changes: 2 additions & 0 deletions pkg/cmd/cli/show/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ package show

import (
"github.com/Cray-HPE/gru/pkg/cmd/cli/chassis/boot"
"github.com/Cray-HPE/gru/pkg/cmd/cli/proc"
"github.com/Cray-HPE/gru/pkg/cmd/cli/system"
"github.com/spf13/cobra"
)
Expand All @@ -41,6 +42,7 @@ func NewCommand() *cobra.Command {
}
c.AddCommand(
boot.NewShowCommand(),
proc.NewShowCommand(),
system.NewShowCommand(),
)
return c
Expand Down

0 comments on commit 554c845

Please sign in to comment.