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

InfluxDB and vSphere can be configured using environemt variables

This commit is contained in:
Mike Liebsch 2017-09-07 11:13:54 +02:00
parent ff4a561165
commit 7c4478c435
2 changed files with 74 additions and 45 deletions

View File

@ -36,6 +36,10 @@ $GOBIN/vsphere-influxdb-go
# Configure # Configure
You'll need a JSON file with all your vCenters/ESXi to connect to, the InfluxDB connection details(url, username/password, database to use), and the metrics to collect. You'll need a JSON file with all your vCenters/ESXi to connect to, the InfluxDB connection details(url, username/password, database to use), and the metrics to collect.
Additionally you can provide a vCenter/ESXi server and InfluxDB connection details via environment variables, wich is extremly helpful when running inside a container.
For InfluxDB set INFLUX\_HOSTNAME, INFLUX\_USERNAME, INFLUX\_PASSWORD and INFLUX\_DATABASE.
For vSphere set VSPHERE\_HOSTNAME, VSPHERE\_USERNAME and VSPHERE\_PASSWORD and keep in mind, that currently only one vCenter/ESXi can be added via environment variable.
If you set a domain, it will be automaticaly removed from the names of the found objects. If you set a domain, it will be automaticaly removed from the names of the found objects.

View File

@ -274,7 +274,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
datastoreRefs = append(datastoreRefs, mor) datastoreRefs = append(datastoreRefs, mor)
} }
} }
// Copy the mors without the clusters // Copy the mors without the clusters
mors = newMors mors = newMors
pc := property.DefaultCollector(client.Client) pc := property.DefaultCollector(client.Client)
@ -294,7 +294,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
fmt.Println(err) fmt.Println(err)
return return
} }
//Retrieve properties for Cluster(s) //Retrieve properties for Cluster(s)
var clmo []mo.ClusterComputeResource var clmo []mo.ClusterComputeResource
err = pc.Retrieve(ctx, clusterRefs, []string{"name", "configuration", "host"}, &clmo) err = pc.Retrieve(ctx, clusterRefs, []string{"name", "configuration", "host"}, &clmo)
@ -302,7 +302,7 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
fmt.Println(err) fmt.Println(err)
return return
} }
//Retrieve properties for ResourcePool //Retrieve properties for ResourcePool
var rpmo []mo.ResourcePool var rpmo []mo.ResourcePool
err = pc.Retrieve(ctx, respoolRefs, []string{"summary"}, &rpmo) err = pc.Retrieve(ctx, respoolRefs, []string{"summary"}, &rpmo)
@ -311,14 +311,14 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
return return
} }
// Retrieve summary property for all datastores // Retrieve summary property for all datastores
var dss []mo.Datastore var dss []mo.Datastore
err = pc.Retrieve(ctx, datastoreRefs, []string{"summary"}, &dss) err = pc.Retrieve(ctx, datastoreRefs, []string{"summary"}, &dss)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
return return
} }
// Initialize the map that will hold the VM MOR to ResourcePool reference // Initialize the map that will hold the VM MOR to ResourcePool reference
vmToPool := make(map[types.ManagedObjectReference]string) vmToPool := make(map[types.ManagedObjectReference]string)
@ -355,37 +355,37 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
// Initialize the map that will hold the VM MOR to cluster reference // Initialize the map that will hold the VM MOR to cluster reference
hostToCluster := make(map[types.ManagedObjectReference]string) hostToCluster := make(map[types.ManagedObjectReference]string)
// Retrieve properties for clusters, if any // Retrieve properties for clusters, if any
if len(clusterRefs) > 0 { if len(clusterRefs) > 0 {
if debug == true { if debug == true {
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
// The host is found under the runtime structure. // The host is found under the runtime structure.
// Step 2 : Step 2: Get the ManagedObjectReference from the Host we just got.
// Step 3 : Get a list all the clusters that vCenter knows about, and for each one, also get the host
// Step 4 : Loop through all clusters that exist (which we got in step 3), and loop through each host
// and see if that host matches the host we got in step 2 as the host of the vm.
// If we find it, return it, otherwise we return null.
for _, vm := range vmmo { // Step 2 : Step 2: Get the ManagedObjectReference from the Host we just got.
vmhost := vm.Summary.Runtime.Host
for _, cl := range clmo { // Step 3 : Get a list all the clusters that vCenter knows about, and for each one, also get the host
for _, host := range cl.Host {
hostToCluster[host] = cl.Name // Step 4 : Loop through all clusters that exist (which we got in step 3), and loop through each host
// and see if that host matches the host we got in step 2 as the host of the vm.
if *vmhost == host { // If we find it, return it, otherwise we return null.
vmToCluster[vm.Self] = cl.Name
} for _, vm := range vmmo {
} vmhost := vm.Summary.Runtime.Host
}
} for _, cl := range clmo {
for _, host := range cl.Host {
hostToCluster[host] = cl.Name
if *vmhost == host {
vmToCluster[vm.Self] = cl.Name
}
}
}
}
} }
@ -653,18 +653,18 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
} }
for _, datastore := range dss { for _, datastore := range dss {
datastoreFields := map[string]interface{}{ datastoreFields := map[string]interface{}{
"capacity": datastore.Summary.Capacity, "capacity": datastore.Summary.Capacity,
"free_space": datastore.Summary.FreeSpace, "free_space": datastore.Summary.FreeSpace,
} }
datastoreTags := map[string]string{"ds_name": datastore.Summary.Name} datastoreTags := map[string]string{"ds_name": datastore.Summary.Name}
pt4, err := influxclient.NewPoint("datastore", datastoreTags, datastoreFields, time.Now()) pt4, err := influxclient.NewPoint("datastore", datastoreTags, datastoreFields, time.Now())
if err != nil { if err != nil {
errlog.Println(err) errlog.Println(err)
continue continue
} }
bp.AddPoint(pt4) bp.AddPoint(pt4)
} }
} }
@ -763,6 +763,31 @@ func main() {
errlog.Fatalln(err) errlog.Fatalln(err)
} }
// Support environemt variables / overrides for Influx Connection
if ihostname := os.Getenv("INFLUX_HOSTNAME"); ihostname != "" {
config.InfluxDB.Hostname = os.Getenv("INFLUX_HOSTNAME")
config.InfluxDB.Username = os.Getenv("INFLUX_USERNAME")
config.InfluxDB.Password = os.Getenv("INFLUX_PASSWORD")
config.InfluxDB.Database = os.Getenv("INFLUX_DATABASE")
}
// Support environment variables for VSphere
// Currently ony one server is supported and added to the list of vSphere servers
if vhostname := os.Getenv("VSPHERE_HOSTNAME"); vhostname != "" {
vc := VCenter{
Hostname: os.Getenv("VSPHERE_HOSTNAME"),
Username: os.Getenv("VSPHERE_USERNAME"),
Password: os.Getenv("VSPHERE_PASSWORD"),
}
config.VCenters = append(config.VCenters, &vc)
}
// Print configuration in debug mode
if debug == true {
stdlog.Println("---Configuration - you should see the config here---")
spew.Dump(config)
}
for _, vcenter := range config.VCenters { for _, vcenter := range config.VCenters {
vcenter.Init(config) vcenter.Init(config)
} }