1
0
mirror of https://github.com/Oxalide/vsphere-influxdb-go.git synced 2023-10-10 13:36:51 +02:00

The tool was not able to gather info from the datacenters in nested folders under the root folder - fixed, added config parameter to specify prefix for measurement, cleaned up logging formatting

This commit is contained in:
Artashes Arabajyan 2018-01-03 21:06:41 +01:00
parent 942b49d61e
commit 8b519e50c8
3 changed files with 847 additions and 853 deletions

14
.gitignore vendored
View File

@ -1,13 +1 @@
### Basic ignore file *.swp
# Binaries for programs and plugins
vsphere-influxdb
# Test binary, build with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Configuration file
vsphere-influxdb.json

View File

@ -60,6 +60,7 @@ type InfluxDB struct {
Username string Username string
Password string Password string
Database string Database string
Prefix string
} }
// VCenter for VMware vCenter connections // VCenter for VMware vCenter connections
@ -106,18 +107,18 @@ func (vcenter *VCenter) Connect() error {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
stdlog.Println("connecting to vcenter: " + vcenter.Hostname) stdlog.Println("Connecting to vcenter:", vcenter.Hostname)
u, err := url.Parse("https://" + vcenter.Username + ":" + vcenter.Password + "@" + vcenter.Hostname + "/sdk") u, err := url.Parse("https://" + vcenter.Username + ":" + vcenter.Password + "@" + vcenter.Hostname + "/sdk")
if err != nil { if err != nil {
errlog.Println("Could not parse vcenter url: ", vcenter.Hostname) errlog.Println("Could not parse vcenter url:", vcenter.Hostname)
errlog.Println("Error: ", err) errlog.Println("Error:", err)
return err return err
} }
client, err := govmomi.NewClient(ctx, u, true) client, err := govmomi.NewClient(ctx, u, true)
if err != nil { if err != nil {
errlog.Println("Could not connect to vcenter: ", vcenter.Hostname) errlog.Println("Could not connect to vcenter:", vcenter.Hostname)
errlog.Println("Error: ", err) errlog.Println("Error:", err)
return err return err
} }
@ -133,7 +134,7 @@ func (vcenter *VCenter) Disconnect() error {
if vcenter.client != nil { if vcenter.client != nil {
if err := vcenter.client.Logout(ctx); err != nil { if err := vcenter.client.Logout(ctx); err != nil {
errlog.Println("Could not disconnect properly from vcenter", vcenter.Hostname, err) errlog.Println("Could not disconnect properly from vcenter:", vcenter.Hostname, err)
return err return err
} }
} }
@ -152,7 +153,7 @@ func (vcenter *VCenter) Init(config Configuration) error {
err := client.RetrieveOne(ctx, *client.ServiceContent.PerfManager, nil, &perfmanager) err := client.RetrieveOne(ctx, *client.ServiceContent.PerfManager, nil, &perfmanager)
if err != nil { if err != nil {
errlog.Println("Could not get performance manager") errlog.Println("Could not get performance manager")
errlog.Println("Error: ", err) errlog.Println("Error:", err)
return err return err
} }
@ -187,7 +188,7 @@ func (vcenter *VCenter) Init(config Configuration) error {
// Query a vcenter // Query a vcenter
func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.Client, nowTime time.Time) { func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.Client, nowTime time.Time) {
stdlog.Println("Setting up query inventory of vcenter: ", vcenter.Hostname) stdlog.Println("Setting up query inventory of vcenter:", vcenter.Hostname)
// Create the contect // Create the contect
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
@ -200,7 +201,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
var viewManager mo.ViewManager var viewManager mo.ViewManager
err := client.RetrieveOne(ctx, *client.ServiceContent.ViewManager, nil, &viewManager) err := client.RetrieveOne(ctx, *client.ServiceContent.ViewManager, nil, &viewManager)
if err != nil { if err != nil {
errlog.Println("Could not get view manager from vcenter: " + vcenter.Hostname) errlog.Println("Could not get view manager from vcenter:", vcenter.Hostname)
errlog.Println("Error: ", err) errlog.Println("Error: ", err)
return return
} }
@ -209,16 +210,16 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
var rootFolder mo.Folder var rootFolder mo.Folder
err = client.RetrieveOne(ctx, client.ServiceContent.RootFolder, nil, &rootFolder) err = client.RetrieveOne(ctx, client.ServiceContent.RootFolder, nil, &rootFolder)
if err != nil { if err != nil {
errlog.Println("Could not get root folder from vcenter: " + vcenter.Hostname) errlog.Println("Could not get root folder from vcenter:", vcenter.Hostname)
errlog.Println("Error: ", err) errlog.Println("Error:", err)
return return
} }
datacenters := []types.ManagedObjectReference{} datacenters := []types.ManagedObjectReference{}
for _, child := range rootFolder.ChildEntity { for _, child := range rootFolder.ChildEntity {
if child.Type == "Datacenter" { //if child.Type == "Datacenter" {
datacenters = append(datacenters, child) datacenters = append(datacenters, child)
} //}
} }
// Get intresting object types from specified queries // Get intresting object types from specified queries
objectTypes := []string{} objectTypes := []string{}
@ -236,16 +237,16 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
req := types.CreateContainerView{This: viewManager.Reference(), Container: datacenter, Type: objectTypes, Recursive: true} req := types.CreateContainerView{This: viewManager.Reference(), Container: datacenter, Type: objectTypes, Recursive: true}
res, err := methods.CreateContainerView(ctx, client.RoundTripper, &req) res, err := methods.CreateContainerView(ctx, client.RoundTripper, &req)
if err != nil { if err != nil {
errlog.Println("Could not create container view from vcenter: " + vcenter.Hostname) errlog.Println("Could not create container view from vcenter:", vcenter.Hostname)
errlog.Println("Error: ", err) errlog.Println("Error:", err)
continue continue
} }
// Retrieve the created ContentView // Retrieve the created ContentView
var containerView mo.ContainerView var containerView mo.ContainerView
err = client.RetrieveOne(ctx, res.Returnval, nil, &containerView) err = client.RetrieveOne(ctx, res.Returnval, nil, &containerView)
if err != nil { if err != nil {
errlog.Println("Could not get container view from vcenter: " + vcenter.Hostname) errlog.Println("Could not get container view from vcenter:", vcenter.Hostname)
errlog.Println("Error: ", err) errlog.Println("Error:", err)
continue continue
} }
// Add found object to object list // Add found object to object list
@ -344,7 +345,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
// Retrieve properties for ResourcePools // Retrieve properties for ResourcePools
if len(respoolRefs) > 0 { if len(respoolRefs) > 0 {
if debug { if debug {
stdlog.Println("going inside ResourcePools") stdlog.Println("Going inside ResourcePools")
} }
err = pc.Retrieve(ctx, respoolRefs, []string{"name", "config", "vm"}, &respool) err = pc.Retrieve(ctx, respoolRefs, []string{"name", "config", "vm"}, &respool)
if err != nil { if err != nil {
@ -352,11 +353,11 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
return return
} }
for _, pool := range respool { for _, pool := range respool {
stdlog.Println(pool.Config.MemoryAllocation.GetResourceAllocationInfo().Limit)
stdlog.Println(pool.Config.CpuAllocation.GetResourceAllocationInfo().Limit)
if debug { if debug {
stdlog.Println("---resourcepool name - you should see every resourcepool here (+VMs inside)----") stdlog.Println("---resourcepool name - you should see every resourcepool here (+VMs inside)----")
stdlog.Println(pool.Name) stdlog.Println(pool.Name)
stdlog.Println(pool.Config.MemoryAllocation.GetResourceAllocationInfo().Limit)
stdlog.Println(pool.Config.CpuAllocation.GetResourceAllocationInfo().Limit)
} }
for _, vm := range pool.Vm { for _, vm := range pool.Vm {
if debug { if debug {
@ -377,7 +378,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
// Retrieve properties for clusters, if any // Retrieve properties for clusters, if any
if len(clusterRefs) > 0 { if len(clusterRefs) > 0 {
if debug { if debug {
stdlog.Println("going inside clusters") stdlog.Println("Going inside clusters")
} }
// Step 1 : Get ObjectContents and Host info for VM // Step 1 : Get ObjectContents and Host info for VM
@ -468,16 +469,16 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
propreq := types.RetrieveProperties{SpecSet: []types.PropertyFilterSpec{{ObjectSet: objectSet, PropSet: []types.PropertySpec{*propSpec}}}} propreq := types.RetrieveProperties{SpecSet: []types.PropertyFilterSpec{{ObjectSet: objectSet, PropSet: []types.PropertySpec{*propSpec}}}}
propres, err := client.PropertyCollector().RetrieveProperties(ctx, propreq) propres, err := client.PropertyCollector().RetrieveProperties(ctx, propreq)
if err != nil { if err != nil {
errlog.Println("Could not retrieve object names from vcenter: " + vcenter.Hostname) errlog.Println("Could not retrieve object names from vcenter:", vcenter.Hostname)
errlog.Println("Error: ", err) errlog.Println("Error:", err)
return return
} }
//load retrieved properties //load retrieved properties
err = mo.LoadRetrievePropertiesResponse(propres, &objects) err = mo.LoadRetrievePropertiesResponse(propres, &objects)
if err != nil { if err != nil {
errlog.Println("Could not retrieve object names from vcenter: " + vcenter.Hostname) errlog.Println("Could not retrieve object names from vcenter:", vcenter.Hostname)
errlog.Println("Error: ", err) errlog.Println("Error:", err)
return return
} }
@ -524,8 +525,8 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
perfreq := types.QueryPerf{This: *client.ServiceContent.PerfManager, QuerySpec: queries} perfreq := types.QueryPerf{This: *client.ServiceContent.PerfManager, QuerySpec: queries}
perfres, err := methods.QueryPerf(ctx, client.RoundTripper, &perfreq) perfres, err := methods.QueryPerf(ctx, client.RoundTripper, &perfreq)
if err != nil { if err != nil {
errlog.Println("Could not request perfs from vcenter: " + vcenter.Hostname) errlog.Println("Could not request perfs from vcenter:", vcenter.Hostname)
errlog.Println("Error: ", err) errlog.Println("Error:", err)
return return
} }
@ -635,7 +636,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
} }
//create InfluxDB points //create InfluxDB points
pt, err := influxclient.NewPoint(entityName, tags, fields, nowTime) pt, err := influxclient.NewPoint(config.InfluxDB.Prefix + entityName, tags, fields, nowTime)
if err != nil { if err != nil {
errlog.Println(err) errlog.Println(err)
continue continue
@ -645,7 +646,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
for measurement, v := range specialFields { for measurement, v := range specialFields {
for name, metric := range v { for name, metric := range v {
for instance, value := range metric { for instance, value := range metric {
pt2, err := influxclient.NewPoint(measurement, specialTags[measurement][name][instance], value, time.Now()) pt2, err := influxclient.NewPoint(config.InfluxDB.Prefix + measurement, specialTags[measurement][name][instance], value, time.Now())
if err != nil { if err != nil {
errlog.Println(err) errlog.Println(err)
continue continue
@ -668,7 +669,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
"memory_limit": pool.Config.MemoryAllocation.GetResourceAllocationInfo().Limit, "memory_limit": pool.Config.MemoryAllocation.GetResourceAllocationInfo().Limit,
} }
respoolTags := map[string]string{"pool_name": pool.Name} respoolTags := map[string]string{"pool_name": pool.Name}
pt3, err := influxclient.NewPoint("resourcepool", respoolTags, respoolFields, time.Now()) pt3, err := influxclient.NewPoint(config.InfluxDB.Prefix + "resourcepool", respoolTags, respoolFields, time.Now())
if err != nil { if err != nil {
errlog.Println(err) errlog.Println(err)
continue continue
@ -682,7 +683,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
"free_space": datastore.Summary.FreeSpace, "free_space": datastore.Summary.FreeSpace,
} }
datastoreTags := map[string]string{"ds_name": datastore.Summary.Name, "host": vcName} datastoreTags := map[string]string{"ds_name": datastore.Summary.Name, "host": vcName}
pt4, err := influxclient.NewPoint("datastore", datastoreTags, datastoreFields, time.Now()) pt4, err := influxclient.NewPoint(config.InfluxDB.Prefix + "datastore", datastoreTags, datastoreFields, time.Now())
if err != nil { if err != nil {
errlog.Println(err) errlog.Println(err)
continue continue
@ -699,7 +700,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
return return
} }
stdlog.Println("sent data to Influxdb") stdlog.Println("Sent data to Influxdb from:", vcenter.Hostname)
} }
func min(n ...int64) int64 { func min(n ...int64) int64 {
@ -785,7 +786,7 @@ func main() {
flag.BoolVar(&debug, "debug", false, "Debug mode") flag.BoolVar(&debug, "debug", false, "Debug mode")
workerCount := flag.Int("workers", 4, "Number of concurrent workers to query vcenters") workerCount := flag.Int("workers", 4, "Number of concurrent workers to query vcenters")
cfgFile := flag.String("config", "/etc/"+baseName+".json", "Config file to use. Default is /etc/"+baseName+".json") cfgFile := flag.String("config", "/etc/"+baseName+".json", "Config file to use")
flag.Parse() flag.Parse()
stdlog.Println("Starting", baseName, "with config file", *cfgFile) stdlog.Println("Starting", baseName, "with config file", *cfgFile)

View File

@ -6,14 +6,16 @@
{ "Username": "monitoring", "Password": "monixx", "Hostname": "vcenter-01.dc-02.lab" }, { "Username": "monitoring", "Password": "monixx", "Hostname": "vcenter-01.dc-02.lab" },
{ "Username": "monitoring", "Password": "monixx", "Hostname": "vcenter-02.dc-02.lab" }, { "Username": "monitoring", "Password": "monixx", "Hostname": "vcenter-02.dc-02.lab" },
{ "Username": "monitoring", "Password": "monixx", "Hostname": "vcenter-01.home.lab" } { "Username": "monitoring", "Password": "monixx", "Hostname": "vcenter-01.home.lab" }
], ],
"InfluxDB": { "InfluxDB": {
"Prefix": "vsphere_",
"Hostname": "http://influxdb-01.dc-01.lab:8086", "Hostname": "http://influxdb-01.dc-01.lab:8086",
"Username": "vm", "Username": "vm",
"Password": "vmware334", "Password": "vmware334",
"Database": "vmware_performance" "Database": "vmware_performance"
}, },
"Metrics": [ "Metrics": [
{ {
"ObjectType": [ "VirtualMachine", "HostSystem" ], "ObjectType": [ "VirtualMachine", "HostSystem" ],
@ -50,12 +52,14 @@
{ "Metric": "cpu.corecount.provisioned.average", "Instances": "*" } { "Metric": "cpu.corecount.provisioned.average", "Instances": "*" }
] ]
}, },
{ {
"ObjectType": [ "VirtualMachine" ], "ObjectType": [ "VirtualMachine" ],
"Definition": [ "Definition": [
{ "Metric": "datastore.datastoreVMObservedLatency.latest", "Instances": "*" } { "Metric": "datastore.datastoreVMObservedLatency.latest", "Instances": "*" }
] ]
}, },
{ {
"ObjectType": [ "HostSystem" ], "ObjectType": [ "HostSystem" ],
"Definition": [ "Definition": [
@ -65,6 +69,7 @@
{ "Metric": "net.throughput.contention.summation", "Instances": "*" } { "Metric": "net.throughput.contention.summation", "Instances": "*" }
] ]
}, },
{ {
"ObjectType": [ "Datastore" ], "ObjectType": [ "Datastore" ],
"Definition": [ "Definition": [