vmware/govmomi 来获取服务器的信息

vmware/govmomi 来获取服务器的信息

连接获取的信息的服务器

通过govmomi的NewClient来创建相应的连接

	u := &url.URL{
		Scheme: "https",
		Host:   服务器的地址,
		Path:   "/sdk",
	}
	u.User = url.UserPassword(account, password)
	client, _ := govmomi.NewClient(ctx, u, true)

获取服务器的集群的信息

通过返回数据,直接获取ClusterComputeResource

	m := view.NewManager(client)
	kind := []string{"ClusterComputeResource"}
	v, _ := m.CreateContainerView(ctx, client.ServiceContent.RootFolder, kind, true)

	var clusters []mo.ClusterComputeResource
	err1 := v.Retrieve(ctx, kind, []string{"summary", "name", "network", "datastore", "host"}, &clusters)

通过相应的字段,获取map型数据

集群的相关字段

perfManager := performance.NewManager(Client.Client)
	counters, _ := perfManager.CounterInfoByName(ctx)
	//进行对其周期和最大的获取的值进行配置
	spec := types.PerfQuerySpec{
		MaxSample:  1,
		MetricId:   []types.PerfMetricId{{Instance: ""}},
		IntervalId: 20,
	}
	//获取想要字段的id在总字段对应的位置和数据
	sample, _ := perfManager.SampleByName(ctx, spec, names, hostList)
	//获取想要的数据全部信息
	res, _ := perfManager.ToMetricSeries(ctx, sample)

获取主机的信息

通过返回数据,直接获取HostSystem

	m := view.NewManager(Client.Client)
	kind := []string{"HostSystem"}
	v, _ := m.CreateContainerView(ctx, Client.ServiceContent.RootFolder, kind, true)
	var hosts []mo.HostSystem
	err1 := v.Retrieve(ctx, kind, []string{"summary", "datastore"}, &hosts)

通过相应的字段,获取map型数据

主机的相关数据字段

	perfManager := performance.NewManager(Client.Client)
	counters, err := perfManager.CounterInfoByName(ctx)
	spec := types.PerfQuerySpec{
		MaxSample:  1,
		MetricId:   []types.PerfMetricId{{Instance: ""}},
		IntervalId: 20,
	}
	sample, err := perfManager.SampleByName(ctx, spec, names, hostList)
	res, _ := perfManager.ToMetricSeries(ctx, sample)

获取储存信息的数据

通过返回数据,直接获取Datastore

	m := view.NewManager(client)
	kind := []string{"Datastore"}
	v, _ := m.CreateContainerView(ctx, client.ServiceContent.RootFolder, kind, true)

	var stores []mo.Datastore
	err1 := v.Retrieve(ctx, kind, []string{"summary", "parent", "host", "vm"}, &stores)

通过相应的字段,获取map型数据

存储信息的相关数据字段

	perfManager := performance.NewManager(Client.Client)
	counters, err := perfManager.CounterInfoByName(ctx)
	spec := types.PerfQuerySpec{
		MaxSample:  1,
		MetricId:   []types.PerfMetricId{{Instance: ""}},
		IntervalId: 20,
	}
	sample, err := perfManager.SampleByName(ctx, spec, names, hostList)
	res, _ := perfManager.ToMetricSeries(ctx, sample)

获取虚拟机的数据

通过返回数据,直接获取VirtualMachine

	m := view.NewManager(client)
	kind := []string{"Datastore"}
	v, _ := m.CreateContainerView(ctx, client.ServiceContent.RootFolder, kind, true)

	var vms []mo.VirtualMachine
	err := collect.Retrieve(ctx, vm, []string{"summary", "network", "datastore", "storage", "config", "guest", "parent", "runtime"}, &vms)
	

通过相应的字段,获取map型数据

虚拟机的相关数据字段

	perfManager := performance.NewManager(Client.Client)
	counters, err := perfManager.CounterInfoByName(ctx)
	spec := types.PerfQuerySpec{
		MaxSample:  1,
		MetricId:   []types.PerfMetricId{{Instance: ""}},
		IntervalId: 20,
	}
	sample, err := perfManager.SampleByName(ctx, spec, names, hostList)
	res, _ := perfManager.ToMetricSeries(ctx, sample)

最后提供 vSphere官方文档

上一篇:国产软件如何让人再次失望——!20824 mindspore1.3.0gpu version can not compile from source code, because openmpi sou


下一篇:KubeVela 上手|让云端应用交付更加丝滑