Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ltpinellia committed Mar 28, 2019
1 parent c82a951 commit 1d33ad2
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 90 deletions.
9 changes: 4 additions & 5 deletions cron/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
"sync"
"time"

"github.com/vmware/govmomi/vim25/mo"

"github.com/vmware/govmomi"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vsphere/g"
)

//Collect 监控项采集
//Collect collect metrics
func Collect(ctx context.Context, c *govmomi.Client, cfg *g.VsphereConfig) {
stime := time.Now().Unix()

Expand All @@ -21,14 +20,14 @@ func Collect(ctx context.Context, c *govmomi.Client, cfg *g.VsphereConfig) {
}

var wg sync.WaitGroup
g.DsWithURL(ctx, c)
dsWURL := g.DsWURL()
mapperEsxi := g.EsxiMappers()
mapperVS := g.VsphereMappers()
esxiList := g.EsxiList(ctx, c)

g.ReportVCStatus(cfg)
g.CounterWithID(ctx, c)
g.DsWithURL(ctx, c)
dsWURL := g.DsWURL()

for _, v := range mapperVS {
wg.Add(1)
Expand Down
2 changes: 1 addition & 1 deletion g/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/vmware/govmomi"
)

//AgentMetrics agent存活监控
//AgentMetrics agent status
func AgentMetrics(ctx context.Context, c *govmomi.Client) []*MetricValue {
return []*MetricValue{GaugeValue("agent.alive", 1)}
}
24 changes: 12 additions & 12 deletions g/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import (
"time"
)

//HeartbeatConfig Hbs配置
//HeartbeatConfig hbs configuration
type HeartbeatConfig struct {
Enabled bool `json:"enabled"`
Addr string `json:"addr"`
Timeout int `json:"timeout"`
}

//TransferConfig Transfer配置
//TransferConfig transfer configuration
type TransferConfig struct {
Enabled bool `json:"enabled"`
Addr []string `json:"addrs"`
Interval int `json:"interval"`
Timeout int `json:"timeout"`
}

//VsphereConfig Vsphere配置
//VsphereConfig vsphere configuration
type VsphereConfig struct {
Hostname string `json:"hostname"`
IP string `json:"ip"`
Expand All @@ -38,7 +38,7 @@ type VsphereConfig struct {
Extend bool `json:"extend"`
}

//GlobalConfig 全局配置
//GlobalConfig global configuration
type GlobalConfig struct {
Debug bool `json:"debug"`
Extend string `json:"extend"`
Expand All @@ -47,7 +47,7 @@ type GlobalConfig struct {
Vsphere []*VsphereConfig `json:"vsphere"`
}

//ExtendConfig 扩展监控项配置
//ExtendConfig extend configuration
type ExtendConfig struct {
Hbr []string `json:"hbr"`
Rescpu []string `json:"rescpu"`
Expand All @@ -66,25 +66,25 @@ var (
config *GlobalConfig
extend *ExtendConfig
lock = new(sync.RWMutex)
//ConfigFile 全局配置
//ConfigFile global config file
ConfigFile string
)

//Config 获取全局配置
//Config get global config
func Config() *GlobalConfig {
lock.RLock()
defer lock.RUnlock()
return config
}

//Extend 获取扩展配置
//Extend get extend config
func Extend() *ExtendConfig {
lock.RLock()
defer lock.RUnlock()
return extend
}

//ParseConfig 解析全局配置
//ParseConfig parse global config
func ParseConfig(cfg string) {
if cfg == "" {
Log.Fatalln("[cfg.go] use -c to specify configuration file")
Expand Down Expand Up @@ -123,7 +123,7 @@ func ParseConfig(cfg string) {
Log.Info("[cfg.go] read global config file: ", cfg, " successfully!")
}

//ReloadConfig 重载全局配置
//ReloadConfig monitor and reload global config
func ReloadConfig(cfg string) {
t := time.NewTicker(time.Second * 5)
defer t.Stop()
Expand All @@ -139,7 +139,7 @@ func ReloadConfig(cfg string) {
}
}

//ParseExtendConfig 解析扩展配置
//ParseExtendConfig parse extend config
func ParseExtendConfig(cfg string) {
if cfg == "" {
Log.Fatalln("[cfg.go] extend config file:", cfg, "is not existent.")
Expand Down Expand Up @@ -173,7 +173,7 @@ func ParseExtendConfig(cfg string) {
Log.Info("[cfg.go] read extend config file: ", cfg, " successfully!")
}

//ReloadExtendConfig 重载扩展配置
//ReloadExtendConfig monitor and reload extend config
func ReloadExtendConfig(cfg string) {
t := time.NewTicker(time.Second * 5)
defer t.Stop()
Expand Down
30 changes: 10 additions & 20 deletions g/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package g

import (
"context"
"os"
"regexp"
"strings"
"time"
Expand All @@ -11,16 +10,7 @@ import (
"github.com/vmware/govmomi/performance"
)

//InitRootDir 初始化主目录
func InitRootDir() {
var err error
Root, err = os.Getwd()
if err != nil {
Log.Fatalln("[common.go] getwd fail: ", err)
}
}

//InitVCIP 初始化Vsphere IP
//InitVCIP get vc ip
func InitVCIP(vc *VsphereConfig) string {
var ip string
if vc.IP != "" {
Expand All @@ -35,7 +25,7 @@ func InitVCIP(vc *VsphereConfig) string {
return ip
}

//InitRPCClients 初始化rpc客户端
//InitRPCClients initialize rpc client
func InitRPCClients() {
if Config().Heartbeat.Enabled {
HbsClient = &SingleConnRPCClient{
Expand All @@ -45,7 +35,7 @@ func InitRPCClients() {
}
}

//NewMetricValue 返回新的metric对象,主要修改了tags跟type
//NewMetricValue decorate metric object,return new metric with tags
func NewMetricValue(metric string, val interface{}, dataType string, tags ...string) *MetricValue {
mv := MetricValue{
Metric: metric,
Expand All @@ -62,12 +52,12 @@ func NewMetricValue(metric string, val interface{}, dataType string, tags ...str
return &mv
}

//GaugeValue Gauge型监控项
//GaugeValue Gauge type
func GaugeValue(metric string, val interface{}, tags ...string) *MetricValue {
return NewMetricValue(metric, val, "GAUGE", tags...)
}

//CounterValue Counter型监控项
//CounterValue Counter type
func CounterValue(metric string, val interface{}, tags ...string) *MetricValue {
return NewMetricValue(metric, val, "COUNTER", tags...)
}
Expand All @@ -77,21 +67,21 @@ var (
dsWUrl *[]DatastoreWithURL
)

//CoID 返回esxi Performance监控项名及其Key之间的映射,类似:disk.io.write_requests:524
//CoID return all esxi Performance。example:disk.io.write_requests:524
func CoID() map[string]int32 {
lock.RLock()
defer lock.RUnlock()
return *coID
}

//DsWURL 存储器ID与存储器命名之间的映射
//DsWURL return dsWUrl
func DsWURL() *[]DatastoreWithURL {
lock.RLock()
defer lock.RUnlock()
return dsWUrl
}

//CounterWithID 获取所有的counter列表(Key:counter name,Value:counter id)
//CounterWithID get all counter list(Key:counter name,Value:counter id)
func CounterWithID(ctx context.Context, c *govmomi.Client) {
CounterNameID := make(map[string]int32)
m := performance.NewManager(c.Client)
Expand All @@ -105,7 +95,7 @@ func CounterWithID(ctx context.Context, c *govmomi.Client) {
coID = &CounterNameID
}

//DsWithURL 存储器命名与存储器ID的映射
//DsWithURL get all datastore with URL
func DsWithURL(ctx context.Context, c *govmomi.Client) {
dss := datastores(ctx, c)
datastoreWithURL := []DatastoreWithURL{}
Expand All @@ -117,7 +107,7 @@ func DsWithURL(ctx context.Context, c *govmomi.Client) {
dsWUrl = &datastoreWithURL
}

//CounterIDByName 通过counter列表获取Key列表
//CounterIDByName get counter key by counter name
func CounterIDByName(CounterNameID map[string]int32, Name []string) []int32 {
IDList := make([]int32, 0)
for _, eachName := range Name {
Expand Down
36 changes: 18 additions & 18 deletions g/esxi.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/vmware/govmomi/vim25/mo"
)

//EsxiList 用于获取VSphere下所有的esxi
//EsxiList get esxi list
func EsxiList(ctx context.Context, c *govmomi.Client) []mo.HostSystem {
m := view.NewManager(c.Client)
var esxiList []mo.HostSystem
Expand All @@ -29,13 +29,13 @@ func EsxiList(ctx context.Context, c *govmomi.Client) []mo.HostSystem {
return esxiList
}

//esxiAlive 电源状态
//esxiAlive power status
func esxiPower(ctx context.Context, c *govmomi.Client, esxi mo.HostSystem, dsWURL *[]DatastoreWithURL) []*MetricValue {
/*
poweredOff:关机
poweredOn:开机
standBy:待机
unknown:主机断开连接或者无响应时被标记为未知
1.0: poweredOff
2.0: poweredOn
3.0: standBy
4.0: unknown
*/
switch esxi.Summary.Runtime.PowerState {
case "poweredOff":
Expand All @@ -51,13 +51,13 @@ func esxiPower(ctx context.Context, c *govmomi.Client, esxi mo.HostSystem, dsWUR
}
}

//esxiStatus 主机状态
//esxiStatus The Status enumeration defines a general "health" value for a managed entity.
func esxiStatus(ctx context.Context, c *govmomi.Client, esxi mo.HostSystem, dsWURL *[]DatastoreWithURL) []*MetricValue {
/*
gray:状态未知
green:实体没问题
red:实体肯定有问题
yellow:实体可能有问题
1.0: gray,The status is unknown.
2.0: green,The entity is OK.
3.0: red,The entity definitely has a problem.
4.0: yellow,The entity might have a problem.
*/
switch esxi.Summary.OverallStatus {
case "gray":
Expand All @@ -73,12 +73,12 @@ func esxiStatus(ctx context.Context, c *govmomi.Client, esxi mo.HostSystem, dsWU
}
}

//esxiUptime 开机时间
//esxiUptime uptime
func esxiUptime(ctx context.Context, c *govmomi.Client, esxi mo.HostSystem, dsWURL *[]DatastoreWithURL) []*MetricValue {
return []*MetricValue{GaugeValue("agent.uptime", esxi.Summary.QuickStats.Uptime)}
}

//esxiCPU CPU相关监控信息
//esxiCPU cpu metrics
func esxiCPU(ctx context.Context, c *govmomi.Client, esxi mo.HostSystem, dsWURL *[]DatastoreWithURL) []*MetricValue {
var total = int64(esxi.Summary.Hardware.CpuMhz) * int64(esxi.Summary.Hardware.NumCpuCores)
totalCPU := GaugeValue("cpu.total", total*1000*1000)
Expand All @@ -88,7 +88,7 @@ func esxiCPU(ctx context.Context, c *govmomi.Client, esxi mo.HostSystem, dsWURL
return []*MetricValue{totalCPU, freeCPU, useCPU, usePercentCPU}
}

//esxiMem 内存相关监控信息
//esxiMem mem metrics
func esxiMem(ctx context.Context, c *govmomi.Client, esxi mo.HostSystem, dsWURL *[]DatastoreWithURL) []*MetricValue {
var total = esxi.Summary.Hardware.MemorySize
var free = int64(esxi.Summary.Hardware.MemorySize) - (int64(esxi.Summary.QuickStats.OverallMemoryUsage) * 1024 * 1024)
Expand All @@ -100,7 +100,7 @@ func esxiMem(ctx context.Context, c *govmomi.Client, esxi mo.HostSystem, dsWURL
return []*MetricValue{totalMem, useMem, freeMem, freeMemPer, usedMemPer}
}

//esxiNet 网络相关监控信息
//esxiNet net metrics
func esxiNet(ctx context.Context, c *govmomi.Client, esxi mo.HostSystem, dsWURL *[]DatastoreWithURL) []*MetricValue {
var netPerf []*MetricValue
counterNameID := CoID()
Expand All @@ -121,7 +121,7 @@ func esxiNet(ctx context.Context, c *govmomi.Client, esxi mo.HostSystem, dsWURL
return netPerf
}

//esxiDatastore 存储相关监控信息
//esxiDatastore datastore metrics
func esxiDatastore(ctx context.Context, c *govmomi.Client, esxi mo.HostSystem, dsWURL *[]DatastoreWithURL) []*MetricValue {
var datastorePerf []*MetricValue
counterNameID := CoID()
Expand Down Expand Up @@ -150,7 +150,7 @@ func esxiDatastore(ctx context.Context, c *govmomi.Client, esxi mo.HostSystem, d
return datastorePerf
}

//esxiDisk 磁盘使用空间
//esxiDisk disk metrics
func esxiDisk(ctx context.Context, c *govmomi.Client, esxi mo.HostSystem, dsWURL *[]DatastoreWithURL) []*MetricValue {
var diskPerf []*MetricValue
pc := property.DefaultCollector(c.Client)
Expand Down Expand Up @@ -190,7 +190,7 @@ func esxiDisk(ctx context.Context, c *govmomi.Client, esxi mo.HostSystem, dsWURL
return diskPerf
}

//EsxiMappers Esxi的Map对象
//EsxiMappers Esxi's mapper object
func EsxiMappers() []EFuncsAndInterval {
interval := Config().Transfer.Interval
mappers := []EFuncsAndInterval{
Expand Down
2 changes: 1 addition & 1 deletion g/extend.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func extendMem(ctx context.Context, c *govmomi.Client, esxi mo.HostSystem, dsWUR
return
}

//EsxiExtendMappers EsxiExtend的Map对象
//EsxiExtendMappers EsxiExtend's mapper status
func EsxiExtendMappers() []EFuncsAndInterval {
interval := Config().Transfer.Interval
mappers := []EFuncsAndInterval{
Expand Down
2 changes: 1 addition & 1 deletion g/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/sirupsen/logrus"
)

// InitLog 初始化log
//InitLog initialize logger
func InitLog(level string) (err error) {
switch level {
case "info":
Expand Down
3 changes: 1 addition & 2 deletions g/performance.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (
"github.com/vmware/govmomi/vim25/types"
)

// Performance 用于根据传入的counterID获取Counter值
// Performance get counter value by counter key
func Performance(ctx context.Context, c *govmomi.Client, MOR types.ManagedObjectReference, counterID int32) ([]*MetricPerf, error) {
pm := performance.NewManager(c.Client)
var pQS = perfQuerySpec(MOR, counterID)
//查询key对应的ID
counterKVL, err := pm.Query(ctx, pQS)
var metricPerf = make([]*MetricPerf, 0)

Expand Down
3 changes: 1 addition & 2 deletions g/reporter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package g

// ReportVCStatus 用于上报VC状态
// ReportVCStatus report vc status
func ReportVCStatus(vc *VsphereConfig, hostname ...string) {
if Config().Heartbeat.Enabled && Config().Heartbeat.Addr != "" {
go reportVCStatus(vc, hostname...)
Expand All @@ -11,7 +11,6 @@ func reportVCStatus(vc *VsphereConfig, hostname ...string) {
var ip string
var plugin string
var sHostName string
//插件版本(是否监控扩展监控项)
if vc.Extend {
plugin = VERSION
} else {
Expand Down
Loading

0 comments on commit 1d33ad2

Please sign in to comment.