Telegraf inputs vSphere: Host memory total and usage available?

Hi there,
I’m using telegraf with inputs.vsphere plugin.
I’m writing the data to an influxdb2 database and showing the data using Grafana (VMware vSphere - Overview | Grafana Labs) .
All is working well, but I’m missing an information which I don’t know if present or not.
Hope this is the best place to ask.
Looking on the data present on the hosts (ESXi monitored), I can find the usage percent of the memory, but not the total available (in KB i.e.) and the total used. The reason is, that, having the information as percent, does not tell me how much free memory I have. It’s possible that 10% of free memory could be a lot of GB or, in the other way, very low memory. This is useful to accomplish capacity needs or requests on the hosts.
Is this info available or are there suggestions?
Thanks!
Simon

Hello @xefil,
You can collect a wide variety of metrics with the vsphere plugin see here:

The mem.usage.average metric gives you the percentage of memory usage. The mem.totalCapacity.average should give you the total memory capacity of the host. However, note that the exact naming of metrics might vary depending on the vSphere version and how metrics are exposed by VMware. You might need to adjust these metric names based on the available metrics in your vSphere environment.

Here’s an example of using flux to calculate the percentage:

// Define the range of time you're interested in
timeRangeStart = -1h
timeRangeStop = now()

// Fetch memory usage percentage
memUsage = from(bucket: "your-bucket")
  |> range(start: timeRangeStart, stop: timeRangeStop)
  |> filter(fn: (r) => 
    r._measurement == "your-measurement" and 
    r._field == "mem.usage.average"
  )
  |> last() // Gets the last recorded value; adjust based on your needs

// Fetch total memory capacity
memTotalCapacity = from(bucket: "your-bucket")
  |> range(start: timeRangeStart, stop: timeRangeStop)
  |> filter(fn: (r) => 
    r._measurement == "your-measurement" and 
    r._field == "mem.totalCapacity.average"
  )
  |> last() // Gets the last recorded value; adjust based on your needs

// Join the streams on the tags you want to match, typically host or instance
// Adjust the tag keys according to your data schema
join(tables: {usage: memUsage, capacity: memTotalCapacity}, on: ["_time", "host"])
  |> map(fn: (r) => ({
      _time: r._time,
      _value: (r._value_usage / r._value_capacity) * 100, // Calculate the percentage
      host: r.host // Include other relevant tags as needed
    })
  )

You could also use the starlark or execd processor plugins to do this work:

I think it would look something like:

[[processors.starlark]]
  ## The Starlark script for processing metrics.
  source = '''
def apply(metric):
    # Ensure both mem_usage and mem_total are present
    mem_usage = metric.fields.get('mem_usage')
    mem_total = metric.fields.get('mem_total')

    if mem_usage == None or mem_total == None or mem_total == 0:
        return None  # Skip the metric if any value is missing or mem_total is 0

    # Calculate the percentage of used memory
    mem_percent = (mem_usage / mem_total) * 100

    # Update the metric with the new field
    metric.fields['mem_percent'] = mem_percent

    return metric
'''

  ## Name a tag to add to the metric.
  namepass = ["vsphere_host_mem"]

Though I haven’t tested it.