Create vROPS Resource Object and Custom Metrics with PowerShell

Create vROPS Resource Object and Custom Metrics with PowerShell

Create vROPS Resource Object and Custom Metrics with PowerShell

Recently we had a customer requirement where he wanted to monitor his active directory password expiry for his vRA Service Account using vROPS, unfortunately this is not part of the AD management pack with vROPS and we had to create a custom solution to attain the same.

We need to tap into the vROPS REST API to attain the same, i wont be going in details on the same as i have other blogs which detail them, please refer the below if you are getting started !.

Access Internal REST API’s in vROPS with PowerShell

First lets create the resource object which is basically the vRA service account used by vRA, we would also define the adapter kind and the resource kind for this object, for our testing we would create it as “AD” as the adapter kind and resource kind as “AD-Password” for this resource object.

We will use the XML structure as below and use the Invoke-RestMethod to invoke the REST API’s to create this resource object

Here’s the code below !

############# Create the Resource Object #################

$XMLFile = @('<ops:resource xmlns:ops="http://webservice.vmware.com/vRealizeOpsMgr/1.0/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <ops:description>Password-Expiry-For-vRA-SVC-Account</ops:description>
   <ops:resourceKey>
      <ops:name>vRA-SVCAccount-1</ops:name>
      <ops:adapterKindKey>AD</ops:adapterKindKey>
      <ops:resourceKindKey>AD-Password</ops:resourceKindKey>
      <ops:resourceIdentifiers>
         <ops:resourceIdentifier>
            <ops:identifierType name="EntityName" dataType="STRING" isPartOfUniqueness="true" />
            <ops:value>vRA-SVCAccount-1</ops:value>
            </ops:resourceIdentifier>
      </ops:resourceIdentifiers>
   </ops:resourceKey>
   <ops:resourceStatusStates />
   <ops:dtEnabled>true</ops:dtEnabled>
</ops:resource>'
)


[xml]$xmlSend = $XMLFile

#Create URL string for Invoke-RestMethod
$urlsend = 'https://' + 'vrops.vmwareebc.in/suite-api' + '/api/resources/adapterkinds/OPENAPI'

## Debug 
echo $urlsend

#Send Attribute data to vRops.
$ContentType = "application/xml;charset=utf-8"
$header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$header.Add("Accept", 'application/xml')
$header.Add("X-vRealizeOps-API-use-unsupported", 'true')

try {
        $result = Invoke-RestMethod -Method POST -uri $urlsend -Body $xmlSend -Credential $cred -ContentType $ContentType -Headers $header
}
catch {
        $result = $_.Exception.Response.GetResponseStream()
        $reader = New-Object System.IO.StreamReader($result)
        $reader.BaseStream.Position = 0
        $reader.DiscardBufferedData()
        $responseBody = $reader.ReadToEnd();
}

Once you execute the above REST call you would see that we get a success in the response.

Now, lets head over to vROPS and you would see the Resource object created with its resource kind and adapter type.

You will see that the icons used by the objects show up as a folder and would change if you give it some time, in my case it changed to a time bomb 🙂 lol ! .

a bigger image of the bomb!! 😀

Changing this icon is simple, head over to administration -> search for the object, in our case its “AD-Password” and change the icon type

Refresh the page again and you should see the new icon in effect.

Now as the object is created, we need to create the custom metrics under them which would hold the password age.

Before we do this we need to convert  a PowerShell/.NET DateTime to a unix timestamp (Milliseconds since 1 Jan, 1970) This is required to create JSON that can be understood by the .NET JavaScriptSerializer

The below github page contains a small function which allows you to do so

https://gist.github.com/rdsimes/4333461

As vROPS colllects at default of 5 minutes, you can have a scheduled task which runs the powershell script to collect the AD password age, also the date using the above function and also execute the REST API to update the password age so you get live data.

Also make sure that you update the metric name and its property and value and also the units for the same (1)

Ok so lets get onto the code to get the metrics for the resource object and its values, you would see the XML has multiple metric names and values, this was just to test how it handles when we throw multiple metrics at it and values, you can delete the ones you dont need from the XML file.

############# Create Metrics for the Resource Object - Values #################

$XMLFile = @('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ops:stat-contents xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ops="http://webservice.vmware.com/vRealizeOpsMgr/1.0/">
    <ops:stat-content statKey="PasswordAge|numhr">
      <ops:timestamps>1507531308000</ops:timestamps>
      <ops:data>5</ops:data>
      <ops:unit>Day(s)</ops:unit>
    </ops:stat-content>
    <ops:stat-content statKey="PasswordAge1|numhr">
      <ops:timestamps>1507531308000</ops:timestamps>
        <ops:data>7</ops:data>
        <ops:unit>Day(s)</ops:unit>
    </ops:stat-content>
    <ops:stat-content statKey="PasswordAge|Totalfailures">
      <ops:timestamps>1507531308000</ops:timestamps>
        <ops:data>20</ops:data>
        <ops:unit>Day(s)</ops:unit>
    </ops:stat-content>
</ops:stat-contents>'
)



[xml]$xmlSend = $XMLFile

#Create URL string for Invoke-RestMethod
$urlsend = 'https://' + 'vrops.vmwareebc.in/suite-api' + '/api/resources/5df0c1f1-495d-438a-9434-7c4060545f35/stats'

## Debug 
echo $urlsend

#Send Attribute data to vRops.
$ContentType = "application/xml;charset=utf-8"
$header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$header.Add("Accept", 'application/xml')
$header.Add("X-vRealizeOps-API-use-unsupported", 'true')

try {
        $result = Invoke-RestMethod -Method POST -uri $urlsend -Body $xmlSend -Credential $cred -ContentType $ContentType -Headers $header
}
catch {
        $result = $_.Exception.Response.GetResponseStream()
        $reader = New-Object System.IO.StreamReader($result)
        $reader.BaseStream.Position = 0
        $reader.DiscardBufferedData()
        $responseBody = $reader.ReadToEnd();
}

Next lets head over to vROPS and see if the values are populated, and indeed we see them!!

Hope this helped you out to dig deep into creating custom vROPS resource objects and metrics and helps you to modify and create your own resource for any of the use case !.

 

Share this post

3 thoughts on “Create vROPS Resource Object and Custom Metrics with PowerShell

  1. How funny I was looking for a script which I could use to create new resources and insert metrics as i couldnt find my original scripts and I found your post only to discover it is based off my custom attributes / properties script… hehe

    Thanks just what I needed, so no need to reinvent the wheel.

    vMan

    Reply

Post Comment