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
# 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
*.swp

View File

@ -60,6 +60,7 @@ type InfluxDB struct {
Username string
Password string
Database string
Prefix string
}
// VCenter for VMware vCenter connections
@ -106,7 +107,7 @@ func (vcenter *VCenter) Connect() error {
ctx, cancel := context.WithCancel(context.Background())
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")
if err != nil {
errlog.Println("Could not parse vcenter url:", vcenter.Hostname)
@ -133,7 +134,7 @@ func (vcenter *VCenter) Disconnect() error {
if vcenter.client != 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
}
}
@ -200,7 +201,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
var viewManager mo.ViewManager
err := client.RetrieveOne(ctx, *client.ServiceContent.ViewManager, nil, &viewManager)
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)
return
}
@ -209,16 +210,16 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
var rootFolder mo.Folder
err = client.RetrieveOne(ctx, client.ServiceContent.RootFolder, nil, &rootFolder)
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)
return
}
datacenters := []types.ManagedObjectReference{}
for _, child := range rootFolder.ChildEntity {
if child.Type == "Datacenter" {
//if child.Type == "Datacenter" {
datacenters = append(datacenters, child)
}
//}
}
// Get intresting object types from specified queries
objectTypes := []string{}
@ -236,7 +237,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
req := types.CreateContainerView{This: viewManager.Reference(), Container: datacenter, Type: objectTypes, Recursive: true}
res, err := methods.CreateContainerView(ctx, client.RoundTripper, &req)
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)
continue
}
@ -244,7 +245,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
var containerView mo.ContainerView
err = client.RetrieveOne(ctx, res.Returnval, nil, &containerView)
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)
continue
}
@ -344,7 +345,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
// Retrieve properties for ResourcePools
if len(respoolRefs) > 0 {
if debug {
stdlog.Println("going inside ResourcePools")
stdlog.Println("Going inside ResourcePools")
}
err = pc.Retrieve(ctx, respoolRefs, []string{"name", "config", "vm"}, &respool)
if err != nil {
@ -352,11 +353,11 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
return
}
for _, pool := range respool {
stdlog.Println(pool.Config.MemoryAllocation.GetResourceAllocationInfo().Limit)
stdlog.Println(pool.Config.CpuAllocation.GetResourceAllocationInfo().Limit)
if debug {
stdlog.Println("---resourcepool name - you should see every resourcepool here (+VMs inside)----")
stdlog.Println(pool.Name)
stdlog.Println(pool.Config.MemoryAllocation.GetResourceAllocationInfo().Limit)
stdlog.Println(pool.Config.CpuAllocation.GetResourceAllocationInfo().Limit)
}
for _, vm := range pool.Vm {
if debug {
@ -377,7 +378,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
// Retrieve properties for clusters, if any
if len(clusterRefs) > 0 {
if debug {
stdlog.Println("going inside clusters")
stdlog.Println("Going inside clusters")
}
// Step 1 : Get ObjectContents and Host info for VM
@ -468,7 +469,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
propreq := types.RetrieveProperties{SpecSet: []types.PropertyFilterSpec{{ObjectSet: objectSet, PropSet: []types.PropertySpec{*propSpec}}}}
propres, err := client.PropertyCollector().RetrieveProperties(ctx, propreq)
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)
return
}
@ -476,7 +477,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
//load retrieved properties
err = mo.LoadRetrievePropertiesResponse(propres, &objects)
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)
return
}
@ -524,7 +525,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
perfreq := types.QueryPerf{This: *client.ServiceContent.PerfManager, QuerySpec: queries}
perfres, err := methods.QueryPerf(ctx, client.RoundTripper, &perfreq)
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)
return
}
@ -635,7 +636,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
}
//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 {
errlog.Println(err)
continue
@ -645,7 +646,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
for measurement, v := range specialFields {
for name, metric := range v {
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 {
errlog.Println(err)
continue
@ -668,7 +669,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
"memory_limit": pool.Config.MemoryAllocation.GetResourceAllocationInfo().Limit,
}
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 {
errlog.Println(err)
continue
@ -682,7 +683,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
"free_space": datastore.Summary.FreeSpace,
}
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 {
errlog.Println(err)
continue
@ -699,7 +700,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
return
}
stdlog.Println("sent data to Influxdb")
stdlog.Println("Sent data to Influxdb from:", vcenter.Hostname)
}
func min(n ...int64) int64 {
@ -785,7 +786,7 @@ func main() {
flag.BoolVar(&debug, "debug", false, "Debug mode")
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()
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-02.dc-02.lab" },
{ "Username": "monitoring", "Password": "monixx", "Hostname": "vcenter-01.home.lab" }
],
"InfluxDB": {
"Prefix": "vsphere_",
"Hostname": "http://influxdb-01.dc-01.lab:8086",
"Username": "vm",
"Password": "vmware334",
"Database": "vmware_performance"
},
"Metrics": [
{
"ObjectType": [ "VirtualMachine", "HostSystem" ],
@ -50,12 +52,14 @@
{ "Metric": "cpu.corecount.provisioned.average", "Instances": "*" }
]
},
{
"ObjectType": [ "VirtualMachine" ],
"Definition": [
{ "Metric": "datastore.datastoreVMObservedLatency.latest", "Instances": "*" }
]
},
{
"ObjectType": [ "HostSystem" ],
"Definition": [
@ -65,6 +69,7 @@
{ "Metric": "net.throughput.contention.summation", "Instances": "*" }
]
},
{
"ObjectType": [ "Datastore" ],
"Definition": [