代码拉取完成,页面将自动刷新
package datastore
import (
"context"
"net/url"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/cfgwarn"
"github.com/elastic/beats/metricbeat/mb"
"github.com/pkg/errors"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)
func init() {
if err := mb.Registry.AddMetricSet("vsphere", "datastore", New); err != nil {
panic(err)
}
}
type MetricSet struct {
mb.BaseMetricSet
Client *vim25.Client
}
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
cfgwarn.Beta("The vsphere datastore metricset is beta")
config := struct {
Username string `config:"username"`
Password string `config:"password"`
Insecure bool `config:"insecure"`
}{}
if err := base.Module().UnpackConfig(&config); err != nil {
return nil, err
}
u, err := url.Parse(base.HostData().URI)
if err != nil {
return nil, err
}
u.User = url.UserPassword(config.Username, config.Password)
c, err := govmomi.NewClient(context.TODO(), u, config.Insecure)
if err != nil {
return nil, err
}
return &MetricSet{
BaseMetricSet: base,
Client: c.Client,
}, nil
}
func (m *MetricSet) Fetch() ([]common.MapStr, error) {
f := find.NewFinder(m.Client, true)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Get all data centers.
dcs, err := f.DatacenterList(ctx, "*")
if err != nil {
return nil, err
}
var events []common.MapStr
for _, dc := range dcs {
f.SetDatacenter(dc)
dss, err := f.DatastoreList(ctx, "*")
if err != nil {
return nil, errors.Wrap(err, "failed to get datastore list")
}
pc := property.DefaultCollector(m.Client)
// Convert datastores into list of references.
var refs []types.ManagedObjectReference
for _, ds := range dss {
refs = append(refs, ds.Reference())
}
// Retrieve summary property.
var dst []mo.Datastore
err = pc.Retrieve(ctx, refs, []string{"summary"}, &dst)
if err != nil {
return nil, err
}
for _, ds := range dst {
var usedSpacePercent int64
if ds.Summary.Capacity > 0 {
usedSpacePercent = 100 * (ds.Summary.Capacity - ds.Summary.FreeSpace) / ds.Summary.Capacity
}
usedSpaceBytes := ds.Summary.Capacity - ds.Summary.FreeSpace
event := common.MapStr{
"datacenter": dc.Name(),
"name": ds.Summary.Name,
"fstype": ds.Summary.Type,
"capacity": common.MapStr{
"total": common.MapStr{
"bytes": ds.Summary.Capacity,
},
"free": common.MapStr{
"bytes": ds.Summary.FreeSpace,
},
"used": common.MapStr{
"bytes": usedSpaceBytes,
"pct": usedSpacePercent,
},
},
}
events = append(events, event)
}
}
return events, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。