Solved

API Call to get information about VMs which are present in VMgroup


Userlevel 3
Badge +11

Hi Team,

I need an API which can give real time information about the VMs which are added in VMgroup/sub client for all vmgroups and VMware centers configured in CommCell console.

Let me know if this is possible.

Regards,Mohit

icon

Best answer by christopherlecky 26 April 2022, 18:07

View original

If you have a question or comment, please create a topic

11 replies

Userlevel 7
Badge +23

@Mohit Chordia , check out this section:

https://api.commvault.com/#c39d4219-26e0-4090-b6a0-103f8319330b

Starting with GET vm details, and continuing with other commands.

Userlevel 5
Badge +16

Hi Mohit, I am pretty sure that real-time monitoring is not possible through the currently exposed apis.

The VM information is gathered during sub-client discovery. and VMgroups are consequently populated.

To get exactly what you are asking for would probably requires that you use a VMware API or Powercli. Even then I’m not sure an API exists that will get the information in real time. 

Perhaps if you tell us what it is that you are trying to accomplish we may be able to help. 

 

Thanks.

Chris.

Userlevel 3
Badge +11

@christopherlecky 

Yes , I need a API call or table in Commvault database or CSV report  which can give me information about VMs which are currently configured in backup sub clients. Not the ones for which backup is completed in last 24 hours or 7 days.

I need to compare it with company wide VMs list on daily basis to ensure than none of the VMs are missed from backups.

Regards, Mohit

Userlevel 5
Badge +16

Is the goal to ensure that every vm in the company is backed up?

 

CommVault obviously can only backup up VMs that it is aware of, so to ensure that something isn’t being missed by a filter or other shenanigans you should really be comparing against vmware.

 

How are your vm backup subclient contents defined?

 

 

 

 

Userlevel 3
Badge +11

We have an automation which adds or remove VMs in backup sub clients , this automation works on VMs which are supposed to be configured in backups.

 

We need to put a check if the automation is doing what is expected by comparing the VMs which are in backup sub clients with the VMs which should be in backups . That's why i need a API using which i can get the information of VMs which are configured in Virtual server sub clients . 

Regards,

Mohit

Userlevel 5
Badge +16

The simplest way to find this is to get the annotations from the VM

If you have access to Powercli you can do this 

Get-vm  myvmhere | get-annotation -name *Backup* | format-list

There are two annotations added by CommVault when a backup completes.

“Backup Status” which includes the Jobid, Backupset, and subclient and  ”Last Backup” which includes the date of the last backup.

 

You can get the same information from the vmware apis, but they are really just horrible.

 

Userlevel 3
Badge +11

 

Please confirm if this API call will provide information about VMs which are being backed up in last how many hours , days etc ?  

http://WebConsoleHostName/webconsole/api/VM?status=1

 

 

Badge +4

 

Please confirm if this API call will provide information about VMs which are being backed up in last how many hours , days etc ?  

http://WebConsoleHostName/webconsole/api/VM?status=1

 

 

Here is the standard output object that you get from GET VM API.

 

vmHost                                : vmhost.home.com
bkpStartTime : 1650582895
type : 9
vmStatus : 1
slaStatus : 1
vmUsedSpace : 1030151006
subclientId : 9560
bkpEndTime : 1650582898
vmFailureReasonForWarning :
name : vm1
storagepolicyName : Backup-Plan
slaCategory : 1
vmGuestSpace : 95629309943
latestBackupTimeCatalogedSuccessfully : 0
isIndexingV2 : True
vmBackupJob : 24026392
strOSName : SUSE Linux Enterprise 12 (64-bit)
isDeleted : False
vendor : 1
osType : 2
applicationSize : 95629309943
vmSize : 164313553758
slaCategoryDescription : Protected
vmAgent : proxy1
retireClientPhase : 0
isContentIndexded : False
isBackupAllowed : True
schedulePolicy : Incremental backup schedule
vmHardwareVer : vmx-09
strGUID : 508990bf-a3e3-06bc-514e-49debd3x56e0
subclientName : SubclientName
vsaNextBackupSubClientEntity : @{backupsetId=2357; subclientId=9560; subclientName=SubclientName; flags=}
instanceEntity : @{instanceId=304}
commCell : @{_type_=1; commCellName=CVCS; csGUID=62cccdd-4DC3-4D2A-A494-54678468AA}
proxyClient : @{clientId=85; clientName=proxy1}
plan : @{planName=Backup-Plan; planId=206}
vsaSubClientEntity : @{backupsetId=2357; subclientId=9560; subclientName=subclientName}
client : @{clientId=2859; clientName=vm1; displayName=vm1}
pseudoClient : @{clientId=2612; clientName=vCenter; flags=}
lastBackupJobInfo : @{jobID=24026652; commCellID=2; failureReasonMessageEnglish=; status=1; startTime=; endTime=}
dcPlan :
vmSubClientEntity : @{backupsetId=2638; instanceId=2; subclientId=3221; subclientName=default}
advancedPrivacySettings : @{authType=1; passkeySettings=}

It has the information like SubclientName, vCenter etc that you can use to compare the data from VMware. 

 

Here are the couple of PowerShell functions that I am using to achieve this:

 

$CV = CVCS.HOME.COM
Function Get-CVToken {
[CmdletBinding(
)]
Param (
[Parameter(Mandatory=$True)]
[string]$UserName,

[Parameter(
Mandatory = $True,
ParameterSetName = 'Secret'
)]
[Security.SecureString]$Password
)
#Write-Verbose $Password
$headers = @{}
$headers.Add("Content-Type","application/json")
$headers.Add("Accept","application/json")
$plainPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))
#Write-Verbose $UserName
#Write-Verbose $plainPassword
$encPassword = [convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($plainPassword))
$body = @{username=$userName;password=$encPassword;}
$response = Invoke-RestMethod "http://$CV/webconsole/api/login" -Method Post -Headers $headers -Body $($body | ConvertTo-Json)
Write-Verbose $response.token
$Global:cvToken = $response.token
}

