From 3442d30e1a2ee6006013309db54c9f613484e17d Mon Sep 17 00:00:00 2001 From: Hela Drira Date: Wed, 3 May 2017 14:23:19 +0200 Subject: [PATCH 1/9] defined ResourcePool as metric --- vsphere-influxdb-go.json | 11 +++++++++-- vsphere-influxdb.go | 21 ++++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/vsphere-influxdb-go.json b/vsphere-influxdb-go.json index 411dc9b..84c05ac 100644 --- a/vsphere-influxdb-go.json +++ b/vsphere-influxdb-go.json @@ -16,7 +16,7 @@ }, "Metrics": [ { - "ObjectType": [ "VirtualMachine", "HostSystem" ], + "ObjectType": [ "VirtualMachine", "HostSystem", "ResourcePool" ], "Definition": [ { "Metric": "cpu.usage.average", "Instances": "*" }, { "Metric": "cpu.usage.maximum", "Instances": "*" }, @@ -64,6 +64,13 @@ { "Metric": "disk.numberWriteAveraged.average", "Instances": "*" }, { "Metric": "net.throughput.contention.summation", "Instances": "*" } ] - } + }, + { "ObjectType": [ "ResourcePool" ], + "Definition": [ + { "Metric": "cpu.usagemhz.average", "Instances": "*" }, + { "Metric": "mem.consumed.average", "Instances": "*" } + ] + } + ] } diff --git a/vsphere-influxdb.go b/vsphere-influxdb.go index a338aaa..71f72dd 100644 --- a/vsphere-influxdb.go +++ b/vsphere-influxdb.go @@ -246,6 +246,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. vm_refs := []types.ManagedObjectReference{} host_refs := []types.ManagedObjectReference{} cluster_refs := []types.ManagedObjectReference{} + resp_refs := []types.ManagedObjectReference{} new_mors := []types.ManagedObjectReference{} @@ -258,9 +259,15 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. } else if mor.Type == "HostSystem" { host_refs = append(host_refs, mor) new_mors = append(new_mors, mor) - } else if mor.Type == "ClusterComputeResource" { + } + else if mor.Type == "ResourcePool" { + resp_refs = append(resp_refs, mor) + new_mors = append(new_mors, mor) + } + else if mor.Type == "ClusterComputeResource" { cluster_refs = append(cluster_refs, mor) } + } // Copy the mors without the clusters mors = new_mors @@ -282,6 +289,13 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. fmt.Println(err) return } + // Retrieve properties for all resourcepools + var respmo []mo.ResourcePool + err = pc.Retrieve(ctx, resp_refs, []string{"summary"}, &respmo) + if err != nil { + fmt.Println(err) + return + } // Initialize the map that will hold the VM MOR to cluster reference vmToCluster := make(map[types.ManagedObjectReference]string) @@ -490,8 +504,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. if special_fields[measurementName] == nil { special_fields[measurementName] = make(map[string]map[string]map[string]interface{}) special_tags[measurementName] = make(map[string]map[string]map[string]string) - - } + } if special_fields[measurementName][tags["name"]] == nil { special_fields[measurementName][tags["name"]] = make(map[string]map[string]interface{}) @@ -651,3 +664,5 @@ func main() { queryVCenter(*vcenter, config, InfluxDBClient) } } + + From 7f61a40f1c4a25c272409e42dc115c72779ba37c Mon Sep 17 00:00:00 2001 From: Hela Drira Date: Wed, 3 May 2017 15:52:20 +0200 Subject: [PATCH 2/9] syntax correction --- vsphere-influxdb.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/vsphere-influxdb.go b/vsphere-influxdb.go index 71f72dd..c3ff034 100644 --- a/vsphere-influxdb.go +++ b/vsphere-influxdb.go @@ -21,8 +21,6 @@ import ( "encoding/json" "flag" "fmt" - "github.com/davecgh/go-spew/spew" - "golang.org/x/net/context" "log" "math" "net/url" @@ -32,6 +30,9 @@ import ( "strings" "time" + "github.com/davecgh/go-spew/spew" + "golang.org/x/net/context" + influxclient "github.com/influxdata/influxdb/client/v2" "github.com/vmware/govmomi" "github.com/vmware/govmomi/property" @@ -259,15 +260,13 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. } else if mor.Type == "HostSystem" { host_refs = append(host_refs, mor) new_mors = append(new_mors, mor) - } - else if mor.Type == "ResourcePool" { + } else if mor.Type == "ResourcePool" { resp_refs = append(resp_refs, mor) new_mors = append(new_mors, mor) - } - else if mor.Type == "ClusterComputeResource" { + } else if mor.Type == "ClusterComputeResource" { cluster_refs = append(cluster_refs, mor) } - + } // Copy the mors without the clusters mors = new_mors @@ -504,7 +503,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. if special_fields[measurementName] == nil { special_fields[measurementName] = make(map[string]map[string]map[string]interface{}) special_tags[measurementName] = make(map[string]map[string]map[string]string) - } + } if special_fields[measurementName][tags["name"]] == nil { special_fields[measurementName][tags["name"]] = make(map[string]map[string]interface{}) @@ -664,5 +663,3 @@ func main() { queryVCenter(*vcenter, config, InfluxDBClient) } } - - From 781d50a12b8edfe7b49d4a4f4df65c0085e16d99 Mon Sep 17 00:00:00 2001 From: Hela Drira Date: Thu, 4 May 2017 17:14:37 +0200 Subject: [PATCH 3/9] "Change ResourcePool configuration " --- vsphere-influxdb-go.json | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/vsphere-influxdb-go.json b/vsphere-influxdb-go.json index 84c05ac..55124d1 100644 --- a/vsphere-influxdb-go.json +++ b/vsphere-influxdb-go.json @@ -65,10 +65,15 @@ { "Metric": "net.throughput.contention.summation", "Instances": "*" } ] }, - { "ObjectType": [ "ResourcePool" ], + { + "ObjectType": [ "ResourcePool" ], "Definition": [ { "Metric": "cpu.usagemhz.average", "Instances": "*" }, - { "Metric": "mem.consumed.average", "Instances": "*" } + { "Metric": "cpu.cpuentitlement.latest", "Instances": "*" }, + { "Metric": "mem.mementitlement.latest", "Instances": "*" }, + { "Metric": "mem.consumed.average", "Instances": "*" }, + { "Metric": "mem.granted.average", "Instances": "*" }, + { "Metric": "mem.active.average", "Instances": "*" } ] } From b16cc794cae6a912c1409067360d230b5f0a76cd Mon Sep 17 00:00:00 2001 From: Hela Drira Date: Thu, 4 May 2017 17:52:45 +0200 Subject: [PATCH 4/9] test --- vsphere-influxdb.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/vsphere-influxdb.go b/vsphere-influxdb.go index c3ff034..c0ed833 100644 --- a/vsphere-influxdb.go +++ b/vsphere-influxdb.go @@ -289,12 +289,12 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. return } // Retrieve properties for all resourcepools - var respmo []mo.ResourcePool - err = pc.Retrieve(ctx, resp_refs, []string{"summary"}, &respmo) - if err != nil { - fmt.Println(err) - return - } + //var respmo []mo.ResourcePool + //err = pc.Retrieve(ctx, resp_refs, []string{"summary"}, &respmo) + //if err != nil { + // fmt.Println(err) + //return + //} // Initialize the map that will hold the VM MOR to cluster reference vmToCluster := make(map[types.ManagedObjectReference]string) From 86b401fae0f548c04d57700530a57497f0e4a74f Mon Sep 17 00:00:00 2001 From: Hela Drira Date: Thu, 4 May 2017 17:55:47 +0200 Subject: [PATCH 5/9] test 2 --- vsphere-influxdb.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vsphere-influxdb.go b/vsphere-influxdb.go index c0ed833..eda11a9 100644 --- a/vsphere-influxdb.go +++ b/vsphere-influxdb.go @@ -632,7 +632,7 @@ func main() { stdlog.Println("Starting :", path.Base(os.Args[0])) // read the configuration - file, err := os.Open("/etc/" + path.Base(os.Args[0]) + ".json") + file, err := os.Open(path.Base(os.Args[0]) + ".json") if err != nil { errlog.Println("Could not open configuration file") errlog.Println(err) From 84d9145c939b0b9341fd06be1402dbc3f28821a4 Mon Sep 17 00:00:00 2001 From: Hela Drira Date: Thu, 4 May 2017 18:00:32 +0200 Subject: [PATCH 6/9] test3 --- vsphere-influxdb.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/vsphere-influxdb.go b/vsphere-influxdb.go index eda11a9..becc54e 100644 --- a/vsphere-influxdb.go +++ b/vsphere-influxdb.go @@ -247,7 +247,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. vm_refs := []types.ManagedObjectReference{} host_refs := []types.ManagedObjectReference{} cluster_refs := []types.ManagedObjectReference{} - resp_refs := []types.ManagedObjectReference{} + //resp_refs := []types.ManagedObjectReference{} new_mors := []types.ManagedObjectReference{} @@ -260,9 +260,9 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. } else if mor.Type == "HostSystem" { host_refs = append(host_refs, mor) new_mors = append(new_mors, mor) - } else if mor.Type == "ResourcePool" { - resp_refs = append(resp_refs, mor) - new_mors = append(new_mors, mor) + //} else if mor.Type == "ResourcePool" { + // resp_refs = append(resp_refs, mor) + // new_mors = append(new_mors, mor) } else if mor.Type == "ClusterComputeResource" { cluster_refs = append(cluster_refs, mor) } From 893e1fead9ee5d9857e0b7640a4340d262db2475 Mon Sep 17 00:00:00 2001 From: Hela Drira Date: Mon, 8 May 2017 09:25:03 +0200 Subject: [PATCH 7/9] "Final Version with ResourcePool" --- vsphere-influxdb-go.json | 18 +++----------- vsphere-influxdb.go | 51 ++++++++++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/vsphere-influxdb-go.json b/vsphere-influxdb-go.json index 55124d1..9421ddc 100644 --- a/vsphere-influxdb-go.json +++ b/vsphere-influxdb-go.json @@ -16,7 +16,7 @@ }, "Metrics": [ { - "ObjectType": [ "VirtualMachine", "HostSystem", "ResourcePool" ], + "ObjectType": [ "VirtualMachine", "HostSystem" ], "Definition": [ { "Metric": "cpu.usage.average", "Instances": "*" }, { "Metric": "cpu.usage.maximum", "Instances": "*" }, @@ -64,18 +64,6 @@ { "Metric": "disk.numberWriteAveraged.average", "Instances": "*" }, { "Metric": "net.throughput.contention.summation", "Instances": "*" } ] - }, - { - "ObjectType": [ "ResourcePool" ], - "Definition": [ - { "Metric": "cpu.usagemhz.average", "Instances": "*" }, - { "Metric": "cpu.cpuentitlement.latest", "Instances": "*" }, - { "Metric": "mem.mementitlement.latest", "Instances": "*" }, - { "Metric": "mem.consumed.average", "Instances": "*" }, - { "Metric": "mem.granted.average", "Instances": "*" }, - { "Metric": "mem.active.average", "Instances": "*" } - ] - } - + } ] -} +} \ No newline at end of file diff --git a/vsphere-influxdb.go b/vsphere-influxdb.go index becc54e..aee6333 100644 --- a/vsphere-influxdb.go +++ b/vsphere-influxdb.go @@ -220,6 +220,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. objectTypes = append(objectTypes, group.ObjectType) } objectTypes = append(objectTypes, "ClusterComputeResource") + objectTypes = append(objectTypes, "ResourcePool") // Loop trought datacenters and create the intersting object reference list mors := []types.ManagedObjectReference{} @@ -247,7 +248,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. vm_refs := []types.ManagedObjectReference{} host_refs := []types.ManagedObjectReference{} cluster_refs := []types.ManagedObjectReference{} - //resp_refs := []types.ManagedObjectReference{} + respool_refs := []types.ManagedObjectReference{} new_mors := []types.ManagedObjectReference{} @@ -260,13 +261,11 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. } else if mor.Type == "HostSystem" { host_refs = append(host_refs, mor) new_mors = append(new_mors, mor) - //} else if mor.Type == "ResourcePool" { - // resp_refs = append(resp_refs, mor) - // new_mors = append(new_mors, mor) } else if mor.Type == "ClusterComputeResource" { cluster_refs = append(cluster_refs, mor) + } else if mor.Type == "ResourcePool" { + respool_refs = append(respool_refs, mor) } - } // Copy the mors without the clusters mors = new_mors @@ -288,13 +287,35 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. fmt.Println(err) return } - // Retrieve properties for all resourcepools - //var respmo []mo.ResourcePool - //err = pc.Retrieve(ctx, resp_refs, []string{"summary"}, &respmo) - //if err != nil { - // fmt.Println(err) - //return - //} + + // Initialize the map that will hold the VM MOR to ResourcePool reference + vmToPool := make(map[types.ManagedObjectReference]string) + + // Retrieve properties for ResourcePools + if len(cluster_refs) > 0 { + if debug == true { + stdlog.Println("going inside ResourcePools") + } + var respool []mo.ResourcePool + err = pc.Retrieve(ctx, respool_refs, []string{"name", "config", "vm"}, &respool) + if err != nil { + fmt.Println(err) + return + } + for _, pool := range respool { + if debug == true { + stdlog.Println("---resourcepool name - you should see every resourcepool here (+VMs inside)----") + stdlog.Println(pool.Name) + } + for _, vm := range pool.Vm { + if debug == true { + stdlog.Println("--VM ID - you should see every VM ID here--") + stdlog.Println(vm) + } + vmToPool[vm] = pool.Name + } + } + } // Initialize the map that will hold the VM MOR to cluster reference vmToCluster := make(map[types.ManagedObjectReference]string) @@ -356,6 +377,9 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. if vmToCluster[vm.Self] != "" { vm_summary[vm.Self]["cluster"] = vmToCluster[vm.Self] } + if vmToPool[vm.Self] != "" { + vm_summary[vm.Self]["respool"] = vmToPool[vm.Self] + } vm_summary[vm.Self]["esx"] = host_summary[*vm.Summary.Runtime.Host]["name"] } @@ -503,6 +527,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. if special_fields[measurementName] == nil { special_fields[measurementName] = make(map[string]map[string]map[string]interface{}) special_tags[measurementName] = make(map[string]map[string]map[string]string) + } if special_fields[measurementName][tags["name"]] == nil { @@ -624,7 +649,7 @@ func queryVCenter(vcenter VCenter, config Configuration, InfluxDBClient influxcl func main() { - flag.BoolVar(&debug, "debug", false, "Debug mode") + flag.BoolVar(&debug, "debug", true, "Debug mode") flag.Parse() stdlog = log.New(os.Stdout, "", log.Ldate|log.Ltime) From 9ce89212b86a1338bd4d65a95573a57fd59d873e Mon Sep 17 00:00:00 2001 From: tsi Date: Tue, 9 May 2017 10:08:07 +0200 Subject: [PATCH 8/9] Final Version with ResourcePool --- vsphere-influxdb.go | 52 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/vsphere-influxdb.go b/vsphere-influxdb.go index aee6333..f5d2226 100644 --- a/vsphere-influxdb.go +++ b/vsphere-influxdb.go @@ -288,11 +288,19 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. return } + //Retrieve properties for ResourcePool + var rpmo []mo.ResourcePool + err = pc.Retrieve(ctx, respool_refs, []string{"summary"}, &rpmo) + if err != nil { + fmt.Println(err) + return + } + // Initialize the map that will hold the VM MOR to ResourcePool reference vmToPool := make(map[types.ManagedObjectReference]string) // Retrieve properties for ResourcePools - if len(cluster_refs) > 0 { + if len(respool_refs) > 0 { if debug == true { stdlog.Println("going inside ResourcePools") } @@ -351,6 +359,13 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. } } + // Retrieve properties for the pools + respool_summary := make(map[types.ManagedObjectReference]map[string]string) + for _, pools := range rpmo { + respool_summary[pools.Self] = make(map[string]string) + respool_summary[pools.Self]["name"] = pools.Summary.GetResourcePoolSummary().Name + } + // Retrieve properties for the hosts host_summary := make(map[types.ManagedObjectReference]map[string]string) host_extra_metrics := make(map[types.ManagedObjectReference]map[string]int64) @@ -493,6 +508,12 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. } } + if summary, ok := respool_summary[pem.Entity]; ok { + for key, tag := range summary { + tags[key] = tag + } + } + special_fields := make(map[string]map[string]map[string]map[string]interface{}) special_tags := make(map[string]map[string]map[string]map[string]string) nowTime := time.Now() @@ -575,6 +596,26 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. } } } + + var respool []mo.ResourcePool + err = pc.Retrieve(ctx, respool_refs, []string{"name", "config", "vm"}, &respool) + if err != nil { + fmt.Println(err) + return + } + for _, pool := range respool { + respoolFields := map[string]interface{}{ + "cpu_limit": pool.Config.CpuAllocation.GetResourceAllocationInfo().Limit, + "memory_limit": pool.Config.MemoryAllocation.GetResourceAllocationInfo().Limit, + } + respoolTags := map[string]string{"pool_name": pool.Name} + pt3, err := influxclient.NewPoint("resourcepool", respoolTags, respoolFields, time.Now()) + if err != nil { + errlog.Println(err) + } + bp.AddPoint(pt3) + } + } //InfluxDB send err = InfluxDBClient.Write(bp) @@ -649,7 +690,8 @@ func queryVCenter(vcenter VCenter, config Configuration, InfluxDBClient influxcl func main() { - flag.BoolVar(&debug, "debug", true, "Debug mode") + flag.BoolVar(&debug, "debug", false, "Debug mode") + var cfgFile = flag.String("config", "/etc/"+path.Base(os.Args[0])+".json", "Config file to use. Default is /etc/"+path.Base(os.Args[0])+".json") flag.Parse() stdlog = log.New(os.Stdout, "", log.Ldate|log.Ltime) @@ -657,16 +699,16 @@ func main() { stdlog.Println("Starting :", path.Base(os.Args[0])) // read the configuration - file, err := os.Open(path.Base(os.Args[0]) + ".json") + file, err := os.Open(*cfgFile) if err != nil { - errlog.Println("Could not open configuration file") + errlog.Println("Could not open configuration file " + *cfgFile) errlog.Println(err) } jsondec := json.NewDecoder(file) config := Configuration{} err = jsondec.Decode(&config) if err != nil { - errlog.Println("Could not decode configuration file") + errlog.Println("Could not decode configuration file " + *cfgFile) errlog.Println(err) } From c60748773f8b658cb74446eaa222a5afa814f5ad Mon Sep 17 00:00:00 2001 From: tsi Date: Thu, 11 May 2017 11:38:33 +0200 Subject: [PATCH 9/9] Added ResourcePool information: --- vsphere-influxdb.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/vsphere-influxdb.go b/vsphere-influxdb.go index f5d2226..f0c5b83 100644 --- a/vsphere-influxdb.go +++ b/vsphere-influxdb.go @@ -606,7 +606,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. for _, pool := range respool { respoolFields := map[string]interface{}{ "cpu_limit": pool.Config.CpuAllocation.GetResourceAllocationInfo().Limit, - "memory_limit": pool.Config.MemoryAllocation.GetResourceAllocationInfo().Limit, + "memory_limit": MBToKB(pool.Config.MemoryAllocation.GetResourceAllocationInfo().Limit), } respoolTags := map[string]string{"pool_name": pool.Name} pt3, err := influxclient.NewPoint("resourcepool", respoolTags, respoolFields, time.Now()) @@ -627,6 +627,13 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient. } +func MBToKB(n int64) int64 { + if n != -1 { + n = 1000 * n + } + return n +} + func min(n ...int64) int64 { var min int64 = -1 for _, i := range n { @@ -691,7 +698,7 @@ func queryVCenter(vcenter VCenter, config Configuration, InfluxDBClient influxcl func main() { flag.BoolVar(&debug, "debug", false, "Debug mode") - var cfgFile = flag.String("config", "/etc/"+path.Base(os.Args[0])+".json", "Config file to use. Default is /etc/"+path.Base(os.Args[0])+".json") + var cfgFile = flag.String("config","/etc/" + path.Base(os.Args[0])+".json", "Config file to use. Default is /etc/"+path.Base(os.Args[0])+".json") flag.Parse() stdlog = log.New(os.Stdout, "", log.Ldate|log.Ltime)