VMware {code} Community
Samadhan
Contributor
Contributor

How to get Snapshot space using vi java api??

Hi,

I am in trouble to get the Vm snapshot space regarding to the specific cluster. I didn't find any property or method in the vi java api. So please tell me how to get snapshot space??? or refer me any sample code related to the VM snapshot Space.

Thanks in advance.

11 Replies
Samadhan
Contributor
Contributor

Nevermind, finally after deep study of vsphere java sdk & Snapshot I got the Solution...we need to use searchDatastoreSubFolder_Task() method with two parameters.....1) Datastore Path 2) HostDatastoreBrowserSearchSpace object.

0 Kudos
rashmimenon
Contributor
Contributor

Hey could you post the sample code for it....I am trying to clone from snapshot but still not able to find the snapshot space through java api

Thanks

Rashmi

0 Kudos
Samadhan
Contributor
Contributor

HostDatastoreBrowserSearchSpec hdbss = new HostDatastoreBrowserSearchSpec();
hdbss.setQuery(new FileQuery[] {new VmDiskFileQuery(),new VmSnapshotFileQuery() });
FileQueryFlags fqf = new FileQueryFlags();
fqf.setFileSize(true);
fqf.setModification(true);
fqf.setFileType(true);
hdbss.setDetails(fqf);
hdbss.setSearchCaseInsensitive(false);                  
hdbss.setSortFoldersFirst(true);
String s1="*-00000*.vmdk";
String s2="*.vmsn";
hdbss.setMatchPattern(new String[] {s1,s2});
Task task=null;
try
{                  
task = hdb.searchDatastoreSubFolders_Task(dspath, hdbss);
}
catch(Exception e)
{
System.out.println("Task Object :: "+task);
System.out.println("Wrong Datastore path.? ::: "+e.getMessage());
e.printStackTrace();
Flag=false;
}
if(task.waitForMe()==Task.SUCCESS)
{
Object obj = task.getTaskInfo().getResult();
if(obj instanceof ArrayOfHostDatastoreBrowserSearchResults)
{
HostDatastoreBrowserSearchResults[] results =((ArrayOfHostDatastoreBrowserSearchResults)obj).getHostDatastoreBrowserSearchResults();
                 
for(int i=0; i<results.length; i++)
{
HostDatastoreBrowserSearchResults result = results[i];
FileInfo[] fis = result.getFile();
if(fis!=null && fis.length>0)
{
String Filename=null;
float totalSnapSize=0.0f;
for(int j=0; j<fis.length; j++)
{
totalSnapSize=totalSnapSize+fis[j].getFileSize();
Filename=fis[j].getPath();
System.out.println("* File name :: "+Filename);
System.out.println("* Snapshot Size in KB :: "+ totalSnapSize/(1024.00f)+" KB");
System.out.println("Modified:"+ fis[j].getModification().getTime());
//                                        System.out.println("Modified:"+ fis[j].getModification().getTime());//
}
System.out.println("* VM Name :: "+vmname);
System.out.println("* File name :: "+Filename);
System.out.println("* No of Snapshots :: "+fis.length);
System.out.println("* Snapshot Size in KB :: "+ totalSnapSize/(1024.00f)+" KB");
if(totalSnapSize/(1024.00f)>999.00)
{
System.out.println("* Snapshot Size in MB :: "+ totalSnapSize/(1024.00f*1024.00f)+" MB");
}
if(totalSnapSize/(1024.00f*1024.00f)>999.00)
{
                       System.out.println("* File Size in GB :: "+ totalSnapSize/(1024.00f*1024.00f*1024.00f)+" GB");
}
System.out.println("==============================================================================");
}
else
{
System.out.println("Error Message :: Snapshot file not found....!!!");
Flag=false;
}
}
Samadhan
Contributor
Contributor

Hi ,,

     if any query then ask me.

samadhan

0 Kudos
vivari
Enthusiast
Enthusiast

Hi ,

There  is one great tab available to get this information at host level.

Select host-->storage views tab. there it will give you the vm's with snap shots and space occupied by snap shot in very detail.

Its pretty simple than else.

Durga

0 Kudos
Samadhan
Contributor
Contributor

Hi durga,

     I think u r talking about snapshot space at vsphere client(vcenter client) level, but rashmi want to take snapshot space using vsphere vi java api.

Samadhan

0 Kudos
profversaggi
Contributor
Contributor

How were you able to determine the value for "dspath ?

- ######################################################### Matthew R. Versaggi, Artificial Intelligence Engineer, Imagine One, LTD President & CEO: Versaggi Information Systems, Inc. Adjunct Professor of eBusiness DePaul University Email: mailto:matt@versaggi.com, ProfVersaggi@gmail.com M: 630-292-8422 LinkedIn: http://www.linkedin.com/in/versaggi About Me: http://www.matt-versaggi.com/resume/ #########################################################
Ajax223344
Contributor
Contributor

hi profversaggi,

I think the param 'dspath' maps to the VM's attribute of vm.config.files.snapshotDirectory.

I've got the dspath as below with perl, and it works perfect!Smiley Happy

eg:

my $ds_path = $vm_view->{'config.files'}->snapshotDirectory;

SathyanRVKS
Contributor
Contributor

Is there any code samples for finding Space Utilization By File Type of a datastore. I googled it but no use.

Thanks in advance.

0 Kudos
SathyanRVKS
Contributor
Contributor

Hi Samadhan. Thanks for your code. It actually worked. Just add fqf.setFileOwner(true); for latest version of vCenter s or else it may throw property fileOwner missing error.

0 Kudos
SathyanRVKS
Contributor
Contributor

For java Datastore Path is nothing but [datastore_name] ​.

Eg: [datastore1]

0 Kudos