function StageVMData {
param ()
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept","application/json")
$headers.Add("AuthToken", $Global:cvToken)
$headers.Add("PagingInfo","0,10000")
$body = ""
$response = Invoke-RestMethod "http://$CV/webconsole/api/VM?status=0" -Method Get -Headers $headers
$Global:CVVMData = $response

}

First function to generate the token and second to stage the data in a global variable that you can use for your reports.

 

Hope this helps.

 

Thanks

Abdul

Userlevel 5
Badge +16
<# 
.SYNOPSIS
Retrieves VM backup information from VMware using PowerCLI
.DESCRIPTION
Retrieves VM backup information and dumps it into the pipeline
.EXAMPLE
commvaultcommunity.ps1 -vCenter myvcenter.com -vmName "myvm.com"
#>
Param (
[Parameter(Mandatory=$false,ValueFromPipeline=$false)]
[string[]]$vCenter,

[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
[alias("ClientName","ComputerName")] # Allows piping from AD or CommVault
[string[]]$vmName,

[Parameter(Mandatory=$true,ValueFromPipeline=$false)]
[string]$username,

[Parameter(Mandatory=$true,ValueFromPipeline=$false)]
[Securestring]$password
)

begin {
Set-PowerCLIConfiguration -DefaultVIServerMode "Multiple" -Confirm:$false
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Set-PowerCLIConfiguration -ProxyPolicy Noproxy -Confirm:$false
$vCenters = [System.Collections.Array]@()
if ($PSBoundParameters.Contains("vCenter")){
foreach($vC in $vCenter){
$vCenters.add($vC)
}
}else{ # Adds default Vcenters that you want hardcoded here
$vCenters.add('vcenter1')
$vCenters.add('vcenter2')
}
$customattributes = {"Last Backup", "Backup Status"} #Specify custom attributes you are looking for

try{
Connect-VIServer -Server $vCenters -user $username -Password $password -ErrorAction stop
}catch{
write-host "Failed to connect to the vcenter(s) specified :("
}
}

Process{
if ($PSBoundParameters.contains("vmName")){# Runs the VM list that you passed
GET-VM -name $vmName | Get-Annotation $vm -CustomAttribute $customattributes -ErrorAction SilentlyContinue
}else {# Runs against every VM you ever even thought about
GET-VM | Get-Annotation $vm -CustomAttribute $customattributes -ErrorAction SilentlyContinue
}
}

end{
Disconnect-VIServer $vCenters -Confirm:$false
}

 

Here is the powercli code, I was already working on something like this and I couldn’t sleep. So…. 

I did ZERO testing on this so you may find a bug here or there, but with what Abdul already gave you this should be enough for you to get it done.

 

Thanks.

Chris.

 

Userlevel 5
Badge +16

There is another way to do this; 

You can refresh the datacenter from CommVault. The reason why I initially suggested you get the data from the Vmware is that by default commvault only sees the VMs that are defined within it’s sub clients, and even then the date is only as up to date as the last backup. You can do a datacenter refresh against your Vcenters, you will need the Vcenter instance information in order to do it. 

I took the liberty of modifying Abdul’s code to add a function to do the DC refresh.

Once again zero testing, so it most likely fail on line 38, because I wrote the code from the instance properties, this also finds all virtual server instances, so you may want to filter for that.

 

 

 

$CV = CVCS.HOME.COM
Function Get-CVToken {
[CmdletBinding(
)]
Param (
[Parameter(Mandatory=$True)]
[string]$UserName,

[Parameter(
Mandatory = $True,
ParameterSetName = 'Secret'
)]
[Security.SecureString]$Password
)
#Write-Verbose $Password
$headers = @{}
$headers.Add("Content-Type","application/json")
$headers.Add("Accept","application/json")
$plainPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))
#Write-Verbose $UserName
#Write-Verbose $plainPassword
$encPassword = [convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($plainPassword))
$body = @{username=$userName;password=$encPassword;}
$response = Invoke-RestMethod "http://$CV/webconsole/api/login" -Method Post -Headers $headers -Body $($body | ConvertTo-Json)
Write-Verbose $response.token
$Global:cvToken = $response.token
}

function RefreshVcenters {
param ()
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept","application/json")
$headers.Add("AuthToken", $Global:cvToken)
$headers.Add("PagingInfo","0,10000")

$vCenterinfo = Invoke-RestMethod "http://$CV/webconsole/api/instance/byName(appName='Virtual Server'))" -Method Get -Headers $headers #
$vCenterinfo.foreach{ #Perform Datacenter Refresh against vCenterID
$body = '<?xml version="1.0" encoding="UTF-8" standalone="no" ?><EVGui_RefreshTemplateInformationReq clientId="0" flags="0" instanceId="' + $_.instanceproperties.instance.instanceID + '"/>'
Invoke-RestMethod "http://$CV/webconsole/api/VM?status=0" -Method Get -Headers $headers -Body $body
}

}
function StageVMData {
param ()
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept","application/json")
$headers.Add("AuthToken", $Global:cvToken)
$headers.Add("PagingInfo","0,10000")
$body = ""
$response = Invoke-RestMethod "http://$CV/webconsole/api/VM?status=0" -Method Get -Headers $headers
$Global:CVVMData = $response

}

 

Userlevel 5
Badge +16

Line 37 should look like this :

 

$vCenterinfo = (Invoke-RestMethod "http://$CV/webconsole/api/instance/byName(appName='Virtual Server'))" -Method Get -Headers $headers).token