How to retrieve information about Guest OS Applications

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
sailendra
Posts: 2
Joined: 6. Mar 2013, 12:20

How to retrieve information about Guest OS Applications

Post by sailendra »

Hi iam working on Oracle VM VB API version 4.1.8 (JAVA API)

Iam using "vboxws_java16.jar"(v3.2) for my development environment. I am trying to fetch the (Application) information like PID, MEMORY,.. parameters from a running process in a Guest OS. How i can achieve this, please direct me towards any examples or any API. Thanks. Please reply me ASAP.
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: How to retrieve information about Guest OS Applications

Post by noteirak »

sailendra wrote:Hi iam working on Oracle VM VB API version 4.1.8 (JAVA API)
Iam using "vboxws_java16.jar"(v3.2) for my development environment.
Are you using missmatching version to connect? or do you use the jar coming from the 4.1.8 SDK?
Did you try the sample code that is in the SDK PDF at section 10.3 (SDK 4.2.x)?
Also, could you post any code you might have already?
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
sailendra
Posts: 2
Joined: 6. Mar 2013, 12:20

Re: How to retrieve information about Guest OS Applications

Post by sailendra »

hI noteirak, thanks for the reply

Yes as you said there is a mismatch, even with that mismatch(version 3.2 and version 4.1.8 ) i'am able to work with VM(in the sense able to connect collect guest OS information).
But my requirement is that to fetch the applications information from the GuestOS. So my doubt is,
"is there any API in any of the vbox vm api (versions below 4.1.8 ) to collect such parameters".



So please help me to know is there such api in oracle vbox vm

Now i changed the api to 4.1.8

As you asked for the code that iam using, iam providing this

Code: Select all

import java.util.List;
import java.util.UUID;

import org.virtualbox_4_1.Holder;
import org.virtualbox_4_1.IConsole;
import org.virtualbox_4_1.IDHCPServer;
import org.virtualbox_4_1.IGuest;
import org.virtualbox_4_1.IHostNetworkInterface;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.IProgress;
import org.virtualbox_4_1.ISession;
import org.virtualbox_4_1.IVirtualBox;
import org.virtualbox_4_1.LockType;
import org.virtualbox_4_1.VirtualBoxManager;



public class ClientTest
{
	VirtualBoxManager mgr;
    IVirtualBox vbox;

    public ClientTest()
    {
    	System.out.println("Before IWebsessionManager");
    	
        mgr = VirtualBoxManager.createInstance(null)
        mgr.connect("h**p://10.0.41.50:18083", "", "");
        
             vbox = mgr.getVBox();//.logon("test", "test");
     System.out.println("Initialized connection to VirtualBox version " + vbox.getVersion());
    }

    public void disconnect()
    {
        mgr.disconnect();
    }
    public void showVMs()
    {
        try
        {
            int i = 0;
            for (IHostNetworkInterface networkIntf : vbox.getHost().getNetworkInterfaces())
            {
            	System.out.println(networkIntf.getIPAddress());
            	
            }
            for(IDHCPServer dhchServer : vbox.getDHCPServers()){
            	System.out.println(dhchServer.getIPAddress()+"::::"+dhchServer.getNetworkMask()
            			+"::::"+dhchServer.getNetworkName()+"::::"+
            	dhchServer.getLowerIP());
            }
            for (IMachine machine : vbox.getMachines())
            {
                machine.getNetworkAdapter(0l).getMACAddress();
                System.out.println("Machine " + (i++) + ": " + " [" + machine.getId() + "]" + " - " + machine.getName());
                System.out.println("Machine Descrption:"+machine.getDescription());
                System.out.println("Machine Firmware Type:"+machine.getFirmwareType());
                System.out.println("Machine UUID:"+machine.getHardwareUUID());
                System.out.println("Parent:"+machine.getParent()+"::::"+machine.getExtraDataKeys());
                System.out.println(machine.getCPUCount());
                for (String dataKey : machine.getExtraDataKeys()) {
					
                	System.out.println("DataKey : "+dataKey+"  DataValue : "+machine.getExtraData(dataKey));
				}
                System.out.println(vbox.getHost().getMemoryAvailable());
                System.out.println("\n\n\n\n\n");
            }
            IMachine machine = vbox.findMachine("redhat");
            ISession session = mgr.getSessionObject();
            machine.lockMachine(session,  LockType.Shared);
            IConsole console = session.getConsole();
            IGuest guest = console.getGuest();
            Holder<Long> h = new Holder();
            h.value = (long) 6784;
            IProgress prog = guest.executeProcess("/usr/bin/animate", new Long(0), null, null, "root", "root123", new Long(0), h);


            session.unlockMachine();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    public void startVM(String strVM)
    {
        ISession oSession = null;
        IMachine oMachine = null;

        try
        {
            oSession = mgr.getSessionObject();

            try
            {
                oMachine = vbox.findMachine(strVM);//.getMachine(strVM);
            }
            catch (Exception e)
            {
                try
                {
                    oMachine = vbox.findMachine(strVM);
                }
                catch (Exception e1)
                {
                }
            }

            if (oMachine == null)
            {
                System.out.println("Error: can't find VM \"" + strVM + "\"");
            }
            else
            {
                String uuid = oMachine.getId();
                String sessionType = "gui";
                String env = "DISPLAY=:0.0";
                
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (oSession != null)
            {
                oSession.unlockMachine();//.close();
            }
        }
    }

    public void cleanup()
    {
        try
        {
            if (vbox != null)
            {
                disconnect();
                vbox = null;
                System.out.println("Logged off.");
            }
            mgr.cleanup();//.cleanupUnused();
            mgr = null;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void printArgs()
    {
        System.out.println(  "Usage: java clienttest <mode> ..." +
                             "\nwith <mode> being:" +
                             "\n   show vms            list installed virtual machines" +
                             "\n   list hostinfo       list host info" +
                             "\n   startvm <vmname|uuid> start the given virtual machine");
    }

    public static void main(String[] args)
    {
    	args = new String[]{"show","vms"};
        if (args.length < 1)
        {
            System.out.println("Error: Must specify at least one argument.");
            printArgs();
        }
        else
        {
            ClientTest c = new ClientTest();
            if (args[0].equals("show"))
            {
                if (args.length == 2)
                {
                    if (args[1].equals("vms")){
                        c.showVMs();
                  }
                    else
                        System.out.println("Error: Unknown argument to \"show\": \"" + args[1] + "\".");
                }
                else
                    System.out.println("Error: Missing argument to \"show\" command");
            }
            else if (args[0].equals("startvm"))
            {
                if (args.length == 2)
                {
                    c.startVM(args[1]);
                }
                else
                    System.out.println("Error: Missing argument to \"startvm\" command");
            }
            else
                System.out.println("Error: Unknown command: \"" + args[0] + "\".");

            c.cleanup();
        }
    }
}
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: How to retrieve information about Guest OS Applications

Post by noteirak »

There is no way to retrive information about processes that were not started by the Virtualbox Guest additions.
On the other hand, nothing stops you to run a process listing (like ps on linux) as a guest process, get the output, and parse it.

You can manage the processes via the IGuestSession interface, accessible via IMachine::getConsole()::getGuest()::createSession()
So that would give something like this (untested code) :

Code: Select all

IMachine vm = virtualbox.findMachine(uuid);
IGuest guest = vm.getConsole().getGuest();
IGuestSession guestSession = guest.createSession(user,pass);
IGuestProcess process = guestSession.processCreate(.....);
Then you can use the process object, which is simply an extended IProcess information. The SDK will give you the list of methods on it.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Post Reply