Hi I am have multiple vms reporting free disk space on each VM. Currently there are 4 VMs reporting this. To calculate total free disk space of all 4 VM I am able to write individual query assign those to seperate variables and then join all for of them.
The issue is, if I want to add 5th VM or remove one VM. I have to modify the query. Is there a way to do this automate this part? I am writing the queries in Flux.
vm1 = from(bucket: “<bucket-name>”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "memory")
|> filter(fn: (r) => r["_field"] == "memfree")
|> filter(fn: (r) => r["host"] == “vm1”)
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> yield(name: "mean")
vm2 = from(bucket: “<bucket-name>”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "memory")
|> filter(fn: (r) => r["_field"] == "memfree")
|> filter(fn: (r) => r["host"] == “vm2”)
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> yield(name: "mean")
vm3 = from(bucket: “<bucket-name>”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "memory")
|> filter(fn: (r) => r["_field"] == "memfree")
|> filter(fn: (r) => r["host"] == “vm3”)
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> yield(name: "mean")
vm4 = from(bucket: “<bucket-name>”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "memory")
|> filter(fn: (r) => r["_field"] == "memfree")
|> filter(fn: (r) => r["host"] == “vm4”)
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> yield(name: "mean")
|> join(tables: {vm1:vm1, vm2:vm2, vm3:vm3, vm4:vm4}, on: ["_time", "_stop", "_start", “_field”, “_measurment”])
|> map(fn: (r) => ({
_time: r._time,
_field: r._field,
_measurement: r._measurement
_value: r._value_vm1 + r._value_vm2 + r._value_vm3 + r._value_vm4
})
)