This is the code that I'm using. Note that this doesn't work on the vcsim. Only on a real vcenter. The vcsim doesn't return the same metrics like a real vcenter does.
metrics := []string{"datastore.read.average", "datastore.write.average"}
hostID := "host-14"
metricInstance := "*"
perfManager := performance.NewManager(client.Client)
start := time.Now().Add(-2 * time.Minute)
end := time.Now()
pq := types.PerfQuerySpec{
MaxSample: 5,
MetricId: []types.PerfMetricId{{Instance: metricInstance}},
IntervalId: int32(20),
StartTime: &start,
EndTime: &end,
}
refs := []types.ManagedObjectReference{{Type: "HostSystem", Value: hostID}}
sample, err := perfManager.SampleByName(ctx, pq, metrics, refs)
if err != nil {
Exit(err)
}
metricSeries, err := perfManager.ToMetricSeries(ctx, sample)
if err != nil {
Exit(err)
}
fmt.Printf("%s,%s,%s,%s,%s,%s\n", "Host", "Time", "MetricName", "Instance", "Unit", "Value")
for _, m := range metricSeries {
for id, sample := range m.SampleInfo {
for _, e := range m.Value {
value := func() string {
if e.Value != nil {
if len(m.SampleInfo) == len(e.Value) {
return strconv.Itoa(int(e.Value[id]))
} else {
return fmt.Sprint(strings.Fields(strings.Trim(fmt.Sprint(e.Value), "[]")), "|")
}
}
return "?"
}()
fmt.Printf("%s,%s,%s,%s,%s,%s\n", m.Entity.Value, sample.Timestamp.String(), e.Name, e.Instance, e.Unit, value)
}
}
}
Hi,
When I query the
datastore.read.averageordatastore.read.average, I get an EntityMetric where the Instance of the MetricSeries is some kind of uuid. For me it is unclear where the uuid maps to. I guess it maps to the datastore but I don't know how. Is there a way I can map the metrics back to the datastore name?This is the code that I'm using. Note that this doesn't work on the vcsim. Only on a real vcenter. The vcsim doesn't return the same metrics like a real vcenter does.