Compare commits
4 Commits
794cdd7a0d
...
main
Author | SHA1 | Date | |
---|---|---|---|
efdcdcebee | |||
f646fa7ea4 | |||
ea2e2e2912 | |||
aced3c7906 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
vm-metrics
|
vm-metrics
|
||||||
|
log.txt
|
269
main.go
269
main.go
@@ -2,11 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
_ "time/tzdata"
|
_ "time/tzdata"
|
||||||
@@ -27,8 +29,28 @@ var (
|
|||||||
location *time.Location
|
location *time.Location
|
||||||
sha1ver string // sha1 revision used to build the program
|
sha1ver string // sha1 revision used to build the program
|
||||||
buildTime string // when the executable was built
|
buildTime string // when the executable was built
|
||||||
|
results []DrsResults
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type DrsResults struct {
|
||||||
|
ClusterName string
|
||||||
|
DrsScore int32
|
||||||
|
Vcenter string
|
||||||
|
NumVmsDrsBucket0To20 int32
|
||||||
|
NumVmsDrsBucket21To40 int32
|
||||||
|
NumVmsDrsBucket41To60 int32
|
||||||
|
NumVmsDrsBucket61To80 int32
|
||||||
|
NumVmsDrsBucket81To100 int32
|
||||||
|
}
|
||||||
|
|
||||||
|
// From https://github.com/vmware/govmomi/blob/main/performance/manager.go#L197
|
||||||
|
/*
|
||||||
|
type groupPerfCounterInfo struct {
|
||||||
|
info map[int32]*types.PerfCounterInfo
|
||||||
|
ids []types.PerfMetricId
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
func findPerfCounter(perfManager *vim25.PerformanceManager, counterName string) (*vim25.PerfCounterInfo, error) {
|
func findPerfCounter(perfManager *vim25.PerformanceManager, counterName string) (*vim25.PerfCounterInfo, error) {
|
||||||
perfCounters, err := perfManager.QueryPerfCounter(context.Background(), nil)
|
perfCounters, err := perfManager.QueryPerfCounter(context.Background(), nil)
|
||||||
@@ -61,15 +83,45 @@ func findVMByName(ctx context.Context, client *vim25.Client, vmName string) ([]m
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var result []mo.VirtualMachine
|
// Temporarily just return all VMs
|
||||||
|
return matchingVMs, nil
|
||||||
|
|
||||||
for _, vm := range matchingVMs {
|
/*
|
||||||
if vm.Name == vmName {
|
var result []mo.VirtualMachine
|
||||||
result = append(result, vm)
|
|
||||||
|
for _, vm := range matchingVMs {
|
||||||
|
if vm.Name == vmName {
|
||||||
|
result = append(result, vm)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
// If we reached here then we didn't find the Cluster
|
||||||
|
return mo.ClusterComputeResource{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMetricIds(counters []types.PerfCounterInfo) []types.PerfMetricId {
|
func getMetricIds(counters []types.PerfCounterInfo) []types.PerfMetricId {
|
||||||
@@ -79,6 +131,7 @@ func getMetricIds(counters []types.PerfCounterInfo) []types.PerfMetricId {
|
|||||||
metricId.CounterId = counter.Key
|
metricId.CounterId = counter.Key
|
||||||
metricIds = append(metricIds, metricId)
|
metricIds = append(metricIds, metricId)
|
||||||
}
|
}
|
||||||
|
fmt.Printf("metric IDs : '%v'\n", metricIds)
|
||||||
return metricIds
|
return metricIds
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,11 +139,15 @@ func getSpecificVMMetrics(ctx context.Context, c *vim25.Client, vmName string) {
|
|||||||
//perfManager := c.ServiceContent.PerfManager
|
//perfManager := c.ServiceContent.PerfManager
|
||||||
perfManager := performance.NewManager(c)
|
perfManager := performance.NewManager(c)
|
||||||
var perfCounterInfos []types.PerfCounterInfo
|
var perfCounterInfos []types.PerfCounterInfo
|
||||||
|
var perfCounterNames []string
|
||||||
startTime := time.Now().Add(-time.Hour)
|
startTime := time.Now().Add(-time.Hour)
|
||||||
timeNow := time.Now()
|
timeNow := time.Now()
|
||||||
|
|
||||||
// Find the performance counters for CPU Entitlement, CPU Consumed, and CPU Demand
|
// Find the performance counters for CPU Entitlement, CPU Consumed, and CPU Demand
|
||||||
counterNames := []string{"cpu.entitlement.latest", "cpu.usage.average", "cpu.demand.average"}
|
// % Entitled Resource Delivered = Consumed/(min(Demand,Entitlement)) * 100
|
||||||
|
// As per https://www.unknownfault.com/posts/how-is-percentage-entitled-resource/
|
||||||
|
counterNames := []string{"cpu.entitlement.latest", "cpu.usagemhz.none", "cpu.demand.average"}
|
||||||
|
//counterNames := []string{"cpu.entitlement.latest"}
|
||||||
|
|
||||||
// Retrieve counters name list
|
// Retrieve counters name list
|
||||||
counters, err := perfManager.CounterInfoByName(ctx)
|
counters, err := perfManager.CounterInfoByName(ctx)
|
||||||
@@ -103,12 +160,19 @@ func getSpecificVMMetrics(ctx context.Context, c *vim25.Client, vmName string) {
|
|||||||
fmt.Printf("Found '%d' counters\n", len(counters))
|
fmt.Printf("Found '%d' counters\n", len(counters))
|
||||||
|
|
||||||
for _, counter := range counters {
|
for _, counter := range counters {
|
||||||
|
|
||||||
|
perfCounterNames = append(perfCounterNames, counter.Name())
|
||||||
|
|
||||||
for _, counterName := range counterNames {
|
for _, counterName := range counterNames {
|
||||||
if counter.Name() == counterName {
|
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)
|
perfCounterInfos = append(perfCounterInfos, *counter)
|
||||||
} else {
|
} else {
|
||||||
//fmt.Printf("Ignoring '%s' : '%s'\n", counter.StatsType, counter.Name())
|
log.Printf("Ignoring '%s' : '%s'\n", counter.StatsType, counter.Name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -122,30 +186,113 @@ func getSpecificVMMetrics(ctx context.Context, c *vim25.Client, vmName string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, vm := range vmMatches {
|
//fmt.Printf("Processing %d vm matches : %v\n", len(vmMatches), vmMatches)
|
||||||
// Create PerfQuerySpec as per https://github.com/vmware/govmomi/blob/main/performance/example_test.go
|
|
||||||
spec := types.PerfQuerySpec{
|
|
||||||
MaxSample: 1800,
|
|
||||||
MetricId: getMetricIds(perfCounterInfos),
|
|
||||||
IntervalId: 20,
|
|
||||||
StartTime: &startTime, // 1 hour ago
|
|
||||||
EndTime: &timeNow,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Query metrics
|
var vmReferences []types.ManagedObjectReference
|
||||||
sample, err := perfManager.SampleByName(ctx, spec, counterNames, []types.ManagedObjectReference{vm.Reference()})
|
|
||||||
|
for _, vm := range vmMatches {
|
||||||
|
vmReferences = append(vmReferences, vm.Reference())
|
||||||
|
}
|
||||||
|
|
||||||
|
//fmt.Printf("Processing %d vm references : %v\n", len(vmReferences), vmReferences)
|
||||||
|
|
||||||
|
// Create PerfQuerySpec as per https://github.com/vmware/govmomi/blob/main/performance/example_test.go
|
||||||
|
spec := types.PerfQuerySpec{
|
||||||
|
//MaxSample: 180, // In one hour there are 180 instances of 20 second intervals
|
||||||
|
MetricId: getMetricIds(perfCounterInfos),
|
||||||
|
IntervalId: 20,
|
||||||
|
StartTime: &startTime, // 1 hour ago
|
||||||
|
EndTime: &timeNow,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Query metrics
|
||||||
|
//sample, err := perfManager.SampleByName(ctx, spec, counterNames, []types.ManagedObjectReference{vm.Reference()})
|
||||||
|
/*
|
||||||
|
sample, err := perfManager.SampleByName(ctx, spec, counterNames, vmReferences)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error getting SampleByName : '%s'\n", err)
|
fmt.Printf("Error getting SampleByName : '%s'\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
fmt.Printf("Sample result is %d\n", len(sample))
|
||||||
|
*/
|
||||||
|
|
||||||
result, err := perfManager.ToMetricSeries(ctx, sample)
|
var query []types.PerfQuerySpec
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Error getting ToMetricSeries : '%s'\n", err)
|
for _, e := range vmMatches {
|
||||||
return
|
spec.Entity = e.Reference()
|
||||||
|
query = append(query, spec)
|
||||||
|
}
|
||||||
|
|
||||||
|
sample, err := perfManager.Query(ctx, query)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error running query : '%s'\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
for _, v := range sample {
|
||||||
|
fmt.Printf("%v\n", v.GetPerfEntityMetricBase().Entity)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
allCounters, err := perfManager.CounterInfoByKey(ctx)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error getting CounterInfoByKey : '%s'\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var result []performance.EntityMetric
|
||||||
|
|
||||||
|
for i := range sample {
|
||||||
|
fmt.Printf("Processing sample %d\n", i)
|
||||||
|
var values []performance.MetricSeries
|
||||||
|
s, ok := sample[i].(*types.PerfEntityMetric)
|
||||||
|
if !ok {
|
||||||
|
panic(fmt.Errorf("expected type %T, got: %T", s, sample[i]))
|
||||||
|
}
|
||||||
|
fmt.Printf("Found %d samples\n", len(s.Value))
|
||||||
|
//fmt.Printf("%v\n", sample[i])
|
||||||
|
|
||||||
|
for j := range s.Value {
|
||||||
|
v := s.Value[j].(*types.PerfMetricIntSeries)
|
||||||
|
info, ok := allCounters[v.Id.CounterId]
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
vm := object.NewVirtualMachine(c, s.Entity)
|
||||||
|
entityName, err := vm.ObjectName(ctx)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error getting vm name : '%s'\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("%s\t%s\t%s\t%v (%s)\n", entityName, info.Name(), v.Id.Instance, v.Value, info.UnitInfo.GetElementDescription().Key)
|
||||||
|
|
||||||
|
/*
|
||||||
|
values = append(values, performance.MetricSeries{
|
||||||
|
Name: info.Name(),
|
||||||
|
unit: info.UnitInfo.GetElementDescription().Key,
|
||||||
|
Instance: v.Id.Instance,
|
||||||
|
Value: v.Value,
|
||||||
|
})
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Retrieved %d metric series\n", len(result))
|
result = append(result, performance.EntityMetric{
|
||||||
|
Entity: s.Entity,
|
||||||
|
SampleInfo: s.SampleInfo,
|
||||||
|
Value: values,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
result, err := perfManager.ToMetricSeries(ctx, sample)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error getting ToMetricSeries : '%s'\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Retrieved %d metric series\n", len(result))
|
||||||
|
|
||||||
|
|
||||||
// Read result
|
// Read result
|
||||||
for _, metric := range result {
|
for _, metric := range result {
|
||||||
@@ -156,9 +303,10 @@ func getSpecificVMMetrics(ctx context.Context, c *vim25.Client, vmName string) {
|
|||||||
return
|
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]
|
counter := counters[v.Name]
|
||||||
units := counter.UnitInfo.GetElementDescription().Label
|
units := counter.UnitInfo.GetElementDescription().Label
|
||||||
@@ -169,32 +317,48 @@ func getSpecificVMMetrics(ctx context.Context, c *vim25.Client, vmName string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(v.Value) != 0 {
|
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\n", name, instance, v.Name, units, v.Value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
fmt.Printf("Finished receiving result.\n")
|
||||||
// Create a query specification
|
*/
|
||||||
query := perfManager.QueryPerf(ctx, &vim25.QueryPerf{
|
|
||||||
Entity: []vim25.ManagedObjectReference{vm.Reference()},
|
|
||||||
MaxSample: 1,
|
|
||||||
MetricId: getMetricIds(perfCounterInfos),
|
|
||||||
IntervalId: 20, // 20-second interval
|
|
||||||
StartTime: time.Now().Add(-time.Hour), // 1 hour ago
|
|
||||||
EndTime: time.Now(),
|
|
||||||
})
|
|
||||||
|
|
||||||
// Process the query result
|
/*
|
||||||
for _, perf := range query {
|
// Create a query specification
|
||||||
fmt.Println("VM:", vmName)
|
query := perfManager.QueryPerf(ctx, &vim25.QueryPerf{
|
||||||
for i, value := range perf.Value {
|
Entity: []vim25.ManagedObjectReference{vm.Reference()},
|
||||||
counterInfo := perfCounterInfos[i]
|
MaxSample: 1,
|
||||||
fmt.Printf("%s: %v\n", counterInfo.NameInfo.GetElement().Key, value.Value[0])
|
MetricId: getMetricIds(perfCounterInfos),
|
||||||
}
|
IntervalId: 20, // 20-second interval
|
||||||
|
StartTime: time.Now().Add(-time.Hour), // 1 hour ago
|
||||||
|
EndTime: time.Now(),
|
||||||
|
})
|
||||||
|
|
||||||
|
// Process the query result
|
||||||
|
for _, perf := range query {
|
||||||
|
fmt.Println("VM:", vmName)
|
||||||
|
for i, value := range perf.Value {
|
||||||
|
counterInfo := perfCounterInfos[i]
|
||||||
|
fmt.Printf("%s: %v\n", counterInfo.NameInfo.GetElement().Key, value.Value[0])
|
||||||
}
|
}
|
||||||
*/
|
}
|
||||||
}
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 main() {
|
func main() {
|
||||||
@@ -252,4 +416,17 @@ func main() {
|
|||||||
defer c.Logout(ctx)
|
defer c.Logout(ctx)
|
||||||
|
|
||||||
getSpecificVMMetrics(ctx, c.Client, *vmName)
|
getSpecificVMMetrics(ctx, c.Client, *vmName)
|
||||||
|
//err = getNumVmsPerDrsScoreBucket(c)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error retrieving NumVmsPerDrsScoreBucket: %s\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output final results in JSON
|
||||||
|
if len(results) > 0 {
|
||||||
|
j, _ := json.Marshal(results)
|
||||||
|
fmt.Println(string(j))
|
||||||
|
} else {
|
||||||
|
fmt.Println("{}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user