Using PowervROps cmdlets (part 3) – Adding metrics and properties to objects
Introduction
In the third part in this series of blog posts we are going to use the cmdlets to add metrics and properties to various objects, these metrics and properties can be used in exactly the same way as those from management packs, although you should be aware that metrics age and are not available on dashboards or for use in super metric calculations after a period of time. For dashboards this appears to be roughly four hours and for super metrics this is after 48 hours.
The functions
There are three functions available within PowervROps for adding properties and metrics:
- addProperties
- addStats
- addStatsforResources
The first and second functions enable properties and stats (metrics) to be added to a single vROps object (multiple properties and metrics can be added in a single command). The third command allows metrics to be added to multiple objects in a single command, which is much more efficient in large environments, although there is a limit of 1000 objects in a single execution. There is no comparable API method to add properties to multiple objects via a single command.
How to use
As discussed in part 1 of this blog series, before any cmdlets can be run a means of authentication needs to be configured. As with previous blog posts the examples shown will use token based authentication, although credentials based is perfectly fine. In all examples shown a variable of $token will be used.
All three functions require body content to be submitted along with the command. The functions will accept both JSON and XML (as the vROps API accepts both). For the purposes of this blog JSON will be used throughout. More information can be found about different request examples via the vROps API document (https://vropsfqdn/suite-api/docs/rest/index.html). The functions within PowervROps match the name shown in the documentation so should be easy to locate.
The addProperties and addStats functions also require an objectid to be submitted along with the request. Examples are shown in part 1 of this blog series
Examples
addProperties
In this example we are going to add a property called ‘CloudKindergarten|Demo|DevicePurpose’ with a value of ‘Active Directory Domain Controller’ to the virtual machine ‘vm-a’
import-module c:\users\taguser\documents\github\powervrops\powervrops.psm1 $resthost = 'vrops-01a.cloudkindergarten.local' $token = acquiretoken -resthost $resthost -username admin -password VMware1! -authsource local $object = getresources -resthost $resthost -token $token -name 'vm-a' -resourceKind 'VirtualMachine' $body = @{ 'property-content' = @( @{ 'statKey' = 'CloudKindergarten|Demo|DevicePurpose' 'timestamps' = @(getTimeSinceEpoch) 'values' = @('Active Directory Domain Controller') 'others' = @() 'otherAttributes' = @{} } ) } addProperties -resthost $resthost -token $token -objectid $object.resourcelist.identifier -body ($body | convertto-json -depth 5) -contenttype 'json'
This example shows a JSON body content. If the body was in XML format then the addProperties command would be altered as below:
addProperties -resthost $resthost -token $token -objectid $object.resourcelist.identifier -body ($body | convertto-json -depth 5) -contenttype 'xml'
We can see below that the property is displayed in the All Metrics page and can be selected in the same way as any other property:
We can also retrieve this property using the getResourceProperties function:
import-module c:\users\taguser\documents\github\powervrops\powervrops.psm1 $resthost = 'vrops-01a.cloudkindergarten.local' $token = acquiretoken -resthost $resthost -username admin -password VMware1! -authsource local $object = getresources -resthost $resthost -token $token -name 'vm-a' -resourceKind 'VirtualMachine' $resourceproperties = (getresourceproperties -resthost $resthost -token $token -objectid $object.resourcelist.identifier).property | where { $_.name -eq 'CloudKindergarten|Demo|DevicePurpose'} $resourceproperties
This returns the following:
addStats
The second example shows adding a single metric to a virtual machine. The metric name is ‘CloudKindergarten|Demo|SingleMetricOne and a random value of between 1 and 100 will be added. The code includes an additional unnecessary line that generates the random number, and this variable is used within the JSON body.
import-module c:\users\taguser\documents\github\powervrops\powervrops.psm1 $resthost = 'vrops-01a.cloudkindergarten.local' $token = acquiretoken -resthost $resthost -username admin -password VMware1! -authsource local $object = getresources -resthost $resthost -token $token -name 'vm-b' -resourceKind 'VirtualMachine' $metricvalue = get-random -Minimum 1 -Maximum 100 $body = @{ 'stat-content' = @( @{ 'statKey' = 'CloudKindergarten|Demo|SingleMetricOne' 'timestamps' = @(getTimeSinceEpoch) 'data' = @($metricvalue) 'others' = @() 'otherAttributes' = @{} } ) } addStats -token $token -resthost $resthost -objectid $object.resourcelist.identifier -body ($body | convertto-json -depth 5) -contenttype json
In the image below the results of running the command many times in a short space are shown.
When running this command, it is not limited to single metric, nor a single data point. In the example below values for two different metrics are being added for datapoints at five minute intervals for the last hour.
import-module c:\users\taguser\documents\github\powervrops\powervrops.psm1import-module c:\users\taguser\documents\github\powervrops\powervrops.psm1 $resthost = 'vrops-01a.cloudkindergarten.local' $token = acquiretoken -resthost $resthost -username admin -password VMware1! -authsource local $object = getresources -resthost $resthost -token $token -name 'vm-c' -resourceKind 'VirtualMachine' $metricvaluesone = @() $metricvaluestwo = @() $timestamps = @() for ($i = 0;$i -lt 12;$i++) { $metricvaluesone += (get-random -Minimum 1 -Maximum 100) $metricvaluestwo += (get-random -Minimum 1 -Maximum 100) $timestamps += gettimesinceepoch -date (get-date).AddMinutes(($i*-5))} $body = @{ 'stat-content' = @( @{ 'statKey' = 'CloudKindergarten|Demo|SingleMetricOne' 'timestamps' = $timestamps 'data' = $metricvaluesone 'others' = @() 'otherAttributes' = @{} } @{ 'statKey' = 'CloudKindergarten|Demo|SingleMetricTwo' 'timestamps' = $timestamps 'data' = $metricvaluestwo 'others' = @() 'otherAttributes' = @{} } ) } addStats -token $token -resthost $resthost -objectid $object.resourcelist.identifier -body ($body | convertto-json -depth 5) -contenttype json
The results can be seen in the image below
addStatsforResources
In the last example multiple metrics are going to be added to multiple VMs. Specifically the following metrics are going to be added:
- CloudKindergarten|Demo|MultiMetricOne – with a random value between 1 and 100
- CloudKindergarten|Demo|MultiMetricTwo – with a random value between 1 and 100
- CloudKindergarten|Demo|MultiMetricThree – with a random value between 1 and 100
These metrics are going to be added to the following VMs:
- vm-a
- vm-b
- vm-c
- vm-d
- vm-e
import-module c:\users\taguser\documents\github\powervrops\powervrops.psm1 $resthost = 'vrops-01a.cloudkindergarten.local' $token = acquiretoken -resthost $resthost -username admin -password VMware1! -authsource local $metrictime = getTimeSinceEpoch $allvms = @('vm-a','vm-b','vm-c','vm-d','vm-e') $allvmstatcontent = @() foreach ($vm in $allvms) { $object = getresources -resthost $resthost -token $token -name $vm -resourceKind 'VirtualMachine' $statcontent = @() $statcontent += (@{statKey='CloudKindergarten|Demo|MultiMetricOne';timestamps=@($metrictime);data=@((get-random -Minimum 1 -Maximum 100));others=@();otherAttributes=@{};}) $statcontent += (@{statKey='CloudKindergarten|Demo|MultiMetricTwo';timestamps=@($metrictime);data=@((get-random -Minimum 1 -Maximum 100));others=@();otherAttributes=@{};}) $statcontent += (@{statKey='CloudKindergarten|Demo|MultiMetricThree';timestamps=@($metrictime);data=@((get-random -Minimum 1 -Maximum 100));others=@();otherAttributes=@{};}) $vmstatdetail = @{ 'id' = $object.resourcelist.identifier 'stat-contents' = $statcontent } $allvmstatcontent += $vmstatdetail } $vmstatcontent = @{ 'resource-stat-content' = $allvmstatcontent } addstatsforresources -resthost $resthost -token $token -body ($vmstatcontent | convertto-json -depth 10)
The outcome of this can be seen via a view in vROps
As with the addStats function, multiple data points can be added with the addstatsforresources function:
import-module c:\users\taguser\documents\github\powervrops\powervrops.psm1 $resthost = 'vrops-01a.cloudkindergarten.local' $token = acquiretoken -resthost $resthost -username admin -password VMware1! -authsource local $allvms = @('vm-a','vm-b','vm-c','vm-d','vm-e') $allvmstatcontent = @() foreach ($vm in $allvms) { $object = getresources -resthost $resthost -token $token -name $vm -resourceKind 'VirtualMachine' $metricvaluesone = @() $metricvaluestwo = @() $metricvaluesthree = @() $timestamps = @() for ($i = 0;$i -lt 12;$i++) { $metricvaluesone += (get-random -Minimum 1 -Maximum 100) $metricvaluestwo += (get-random -Minimum 1 -Maximum 100) $metricvaluesthree += (get-random -Minimum 1 -Maximum 100) $timestamps += gettimesinceepoch -date (get-date).AddMinutes(($i*-5))} $statcontent = @() $statcontent += (@{statKey='CloudKindergarten|Demo|MultiMetricOne';timestamps=$timestamps;data=$metricvaluesone;others=@();otherAttributes=@{};}) $statcontent += (@{statKey='CloudKindergarten|Demo|MultiMetricTwo';timestamps=$timestamps;data=$metricvaluestwo;others=@();otherAttributes=@{};}) $statcontent += (@{statKey='CloudKindergarten|Demo|MultiMetricThree';timestamps=$timestamps;data=$metricvaluesthree;others=@();otherAttributes=@{};}) $vmstatdetail = @{ 'id' = $object.resourcelist.identifier 'stat-contents' = $statcontent} $allvmstatcontent += $vmstatdetail} $vmstatcontent = @{ 'resource-stat-content' = $allvmstatcontent } addstatsforresources -resthost $resthost -token $token -body ($vmstatcontent | convertto-json -depth 10)
The results can be seen for the three metrics on three different virtual machines:
All posts in this series:
Using PowervROps cmdlets (part 1) – Introduction
Using PowervROps cmdlets (part 2) – Retrieving metrics from objects
Using PowervROps cmdlets (part 3) – Adding metrics and properties to objects
Using PowervROps cmdlets (part 4) – Working with objects
Using PowervROps cmdlets (part 5) – Working with relationships
Using PowervROps cmdlets (part 6) – Working with custom groups
Using PowervROps cmdlets (part 7) – Working with supermetrics
Using PowervROps cmdlets (part 8) – Working with reports
Using PowervROps cmdlets (part 9) – Working with alerts