I am currently cloning a vm template and specifying a pre-configured customization spec, currently I am doing this manually using vCenter for testing.
I have a sample c# program that I have included at the end of this email, it is not the final code as I used a working example with modifications.
The issue is that I can use this filter type below and see the vm power on and off just fine. But I never see any of the customization events.
eventFilter.type = new String[] { "VmPoweredOffEvent", "VmPoweredOnEvent", "CustomizationEvent", "CustomizationStartedEvent", "CustomizationSucceeded" };
If I comment out this filter type line, I see all events including the customization. But the customization events don't appear to be firing as customizationEvents but rather simply an Event. As I have to make this call: ((Event)value).fullFormattedMessage
Then I see the two start/success messages ie: "Started customization of VM TOMZ2. Customization log located at C:
Windows
TEMP
vmware-imc
guestcust.log in the guest OS."
Is this the proper way to determine when customization is started and succeeds? Or am I doing this wrong? Any suggestions or guidance is appreciated.
BTW: the latest vmware tools are installed in the vm/template.
-
-
private void createEventHistoryCollector() {
// Create an Entity Event Filter Spec to
// specify the MoRef of the VM to be get events filtered for
EventFilterSpecByEntity entitySpec = new EventFilterSpecByEntity();
entitySpec.entity =_virtualMachine;
// we are only interested in getting events for the VM
//entitySpec.recursion = EventFilterSpecRecursionOption.self;
entitySpec.recursion = EventFilterSpecRecursionOption.all;
// set the entity spec in the EventFilter
EventFilterSpec eventFilter = new EventFilterSpec();
eventFilter.entity = entitySpec;
// we are only interested in getting events for the VM.
// Add as many events you want to track relating to vm.
// Refer to API Data Object vmEvent and see the extends class list for elaborate list of vmEvents
// with this enabled do not see customization events, comment out will see
eventFilter.type = new String[] { "VmPoweredOffEvent", "VmPoweredOnEvent", "CustomizationEvent", "CustomizationStartedEvent", "CustomizationSucceeded" };
// create the EventHistoryCollector to monitor events for a VM
// and get the ManagedObjectReference of the EventHistoryCollector returned
_eventHistoryCollector = service.CreateCollectorForEvents(eventManager, eventFilter);
}
private PropertyFilterSpec createEventFilterSpec() {
// Set up a PropertySpec to use the latestPage attribute
// of the EventHistoryCollector
PropertySpec propSpec = new PropertySpec();
propSpec.all = false;
propSpec.pathSet=new String[] { "latestPage" };
propSpec.type =_eventHistoryCollector.type;
// PropertySpecs are wrapped in a PropertySpec array
PropertySpec[] propSpecAry = new PropertySpec[http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=%5DpropSpecAry%3DnewPropertySpec%5B] ;
// Set up an ObjectSpec with the above PropertySpec for the
// EventHistoryCollector we just created
// as the Root or Starting Object to get Attributes for.
ObjectSpec objSpec = new ObjectSpec();
objSpec.obj =_eventHistoryCollector;
objSpec.skip = false;
// Get Event objects in "latestPage" from "EventHistoryCollector"
// and no "traversl" further, so, no SelectionSpec is specified
objSpec.selectSet= new SelectionSpec[] { };
// ObjectSpecs are wrapped in an ObjectSpec array
ObjectSpec[] objSpecAry = new ObjectSpec[http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=%5DobjSpecAry%3DnewObjectSpec%5B] ;
PropertyFilterSpec spec = new PropertyFilterSpec();
spec.propSet= propSpecAry;
spec.objectSet= objSpecAry;
return spec;
}
void handleUpdate(UpdateSet update)
{
ArrayList vmUpdates = new ArrayList();
PropertyFilterUpdate[] pfus = update.filterSet;
for (int pfui = 0; pfui < pfus.Length; ++pfui)
{
ObjectUpdate[] ous = pfus[pfui|http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=pfui].objectSet;
for (int oui = 0; oui < ous.Length; ++oui)
{
if (ous[oui|http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=oui].obj.type.Equals("EventHistoryCollector"))
{
vmUpdates.Add(ous[oui|http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=oui]);
}
}
}
if (vmUpdates.Count > 0)
{
Console.WriteLine("Virtual Machine updates:");
for (IEnumerator vmi = vmUpdates.GetEnumerator(); vmi.MoveNext(); )
{
handleObjectUpdate((ObjectUpdate)vmi.Current);
}
}
}
void handleObjectUpdate(ObjectUpdate oUpdate)
{
PropertyChange[] pc = oUpdate.changeSet;
if (oUpdate.kind == ObjectUpdateKind.enter)
{
Console.WriteLine(" New Data:");
handleChanges(pc);
}
else if (oUpdate.kind == ObjectUpdateKind.leave)
{
Console.WriteLine(" Removed Data:");
handleChanges(pc);
}
else if (oUpdate.kind == ObjectUpdateKind.modify)
{
Console.WriteLine(" Changed Data:");
handleChanges(pc);
}
}
void handleChanges(PropertyChange[] changes)
{
for (int pci = 0; pci < changes.Length; ++pci)
{
String name = changes[pci|http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=pci].name;
Object value = changes[pci|http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=pci].val;
PropertyChangeOp op = changes[pci|http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=pci].op;
//Console.WriteLine(" S----
");
//Console.WriteLine(" xProperty Name: " + name);
//Console.WriteLine(" xProperty Value name: " + value.GetType().Name);
//Console.WriteLine(" xProperty Value base name: " + value.GetType().BaseType.Name);
//Console.WriteLine(" E----
");
if (op != PropertyChangeOp.remove)
{
Console.WriteLine(" Property Name: " + name);
if (value.GetType().BaseType.Name == "VmEvent")
{
VmEvent anEvent = (VmEvent)value;
Console.WriteLine(" Event Type: " + anEvent.GetType().Name);
Console.WriteLine(" Event msg: " + anEvent.fullFormattedMessage);
}
else if (value.GetType().BaseType.Name == "Event")
{
Event anEvent = (Event)value;
Console.WriteLine(" Event Type: " + anEvent.GetType().Name);
Console.WriteLine(" Event msg: " + anEvent.fullFormattedMessage);
}
else if ("name".Equals(name))
{
Console.WriteLine(" " + value);
}
else
{
Console.WriteLine(" " + value.ToString());
}
}
else
{
Console.WriteLine("Property Name: " + name + " value removed.");
}
}
}
private void Check()
{
bool shouldRun = true;
string version = "";
do
{
UpdateSet update = cb.getConnection().Service.WaitForUpdates(propColl, version);
if (update != null && update.filterSet != null)
{
this.handleUpdate(update);
version = update.version;
System.Console.WriteLine(" Current Version: " + version);
}
else
{
System.Console.WriteLine("No update is present!");
}
}
while (shouldRun);
}
private static OptionSpec[] constructOptions() {
OptionSpec [] useroptions = new OptionSpec[1];
useroptions[0] = new OptionSpec("vmpath","String",1
,"A VM Inventory Path"
,null);
return useroptions;
}
public static void Main(String[] args) {
try {
VMEventHistoryCollectorMonitor eventMonitor = new VMEventHistoryCollectorMonitor();
cb = AppUtil.AppUtil.initialize("VMEventHistoryCollectorMonitor", VMEventHistoryCollectorMonitor.constructOptions(), args);
cb.connect();
eventMonitor.initialize();
// eventMonitor.findVirtualMachine();
if(eventMonitor.findVirtualMachine())
{
eventMonitor.createEventHistoryCollector();
PropertyFilterSpec eventFilterSpec = eventMonitor.createEventFilterSpec();
propColl = cb.getConnection().PropCol;
propFilter = cb.getConnection().Service.CreateFilter(propColl, eventFilterSpec, true);
eventMonitor.Check();
//eventMonitor.monitorEvents(eventFilterSpec);
}
else {
Console.WriteLine("Virtual Machine not found from the vmPath specified");
}
cb.disConnect();
Console.WriteLine("Press enter to exit: ");
Console.Read();
}
catch (Exception e) {
Console.WriteLine("Caught Exception : " +
" Name : " + e.Data.ToString() +
" Message : " + e.Message.ToString() +
" Trace : ");
Console.Read();
}
}
}
}