Foglight

How to create a Derived Metric (to count powered on VMs)

Quest Solution Architect Brian Wheeldon here with a quick tutorial about creating derived metrics.

Derived metrics are calculated on the FMS. They typically augment metrics collected by various agents.

Once calculated, derived metrics are first-class citizens of the Foglight model and can be used in rules, dashboards and reports.

A Foglight Community member wanted to chart the number of powered on VMs in a particular time period.

He noted that this metric is available out of the box for ESX Hosts and Clusters, but this metric is not aggregated up to the Data Center level.

In order to define a derived metric, we need:

  1. a scope, or place to store the metric
  2. a calculation to define it
  3. a policy to determine when the metric is calculated

To implement this requirement, the scope is the Data Center object, or 'VMWDatacenter'.

The calculation is based on the fact that VMWDatacenter has a field called 'virtualMachines' which contains a list of all the VMs in the Data Center, and each VMWVirtualMachine has a String Observation field called 'vmState' whose value is "poweredOn" if the VM is powered on,

Here's the groovy script that gets the list of VMs then iterates through the list pulling out the ones that are "poweredOn", then counts the items in the list:

def vmlist = scope.get('virtualMachines')

ds=server.DataService

poweredOnVMs = vmlist.collect{vm->

vmState = ds.retrieveLatestValue(vm, 'vmState')?.value

(vmState == 'poweredOn') ? vm : null

} - null

return poweredOnVMs.size()

I discovered these names and relationships by exploring the Data browser under Configuration in the left navigation panel.

I prototype and test expressions like this in the Administration | Tooling | Script Console 

The calculation policy can be:

Data Driven - the metric is updated whenever the Data Center object is updated

Time Driven - the metric is calculated every X hours, Y minutes and Z seconds

Schedule Driven - the metric is calculated according to a specified schedule

For this example, I didn't see the value of calculating this value too frequently, so I decided that once an hour (Time Driven) would be adequate.

  1. To create this derived metric, I navigated to the Administration | Data | Derived Metrics dashboards and clicked "Add Derived Metric"
  2. Next specify the Derived Metric Name "virtualMachinesPoweredOnCount" and click Add Calculation.
  3. Select the Topology Type that this metric will be scoped to from the drop down list then click on the green arrow button to append this to the field below.
    Remember to validate the scope by clicking the green check button on the far right.Next, enter the calculation above in the expression field. There are buttons to test the expression, build the expression and validate the expression on the right.
    Make sure that you test the expression and validate it before proceeding.
  4. Next, specify the Trigger Type. I checked "Enable Trigger without Data" to ensure that this metric would always be calculated and available.
    Optionally enter a descriptions and click Add.
  5. You can add the same metric to different scopes. For example, you could use the same expression to add this metric to the Virtual Center level.
  6. Specify the Unit for the metric and add a comment, then click Add to create the metric.

Once created, the derived metrics is available for rules, reports and dashboards.

Derived metrics are a great way to enhance the capabilities of your Foglight monitoring environment!