chatgpt produces rubbish
This commit is contained in:
141
main.go
141
main.go
@@ -7,6 +7,7 @@ import (
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
_ "time/tzdata"
|
||||
@@ -14,6 +15,7 @@ import (
|
||||
"github.com/vmware/govmomi"
|
||||
"github.com/vmware/govmomi/object"
|
||||
"github.com/vmware/govmomi/performance"
|
||||
"github.com/vmware/govmomi/property"
|
||||
"github.com/vmware/govmomi/view"
|
||||
"github.com/vmware/govmomi/vim25"
|
||||
"github.com/vmware/govmomi/vim25/mo"
|
||||
@@ -72,6 +74,30 @@ func findVMByName(ctx context.Context, client *vim25.Client, vmName string) ([]m
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func findClusterByName(ctx context.Context, client *vim25.Client, name string) mo.ClusterComputeResource {
|
||||
// Create a container view so that we can search vCenter
|
||||
m := view.NewManager(client)
|
||||
cv, _ := m.CreateContainerView(ctx, c.ServiceContent.RootFolder, []string{"ClusterComputeResource"}, true)
|
||||
|
||||
var clusters []mo.ClusterComputeResource
|
||||
log.Printf("Searching for Cluster '%s'\n", name)
|
||||
err := cv.Retrieve(ctx, []string{"ClusterComputeResource"}, []string{"summary", "name"}, &clusters)
|
||||
if err != nil {
|
||||
log.Printf("Failed searching for Cluster %s : %s\n", name, err)
|
||||
return mo.ClusterComputeResource{}
|
||||
} else {
|
||||
for _, cluster := range clusters {
|
||||
if cluster.Name == name {
|
||||
log.Printf("Found corresponding Cluster with MoRef '%s'\n", cluster.Reference())
|
||||
return cluster
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If we reached here then we didn't find the Cluster
|
||||
return mo.ClusterComputeResource{}
|
||||
}
|
||||
|
||||
func getMetricIds(counters []types.PerfCounterInfo) []types.PerfMetricId {
|
||||
var metricIds []types.PerfMetricId
|
||||
for _, counter := range counters {
|
||||
@@ -79,6 +105,7 @@ func getMetricIds(counters []types.PerfCounterInfo) []types.PerfMetricId {
|
||||
metricId.CounterId = counter.Key
|
||||
metricIds = append(metricIds, metricId)
|
||||
}
|
||||
fmt.Printf("metric IDs : '%v'\n", metricIds)
|
||||
return metricIds
|
||||
}
|
||||
|
||||
@@ -90,7 +117,8 @@ func getSpecificVMMetrics(ctx context.Context, c *vim25.Client, vmName string) {
|
||||
timeNow := time.Now()
|
||||
|
||||
// Find the performance counters for CPU Entitlement, CPU Consumed, and CPU Demand
|
||||
counterNames := []string{"cpu.entitlement.latest", "cpu.usage.average", "cpu.demand.average"}
|
||||
counterNames := []string{"cpu.entitlement.latest", "cpu.usage.average", "cpu.demand.average", "cpu.readiness.average"}
|
||||
//counterNames := []string{"cpu.entitlement.latest", "cpu.usage.average"}
|
||||
|
||||
// Retrieve counters name list
|
||||
counters, err := perfManager.CounterInfoByName(ctx)
|
||||
@@ -105,7 +133,11 @@ func getSpecificVMMetrics(ctx context.Context, c *vim25.Client, vmName string) {
|
||||
for _, counter := range counters {
|
||||
for _, counterName := range counterNames {
|
||||
if counter.Name() == counterName {
|
||||
fmt.Printf("Found counter matching name '%s' : '%v'\n", counterName, counter.PerDeviceLevel)
|
||||
groupInfo := counter.GroupInfo.GetElementDescription()
|
||||
nameInfo := counter.NameInfo.GetElementDescription()
|
||||
|
||||
fmt.Printf("Found counter matching name '%s' : '%s';'%s'\n", counterName, groupInfo, nameInfo)
|
||||
|
||||
perfCounterInfos = append(perfCounterInfos, *counter)
|
||||
} else {
|
||||
//fmt.Printf("Ignoring '%s' : '%s'\n", counter.StatsType, counter.Name())
|
||||
@@ -125,7 +157,7 @@ func getSpecificVMMetrics(ctx context.Context, c *vim25.Client, vmName string) {
|
||||
for _, vm := range vmMatches {
|
||||
// Create PerfQuerySpec as per https://github.com/vmware/govmomi/blob/main/performance/example_test.go
|
||||
spec := types.PerfQuerySpec{
|
||||
MaxSample: 1800,
|
||||
//MaxSample: 180, // In one hour there are 180 instances of 20 second intervals
|
||||
MetricId: getMetricIds(perfCounterInfos),
|
||||
IntervalId: 20,
|
||||
StartTime: &startTime, // 1 hour ago
|
||||
@@ -138,6 +170,13 @@ func getSpecificVMMetrics(ctx context.Context, c *vim25.Client, vmName string) {
|
||||
fmt.Printf("Error getting SampleByName : '%s'\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("Sample result is %d\n", len(sample))
|
||||
|
||||
/*
|
||||
for _, v := range sample {
|
||||
fmt.Printf("%v\n", v.GetPerfEntityMetricBase().Entity)
|
||||
}
|
||||
*/
|
||||
|
||||
result, err := perfManager.ToMetricSeries(ctx, sample)
|
||||
if err != nil {
|
||||
@@ -156,9 +195,10 @@ func getSpecificVMMetrics(ctx context.Context, c *vim25.Client, vmName string) {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Vm %s has %d metric values\n", name, len(metric.Value))
|
||||
fmt.Printf("Vm %s has %d metric values : %v\n", name, len(metric.Value), metric)
|
||||
|
||||
for _, v := range metric.Value {
|
||||
for i, v := range metric.Value {
|
||||
fmt.Printf("Processing [%d] : '%v'\n", i, v)
|
||||
|
||||
counter := counters[v.Name]
|
||||
units := counter.UnitInfo.GetElementDescription().Label
|
||||
@@ -169,11 +209,14 @@ func getSpecificVMMetrics(ctx context.Context, c *vim25.Client, vmName string) {
|
||||
}
|
||||
|
||||
if len(v.Value) != 0 {
|
||||
fmt.Printf("%s\t%s\t%s\t%s : %v\n", name, instance, v.Name, units, v.ValueCSV())
|
||||
//fmt.Printf("%s\t%s\t%s\t%s : %v\n", name, instance, v.Name, units, v.ValueCSV())
|
||||
fmt.Printf("%s\t%s\t%s\t%s : %v (%d)\n", name, instance, v.Name, units, v.Value, len(v.Value))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("Finished receiving result.\n")
|
||||
|
||||
/*
|
||||
// Create a query specification
|
||||
query := perfManager.QueryPerf(ctx, &vim25.QueryPerf{
|
||||
@@ -197,6 +240,83 @@ func getSpecificVMMetrics(ctx context.Context, c *vim25.Client, vmName string) {
|
||||
}
|
||||
}
|
||||
|
||||
// From https://stackoverflow.com/a/33769379
|
||||
func Scan(d interface{}) {
|
||||
v := reflect.ValueOf(d)
|
||||
i := reflect.Indirect(v)
|
||||
s := i.Type()
|
||||
println(s.NumField()) // will print out 0, if you change Host to have 1 field, it prints out 1
|
||||
}
|
||||
|
||||
func query(t reflect.Type) {
|
||||
value := reflect.New(t).Interface()
|
||||
Scan(value)
|
||||
}
|
||||
|
||||
func getNumVmsPerDrsScoreBucket(client *govmomi.Client) error {
|
||||
//properties := []string{"drsVmConfig.numVmPerDrsScoreBucket"}
|
||||
properties := []string{"configurationEx"}
|
||||
ctx := context.Background()
|
||||
m := view.NewManager(client.Client)
|
||||
|
||||
clusters, err := m.CreateContainerView(ctx, client.ServiceContent.RootFolder, []string{"ClusterComputeResource"}, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer clusters.Destroy(ctx)
|
||||
|
||||
var clusterList []mo.ClusterComputeResource
|
||||
|
||||
err = clusters.Retrieve(ctx, []string{"ClusterComputeResource"}, nil, &clusterList)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, cluster := range clusterList {
|
||||
fmt.Printf("Cluster: %s\n", cluster.Name)
|
||||
|
||||
pc := property.DefaultCollector(client.Client)
|
||||
|
||||
//var clusterProps *types.ComputeResourceSummary
|
||||
clusterProps := mo.ClusterComputeResource{}
|
||||
|
||||
err := pc.RetrieveOne(ctx, cluster.Reference(), properties, &clusterProps)
|
||||
if err != nil {
|
||||
fmt.Printf("Error retrieving property : '%s'\n", err)
|
||||
return err
|
||||
}
|
||||
|
||||
foo := clusterProps.ConfigurationEx
|
||||
bar := foo.GetComputeResourceConfigInfo()
|
||||
fmt.Printf("foo: %v\n", foo)
|
||||
fmt.Printf("bar: %v\n", bar)
|
||||
|
||||
/*
|
||||
// Print properties (fields)
|
||||
objType := reflect.TypeOf(clusterProps)
|
||||
for i := 0; i < objType.NumField(); i++ {
|
||||
field := objType.Field(i)
|
||||
fmt.Printf("Field %d: %s (Type: %s)\n", i, field.Name, field.Type)
|
||||
}
|
||||
|
||||
// Print methods
|
||||
methods := reflect.TypeOf(clusterProps)
|
||||
for i := 0; i < methods.NumMethod(); i++ {
|
||||
method := methods.Method(i)
|
||||
fmt.Printf("Method %d: %s\n", i, method.Name)
|
||||
}
|
||||
*/
|
||||
|
||||
//bar := types.ClusterConfigInfoEx(foo).getNumVmsPerDrsScoreBucket()
|
||||
//fmt.Printf("%v\n", clusterProps)
|
||||
|
||||
//fmt.Printf("Cluster: %s\n", clusterProps.Name)
|
||||
//fmt.Printf("NumVmsPerDrsScoreBucket: %d\n", clusterProps.DrsVmConfig.NumVmPerDrsScoreBucket)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Command line flags
|
||||
vURL := flag.String("url", "", "The URL of a vCenter server, eg https://server.domain.example/sdk")
|
||||
@@ -204,7 +324,7 @@ func main() {
|
||||
vPass := flag.String("password", "", "The password to use when connecting to vCenter")
|
||||
vTZ := flag.String("tz", "Australia/Sydney", "The timezone to use when converting vCenter UTC times")
|
||||
vInsecure := flag.Bool("insecure", true, "Allow insecure connections to vCenter")
|
||||
vmName := flag.String("vmname", "example-vm", "The vm to query metrics")
|
||||
//vmName := flag.String("vmname", "example-vm", "The vm to query metrics")
|
||||
//begin := flag.Duration("b", time.Hour, "Begin time") // default BeginTime is 1h ago
|
||||
flag.Parse()
|
||||
|
||||
@@ -251,5 +371,10 @@ func main() {
|
||||
}
|
||||
defer c.Logout(ctx)
|
||||
|
||||
getSpecificVMMetrics(ctx, c.Client, *vmName)
|
||||
//getSpecificVMMetrics(ctx, c.Client, *vmName)
|
||||
err = getNumVmsPerDrsScoreBucket(c)
|
||||
if err != nil {
|
||||
fmt.Println("Error retrieving NumVmsPerDrsScoreBucket:", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user