Skip to content

Commit

Permalink
Merge pull request #20 from adongy/master
Browse files Browse the repository at this point in the history
Fix potentially incorrect data points
  • Loading branch information
sofixa committed May 24, 2017
2 parents d7b9379 + eba6b58 commit 7e53ec6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 45 deletions.
Binary file removed debug
Binary file not shown.
97 changes: 52 additions & 45 deletions vsphere-influxdb.go
@@ -1,18 +1,17 @@
/* Copyright 2016 Adrian Todorov, Oxalide ato@oxalide.com
/* Copyright 2016 Adrian Todorov, Oxalide ato@oxalide.com
Original project author: https://github.com/cblomart
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package main
Expand All @@ -31,14 +30,13 @@ import (
"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"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
"golang.org/x/net/context"
)

const (
Expand Down Expand Up @@ -79,9 +77,6 @@ type MetricDef struct {
Key int32
}

var vmRefs []types.ManagedObjectReference
var debug bool

// Metric is used for metrics retrieval
type Metric struct {
ObjectType []string
Expand All @@ -102,50 +97,54 @@ type EntityQuery struct {
Metrics []int32
}

// A few global variables
var dependencies = []string{}

var debug bool
var stdlog, errlog *log.Logger

// Connect to the actual vCenter connection used to query data
func (vcenter *VCenter) Connect() (*govmomi.Client, error) {
// Prepare vCenter Connections
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

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)
errlog.Println("Error: ", err)
return nil, err
}

client, err := govmomi.NewClient(ctx, u, true)
if err != nil {
errlog.Println("Could not connect to vcenter: ", vcenter.Hostname)
errlog.Println("Error: ", err)
return nil, err
}

return client, nil
}

// Init the VCenter connection
func (vcenter *VCenter) Init(config Configuration) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

client, err := vcenter.Connect()
if err != nil {
errlog.Println("Could not connect to vcenter: ", vcenter.Hostname)
errlog.Println("Error: ", err)
return
}
defer client.Logout(ctx)

var perfmanager mo.PerformanceManager
err = client.RetrieveOne(ctx, *client.ServiceContent.PerfManager, nil, &perfmanager)
if err != nil {
errlog.Println("Could not get performance manager")
errlog.Println("Error: ", err)
return
}

for _, perf := range perfmanager.PerfCounter {
groupinfo := perf.GroupInfo.GetElementDescription()
nameinfo := perf.NameInfo.GetElementDescription()
Expand Down Expand Up @@ -245,8 +244,8 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
// Add found object to object list
mors = append(mors, containerView.View...)
}
// Create MORS for each object type

// Create MORS for each object type
vmRefs := []types.ManagedObjectReference{}
hostRefs := []types.ManagedObjectReference{}
clusterRefs := []types.ManagedObjectReference{}
Expand All @@ -264,12 +263,12 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
hostRefs = append(hostRefs, mor)
newMors = append(newMors, mor)
} else if mor.Type == "ClusterComputeResource" {
clusterRefs = append(cluster_refs, mor)
clusterRefs = append(clusterRefs, mor)
} else if mor.Type == "ResourcePool" {
respoolRefs = append(respool_refs, mor)
respoolRefs = append(respoolRefs, mor)
}
}
// Copy the mors without the clusters
// Copy the mors without the clusters
mors = newMors

pc := property.DefaultCollector(client.Client)
Expand Down Expand Up @@ -307,7 +306,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
stdlog.Println("going inside ResourcePools")
}
var respool []mo.ResourcePool
err = pc.Retrieve(ctx, respool_refs, []string{"name", "config", "vm"}, &respool)
err = pc.Retrieve(ctx, respoolRefs, []string{"name", "config", "vm"}, &respool)
if err != nil {
fmt.Println(err)
return
Expand Down Expand Up @@ -352,6 +351,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
spew.Dump(cl.Configuration)
spew.Dump(cl.Configuration.DasVmConfig)
}

for _, vm := range cl.Configuration.DasVmConfig {
if debug == true {
stdlog.Println("--VM ID - you should see every VM ID here--")
Expand All @@ -364,10 +364,10 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
}

// Retrieve properties for the pools
respool_summary := make(map[types.ManagedObjectReference]map[string]string)
respoolSummary := 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
respoolSummary[pools.Self] = make(map[string]string)
respoolSummary[pools.Self]["name"] = pools.Summary.GetResourcePoolSummary().Name
}

// Retrieve properties for the hosts
Expand Down Expand Up @@ -487,6 +487,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
})
if err != nil {
errlog.Println(err)
return
}

for _, base := range perfres.Returnval {
Expand All @@ -512,7 +513,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
}
}

if summary, ok := respool_summary[pem.Entity]; ok {
if summary, ok := respoolSummary[pem.Entity]; ok {
for key, tag := range summary {
tags[key] = tag
}
Expand Down Expand Up @@ -572,7 +573,6 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
specialTags[measurementName][tags["name"]][instanceName][k] = v
}
specialTags[measurementName][tags["name"]][instanceName]["instance"] = instanceName

}
}

Expand All @@ -586,6 +586,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
pt, err := influxclient.NewPoint(entityName, tags, fields, nowTime)
if err != nil {
errlog.Println(err)
continue
}
bp.AddPoint(pt)

Expand All @@ -595,18 +596,20 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
pt2, err := influxclient.NewPoint(measurement, specialTags[measurement][name][instance], value, time.Now())
if err != nil {
errlog.Println(err)
continue
}
bp.AddPoint(pt2)
}
}
}

var respool []mo.ResourcePool
err = pc.Retrieve(ctx, respool_refs, []string{"name", "config", "vm"}, &respool)
err = pc.Retrieve(ctx, respoolRefs, []string{"name", "config", "vm"}, &respool)
if err != nil {
fmt.Println(err)
return
errlog.Println(err)
continue
}

for _, pool := range respool {
respoolFields := map[string]interface{}{
"cpu_limit": pool.Config.CpuAllocation.GetResourceAllocationInfo().Limit,
Expand All @@ -616,19 +619,21 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
pt3, err := influxclient.NewPoint("resourcepool", respoolTags, respoolFields, time.Now())
if err != nil {
errlog.Println(err)
continue
}
bp.AddPoint(pt3)
}

}

//InfluxDB send
err = InfluxDBClient.Write(bp)
if err != nil {
errlog.Println(err)
} else {
stdlog.Println("sent data to Influxdb")
return
}

stdlog.Println("sent data to Influxdb")
}

func min(n ...int64) int64 {
Expand Down Expand Up @@ -689,47 +694,49 @@ func average(n ...int64) int64 {
func queryVCenter(vcenter VCenter, config Configuration, InfluxDBClient influxclient.Client) {
stdlog.Println("Querying vcenter")
vcenter.Query(config, InfluxDBClient)

}

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)
errlog = log.New(os.Stderr, "", log.Ldate|log.Ltime)

stdlog.Println("Starting :", path.Base(os.Args[0]))

// read the configuration
file, err := os.Open(*cfgFile)
if err != nil {
errlog.Println("Could not open configuration file " + *cfgFile)
errlog.Println(err)
errlog.Println("Could not open configuration file", *cfgFile)
errlog.Fatalln(err)
}

jsondec := json.NewDecoder(file)
config := Configuration{}
err = jsondec.Decode(&config)
if err != nil {
errlog.Println("Could not decode configuration file " + *cfgFile)
errlog.Println(err)

errlog.Println("Could not decode configuration file", *cfgFile)
errlog.Fatalln(err)
}

for _, vcenter := range config.VCenters {
vcenter.Init(config)
}

InfluxDBClient, err := influxclient.NewHTTPClient(influxclient.HTTPConfig{
Addr: config.InfluxDB.Hostname,
Username: config.InfluxDB.Username,
Password: config.InfluxDB.Password,
})
if err != nil {
errlog.Println("Could not connect to InfluxDB")
errlog.Println(err)
} else {
stdlog.Println("Successfully connected to Influx")
errlog.Fatalln(err)
}

stdlog.Println("Successfully connected to Influx")

for _, vcenter := range config.VCenters {
queryVCenter(*vcenter, config, InfluxDBClient)
}
Expand Down

0 comments on commit 7e53ec6

Please sign in to comment.