Skip to content

Instantly share code, notes, and snippets.

@ZuluagaSD
Created October 5, 2012 17:42
Show Gist options
  • Save ZuluagaSD/3841240 to your computer and use it in GitHub Desktop.
Save ZuluagaSD/3841240 to your computer and use it in GitHub Desktop.
package DAO;
import java.rmi.RemoteException;
import com.vmware.vim25.InvalidProperty;
import com.vmware.vim25.RuntimeFault;
import com.vmware.vim25.VirtualHardware;
import com.vmware.vim25.mo.*;
import java.util.ArrayList;
/**
* Using the VI Java in order to connect with VMWare This class provide the
* VirtualMachine Information
*/
public class VirtualMachineDAO {
public ArrayList<String[]> getInfo(ServiceInstance si, Folder rootFolder) {
ArrayList<String[]> restulArr = new ArrayList<String[]>();
//result += "Searching VirtualMachines...." + "\n";
try {
ManagedEntity[] mes = new InventoryNavigator(rootFolder).searchManagedEntities("VirtualMachine");
if (mes == null || mes.length == 0) {
//result += "There aren´t VirtualMachines" + "\n";
} else {
//result += "Virtual Machines\n==============================" + "\n";
//result += "I´ve found " + mes.length + " Virtual Machines" + "\n";
int x = 0;
for (int i = 0; i < mes.length; i++) {
VirtualMachine vm = (VirtualMachine) mes[i];
String[] vmArray = new String[10];
vmArray[x] = vm.getName();
vmArray[x + 1] = vm.getRuntime().getPowerState().toString();
vmArray[x + 2] = vm.getGuest().getGuestState();
vmArray[x + 3] = vm.getConfig().getGuestFullName();
vmArray[x + 4] = vm.getGuest().getIpAddress();
vmArray[x + 5] = vm.getGuest().getHostName();
VirtualHardware vmHrd = vm.getConfig().getHardware();
vmArray[x + 6] = String.valueOf(vmHrd.getMemoryMB() / 1024);
vmArray[x + 7] = String.valueOf(vmHrd.getNumCPU());
Datastore[] vmDataStores = vm.getDatastores();
vmArray[x + 8] = String.valueOf(vmDataStores.length);
for (int j = 0; j < vmDataStores.length; j++) {
vmArray[x + 9] = "#" + j + ": " + vmDataStores[j].getName();
}
restulArr.add(vmArray);
// result += "==============================" + "\n";
}
}
} catch (InvalidProperty e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RuntimeFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return restulArr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment