CPUController.java

//
// CPUController.java
//
//
// Created by Erin Taylor on 8/25/08.
//

package com.vmware.samples;

import java.net.URL;

//import com.vmware.vim25.*;
//import com.vmware.vim25.mo.*;
//import com.vmware.vim25.mo.util.*;

import com.vmware.vim.*;
import com.vmware.apputils.*;
import com.vmware.apputils.vim.*;

import java.io.*;
import java.util.*;

/**
*The CPUController class contains attributes and methods that facilitate the
*creation of a controller that will monitor and adjust the CPU resources
*allocated to VMs.
*
*Parameters:
*url [required]
*username [required]
*password [required]
*controlFile [required]: This text file contains information on VMs to be
* controlled and their relative responsibility
*
*Command:com.vmware.samples.vm.CPUController –url [webserviceurl]
*–username [username] –password [password] –controlFile [controlFile]
*
*/

public class CPUController {

static String controlFile = null;
static AppUtil connection = null;

//Connects to server
private static void connect(String[] args) throws Exception{

OptionSpec[] useroptions = new OptionSpec[1];
useroptions[0] = new OptionSpec(“controlFile”,”String”,1,”File with control data”, null);

connection = AppUtil.initialize(“CPUController”, useroptions, args);
connection.connect();

controlFile = connection.get_option(“controlFile”);

}

//Disconnects from server
private static void disconnect() throws Exception{

connection.disConnect();

}

//Get list of VMs from control file
private static ArrayList getVMs() throws Exception{

String[] words;
String line = null;
ArrayList data = new ArrayList();

try {
FileReader fr = new FileReader(controlFile);
BufferedReader br = new BufferedReader(fr);
while((line = br.readLine()) != null) {
words = line.split(“\\s”);
data.add(words[0]);
}
fr.close();
br.close();
}

catch(FileNotFoundException fN) {
fN.printStackTrace();
}

catch(IOException e) {
System.out.println(e);
}

return data;
}

//Get list of responsibilities from control file
private static ArrayList getResponsibility() throws Exception{

String[] words;
String line = null;
ArrayList data = new ArrayList();

try {
FileReader fr = new FileReader(controlFile);
BufferedReader br = new BufferedReader(fr);
while((line = br.readLine()) != null) {
words = line.split(“\\s”);
data.add(words[1]);
}
fr.close();
br.close();
}

catch(FileNotFoundException fN) {
fN.printStackTrace();
}

catch(IOException e) {
System.out.println(e);
}

return data;
}

//Sets exact CPU resources for a VM
private static int reconfigCPUExact(ManagedObjectReference VM, long CPUExact) throws Exception{

int success = 1;

VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
ResourceAllocationInfo raInfo = new ResourceAllocationInfo();
VirtualMachineConfigInfo vmConfigInfo = getVMConfigInfo(VM);

raInfo.setLimit(CPUExact);
raInfo.setReservation(CPUExact);

System.out.println(“Reconfiguring The Virtual Machine For Exact CPU allocation ” + vmConfigInfo.getName() +” ” + CPUExact);
vmConfigSpec.setCpuAllocation(raInfo);

ManagedObjectReference tmor = connection.getConnection().getService().reconfigVM_Task(VM, vmConfigSpec);
monitorTask(tmor);

return success;

}

//CPU Limit is the max CPU power a VM can use
private static int reconfigCPUMax(ManagedObjectReference VM, long CPULimit) throws Exception{

int success = 1;
long currReservation;
long currLimit;

VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
ResourceAllocationInfo raInfo = new ResourceAllocationInfo();
VirtualMachineConfigInfo vmConfigInfo = getVMConfigInfo(VM);

currReservation = vmConfigInfo.getCpuAllocation().getReservation();
currLimit = vmConfigInfo.getCpuAllocation().getLimit();

if(CPULimit < currReservation){
success=0;
System.out.println(“Limit cannot be less than current CPU reservation for a VM”);
}
else{

raInfo.setLimit(CPULimit);

System.out.println(“Reconfiguring The Virtual Machine For Max CPU allocation ” + vmConfigInfo.getName() +” ” + CPULimit);
vmConfigSpec.setCpuAllocation(raInfo);

ManagedObjectReference tmor = connection.getConnection().getService().reconfigVM_Task(VM, vmConfigSpec);
monitorTask(tmor);
}

return success;

}

//Reservation is the min CPU power a VM is guaranteed
private static int reconfigCPUMin(ManagedObjectReference VM, long CPUReservation) throws Exception{

int success = 1;
long currReservation;
long currLimit;

VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
ResourceAllocationInfo raInfo = new ResourceAllocationInfo();
VirtualMachineConfigInfo vmConfigInfo = getVMConfigInfo(VM);

currReservation = vmConfigInfo.getCpuAllocation().getReservation();
currLimit = vmConfigInfo.getCpuAllocation().getLimit();

if(CPUReservation > currLimit){
success=0;
System.out.println(“CPU reservation cannot be more than limit for a VM”);
}
else{

raInfo.setLimit(CPUReservation);

System.out.println(“Reconfiguring The Virtual Machine For Min CPU allocation ” + vmConfigInfo.getName() +” ” + CPUReservation);
vmConfigSpec.setCpuAllocation(raInfo);

ManagedObjectReference tmor = connection.getConnection().getService().reconfigVM_Task(VM, vmConfigSpec);
monitorTask(tmor);
}

return success;

}

private static void monitorTask(ManagedObjectReference tmor) throws Exception {
if(tmor != null) {
String result = connection.getServiceUtil().waitForTask(tmor);
if(result.equalsIgnoreCase(“sucess”)) {
System.out.println(“Task Completed Sucessfully”);
}
else {
System.out.println(“Failure ” + result);
}
}
}

private static int getActualCPU(ManagedObjectReference VM) throws Exception{

VirtualMachineSummary vms = getVMSummary(VM);
return vms.getQuickStats().getOverallCpuUsage();

}

private static void control(){

//This is the main control method and will contain the control logic
//There can be different methods for different types of control

}

private static VirtualMachineConfigInfo getVMConfigInfo(ManagedObjectReference VM) throws Exception{

return (VirtualMachineConfigInfo)connection.getServiceUtil().getDynamicProperty(VM,”config”);

}

private static VirtualMachineSummary getVMSummary(ManagedObjectReference VM) throws Exception{

return (VirtualMachineSummary)connection.getServiceUtil().getDynamicProperty(VM,”summary”);

}

private static ManagedObjectReference getVM(String vmname) throws Exception{

return (connection.getServiceUtil().getDecendentMoRef(null, “VirtualMachine”, vmname));

}

// This serves as an example of the Controller function.
//This main method can be modified to execute the necessary
//controller functionality.
public static void main(String[] args) throws Exception
{
ArrayList vms = null;
ArrayList responsibility = null;

//Connect to VI3 server
connect(args);

//Get VMs to be controlled and their responsibilities
vms = getVMs();
responsibility = getResponsibility();

//Example of getting the current CPU limit of a VM
System.out.println((String)vms.get(0) + ” has currLimit “+ getVMConfigInfo(getVM((String)vms.get(0))).getCpuAllocation().getLimit());

//Example of setting the exact CPU resources for a VM
reconfigCPUExact(getVM((String)vms.get(0)),1000);

//Example of getting the actual CPU usage for a VM
System.out.println(“VM0 has actual CPU ” + getActualCPU(getVM((String)vms.get(0))));

control();

//Disconnect from VI3 server
disconnect();
}

}

One Response to “CPUController.java”


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.