1 Star 0 Fork 0

sqos/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
datastore.go 2.78 KB
一键复制 编辑 原始数据 按行查看 历史
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqos/beats.git
git@gitee.com:sqos/beats.git
sqos
beats
beats
v6.1.2

搜索帮助