diff --git a/README.md b/README.md index 104cc66..ff574d5 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ Cloud-Z gathers information and perform benchmarks on cloud instances in multiple cloud providers. - [x] Cloud type, instance id, and type -- [ ] CPU information including type, number of available cores, and cache sizes +- [x] CPU information including type, number of available cores, and cache sizes +- [ ] RAM information - [ ] Benchmark CPU - [ ] Storage devices information - [ ] Benchmark storage diff --git a/cmd/root.go b/cmd/root.go index c97a8e3..335fe23 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -21,13 +21,14 @@ var rootCmd = &cobra.Command{ Short: "Cloud-Z gathers information on cloud instances", Version: fmt.Sprintf("%s, commit %s, built at %s by %s", version, commit, date, builtBy), Run: func(cmd *cobra.Command, args []string) { - allProviders := []providers.Provider{ + allCloudProviders := []providers.CloudProvider{ &providers.AwsProvider{}, &providers.GcpProvider{}, &providers.AzureProvider{}, } - for _, provider := range allProviders { + detectedCloud := false + for _, provider := range allCloudProviders { // TODO detect faster with goroutines? if provider.Detect() { data, err := provider.GetData() @@ -35,20 +36,28 @@ var rootCmd = &cobra.Command{ log.Fatalln(err) } - table := tablewriter.NewWriter(os.Stdout) - for _, v := range data { - table.Append(v) - } - table.Render() + printTable(data) - return + detectedCloud = true } } - println("Unable to detect cloud provider") + if !detectedCloud { + println("Unable to detect cloud provider") + } + + printTable(providers.GetCPUInfo()) }, } +func printTable(data [][]string) { + table := tablewriter.NewWriter(os.Stdout) + for _, v := range data { + table.Append(v) + } + table.Render() +} + func Execute() { if err := rootCmd.Execute(); err != nil { _, _ = fmt.Fprintln(os.Stderr, err) diff --git a/go.mod b/go.mod index 95fff6e..4d73c7a 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,8 @@ module cloud-z go 1.17 require ( + github.com/inhies/go-bytesize v0.0.0-20210819104631-275770b98743 + github.com/klauspost/cpuid/v2 v2.0.9 github.com/olekukonko/tablewriter v0.0.5 github.com/spf13/cobra v1.3.0 ) diff --git a/go.sum b/go.sum index 29bf336..78f16d7 100644 --- a/go.sum +++ b/go.sum @@ -224,6 +224,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1: github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/inhies/go-bytesize v0.0.0-20210819104631-275770b98743 h1:X3Xxno5Ji8idrNiUoFc7QyXpqhSYlDRYQmc7mlpMBzU= +github.com/inhies/go-bytesize v0.0.0-20210819104631-275770b98743/go.mod h1:KrtyD5PFj++GKkFS/7/RRrfnRhAMGQwy75GLCHWrCNs= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= @@ -233,6 +235,8 @@ github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/X github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= diff --git a/providers/cpuid.go b/providers/cpuid.go new file mode 100644 index 0000000..db38795 --- /dev/null +++ b/providers/cpuid.go @@ -0,0 +1,33 @@ +package providers + +import ( + "fmt" + "github.com/inhies/go-bytesize" + "github.com/klauspost/cpuid/v2" + "strings" +) + +func bytes(b int) string { + //return bytesize.New(float64(b)).Format("%d", "", false) + return bytesize.New(float64(b)).String() +} + +func GetCPUInfo() [][]string { + return [][]string{ + {"CPU", cpuid.CPU.BrandName}, + {"Vendor", cpuid.CPU.VendorString}, + {"Vendor ID", fmt.Sprintf("%v", cpuid.CPU.VendorID.String())}, + {"Family", fmt.Sprintf("%v", cpuid.CPU.Family)}, + {"MHz", fmt.Sprintf("%v", cpuid.CPU.Hz/1_000_000)}, + {"Logical cores", fmt.Sprintf("%v", cpuid.CPU.LogicalCores)}, + {"Physical cores", fmt.Sprintf("%v", cpuid.CPU.PhysicalCores)}, + {"Thread per core", fmt.Sprintf("%v", cpuid.CPU.ThreadsPerCore)}, + {"Boost frequency", fmt.Sprintf("%v", cpuid.CPU.BoostFreq)}, + {"L1 Cache", fmt.Sprintf("%v instruction, %v data", bytes(cpuid.CPU.Cache.L1I), bytes(cpuid.CPU.Cache.L1D))}, + {"L2 Cache", bytes(cpuid.CPU.Cache.L2)}, + {"L2 Cache", bytes(cpuid.CPU.Cache.L2)}, + {"L3 Cache", bytes(cpuid.CPU.Cache.L3)}, + {"Cache line", fmt.Sprintf("%v", cpuid.CPU.CacheLine)}, + {"Features", strings.Join(cpuid.CPU.FeatureSet(), ", ")}, + } +} diff --git a/providers/provider.go b/providers/provider.go index df1dde2..8ca3549 100644 --- a/providers/provider.go +++ b/providers/provider.go @@ -1,6 +1,6 @@ package providers -type Provider interface { +type CloudProvider interface { Detect() bool GetData() ([][]string, error) }